blob: b37f3aaad64993188aa3a9e22872e217abb0ab96 [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
2// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000010#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000011#include "libGLESv2/ProgramBinary.h"
12
13#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000014#include "common/version.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/Shader.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000018
19#include <string>
20
daniel@transgaming.com88853c52012-12-20 20:56:40 +000021#undef near
22#undef far
23
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000024namespace gl
25{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000026std::string str(int i)
27{
28 char buffer[20];
29 snprintf(buffer, sizeof(buffer), "%d", i);
30 return buffer;
31}
32
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000033UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +000034 : name(Uniform::undecorate(_name)), element(element), index(index)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000035{
36}
37
daniel@transgaming.come87ca002012-07-24 18:30:43 +000038unsigned int ProgramBinary::mCurrentSerial = 1;
39
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000040ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000041{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000042 mPixelExecutable = NULL;
43 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000044
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000045 mValidated = false;
46
47 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
48 {
49 mSemanticIndex[index] = -1;
50 }
51
52 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
53 {
54 mSamplersPS[index].active = false;
55 }
56
57 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
58 {
59 mSamplersVS[index].active = false;
60 }
61
62 mUsedVertexSamplerRange = 0;
63 mUsedPixelSamplerRange = 0;
64
daniel@transgaming.com593ebc42012-12-20 20:56:46 +000065 mDxDepthRangeRegisterVS = -1;
66 mDxDepthRangeRegisterPS = -1;
67 mDxDepthFrontRegister = -1;
68 mDxCoordRegister = -1;
69 mDxHalfPixelSizeRegister = -1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000070}
71
72ProgramBinary::~ProgramBinary()
73{
daniel@transgaming.com95892412012-11-28 20:59:09 +000074 delete mPixelExecutable;
75 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000076
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000077 while (!mUniforms.empty())
78 {
79 delete mUniforms.back();
80 mUniforms.pop_back();
81 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000082}
83
daniel@transgaming.come87ca002012-07-24 18:30:43 +000084unsigned int ProgramBinary::getSerial() const
85{
86 return mSerial;
87}
88
89unsigned int ProgramBinary::issueSerial()
90{
91 return mCurrentSerial++;
92}
93
daniel@transgaming.com95892412012-11-28 20:59:09 +000094rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000095{
96 return mPixelExecutable;
97}
98
daniel@transgaming.com95892412012-11-28 20:59:09 +000099rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000100{
101 return mVertexExecutable;
102}
103
104GLuint ProgramBinary::getAttributeLocation(const char *name)
105{
106 if (name)
107 {
108 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
109 {
110 if (mLinkedAttribute[index].name == std::string(name))
111 {
112 return index;
113 }
114 }
115 }
116
117 return -1;
118}
119
120int ProgramBinary::getSemanticIndex(int attributeIndex)
121{
122 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
123
124 return mSemanticIndex[attributeIndex];
125}
126
127// Returns one more than the highest sampler index used.
128GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
129{
130 switch (type)
131 {
132 case SAMPLER_PIXEL:
133 return mUsedPixelSamplerRange;
134 case SAMPLER_VERTEX:
135 return mUsedVertexSamplerRange;
136 default:
137 UNREACHABLE();
138 return 0;
139 }
140}
141
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000142bool ProgramBinary::usesPointSize() const
143{
144 return mUsesPointSize;
145}
146
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000147// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
148// index (0-15 for the pixel shader and 0-3 for the vertex shader).
149GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
150{
151 GLint logicalTextureUnit = -1;
152
153 switch (type)
154 {
155 case SAMPLER_PIXEL:
156 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
157
158 if (mSamplersPS[samplerIndex].active)
159 {
160 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
161 }
162 break;
163 case SAMPLER_VERTEX:
164 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
165
166 if (mSamplersVS[samplerIndex].active)
167 {
168 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
169 }
170 break;
171 default: UNREACHABLE();
172 }
173
174 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
175 {
176 return logicalTextureUnit;
177 }
178
179 return -1;
180}
181
182// Returns the texture type for a given Direct3D 9 sampler type and
183// index (0-15 for the pixel shader and 0-3 for the vertex shader).
184TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
185{
186 switch (type)
187 {
188 case SAMPLER_PIXEL:
189 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
190 ASSERT(mSamplersPS[samplerIndex].active);
191 return mSamplersPS[samplerIndex].textureType;
192 case SAMPLER_VERTEX:
193 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
194 ASSERT(mSamplersVS[samplerIndex].active);
195 return mSamplersVS[samplerIndex].textureType;
196 default: UNREACHABLE();
197 }
198
199 return TEXTURE_2D;
200}
201
202GLint ProgramBinary::getUniformLocation(std::string name)
203{
204 unsigned int subscript = 0;
205
206 // Strip any trailing array operator and retrieve the subscript
207 size_t open = name.find_last_of('[');
208 size_t close = name.find_last_of(']');
209 if (open != std::string::npos && close == name.length() - 1)
210 {
211 subscript = atoi(name.substr(open + 1).c_str());
212 name.erase(open);
213 }
214
215 unsigned int numUniforms = mUniformIndex.size();
216 for (unsigned int location = 0; location < numUniforms; location++)
217 {
218 if (mUniformIndex[location].name == name &&
219 mUniformIndex[location].element == subscript)
220 {
221 return location;
222 }
223 }
224
225 return -1;
226}
227
228bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
229{
230 if (location < 0 || location >= (int)mUniformIndex.size())
231 {
232 return false;
233 }
234
235 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
236 targetUniform->dirty = true;
237
238 if (targetUniform->type == GL_FLOAT)
239 {
240 int arraySize = targetUniform->arraySize;
241
242 if (arraySize == 1 && count > 1)
243 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
244
245 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
246
247 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
248
249 for (int i = 0; i < count; i++)
250 {
251 target[0] = v[0];
252 target[1] = 0;
253 target[2] = 0;
254 target[3] = 0;
255 target += 4;
256 v += 1;
257 }
258 }
259 else if (targetUniform->type == GL_BOOL)
260 {
261 int arraySize = targetUniform->arraySize;
262
263 if (arraySize == 1 && count > 1)
264 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
265
266 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
267 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
268
269 for (int i = 0; i < count; ++i)
270 {
271 if (v[i] == 0.0f)
272 {
273 boolParams[i] = GL_FALSE;
274 }
275 else
276 {
277 boolParams[i] = GL_TRUE;
278 }
279 }
280 }
281 else
282 {
283 return false;
284 }
285
286 return true;
287}
288
289bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
290{
291 if (location < 0 || location >= (int)mUniformIndex.size())
292 {
293 return false;
294 }
295
296 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
297 targetUniform->dirty = true;
298
299 if (targetUniform->type == GL_FLOAT_VEC2)
300 {
301 int arraySize = targetUniform->arraySize;
302
303 if (arraySize == 1 && count > 1)
304 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
305
306 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
307
308 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
309
310 for (int i = 0; i < count; i++)
311 {
312 target[0] = v[0];
313 target[1] = v[1];
314 target[2] = 0;
315 target[3] = 0;
316 target += 4;
317 v += 2;
318 }
319 }
320 else if (targetUniform->type == GL_BOOL_VEC2)
321 {
322 int arraySize = targetUniform->arraySize;
323
324 if (arraySize == 1 && count > 1)
325 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
326
327 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
328
329 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
330
331 for (int i = 0; i < count * 2; ++i)
332 {
333 if (v[i] == 0.0f)
334 {
335 boolParams[i] = GL_FALSE;
336 }
337 else
338 {
339 boolParams[i] = GL_TRUE;
340 }
341 }
342 }
343 else
344 {
345 return false;
346 }
347
348 return true;
349}
350
351bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
352{
353 if (location < 0 || location >= (int)mUniformIndex.size())
354 {
355 return false;
356 }
357
358 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
359 targetUniform->dirty = true;
360
361 if (targetUniform->type == GL_FLOAT_VEC3)
362 {
363 int arraySize = targetUniform->arraySize;
364
365 if (arraySize == 1 && count > 1)
366 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
367
368 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
369
370 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
371
372 for (int i = 0; i < count; i++)
373 {
374 target[0] = v[0];
375 target[1] = v[1];
376 target[2] = v[2];
377 target[3] = 0;
378 target += 4;
379 v += 3;
380 }
381 }
382 else if (targetUniform->type == GL_BOOL_VEC3)
383 {
384 int arraySize = targetUniform->arraySize;
385
386 if (arraySize == 1 && count > 1)
387 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
388
389 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
390 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
391
392 for (int i = 0; i < count * 3; ++i)
393 {
394 if (v[i] == 0.0f)
395 {
396 boolParams[i] = GL_FALSE;
397 }
398 else
399 {
400 boolParams[i] = GL_TRUE;
401 }
402 }
403 }
404 else
405 {
406 return false;
407 }
408
409 return true;
410}
411
412bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
413{
414 if (location < 0 || location >= (int)mUniformIndex.size())
415 {
416 return false;
417 }
418
419 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
420 targetUniform->dirty = true;
421
422 if (targetUniform->type == GL_FLOAT_VEC4)
423 {
424 int arraySize = targetUniform->arraySize;
425
426 if (arraySize == 1 && count > 1)
427 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
428
429 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
430
431 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
432 v, 4 * sizeof(GLfloat) * count);
433 }
434 else if (targetUniform->type == GL_BOOL_VEC4)
435 {
436 int arraySize = targetUniform->arraySize;
437
438 if (arraySize == 1 && count > 1)
439 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
440
441 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
442 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
443
444 for (int i = 0; i < count * 4; ++i)
445 {
446 if (v[i] == 0.0f)
447 {
448 boolParams[i] = GL_FALSE;
449 }
450 else
451 {
452 boolParams[i] = GL_TRUE;
453 }
454 }
455 }
456 else
457 {
458 return false;
459 }
460
461 return true;
462}
463
464template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
465void transposeMatrix(T *target, const GLfloat *value)
466{
467 int copyWidth = std::min(targetWidth, srcWidth);
468 int copyHeight = std::min(targetHeight, srcHeight);
469
470 for (int x = 0; x < copyWidth; x++)
471 {
472 for (int y = 0; y < copyHeight; y++)
473 {
474 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
475 }
476 }
477 // clear unfilled right side
478 for (int y = 0; y < copyHeight; y++)
479 {
480 for (int x = srcWidth; x < targetWidth; x++)
481 {
482 target[y * targetWidth + x] = (T)0;
483 }
484 }
485 // clear unfilled bottom.
486 for (int y = srcHeight; y < targetHeight; y++)
487 {
488 for (int x = 0; x < targetWidth; x++)
489 {
490 target[y * targetWidth + x] = (T)0;
491 }
492 }
493}
494
495bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
496{
497 if (location < 0 || location >= (int)mUniformIndex.size())
498 {
499 return false;
500 }
501
502 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
503 targetUniform->dirty = true;
504
505 if (targetUniform->type != GL_FLOAT_MAT2)
506 {
507 return false;
508 }
509
510 int arraySize = targetUniform->arraySize;
511
512 if (arraySize == 1 && count > 1)
513 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
514
515 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
516
517 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
518 for (int i = 0; i < count; i++)
519 {
520 transposeMatrix<GLfloat,4,2,2,2>(target, value);
521 target += 8;
522 value += 4;
523 }
524
525 return true;
526}
527
528bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
529{
530 if (location < 0 || location >= (int)mUniformIndex.size())
531 {
532 return false;
533 }
534
535 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
536 targetUniform->dirty = true;
537
538 if (targetUniform->type != GL_FLOAT_MAT3)
539 {
540 return false;
541 }
542
543 int arraySize = targetUniform->arraySize;
544
545 if (arraySize == 1 && count > 1)
546 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
547
548 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
549
550 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
551 for (int i = 0; i < count; i++)
552 {
553 transposeMatrix<GLfloat,4,3,3,3>(target, value);
554 target += 12;
555 value += 9;
556 }
557
558 return true;
559}
560
561
562bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
563{
564 if (location < 0 || location >= (int)mUniformIndex.size())
565 {
566 return false;
567 }
568
569 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
570 targetUniform->dirty = true;
571
572 if (targetUniform->type != GL_FLOAT_MAT4)
573 {
574 return false;
575 }
576
577 int arraySize = targetUniform->arraySize;
578
579 if (arraySize == 1 && count > 1)
580 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
581
582 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
583
584 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
585 for (int i = 0; i < count; i++)
586 {
587 transposeMatrix<GLfloat,4,4,4,4>(target, value);
588 target += 16;
589 value += 16;
590 }
591
592 return true;
593}
594
595bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
596{
597 if (location < 0 || location >= (int)mUniformIndex.size())
598 {
599 return false;
600 }
601
602 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
603 targetUniform->dirty = true;
604
605 if (targetUniform->type == GL_INT ||
606 targetUniform->type == GL_SAMPLER_2D ||
607 targetUniform->type == GL_SAMPLER_CUBE)
608 {
609 int arraySize = targetUniform->arraySize;
610
611 if (arraySize == 1 && count > 1)
612 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
613
614 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
615
616 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
617 v, sizeof(GLint) * count);
618 }
619 else if (targetUniform->type == GL_BOOL)
620 {
621 int arraySize = targetUniform->arraySize;
622
623 if (arraySize == 1 && count > 1)
624 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
625
626 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
627 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
628
629 for (int i = 0; i < count; ++i)
630 {
631 if (v[i] == 0)
632 {
633 boolParams[i] = GL_FALSE;
634 }
635 else
636 {
637 boolParams[i] = GL_TRUE;
638 }
639 }
640 }
641 else
642 {
643 return false;
644 }
645
646 return true;
647}
648
649bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
650{
651 if (location < 0 || location >= (int)mUniformIndex.size())
652 {
653 return false;
654 }
655
656 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
657 targetUniform->dirty = true;
658
659 if (targetUniform->type == GL_INT_VEC2)
660 {
661 int arraySize = targetUniform->arraySize;
662
663 if (arraySize == 1 && count > 1)
664 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
665
666 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
667
668 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
669 v, 2 * sizeof(GLint) * count);
670 }
671 else if (targetUniform->type == GL_BOOL_VEC2)
672 {
673 int arraySize = targetUniform->arraySize;
674
675 if (arraySize == 1 && count > 1)
676 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
677
678 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
679 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
680
681 for (int i = 0; i < count * 2; ++i)
682 {
683 if (v[i] == 0)
684 {
685 boolParams[i] = GL_FALSE;
686 }
687 else
688 {
689 boolParams[i] = GL_TRUE;
690 }
691 }
692 }
693 else
694 {
695 return false;
696 }
697
698 return true;
699}
700
701bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
702{
703 if (location < 0 || location >= (int)mUniformIndex.size())
704 {
705 return false;
706 }
707
708 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
709 targetUniform->dirty = true;
710
711 if (targetUniform->type == GL_INT_VEC3)
712 {
713 int arraySize = targetUniform->arraySize;
714
715 if (arraySize == 1 && count > 1)
716 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
717
718 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
719
720 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
721 v, 3 * sizeof(GLint) * count);
722 }
723 else if (targetUniform->type == GL_BOOL_VEC3)
724 {
725 int arraySize = targetUniform->arraySize;
726
727 if (arraySize == 1 && count > 1)
728 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
729
730 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
731 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
732
733 for (int i = 0; i < count * 3; ++i)
734 {
735 if (v[i] == 0)
736 {
737 boolParams[i] = GL_FALSE;
738 }
739 else
740 {
741 boolParams[i] = GL_TRUE;
742 }
743 }
744 }
745 else
746 {
747 return false;
748 }
749
750 return true;
751}
752
753bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
754{
755 if (location < 0 || location >= (int)mUniformIndex.size())
756 {
757 return false;
758 }
759
760 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
761 targetUniform->dirty = true;
762
763 if (targetUniform->type == GL_INT_VEC4)
764 {
765 int arraySize = targetUniform->arraySize;
766
767 if (arraySize == 1 && count > 1)
768 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
769
770 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
771
772 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
773 v, 4 * sizeof(GLint) * count);
774 }
775 else if (targetUniform->type == GL_BOOL_VEC4)
776 {
777 int arraySize = targetUniform->arraySize;
778
779 if (arraySize == 1 && count > 1)
780 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
781
782 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
783 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
784
785 for (int i = 0; i < count * 4; ++i)
786 {
787 if (v[i] == 0)
788 {
789 boolParams[i] = GL_FALSE;
790 }
791 else
792 {
793 boolParams[i] = GL_TRUE;
794 }
795 }
796 }
797 else
798 {
799 return false;
800 }
801
802 return true;
803}
804
805bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
806{
807 if (location < 0 || location >= (int)mUniformIndex.size())
808 {
809 return false;
810 }
811
812 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
813
814 // sized queries -- ensure the provided buffer is large enough
815 if (bufSize)
816 {
817 int requiredBytes = UniformExternalSize(targetUniform->type);
818 if (*bufSize < requiredBytes)
819 {
820 return false;
821 }
822 }
823
824 switch (targetUniform->type)
825 {
826 case GL_FLOAT_MAT2:
827 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
828 break;
829 case GL_FLOAT_MAT3:
830 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
831 break;
832 case GL_FLOAT_MAT4:
833 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
834 break;
835 default:
836 {
837 unsigned int count = UniformExternalComponentCount(targetUniform->type);
838 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
839
840 switch (UniformComponentType(targetUniform->type))
841 {
842 case GL_BOOL:
843 {
844 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
845
846 for (unsigned int i = 0; i < count; ++i)
847 {
848 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
849 }
850 }
851 break;
852 case GL_FLOAT:
853 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
854 count * sizeof(GLfloat));
855 break;
856 case GL_INT:
857 {
858 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
859
860 for (unsigned int i = 0; i < count; ++i)
861 {
862 params[i] = (float)intParams[i];
863 }
864 }
865 break;
866 default: UNREACHABLE();
867 }
868 }
869 }
870
871 return true;
872}
873
874bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
875{
876 if (location < 0 || location >= (int)mUniformIndex.size())
877 {
878 return false;
879 }
880
881 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
882
883 // sized queries -- ensure the provided buffer is large enough
884 if (bufSize)
885 {
886 int requiredBytes = UniformExternalSize(targetUniform->type);
887 if (*bufSize < requiredBytes)
888 {
889 return false;
890 }
891 }
892
893 switch (targetUniform->type)
894 {
895 case GL_FLOAT_MAT2:
896 {
897 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
898 }
899 break;
900 case GL_FLOAT_MAT3:
901 {
902 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
903 }
904 break;
905 case GL_FLOAT_MAT4:
906 {
907 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
908 }
909 break;
910 default:
911 {
912 unsigned int count = UniformExternalComponentCount(targetUniform->type);
913 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
914
915 switch (UniformComponentType(targetUniform->type))
916 {
917 case GL_BOOL:
918 {
919 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
920
921 for (unsigned int i = 0; i < count; ++i)
922 {
923 params[i] = (GLint)boolParams[i];
924 }
925 }
926 break;
927 case GL_FLOAT:
928 {
929 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
930
931 for (unsigned int i = 0; i < count; ++i)
932 {
933 params[i] = (GLint)floatParams[i];
934 }
935 }
936 break;
937 case GL_INT:
938 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
939 count * sizeof(GLint));
940 break;
941 default: UNREACHABLE();
942 }
943 }
944 }
945
946 return true;
947}
948
949void ProgramBinary::dirtyAllUniforms()
950{
951 unsigned int numUniforms = mUniforms.size();
952 for (unsigned int index = 0; index < numUniforms; index++)
953 {
954 mUniforms[index]->dirty = true;
955 }
956}
957
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000958// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000959void ProgramBinary::applyUniforms()
960{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000961 // Retrieve sampler uniform values
962 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
963 {
964 Uniform *targetUniform = *ub;
965
966 if (targetUniform->dirty)
967 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000968 if (targetUniform->type == GL_SAMPLER_2D ||
969 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000970 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000971 int count = targetUniform->arraySize;
972 GLint *v = (GLint*)targetUniform->data;
973
974 if (targetUniform->ps.registerCount)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000975 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000976 ASSERT(targetUniform->ps.registerIndex >= 0);
977 unsigned int firstIndex = targetUniform->ps.registerIndex;
978
979 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000980 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000981 unsigned int samplerIndex = firstIndex + i;
982
983 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000984 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000985 ASSERT(mSamplersPS[samplerIndex].active);
986 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000987 }
988 }
989 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000990
991 if (targetUniform->vs.registerCount)
992 {
993 ASSERT(targetUniform->vs.registerIndex >= 0);
994 unsigned int firstIndex = targetUniform->vs.registerIndex;
995
996 for (int i = 0; i < count; i++)
997 {
998 unsigned int samplerIndex = firstIndex + i;
999
1000 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
1001 {
1002 ASSERT(mSamplersVS[samplerIndex].active);
1003 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
1004 }
1005 }
1006 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001007 }
1008 }
1009 }
1010
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001011 mRenderer->applyUniforms(&mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001012}
1013
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001014// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1015// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001016int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001017{
1018 Context *context = getContext();
1019 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1020
1021 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1022 {
1023 int n = VariableRowCount(varying->type) * varying->size;
1024 int m = VariableColumnCount(varying->type);
1025 bool success = false;
1026
1027 if (m == 2 || m == 3 || m == 4)
1028 {
1029 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1030 {
1031 bool available = true;
1032
1033 for (int y = 0; y < n && available; y++)
1034 {
1035 for (int x = 0; x < m && available; x++)
1036 {
1037 if (packing[r + y][x])
1038 {
1039 available = false;
1040 }
1041 }
1042 }
1043
1044 if (available)
1045 {
1046 varying->reg = r;
1047 varying->col = 0;
1048
1049 for (int y = 0; y < n; y++)
1050 {
1051 for (int x = 0; x < m; x++)
1052 {
1053 packing[r + y][x] = &*varying;
1054 }
1055 }
1056
1057 success = true;
1058 }
1059 }
1060
1061 if (!success && m == 2)
1062 {
1063 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1064 {
1065 bool available = true;
1066
1067 for (int y = 0; y < n && available; y++)
1068 {
1069 for (int x = 2; x < 4 && available; x++)
1070 {
1071 if (packing[r + y][x])
1072 {
1073 available = false;
1074 }
1075 }
1076 }
1077
1078 if (available)
1079 {
1080 varying->reg = r;
1081 varying->col = 2;
1082
1083 for (int y = 0; y < n; y++)
1084 {
1085 for (int x = 2; x < 4; x++)
1086 {
1087 packing[r + y][x] = &*varying;
1088 }
1089 }
1090
1091 success = true;
1092 }
1093 }
1094 }
1095 }
1096 else if (m == 1)
1097 {
1098 int space[4] = {0};
1099
1100 for (int y = 0; y < maxVaryingVectors; y++)
1101 {
1102 for (int x = 0; x < 4; x++)
1103 {
1104 space[x] += packing[y][x] ? 0 : 1;
1105 }
1106 }
1107
1108 int column = 0;
1109
1110 for (int x = 0; x < 4; x++)
1111 {
1112 if (space[x] >= n && space[x] < space[column])
1113 {
1114 column = x;
1115 }
1116 }
1117
1118 if (space[column] >= n)
1119 {
1120 for (int r = 0; r < maxVaryingVectors; r++)
1121 {
1122 if (!packing[r][column])
1123 {
1124 varying->reg = r;
1125
1126 for (int y = r; y < r + n; y++)
1127 {
1128 packing[y][column] = &*varying;
1129 }
1130
1131 break;
1132 }
1133 }
1134
1135 varying->col = column;
1136
1137 success = true;
1138 }
1139 }
1140 else UNREACHABLE();
1141
1142 if (!success)
1143 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001144 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001145
1146 return -1;
1147 }
1148 }
1149
1150 // Return the number of used registers
1151 int registers = 0;
1152
1153 for (int r = 0; r < maxVaryingVectors; r++)
1154 {
1155 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1156 {
1157 registers++;
1158 }
1159 }
1160
1161 return registers;
1162}
1163
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001164bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001165{
1166 if (pixelHLSL.empty() || vertexHLSL.empty())
1167 {
1168 return false;
1169 }
1170
1171 // Reset the varying register assignments
1172 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1173 {
1174 fragVar->reg = -1;
1175 fragVar->col = -1;
1176 }
1177
1178 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1179 {
1180 vtxVar->reg = -1;
1181 vtxVar->col = -1;
1182 }
1183
1184 // Map the varyings to the register file
1185 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001186 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001187
1188 if (registers < 0)
1189 {
1190 return false;
1191 }
1192
1193 // Write the HLSL input/output declarations
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001194 const bool sm3 = (mRenderer->getMajorShaderModel() >= 3);
1195 const bool sm4 = (mRenderer->getMajorShaderModel() >= 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001196 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001197 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1198
1199 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1200 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001201 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001202
1203 return false;
1204 }
1205
1206 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1207 {
1208 bool matched = false;
1209
1210 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1211 {
1212 if (output->name == input->name)
1213 {
1214 if (output->type != input->type || output->size != input->size)
1215 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001216 infoLog.append("Type of vertex varying %s does not match that of the fragment varying", output->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001217
1218 return false;
1219 }
1220
1221 output->reg = input->reg;
1222 output->col = input->col;
1223
1224 matched = true;
1225 break;
1226 }
1227 }
1228
1229 if (!matched)
1230 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001231 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001232
1233 return false;
1234 }
1235 }
1236
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001237 mUsesPointSize = vertexShader->mUsesPointSize;
1238 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001239 std::string targetSemantic = sm4 ? "SV_Target" : "COLOR";
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001240 std::string positionSemantic = sm4 ? "SV_POSITION" : "POSITION";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001241
1242 vertexHLSL += "struct VS_INPUT\n"
1243 "{\n";
1244
1245 int semanticIndex = 0;
1246 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1247 {
1248 switch (attribute->type)
1249 {
1250 case GL_FLOAT: vertexHLSL += " float "; break;
1251 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1252 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1253 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1254 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1255 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1256 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1257 default: UNREACHABLE();
1258 }
1259
1260 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1261
1262 semanticIndex += VariableRowCount(attribute->type);
1263 }
1264
1265 vertexHLSL += "};\n"
1266 "\n"
1267 "struct VS_OUTPUT\n"
1268 "{\n"
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001269 " float4 gl_Position : " + positionSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001270
1271 for (int r = 0; r < registers; r++)
1272 {
1273 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1274
1275 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1276 }
1277
1278 if (fragmentShader->mUsesFragCoord)
1279 {
1280 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1281 }
1282
1283 if (vertexShader->mUsesPointSize && sm3)
1284 {
1285 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1286 }
1287
1288 vertexHLSL += "};\n"
1289 "\n"
1290 "VS_OUTPUT main(VS_INPUT input)\n"
1291 "{\n";
1292
1293 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1294 {
1295 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1296
1297 if (VariableRowCount(attribute->type) > 1) // Matrix
1298 {
1299 vertexHLSL += "transpose";
1300 }
1301
1302 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1303 }
1304
1305 vertexHLSL += "\n"
1306 " gl_main();\n"
1307 "\n"
1308 " VS_OUTPUT output;\n"
1309 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001310 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001311 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1312 " output.gl_Position.w = gl_Position.w;\n";
1313
1314 if (vertexShader->mUsesPointSize && sm3)
1315 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001316 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001317 }
1318
1319 if (fragmentShader->mUsesFragCoord)
1320 {
1321 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1322 }
1323
1324 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1325 {
1326 if (varying->reg >= 0)
1327 {
1328 for (int i = 0; i < varying->size; i++)
1329 {
1330 int rows = VariableRowCount(varying->type);
1331
1332 for (int j = 0; j < rows; j++)
1333 {
1334 int r = varying->reg + i * rows + j;
1335 vertexHLSL += " output.v" + str(r);
1336
1337 bool sharedRegister = false; // Register used by multiple varyings
1338
1339 for (int x = 0; x < 4; x++)
1340 {
1341 if (packing[r][x] && packing[r][x] != packing[r][0])
1342 {
1343 sharedRegister = true;
1344 break;
1345 }
1346 }
1347
1348 if(sharedRegister)
1349 {
1350 vertexHLSL += ".";
1351
1352 for (int x = 0; x < 4; x++)
1353 {
1354 if (packing[r][x] == &*varying)
1355 {
1356 switch(x)
1357 {
1358 case 0: vertexHLSL += "x"; break;
1359 case 1: vertexHLSL += "y"; break;
1360 case 2: vertexHLSL += "z"; break;
1361 case 3: vertexHLSL += "w"; break;
1362 }
1363 }
1364 }
1365 }
1366
1367 vertexHLSL += " = " + varying->name;
1368
1369 if (varying->array)
1370 {
1371 vertexHLSL += "[" + str(i) + "]";
1372 }
1373
1374 if (rows > 1)
1375 {
1376 vertexHLSL += "[" + str(j) + "]";
1377 }
1378
1379 vertexHLSL += ";\n";
1380 }
1381 }
1382 }
1383 }
1384
1385 vertexHLSL += "\n"
1386 " return output;\n"
1387 "}\n";
1388
1389 pixelHLSL += "struct PS_INPUT\n"
1390 "{\n";
1391
1392 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1393 {
1394 if (varying->reg >= 0)
1395 {
1396 for (int i = 0; i < varying->size; i++)
1397 {
1398 int rows = VariableRowCount(varying->type);
1399 for (int j = 0; j < rows; j++)
1400 {
1401 std::string n = str(varying->reg + i * rows + j);
1402 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1403 }
1404 }
1405 }
1406 else UNREACHABLE();
1407 }
1408
1409 if (fragmentShader->mUsesFragCoord)
1410 {
1411 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1412 if (sm3) {
1413 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1414 }
1415 }
1416
1417 if (fragmentShader->mUsesPointCoord && sm3)
1418 {
1419 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1420 }
1421
1422 if (fragmentShader->mUsesFrontFacing)
1423 {
1424 pixelHLSL += " float vFace : VFACE;\n";
1425 }
1426
1427 pixelHLSL += "};\n"
1428 "\n"
1429 "struct PS_OUTPUT\n"
1430 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001431 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001432 "};\n"
1433 "\n"
1434 "PS_OUTPUT main(PS_INPUT input)\n"
1435 "{\n";
1436
1437 if (fragmentShader->mUsesFragCoord)
1438 {
1439 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1440
1441 if (sm3)
1442 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001443 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001444 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001445 }
1446 else
1447 {
1448 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1449 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001450 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001451 }
1452
daniel@transgaming.com12985182012-12-20 20:56:31 +00001453 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001454 " gl_FragCoord.w = rhw;\n";
1455 }
1456
1457 if (fragmentShader->mUsesPointCoord && sm3)
1458 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001459 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1460 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001461 }
1462
1463 if (fragmentShader->mUsesFrontFacing)
1464 {
daniel@transgaming.com12985182012-12-20 20:56:31 +00001465 pixelHLSL += " gl_FrontFacing = (input.vFace * dx_DepthFront.z >= 0.0);\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001466 }
1467
1468 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1469 {
1470 if (varying->reg >= 0)
1471 {
1472 for (int i = 0; i < varying->size; i++)
1473 {
1474 int rows = VariableRowCount(varying->type);
1475 for (int j = 0; j < rows; j++)
1476 {
1477 std::string n = str(varying->reg + i * rows + j);
1478 pixelHLSL += " " + varying->name;
1479
1480 if (varying->array)
1481 {
1482 pixelHLSL += "[" + str(i) + "]";
1483 }
1484
1485 if (rows > 1)
1486 {
1487 pixelHLSL += "[" + str(j) + "]";
1488 }
1489
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001490 switch (VariableColumnCount(varying->type))
1491 {
1492 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1493 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1494 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1495 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1496 default: UNREACHABLE();
1497 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001498 }
1499 }
1500 }
1501 else UNREACHABLE();
1502 }
1503
1504 pixelHLSL += "\n"
1505 " gl_main();\n"
1506 "\n"
1507 " PS_OUTPUT output;\n"
1508 " output.gl_Color[0] = gl_Color[0];\n"
1509 "\n"
1510 " return output;\n"
1511 "}\n";
1512
1513 return true;
1514}
1515
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001516bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1517{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001518 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001519
1520 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001521 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001522 if (format != GL_PROGRAM_BINARY_ANGLE)
1523 {
1524 infoLog.append("Invalid program binary format.");
1525 return false;
1526 }
1527
1528 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001529 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001530 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001531 {
1532 infoLog.append("Invalid program binary version.");
1533 return false;
1534 }
1535
1536 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1537 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001538 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001539 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001540 stream.read(&name);
1541 mLinkedAttribute[i].name = name;
1542 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001543 }
1544
1545 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1546 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001547 stream.read(&mSamplersPS[i].active);
1548 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001549
1550 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001551 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001552 mSamplersPS[i].textureType = (TextureType) textureType;
1553 }
1554
1555 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1556 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001557 stream.read(&mSamplersVS[i].active);
1558 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001559
1560 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001561 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001562 mSamplersVS[i].textureType = (TextureType) textureType;
1563 }
1564
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001565 stream.read(&mUsedVertexSamplerRange);
1566 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001567
1568 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001569 stream.read(&size);
1570 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001571 {
1572 infoLog.append("Invalid program binary.");
1573 return false;
1574 }
1575
1576 mUniforms.resize(size);
1577 for (unsigned int i = 0; i < size; ++i)
1578 {
1579 GLenum type;
1580 std::string _name;
1581 unsigned int arraySize;
1582
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001583 stream.read(&type);
1584 stream.read(&_name);
1585 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001586
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001587 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001588
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001589 stream.read(&mUniforms[i]->ps.registerIndex);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001590 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001591
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001592 stream.read(&mUniforms[i]->vs.registerIndex);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001593 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001594 }
1595
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001596 stream.read(&size);
1597 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001598 {
1599 infoLog.append("Invalid program binary.");
1600 return false;
1601 }
1602
1603 mUniformIndex.resize(size);
1604 for (unsigned int i = 0; i < size; ++i)
1605 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001606 stream.read(&mUniformIndex[i].name);
1607 stream.read(&mUniformIndex[i].element);
1608 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001609 }
1610
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001611 stream.read(&mDxDepthRangeRegisterVS);
1612 stream.read(&mDxDepthRangeRegisterPS);
1613 stream.read(&mDxDepthFrontRegister);
1614 stream.read(&mDxCoordRegister);
1615 stream.read(&mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001616
1617 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001618 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001619
1620 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001621 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001622
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001623 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001624
daniel@transgaming.com36038542012-11-28 20:59:26 +00001625 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001626 ptr += sizeof(GUID);
1627
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001628 GUID identifier = mRenderer->getAdapterIdentifier();
1629 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001630 {
1631 infoLog.append("Invalid program binary.");
1632 return false;
1633 }
1634
1635 const char *pixelShaderFunction = ptr;
1636 ptr += pixelShaderSize;
1637
1638 const char *vertexShaderFunction = ptr;
1639 ptr += vertexShaderSize;
1640
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001641 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1642 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1643 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001644 {
1645 infoLog.append("Could not create pixel shader.");
1646 return false;
1647 }
1648
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001649 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1650 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1651 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001652 {
1653 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001654 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001655 mPixelExecutable = NULL;
1656 return false;
1657 }
1658
1659 return true;
1660}
1661
1662bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1663{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001664 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001665
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001666 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001667 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001668
1669 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1670 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001671 stream.write(mLinkedAttribute[i].type);
1672 stream.write(mLinkedAttribute[i].name);
1673 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001674 }
1675
1676 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1677 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001678 stream.write(mSamplersPS[i].active);
1679 stream.write(mSamplersPS[i].logicalTextureUnit);
1680 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001681 }
1682
1683 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1684 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001685 stream.write(mSamplersVS[i].active);
1686 stream.write(mSamplersVS[i].logicalTextureUnit);
1687 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001688 }
1689
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001690 stream.write(mUsedVertexSamplerRange);
1691 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001692
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001693 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001694 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1695 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001696 stream.write(mUniforms[i]->type);
1697 stream.write(mUniforms[i]->_name);
1698 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001699
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001700 stream.write(mUniforms[i]->ps.registerIndex);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001701 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001702
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001703 stream.write(mUniforms[i]->vs.registerIndex);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001704 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001705 }
1706
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001707 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001708 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1709 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001710 stream.write(mUniformIndex[i].name);
1711 stream.write(mUniformIndex[i].element);
1712 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001713 }
1714
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001715 stream.write(mDxDepthRangeRegisterVS);
1716 stream.write(mDxDepthRangeRegisterPS);
1717 stream.write(mDxDepthFrontRegister);
1718 stream.write(mDxCoordRegister);
1719 stream.write(mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001720
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001721 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001722 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001723
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001724 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001725 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001726
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001727 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001728
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001729 GLsizei streamLength = stream.length();
1730 const void *streamData = stream.data();
1731
1732 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001733 if (totalLength > bufSize)
1734 {
1735 if (length)
1736 {
1737 *length = 0;
1738 }
1739
1740 return false;
1741 }
1742
1743 if (binary)
1744 {
1745 char *ptr = (char*) binary;
1746
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001747 memcpy(ptr, streamData, streamLength);
1748 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001749
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001750 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001751 ptr += sizeof(GUID);
1752
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001753 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001754 ptr += pixelShaderSize;
1755
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001756 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001757 ptr += vertexShaderSize;
1758
1759 ASSERT(ptr - totalLength == binary);
1760 }
1761
1762 if (length)
1763 {
1764 *length = totalLength;
1765 }
1766
1767 return true;
1768}
1769
1770GLint ProgramBinary::getLength()
1771{
1772 GLint length;
1773 if (save(NULL, INT_MAX, &length))
1774 {
1775 return length;
1776 }
1777 else
1778 {
1779 return 0;
1780 }
1781}
1782
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001783bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001784{
1785 if (!fragmentShader || !fragmentShader->isCompiled())
1786 {
1787 return false;
1788 }
1789
1790 if (!vertexShader || !vertexShader->isCompiled())
1791 {
1792 return false;
1793 }
1794
1795 std::string pixelHLSL = fragmentShader->getHLSL();
1796 std::string vertexHLSL = vertexShader->getHLSL();
1797
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001798 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001799 {
1800 return false;
1801 }
1802
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001803 bool success = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001804 rx::D3DConstantTable *constantTableVS = NULL;
1805 rx::D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001806 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1807 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001808
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001809 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001810 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001811 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001812 constantTableVS = mVertexExecutable->getConstantTable();
1813 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001814 }
1815 else
1816 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001817 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001818 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001819
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001820 delete mVertexExecutable;
1821 mVertexExecutable = NULL;
1822 delete mPixelExecutable;
1823 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001824 }
1825
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001826 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1827 {
1828 success = false;
1829 }
1830
1831 if (constantTableVS && constantTablePS)
1832 {
1833 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1834 {
1835 success = false;
1836 }
1837 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001838
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001839 Context *context = getContext();
1840 context->markDxUniformsDirty();
1841
1842 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001843}
1844
1845// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001846bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001847{
1848 unsigned int usedLocations = 0;
1849
1850 // Link attributes that have a binding location
1851 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1852 {
1853 int location = attributeBindings.getAttributeBinding(attribute->name);
1854
1855 if (location != -1) // Set by glBindAttribLocation
1856 {
1857 if (!mLinkedAttribute[location].name.empty())
1858 {
1859 // Multiple active attributes bound to the same location; not an error
1860 }
1861
1862 mLinkedAttribute[location] = *attribute;
1863
1864 int rows = VariableRowCount(attribute->type);
1865
1866 if (rows + location > MAX_VERTEX_ATTRIBS)
1867 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001868 infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute->name.c_str(), location);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001869
1870 return false;
1871 }
1872
1873 for (int i = 0; i < rows; i++)
1874 {
1875 usedLocations |= 1 << (location + i);
1876 }
1877 }
1878 }
1879
1880 // Link attributes that don't have a binding location
1881 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1882 {
1883 int location = attributeBindings.getAttributeBinding(attribute->name);
1884
1885 if (location == -1) // Not set by glBindAttribLocation
1886 {
1887 int rows = VariableRowCount(attribute->type);
1888 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1889
1890 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1891 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001892 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001893
1894 return false; // Fail to link
1895 }
1896
1897 mLinkedAttribute[availableIndex] = *attribute;
1898 }
1899 }
1900
1901 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1902 {
1903 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1904 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1905
1906 for (int r = 0; r < rows; r++)
1907 {
1908 mSemanticIndex[attributeIndex++] = index++;
1909 }
1910 }
1911
1912 return true;
1913}
1914
daniel@transgaming.com31240482012-11-28 21:06:41 +00001915bool ProgramBinary::linkUniforms(InfoLog &infoLog, rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001916{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001917 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001918 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001919 const rx::D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001920
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001921 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001922 {
1923 return false;
1924 }
1925 }
1926
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001927 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1928 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001929 const rx::D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001930
1931 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1932 {
1933 return false;
1934 }
1935 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001936 return true;
1937}
1938
1939// Adds the description of a constant found in the binary shader to the list of uniforms
1940// Returns true if succesful (uniform not already defined)
daniel@transgaming.com31240482012-11-28 21:06:41 +00001941bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const rx::D3DConstant *constant, const std::string &name,
1942 rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001943{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001944 if (constant->registerSet == rx::D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001945 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001946 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001947 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001948 const rx::D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1949 const rx::D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001950
1951 if (psConstant)
1952 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001953 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001954
1955 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1956 {
1957 mSamplersPS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001958 mSamplersPS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001959 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1960 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1961 }
1962 else
1963 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001964 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001965 return false;
1966 }
1967 }
1968
1969 if (vsConstant)
1970 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001971 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001972
1973 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
1974 {
1975 mSamplersVS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001976 mSamplersVS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001977 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
1978 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
1979 }
1980 else
1981 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001982 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001983 return false;
1984 }
1985 }
1986 }
1987 }
1988
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001989 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001990 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001991 case rx::D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001992 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001993 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001994 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001995 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001996 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001997 const rx::D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001998
apatrick@chromium.org85fee292012-09-06 00:43:06 +00001999 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002000
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002001 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002002 {
2003 return false;
2004 }
2005 }
2006 }
2007
2008 return true;
2009 }
daniel@transgaming.com31240482012-11-28 21:06:41 +00002010 case rx::D3DConstant::CLASS_SCALAR:
2011 case rx::D3DConstant::CLASS_VECTOR:
2012 case rx::D3DConstant::CLASS_MATRIX_COLUMNS:
2013 case rx::D3DConstant::CLASS_OBJECT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002014 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002015 default:
2016 UNREACHABLE();
2017 return false;
2018 }
2019}
2020
daniel@transgaming.com31240482012-11-28 21:06:41 +00002021bool ProgramBinary::defineUniform(GLenum shader, const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002023 if (_name == "dx_DepthRange")
2024 {
2025 if (shader == GL_VERTEX_SHADER) mDxDepthRangeRegisterVS = constant->registerIndex;
2026 if (shader == GL_FRAGMENT_SHADER) mDxDepthRangeRegisterPS = constant->registerIndex;
2027 return true;
2028 }
2029
2030 if (_name == "dx_DepthFront")
2031 {
2032 mDxDepthFrontRegister = constant->registerIndex;
2033 return true;
2034 }
2035
2036 if (_name == "dx_Coord")
2037 {
2038 mDxCoordRegister = constant->registerIndex;
2039 return true;
2040 }
2041
2042 if (_name == "dx_HalfPixelSize")
2043 {
2044 mDxHalfPixelSizeRegister = constant->registerIndex;
2045 return true;
2046 }
2047
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002048 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002049
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002050 if (!uniform)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002051 {
2052 return false;
2053 }
2054
2055 // Check if already defined
2056 GLint location = getUniformLocation(uniform->name);
2057 GLenum type = uniform->type;
2058
2059 if (location >= 0)
2060 {
2061 delete uniform;
2062 uniform = mUniforms[mUniformIndex[location].index];
2063 }
2064
daniel@transgaming.comf9561862012-12-20 21:12:07 +00002065 if (shader == GL_FRAGMENT_SHADER)
2066 {
2067 uniform->ps.registerIndex = constant->registerIndex;
2068 uniform->ps.registerCount = constant->registerCount;
2069 }
2070 else if (shader == GL_VERTEX_SHADER)
2071 {
2072 uniform->vs.registerIndex = constant->registerIndex;
2073 uniform->vs.registerCount = constant->registerCount;
2074 }
2075 else UNREACHABLE();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002076
2077 if (location >= 0)
2078 {
2079 return uniform->type == type;
2080 }
2081
2082 mUniforms.push_back(uniform);
2083 unsigned int uniformIndex = mUniforms.size() - 1;
2084
2085 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2086 {
2087 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2088 }
2089
2090 return true;
2091}
2092
daniel@transgaming.com31240482012-11-28 21:06:41 +00002093Uniform *ProgramBinary::createUniform(const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002094{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002095 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002096 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002097 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002098 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002099 case rx::D3DConstant::PT_SAMPLER2D:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002100 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002101 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002102 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002103 default: UNREACHABLE();
2104 }
2105 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002106 case rx::D3DConstant::PT_SAMPLERCUBE:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002107 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002108 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002109 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002110 default: UNREACHABLE();
2111 }
2112 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002113 case rx::D3DConstant::PT_BOOL:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002114 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002115 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002116 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2117 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2118 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2119 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002120 default: UNREACHABLE();
2121 }
2122 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002123 case rx::D3DConstant::PT_INT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002124 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002125 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002126 case 1: return new Uniform(GL_INT, _name, constant->elements);
2127 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2128 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2129 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002130 default: UNREACHABLE();
2131 }
2132 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002133 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002134 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002135 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002136 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2137 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2138 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2139 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002140 default: UNREACHABLE();
2141 }
2142 break;
2143 default:
2144 UNREACHABLE();
2145 }
2146 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002147 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002148 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002149 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002150 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002151 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002152 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002153 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002154 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2155 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2156 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002157 default: UNREACHABLE();
2158 }
2159 break;
2160 default: UNREACHABLE();
2161 }
2162 }
2163 else UNREACHABLE();
2164
2165 return 0;
2166}
2167
2168// This method needs to match OutputHLSL::decorate
2169std::string ProgramBinary::decorateAttribute(const std::string &name)
2170{
2171 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2172 {
2173 return "_" + name;
2174 }
2175
2176 return name;
2177}
2178
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002179bool ProgramBinary::isValidated() const
2180{
2181 return mValidated;
2182}
2183
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002184void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2185{
2186 // Skip over inactive attributes
2187 unsigned int activeAttribute = 0;
2188 unsigned int attribute;
2189 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2190 {
2191 if (mLinkedAttribute[attribute].name.empty())
2192 {
2193 continue;
2194 }
2195
2196 if (activeAttribute == index)
2197 {
2198 break;
2199 }
2200
2201 activeAttribute++;
2202 }
2203
2204 if (bufsize > 0)
2205 {
2206 const char *string = mLinkedAttribute[attribute].name.c_str();
2207
2208 strncpy(name, string, bufsize);
2209 name[bufsize - 1] = '\0';
2210
2211 if (length)
2212 {
2213 *length = strlen(name);
2214 }
2215 }
2216
2217 *size = 1; // Always a single 'type' instance
2218
2219 *type = mLinkedAttribute[attribute].type;
2220}
2221
2222GLint ProgramBinary::getActiveAttributeCount()
2223{
2224 int count = 0;
2225
2226 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2227 {
2228 if (!mLinkedAttribute[attributeIndex].name.empty())
2229 {
2230 count++;
2231 }
2232 }
2233
2234 return count;
2235}
2236
2237GLint ProgramBinary::getActiveAttributeMaxLength()
2238{
2239 int maxLength = 0;
2240
2241 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2242 {
2243 if (!mLinkedAttribute[attributeIndex].name.empty())
2244 {
2245 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2246 }
2247 }
2248
2249 return maxLength;
2250}
2251
2252void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2253{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002254 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002255
2256 if (bufsize > 0)
2257 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002258 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002259
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002260 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002261 {
2262 string += "[0]";
2263 }
2264
2265 strncpy(name, string.c_str(), bufsize);
2266 name[bufsize - 1] = '\0';
2267
2268 if (length)
2269 {
2270 *length = strlen(name);
2271 }
2272 }
2273
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002274 *size = mUniforms[index]->arraySize;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002275
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002276 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002277}
2278
2279GLint ProgramBinary::getActiveUniformCount()
2280{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002281 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002282}
2283
2284GLint ProgramBinary::getActiveUniformMaxLength()
2285{
2286 int maxLength = 0;
2287
2288 unsigned int numUniforms = mUniforms.size();
2289 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2290 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002291 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002292 {
2293 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2294 if (mUniforms[uniformIndex]->isArray())
2295 {
2296 length += 3; // Counting in "[0]".
2297 }
2298 maxLength = std::max(length, maxLength);
2299 }
2300 }
2301
2302 return maxLength;
2303}
2304
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002305void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002306{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002307 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002308 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002309 {
2310 mValidated = false;
2311 }
2312 else
2313 {
2314 mValidated = true;
2315 }
2316}
2317
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002318bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002319{
2320 // if any two active samplers in a program are of different types, but refer to the same
2321 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2322 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2323
2324 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2325 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2326
2327 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2328 {
2329 textureUnitType[i] = TEXTURE_UNKNOWN;
2330 }
2331
2332 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2333 {
2334 if (mSamplersPS[i].active)
2335 {
2336 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2337
2338 if (unit >= maxCombinedTextureImageUnits)
2339 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002340 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002341 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002342 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002343 }
2344
2345 return false;
2346 }
2347
2348 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2349 {
2350 if (mSamplersPS[i].textureType != textureUnitType[unit])
2351 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002352 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002353 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002354 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002355 }
2356
2357 return false;
2358 }
2359 }
2360 else
2361 {
2362 textureUnitType[unit] = mSamplersPS[i].textureType;
2363 }
2364 }
2365 }
2366
2367 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2368 {
2369 if (mSamplersVS[i].active)
2370 {
2371 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2372
2373 if (unit >= maxCombinedTextureImageUnits)
2374 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002375 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002376 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002377 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002378 }
2379
2380 return false;
2381 }
2382
2383 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2384 {
2385 if (mSamplersVS[i].textureType != textureUnitType[unit])
2386 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002387 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002388 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002389 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002390 }
2391
2392 return false;
2393 }
2394 }
2395 else
2396 {
2397 textureUnitType[unit] = mSamplersVS[i].textureType;
2398 }
2399 }
2400 }
2401
2402 return true;
2403}
2404
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002405void ProgramBinary::applyDxDepthRange(float near, float far, float diff)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002406{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002407 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2408 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2409
2410 if (mDxDepthRangeRegisterVS >= 0)
2411 {
2412 float nearFarDiff[4] = {near, far, diff};
2413 device->SetVertexShaderConstantF(mDxDepthRangeRegisterVS, nearFarDiff, 1);
2414 }
2415
2416 if (mDxDepthRangeRegisterPS >= 0)
2417 {
2418 float nearFarDiff[4] = {near, far, diff};
2419 device->SetPixelShaderConstantF(mDxDepthRangeRegisterPS, nearFarDiff, 1);
2420 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002421}
2422
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002423void ProgramBinary::applyDxDepthFront(float range, float start, float frontCCW)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002424{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002425 if (mDxDepthFrontRegister >= 0)
2426 {
2427 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2428 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2429
2430 GLfloat dz[4] = {range, start, frontCCW};
2431 device->SetPixelShaderConstantF(mDxDepthFrontRegister, dz, 1);
2432 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002433}
2434
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002435void ProgramBinary::applyDxCoord(float halfWidth, float halfHeight, float x0, float y0)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002436{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002437 if (mDxCoordRegister >= 0)
2438 {
2439 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2440 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2441
2442 GLfloat whxy[4] = {halfWidth,halfHeight, x0, y0};
2443 device->SetPixelShaderConstantF(mDxCoordRegister, whxy, 1);
2444 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002445}
2446
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002447void ProgramBinary::applyDxHalfPixelSize(float width, float height)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002448{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002449 if (mDxHalfPixelSizeRegister >= 0)
2450 {
2451 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2452 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2453
2454 GLfloat xy[4] = {width, height};
2455 device->SetVertexShaderConstantF(mDxHalfPixelSizeRegister, xy, 1);
2456 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002457}
2458
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002459ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2460{
2461}
2462
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002463}