blob: cb7f3aa1d42ff8add7654855c5a735188d46198e [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"
18#include "libGLESv2/utilities.h"
19
20#include <string>
21
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000022namespace gl
23{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000024std::string str(int i)
25{
26 char buffer[20];
27 snprintf(buffer, sizeof(buffer), "%d", i);
28 return buffer;
29}
30
31Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
32 : type(type), _name(_name), name(ProgramBinary::undecorateUniform(_name)), arraySize(arraySize)
33{
34 int bytes = UniformInternalSize(type) * arraySize;
35 data = new unsigned char[bytes];
36 memset(data, 0, bytes);
37 dirty = true;
38}
39
40Uniform::~Uniform()
41{
42 delete[] data;
43}
44
45bool Uniform::isArray()
46{
daniel@transgaming.com22ba0f72012-09-27 17:46:04 +000047 size_t dot = _name.find_last_of('.');
48 if (dot == std::string::npos) dot = -1;
49
50 return _name.compare(dot + 1, dot + 4, "ar_") == 0;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000051}
52
53UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
54 : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index)
55{
56}
57
daniel@transgaming.come87ca002012-07-24 18:30:43 +000058unsigned int ProgramBinary::mCurrentSerial = 1;
59
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000060ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000061{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000062 mPixelExecutable = NULL;
63 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000064
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000065 mValidated = false;
66
67 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
68 {
69 mSemanticIndex[index] = -1;
70 }
71
72 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
73 {
74 mSamplersPS[index].active = false;
75 }
76
77 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
78 {
79 mSamplersVS[index].active = false;
80 }
81
82 mUsedVertexSamplerRange = 0;
83 mUsedPixelSamplerRange = 0;
84
85 mDxDepthRangeLocation = -1;
86 mDxDepthLocation = -1;
87 mDxCoordLocation = -1;
88 mDxHalfPixelSizeLocation = -1;
89 mDxFrontCCWLocation = -1;
90 mDxPointsOrLinesLocation = -1;
91}
92
93ProgramBinary::~ProgramBinary()
94{
daniel@transgaming.com95892412012-11-28 20:59:09 +000095 delete mPixelExecutable;
96 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000097
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000098 while (!mUniforms.empty())
99 {
100 delete mUniforms.back();
101 mUniforms.pop_back();
102 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000103}
104
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000105unsigned int ProgramBinary::getSerial() const
106{
107 return mSerial;
108}
109
110unsigned int ProgramBinary::issueSerial()
111{
112 return mCurrentSerial++;
113}
114
daniel@transgaming.com95892412012-11-28 20:59:09 +0000115rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000116{
117 return mPixelExecutable;
118}
119
daniel@transgaming.com95892412012-11-28 20:59:09 +0000120rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000121{
122 return mVertexExecutable;
123}
124
125GLuint ProgramBinary::getAttributeLocation(const char *name)
126{
127 if (name)
128 {
129 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
130 {
131 if (mLinkedAttribute[index].name == std::string(name))
132 {
133 return index;
134 }
135 }
136 }
137
138 return -1;
139}
140
141int ProgramBinary::getSemanticIndex(int attributeIndex)
142{
143 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
144
145 return mSemanticIndex[attributeIndex];
146}
147
148// Returns one more than the highest sampler index used.
149GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
150{
151 switch (type)
152 {
153 case SAMPLER_PIXEL:
154 return mUsedPixelSamplerRange;
155 case SAMPLER_VERTEX:
156 return mUsedVertexSamplerRange;
157 default:
158 UNREACHABLE();
159 return 0;
160 }
161}
162
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000163bool ProgramBinary::usesPointSize() const
164{
165 return mUsesPointSize;
166}
167
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000168// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
169// index (0-15 for the pixel shader and 0-3 for the vertex shader).
170GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
171{
172 GLint logicalTextureUnit = -1;
173
174 switch (type)
175 {
176 case SAMPLER_PIXEL:
177 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
178
179 if (mSamplersPS[samplerIndex].active)
180 {
181 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
182 }
183 break;
184 case SAMPLER_VERTEX:
185 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
186
187 if (mSamplersVS[samplerIndex].active)
188 {
189 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
190 }
191 break;
192 default: UNREACHABLE();
193 }
194
195 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
196 {
197 return logicalTextureUnit;
198 }
199
200 return -1;
201}
202
203// Returns the texture type for a given Direct3D 9 sampler type and
204// index (0-15 for the pixel shader and 0-3 for the vertex shader).
205TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
206{
207 switch (type)
208 {
209 case SAMPLER_PIXEL:
210 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
211 ASSERT(mSamplersPS[samplerIndex].active);
212 return mSamplersPS[samplerIndex].textureType;
213 case SAMPLER_VERTEX:
214 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
215 ASSERT(mSamplersVS[samplerIndex].active);
216 return mSamplersVS[samplerIndex].textureType;
217 default: UNREACHABLE();
218 }
219
220 return TEXTURE_2D;
221}
222
223GLint ProgramBinary::getUniformLocation(std::string name)
224{
225 unsigned int subscript = 0;
226
227 // Strip any trailing array operator and retrieve the subscript
228 size_t open = name.find_last_of('[');
229 size_t close = name.find_last_of(']');
230 if (open != std::string::npos && close == name.length() - 1)
231 {
232 subscript = atoi(name.substr(open + 1).c_str());
233 name.erase(open);
234 }
235
236 unsigned int numUniforms = mUniformIndex.size();
237 for (unsigned int location = 0; location < numUniforms; location++)
238 {
239 if (mUniformIndex[location].name == name &&
240 mUniformIndex[location].element == subscript)
241 {
242 return location;
243 }
244 }
245
246 return -1;
247}
248
249bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
250{
251 if (location < 0 || location >= (int)mUniformIndex.size())
252 {
253 return false;
254 }
255
256 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
257 targetUniform->dirty = true;
258
259 if (targetUniform->type == GL_FLOAT)
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
268 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
269
270 for (int i = 0; i < count; i++)
271 {
272 target[0] = v[0];
273 target[1] = 0;
274 target[2] = 0;
275 target[3] = 0;
276 target += 4;
277 v += 1;
278 }
279 }
280 else if (targetUniform->type == GL_BOOL)
281 {
282 int arraySize = targetUniform->arraySize;
283
284 if (arraySize == 1 && count > 1)
285 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
286
287 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
288 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
289
290 for (int i = 0; i < count; ++i)
291 {
292 if (v[i] == 0.0f)
293 {
294 boolParams[i] = GL_FALSE;
295 }
296 else
297 {
298 boolParams[i] = GL_TRUE;
299 }
300 }
301 }
302 else
303 {
304 return false;
305 }
306
307 return true;
308}
309
310bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
311{
312 if (location < 0 || location >= (int)mUniformIndex.size())
313 {
314 return false;
315 }
316
317 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
318 targetUniform->dirty = true;
319
320 if (targetUniform->type == GL_FLOAT_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 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
330
331 for (int i = 0; i < count; i++)
332 {
333 target[0] = v[0];
334 target[1] = v[1];
335 target[2] = 0;
336 target[3] = 0;
337 target += 4;
338 v += 2;
339 }
340 }
341 else if (targetUniform->type == GL_BOOL_VEC2)
342 {
343 int arraySize = targetUniform->arraySize;
344
345 if (arraySize == 1 && count > 1)
346 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
347
348 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
349
350 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
351
352 for (int i = 0; i < count * 2; ++i)
353 {
354 if (v[i] == 0.0f)
355 {
356 boolParams[i] = GL_FALSE;
357 }
358 else
359 {
360 boolParams[i] = GL_TRUE;
361 }
362 }
363 }
364 else
365 {
366 return false;
367 }
368
369 return true;
370}
371
372bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
373{
374 if (location < 0 || location >= (int)mUniformIndex.size())
375 {
376 return false;
377 }
378
379 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
380 targetUniform->dirty = true;
381
382 if (targetUniform->type == GL_FLOAT_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
391 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
392
393 for (int i = 0; i < count; i++)
394 {
395 target[0] = v[0];
396 target[1] = v[1];
397 target[2] = v[2];
398 target[3] = 0;
399 target += 4;
400 v += 3;
401 }
402 }
403 else if (targetUniform->type == GL_BOOL_VEC3)
404 {
405 int arraySize = targetUniform->arraySize;
406
407 if (arraySize == 1 && count > 1)
408 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
409
410 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
411 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
412
413 for (int i = 0; i < count * 3; ++i)
414 {
415 if (v[i] == 0.0f)
416 {
417 boolParams[i] = GL_FALSE;
418 }
419 else
420 {
421 boolParams[i] = GL_TRUE;
422 }
423 }
424 }
425 else
426 {
427 return false;
428 }
429
430 return true;
431}
432
433bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
434{
435 if (location < 0 || location >= (int)mUniformIndex.size())
436 {
437 return false;
438 }
439
440 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
441 targetUniform->dirty = true;
442
443 if (targetUniform->type == GL_FLOAT_VEC4)
444 {
445 int arraySize = targetUniform->arraySize;
446
447 if (arraySize == 1 && count > 1)
448 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
449
450 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
451
452 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
453 v, 4 * sizeof(GLfloat) * count);
454 }
455 else if (targetUniform->type == GL_BOOL_VEC4)
456 {
457 int arraySize = targetUniform->arraySize;
458
459 if (arraySize == 1 && count > 1)
460 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
461
462 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
463 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
464
465 for (int i = 0; i < count * 4; ++i)
466 {
467 if (v[i] == 0.0f)
468 {
469 boolParams[i] = GL_FALSE;
470 }
471 else
472 {
473 boolParams[i] = GL_TRUE;
474 }
475 }
476 }
477 else
478 {
479 return false;
480 }
481
482 return true;
483}
484
485template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
486void transposeMatrix(T *target, const GLfloat *value)
487{
488 int copyWidth = std::min(targetWidth, srcWidth);
489 int copyHeight = std::min(targetHeight, srcHeight);
490
491 for (int x = 0; x < copyWidth; x++)
492 {
493 for (int y = 0; y < copyHeight; y++)
494 {
495 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
496 }
497 }
498 // clear unfilled right side
499 for (int y = 0; y < copyHeight; y++)
500 {
501 for (int x = srcWidth; x < targetWidth; x++)
502 {
503 target[y * targetWidth + x] = (T)0;
504 }
505 }
506 // clear unfilled bottom.
507 for (int y = srcHeight; y < targetHeight; y++)
508 {
509 for (int x = 0; x < targetWidth; x++)
510 {
511 target[y * targetWidth + x] = (T)0;
512 }
513 }
514}
515
516bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
517{
518 if (location < 0 || location >= (int)mUniformIndex.size())
519 {
520 return false;
521 }
522
523 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
524 targetUniform->dirty = true;
525
526 if (targetUniform->type != GL_FLOAT_MAT2)
527 {
528 return false;
529 }
530
531 int arraySize = targetUniform->arraySize;
532
533 if (arraySize == 1 && count > 1)
534 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
535
536 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
537
538 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
539 for (int i = 0; i < count; i++)
540 {
541 transposeMatrix<GLfloat,4,2,2,2>(target, value);
542 target += 8;
543 value += 4;
544 }
545
546 return true;
547}
548
549bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
550{
551 if (location < 0 || location >= (int)mUniformIndex.size())
552 {
553 return false;
554 }
555
556 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
557 targetUniform->dirty = true;
558
559 if (targetUniform->type != GL_FLOAT_MAT3)
560 {
561 return false;
562 }
563
564 int arraySize = targetUniform->arraySize;
565
566 if (arraySize == 1 && count > 1)
567 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
568
569 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
570
571 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
572 for (int i = 0; i < count; i++)
573 {
574 transposeMatrix<GLfloat,4,3,3,3>(target, value);
575 target += 12;
576 value += 9;
577 }
578
579 return true;
580}
581
582
583bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
584{
585 if (location < 0 || location >= (int)mUniformIndex.size())
586 {
587 return false;
588 }
589
590 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
591 targetUniform->dirty = true;
592
593 if (targetUniform->type != GL_FLOAT_MAT4)
594 {
595 return false;
596 }
597
598 int arraySize = targetUniform->arraySize;
599
600 if (arraySize == 1 && count > 1)
601 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
602
603 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
604
605 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
606 for (int i = 0; i < count; i++)
607 {
608 transposeMatrix<GLfloat,4,4,4,4>(target, value);
609 target += 16;
610 value += 16;
611 }
612
613 return true;
614}
615
616bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
617{
618 if (location < 0 || location >= (int)mUniformIndex.size())
619 {
620 return false;
621 }
622
623 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
624 targetUniform->dirty = true;
625
626 if (targetUniform->type == GL_INT ||
627 targetUniform->type == GL_SAMPLER_2D ||
628 targetUniform->type == GL_SAMPLER_CUBE)
629 {
630 int arraySize = targetUniform->arraySize;
631
632 if (arraySize == 1 && count > 1)
633 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
634
635 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
636
637 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
638 v, sizeof(GLint) * count);
639 }
640 else if (targetUniform->type == GL_BOOL)
641 {
642 int arraySize = targetUniform->arraySize;
643
644 if (arraySize == 1 && count > 1)
645 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
646
647 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
648 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
649
650 for (int i = 0; i < count; ++i)
651 {
652 if (v[i] == 0)
653 {
654 boolParams[i] = GL_FALSE;
655 }
656 else
657 {
658 boolParams[i] = GL_TRUE;
659 }
660 }
661 }
662 else
663 {
664 return false;
665 }
666
667 return true;
668}
669
670bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
671{
672 if (location < 0 || location >= (int)mUniformIndex.size())
673 {
674 return false;
675 }
676
677 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
678 targetUniform->dirty = true;
679
680 if (targetUniform->type == GL_INT_VEC2)
681 {
682 int arraySize = targetUniform->arraySize;
683
684 if (arraySize == 1 && count > 1)
685 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
686
687 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
688
689 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
690 v, 2 * sizeof(GLint) * count);
691 }
692 else if (targetUniform->type == GL_BOOL_VEC2)
693 {
694 int arraySize = targetUniform->arraySize;
695
696 if (arraySize == 1 && count > 1)
697 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
698
699 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
700 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
701
702 for (int i = 0; i < count * 2; ++i)
703 {
704 if (v[i] == 0)
705 {
706 boolParams[i] = GL_FALSE;
707 }
708 else
709 {
710 boolParams[i] = GL_TRUE;
711 }
712 }
713 }
714 else
715 {
716 return false;
717 }
718
719 return true;
720}
721
722bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
723{
724 if (location < 0 || location >= (int)mUniformIndex.size())
725 {
726 return false;
727 }
728
729 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
730 targetUniform->dirty = true;
731
732 if (targetUniform->type == GL_INT_VEC3)
733 {
734 int arraySize = targetUniform->arraySize;
735
736 if (arraySize == 1 && count > 1)
737 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
738
739 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
740
741 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
742 v, 3 * sizeof(GLint) * count);
743 }
744 else if (targetUniform->type == GL_BOOL_VEC3)
745 {
746 int arraySize = targetUniform->arraySize;
747
748 if (arraySize == 1 && count > 1)
749 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
750
751 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
752 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
753
754 for (int i = 0; i < count * 3; ++i)
755 {
756 if (v[i] == 0)
757 {
758 boolParams[i] = GL_FALSE;
759 }
760 else
761 {
762 boolParams[i] = GL_TRUE;
763 }
764 }
765 }
766 else
767 {
768 return false;
769 }
770
771 return true;
772}
773
774bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
775{
776 if (location < 0 || location >= (int)mUniformIndex.size())
777 {
778 return false;
779 }
780
781 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
782 targetUniform->dirty = true;
783
784 if (targetUniform->type == GL_INT_VEC4)
785 {
786 int arraySize = targetUniform->arraySize;
787
788 if (arraySize == 1 && count > 1)
789 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
790
791 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
792
793 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
794 v, 4 * sizeof(GLint) * count);
795 }
796 else if (targetUniform->type == GL_BOOL_VEC4)
797 {
798 int arraySize = targetUniform->arraySize;
799
800 if (arraySize == 1 && count > 1)
801 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
802
803 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
804 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
805
806 for (int i = 0; i < count * 4; ++i)
807 {
808 if (v[i] == 0)
809 {
810 boolParams[i] = GL_FALSE;
811 }
812 else
813 {
814 boolParams[i] = GL_TRUE;
815 }
816 }
817 }
818 else
819 {
820 return false;
821 }
822
823 return true;
824}
825
826bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
827{
828 if (location < 0 || location >= (int)mUniformIndex.size())
829 {
830 return false;
831 }
832
833 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
834
835 // sized queries -- ensure the provided buffer is large enough
836 if (bufSize)
837 {
838 int requiredBytes = UniformExternalSize(targetUniform->type);
839 if (*bufSize < requiredBytes)
840 {
841 return false;
842 }
843 }
844
845 switch (targetUniform->type)
846 {
847 case GL_FLOAT_MAT2:
848 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
849 break;
850 case GL_FLOAT_MAT3:
851 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
852 break;
853 case GL_FLOAT_MAT4:
854 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
855 break;
856 default:
857 {
858 unsigned int count = UniformExternalComponentCount(targetUniform->type);
859 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
860
861 switch (UniformComponentType(targetUniform->type))
862 {
863 case GL_BOOL:
864 {
865 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
866
867 for (unsigned int i = 0; i < count; ++i)
868 {
869 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
870 }
871 }
872 break;
873 case GL_FLOAT:
874 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
875 count * sizeof(GLfloat));
876 break;
877 case GL_INT:
878 {
879 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
880
881 for (unsigned int i = 0; i < count; ++i)
882 {
883 params[i] = (float)intParams[i];
884 }
885 }
886 break;
887 default: UNREACHABLE();
888 }
889 }
890 }
891
892 return true;
893}
894
895bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
896{
897 if (location < 0 || location >= (int)mUniformIndex.size())
898 {
899 return false;
900 }
901
902 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
903
904 // sized queries -- ensure the provided buffer is large enough
905 if (bufSize)
906 {
907 int requiredBytes = UniformExternalSize(targetUniform->type);
908 if (*bufSize < requiredBytes)
909 {
910 return false;
911 }
912 }
913
914 switch (targetUniform->type)
915 {
916 case GL_FLOAT_MAT2:
917 {
918 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
919 }
920 break;
921 case GL_FLOAT_MAT3:
922 {
923 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
924 }
925 break;
926 case GL_FLOAT_MAT4:
927 {
928 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
929 }
930 break;
931 default:
932 {
933 unsigned int count = UniformExternalComponentCount(targetUniform->type);
934 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
935
936 switch (UniformComponentType(targetUniform->type))
937 {
938 case GL_BOOL:
939 {
940 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
941
942 for (unsigned int i = 0; i < count; ++i)
943 {
944 params[i] = (GLint)boolParams[i];
945 }
946 }
947 break;
948 case GL_FLOAT:
949 {
950 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
951
952 for (unsigned int i = 0; i < count; ++i)
953 {
954 params[i] = (GLint)floatParams[i];
955 }
956 }
957 break;
958 case GL_INT:
959 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
960 count * sizeof(GLint));
961 break;
962 default: UNREACHABLE();
963 }
964 }
965 }
966
967 return true;
968}
969
970void ProgramBinary::dirtyAllUniforms()
971{
972 unsigned int numUniforms = mUniforms.size();
973 for (unsigned int index = 0; index < numUniforms; index++)
974 {
975 mUniforms[index]->dirty = true;
976 }
977}
978
979// Applies all the uniforms set for this program object to the Direct3D 9 device
980void ProgramBinary::applyUniforms()
981{
daniel@transgaming.com77fbf972012-11-28 21:02:55 +0000982 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) // D3D9_REPLACE
983 {
984 return; // UNIMPLEMENTED
985 }
986
987 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
988
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000989 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) {
990 Uniform *targetUniform = *ub;
991
992 if (targetUniform->dirty)
993 {
994 int arraySize = targetUniform->arraySize;
995 GLfloat *f = (GLfloat*)targetUniform->data;
996 GLint *i = (GLint*)targetUniform->data;
997 GLboolean *b = (GLboolean*)targetUniform->data;
998
999 switch (targetUniform->type)
1000 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001001 case GL_BOOL: applyUniformnbv(device, targetUniform, arraySize, 1, b); break;
1002 case GL_BOOL_VEC2: applyUniformnbv(device, targetUniform, arraySize, 2, b); break;
1003 case GL_BOOL_VEC3: applyUniformnbv(device, targetUniform, arraySize, 3, b); break;
1004 case GL_BOOL_VEC4: applyUniformnbv(device, targetUniform, arraySize, 4, b); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001005 case GL_FLOAT:
1006 case GL_FLOAT_VEC2:
1007 case GL_FLOAT_VEC3:
1008 case GL_FLOAT_VEC4:
1009 case GL_FLOAT_MAT2:
1010 case GL_FLOAT_MAT3:
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001011 case GL_FLOAT_MAT4: applyUniformnfv(device, targetUniform, f); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001012 case GL_SAMPLER_2D:
1013 case GL_SAMPLER_CUBE:
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001014 case GL_INT: applyUniform1iv(device, targetUniform, arraySize, i); break;
1015 case GL_INT_VEC2: applyUniform2iv(device, targetUniform, arraySize, i); break;
1016 case GL_INT_VEC3: applyUniform3iv(device, targetUniform, arraySize, i); break;
1017 case GL_INT_VEC4: applyUniform4iv(device, targetUniform, arraySize, i); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001018 default:
1019 UNREACHABLE();
1020 }
1021
1022 targetUniform->dirty = false;
1023 }
1024 }
1025}
1026
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001027// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1028// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001029int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001030{
1031 Context *context = getContext();
1032 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1033
1034 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1035 {
1036 int n = VariableRowCount(varying->type) * varying->size;
1037 int m = VariableColumnCount(varying->type);
1038 bool success = false;
1039
1040 if (m == 2 || m == 3 || m == 4)
1041 {
1042 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1043 {
1044 bool available = true;
1045
1046 for (int y = 0; y < n && available; y++)
1047 {
1048 for (int x = 0; x < m && available; x++)
1049 {
1050 if (packing[r + y][x])
1051 {
1052 available = false;
1053 }
1054 }
1055 }
1056
1057 if (available)
1058 {
1059 varying->reg = r;
1060 varying->col = 0;
1061
1062 for (int y = 0; y < n; y++)
1063 {
1064 for (int x = 0; x < m; x++)
1065 {
1066 packing[r + y][x] = &*varying;
1067 }
1068 }
1069
1070 success = true;
1071 }
1072 }
1073
1074 if (!success && m == 2)
1075 {
1076 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1077 {
1078 bool available = true;
1079
1080 for (int y = 0; y < n && available; y++)
1081 {
1082 for (int x = 2; x < 4 && available; x++)
1083 {
1084 if (packing[r + y][x])
1085 {
1086 available = false;
1087 }
1088 }
1089 }
1090
1091 if (available)
1092 {
1093 varying->reg = r;
1094 varying->col = 2;
1095
1096 for (int y = 0; y < n; y++)
1097 {
1098 for (int x = 2; x < 4; x++)
1099 {
1100 packing[r + y][x] = &*varying;
1101 }
1102 }
1103
1104 success = true;
1105 }
1106 }
1107 }
1108 }
1109 else if (m == 1)
1110 {
1111 int space[4] = {0};
1112
1113 for (int y = 0; y < maxVaryingVectors; y++)
1114 {
1115 for (int x = 0; x < 4; x++)
1116 {
1117 space[x] += packing[y][x] ? 0 : 1;
1118 }
1119 }
1120
1121 int column = 0;
1122
1123 for (int x = 0; x < 4; x++)
1124 {
1125 if (space[x] >= n && space[x] < space[column])
1126 {
1127 column = x;
1128 }
1129 }
1130
1131 if (space[column] >= n)
1132 {
1133 for (int r = 0; r < maxVaryingVectors; r++)
1134 {
1135 if (!packing[r][column])
1136 {
1137 varying->reg = r;
1138
1139 for (int y = r; y < r + n; y++)
1140 {
1141 packing[y][column] = &*varying;
1142 }
1143
1144 break;
1145 }
1146 }
1147
1148 varying->col = column;
1149
1150 success = true;
1151 }
1152 }
1153 else UNREACHABLE();
1154
1155 if (!success)
1156 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001157 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001158
1159 return -1;
1160 }
1161 }
1162
1163 // Return the number of used registers
1164 int registers = 0;
1165
1166 for (int r = 0; r < maxVaryingVectors; r++)
1167 {
1168 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1169 {
1170 registers++;
1171 }
1172 }
1173
1174 return registers;
1175}
1176
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001177bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001178{
1179 if (pixelHLSL.empty() || vertexHLSL.empty())
1180 {
1181 return false;
1182 }
1183
1184 // Reset the varying register assignments
1185 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1186 {
1187 fragVar->reg = -1;
1188 fragVar->col = -1;
1189 }
1190
1191 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1192 {
1193 vtxVar->reg = -1;
1194 vtxVar->col = -1;
1195 }
1196
1197 // Map the varyings to the register file
1198 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001199 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001200
1201 if (registers < 0)
1202 {
1203 return false;
1204 }
1205
1206 // Write the HLSL input/output declarations
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001207 const bool sm3 = mRenderer->getMajorShaderModel() >= 3;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001208 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001209 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1210
1211 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1212 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001213 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001214
1215 return false;
1216 }
1217
1218 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1219 {
1220 bool matched = false;
1221
1222 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1223 {
1224 if (output->name == input->name)
1225 {
1226 if (output->type != input->type || output->size != input->size)
1227 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001228 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 +00001229
1230 return false;
1231 }
1232
1233 output->reg = input->reg;
1234 output->col = input->col;
1235
1236 matched = true;
1237 break;
1238 }
1239 }
1240
1241 if (!matched)
1242 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001243 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001244
1245 return false;
1246 }
1247 }
1248
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001249 mUsesPointSize = vertexShader->mUsesPointSize;
1250 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001251
1252 vertexHLSL += "struct VS_INPUT\n"
1253 "{\n";
1254
1255 int semanticIndex = 0;
1256 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1257 {
1258 switch (attribute->type)
1259 {
1260 case GL_FLOAT: vertexHLSL += " float "; break;
1261 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1262 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1263 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1264 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1265 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1266 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1267 default: UNREACHABLE();
1268 }
1269
1270 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1271
1272 semanticIndex += VariableRowCount(attribute->type);
1273 }
1274
1275 vertexHLSL += "};\n"
1276 "\n"
1277 "struct VS_OUTPUT\n"
1278 "{\n"
1279 " float4 gl_Position : POSITION;\n";
1280
1281 for (int r = 0; r < registers; r++)
1282 {
1283 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1284
1285 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1286 }
1287
1288 if (fragmentShader->mUsesFragCoord)
1289 {
1290 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1291 }
1292
1293 if (vertexShader->mUsesPointSize && sm3)
1294 {
1295 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1296 }
1297
1298 vertexHLSL += "};\n"
1299 "\n"
1300 "VS_OUTPUT main(VS_INPUT input)\n"
1301 "{\n";
1302
1303 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1304 {
1305 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1306
1307 if (VariableRowCount(attribute->type) > 1) // Matrix
1308 {
1309 vertexHLSL += "transpose";
1310 }
1311
1312 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1313 }
1314
1315 vertexHLSL += "\n"
1316 " gl_main();\n"
1317 "\n"
1318 " VS_OUTPUT output;\n"
1319 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001320 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001321 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1322 " output.gl_Position.w = gl_Position.w;\n";
1323
1324 if (vertexShader->mUsesPointSize && sm3)
1325 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001326 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001327 }
1328
1329 if (fragmentShader->mUsesFragCoord)
1330 {
1331 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1332 }
1333
1334 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1335 {
1336 if (varying->reg >= 0)
1337 {
1338 for (int i = 0; i < varying->size; i++)
1339 {
1340 int rows = VariableRowCount(varying->type);
1341
1342 for (int j = 0; j < rows; j++)
1343 {
1344 int r = varying->reg + i * rows + j;
1345 vertexHLSL += " output.v" + str(r);
1346
1347 bool sharedRegister = false; // Register used by multiple varyings
1348
1349 for (int x = 0; x < 4; x++)
1350 {
1351 if (packing[r][x] && packing[r][x] != packing[r][0])
1352 {
1353 sharedRegister = true;
1354 break;
1355 }
1356 }
1357
1358 if(sharedRegister)
1359 {
1360 vertexHLSL += ".";
1361
1362 for (int x = 0; x < 4; x++)
1363 {
1364 if (packing[r][x] == &*varying)
1365 {
1366 switch(x)
1367 {
1368 case 0: vertexHLSL += "x"; break;
1369 case 1: vertexHLSL += "y"; break;
1370 case 2: vertexHLSL += "z"; break;
1371 case 3: vertexHLSL += "w"; break;
1372 }
1373 }
1374 }
1375 }
1376
1377 vertexHLSL += " = " + varying->name;
1378
1379 if (varying->array)
1380 {
1381 vertexHLSL += "[" + str(i) + "]";
1382 }
1383
1384 if (rows > 1)
1385 {
1386 vertexHLSL += "[" + str(j) + "]";
1387 }
1388
1389 vertexHLSL += ";\n";
1390 }
1391 }
1392 }
1393 }
1394
1395 vertexHLSL += "\n"
1396 " return output;\n"
1397 "}\n";
1398
1399 pixelHLSL += "struct PS_INPUT\n"
1400 "{\n";
1401
1402 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1403 {
1404 if (varying->reg >= 0)
1405 {
1406 for (int i = 0; i < varying->size; i++)
1407 {
1408 int rows = VariableRowCount(varying->type);
1409 for (int j = 0; j < rows; j++)
1410 {
1411 std::string n = str(varying->reg + i * rows + j);
1412 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1413 }
1414 }
1415 }
1416 else UNREACHABLE();
1417 }
1418
1419 if (fragmentShader->mUsesFragCoord)
1420 {
1421 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1422 if (sm3) {
1423 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1424 }
1425 }
1426
1427 if (fragmentShader->mUsesPointCoord && sm3)
1428 {
1429 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1430 }
1431
1432 if (fragmentShader->mUsesFrontFacing)
1433 {
1434 pixelHLSL += " float vFace : VFACE;\n";
1435 }
1436
1437 pixelHLSL += "};\n"
1438 "\n"
1439 "struct PS_OUTPUT\n"
1440 "{\n"
1441 " float4 gl_Color[1] : COLOR;\n"
1442 "};\n"
1443 "\n"
1444 "PS_OUTPUT main(PS_INPUT input)\n"
1445 "{\n";
1446
1447 if (fragmentShader->mUsesFragCoord)
1448 {
1449 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1450
1451 if (sm3)
1452 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001453 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001454 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001455 }
1456 else
1457 {
1458 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1459 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001460 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001461 }
1462
1463 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n"
1464 " gl_FragCoord.w = rhw;\n";
1465 }
1466
1467 if (fragmentShader->mUsesPointCoord && sm3)
1468 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001469 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1470 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001471 }
1472
1473 if (fragmentShader->mUsesFrontFacing)
1474 {
1475 pixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n";
1476 }
1477
1478 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1479 {
1480 if (varying->reg >= 0)
1481 {
1482 for (int i = 0; i < varying->size; i++)
1483 {
1484 int rows = VariableRowCount(varying->type);
1485 for (int j = 0; j < rows; j++)
1486 {
1487 std::string n = str(varying->reg + i * rows + j);
1488 pixelHLSL += " " + varying->name;
1489
1490 if (varying->array)
1491 {
1492 pixelHLSL += "[" + str(i) + "]";
1493 }
1494
1495 if (rows > 1)
1496 {
1497 pixelHLSL += "[" + str(j) + "]";
1498 }
1499
1500 pixelHLSL += " = input.v" + n + ";\n";
1501 }
1502 }
1503 }
1504 else UNREACHABLE();
1505 }
1506
1507 pixelHLSL += "\n"
1508 " gl_main();\n"
1509 "\n"
1510 " PS_OUTPUT output;\n"
1511 " output.gl_Color[0] = gl_Color[0];\n"
1512 "\n"
1513 " return output;\n"
1514 "}\n";
1515
1516 return true;
1517}
1518
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001519bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1520{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001521 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001522
1523 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001524 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001525 if (format != GL_PROGRAM_BINARY_ANGLE)
1526 {
1527 infoLog.append("Invalid program binary format.");
1528 return false;
1529 }
1530
1531 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001532 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001533 if (version != BUILD_REVISION)
1534 {
1535 infoLog.append("Invalid program binary version.");
1536 return false;
1537 }
1538
1539 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1540 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001541 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001542 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001543 stream.read(&name);
1544 mLinkedAttribute[i].name = name;
1545 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001546 }
1547
1548 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1549 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001550 stream.read(&mSamplersPS[i].active);
1551 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001552
1553 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001554 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001555 mSamplersPS[i].textureType = (TextureType) textureType;
1556 }
1557
1558 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1559 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001560 stream.read(&mSamplersVS[i].active);
1561 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001562
1563 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001564 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001565 mSamplersVS[i].textureType = (TextureType) textureType;
1566 }
1567
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001568 stream.read(&mUsedVertexSamplerRange);
1569 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001570
1571 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001572 stream.read(&size);
1573 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001574 {
1575 infoLog.append("Invalid program binary.");
1576 return false;
1577 }
1578
1579 mUniforms.resize(size);
1580 for (unsigned int i = 0; i < size; ++i)
1581 {
1582 GLenum type;
1583 std::string _name;
1584 unsigned int arraySize;
1585
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001586 stream.read(&type);
1587 stream.read(&_name);
1588 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001589
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001590 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001591
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001592 stream.read(&mUniforms[i]->ps.float4Index);
1593 stream.read(&mUniforms[i]->ps.samplerIndex);
1594 stream.read(&mUniforms[i]->ps.boolIndex);
1595 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001596
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001597 stream.read(&mUniforms[i]->vs.float4Index);
1598 stream.read(&mUniforms[i]->vs.samplerIndex);
1599 stream.read(&mUniforms[i]->vs.boolIndex);
1600 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001601 }
1602
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001603 stream.read(&size);
1604 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001605 {
1606 infoLog.append("Invalid program binary.");
1607 return false;
1608 }
1609
1610 mUniformIndex.resize(size);
1611 for (unsigned int i = 0; i < size; ++i)
1612 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001613 stream.read(&mUniformIndex[i].name);
1614 stream.read(&mUniformIndex[i].element);
1615 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001616 }
1617
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001618 stream.read(&mDxDepthRangeLocation);
1619 stream.read(&mDxDepthLocation);
1620 stream.read(&mDxCoordLocation);
1621 stream.read(&mDxHalfPixelSizeLocation);
1622 stream.read(&mDxFrontCCWLocation);
1623 stream.read(&mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001624
1625 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001626 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001627
1628 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001629 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001630
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001631 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001632
daniel@transgaming.com36038542012-11-28 20:59:26 +00001633 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001634 ptr += sizeof(GUID);
1635
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001636 GUID identifier = mRenderer->getAdapterIdentifier();
1637 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001638 {
1639 infoLog.append("Invalid program binary.");
1640 return false;
1641 }
1642
1643 const char *pixelShaderFunction = ptr;
1644 ptr += pixelShaderSize;
1645
1646 const char *vertexShaderFunction = ptr;
1647 ptr += vertexShaderSize;
1648
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001649 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1650 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1651 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001652 {
1653 infoLog.append("Could not create pixel shader.");
1654 return false;
1655 }
1656
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001657 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1658 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1659 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001660 {
1661 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001662 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001663 mPixelExecutable = NULL;
1664 return false;
1665 }
1666
1667 return true;
1668}
1669
1670bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1671{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001672 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001673
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001674 stream.write(GL_PROGRAM_BINARY_ANGLE);
1675 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001676
1677 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1678 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001679 stream.write(mLinkedAttribute[i].type);
1680 stream.write(mLinkedAttribute[i].name);
1681 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001682 }
1683
1684 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1685 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001686 stream.write(mSamplersPS[i].active);
1687 stream.write(mSamplersPS[i].logicalTextureUnit);
1688 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001689 }
1690
1691 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1692 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001693 stream.write(mSamplersVS[i].active);
1694 stream.write(mSamplersVS[i].logicalTextureUnit);
1695 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001696 }
1697
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001698 stream.write(mUsedVertexSamplerRange);
1699 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001700
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001701 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001702 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1703 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001704 stream.write(mUniforms[i]->type);
1705 stream.write(mUniforms[i]->_name);
1706 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001707
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001708 stream.write(mUniforms[i]->ps.float4Index);
1709 stream.write(mUniforms[i]->ps.samplerIndex);
1710 stream.write(mUniforms[i]->ps.boolIndex);
1711 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001712
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001713 stream.write(mUniforms[i]->vs.float4Index);
1714 stream.write(mUniforms[i]->vs.samplerIndex);
1715 stream.write(mUniforms[i]->vs.boolIndex);
1716 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001717 }
1718
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001719 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001720 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1721 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001722 stream.write(mUniformIndex[i].name);
1723 stream.write(mUniformIndex[i].element);
1724 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001725 }
1726
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001727 stream.write(mDxDepthRangeLocation);
1728 stream.write(mDxDepthLocation);
1729 stream.write(mDxCoordLocation);
1730 stream.write(mDxHalfPixelSizeLocation);
1731 stream.write(mDxFrontCCWLocation);
1732 stream.write(mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001733
1734 UINT pixelShaderSize;
daniel@transgaming.comc0ccbd82012-11-28 20:59:37 +00001735 bool result = mPixelExecutable->getPixelFunction(NULL, &pixelShaderSize);
1736 ASSERT(result);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001737 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001738
1739 UINT vertexShaderSize;
daniel@transgaming.comc0ccbd82012-11-28 20:59:37 +00001740 result = mVertexExecutable->getVertexFunction(NULL, &vertexShaderSize);
1741 ASSERT(result);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001742 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001743
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001744 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001745
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001746 GLsizei streamLength = stream.length();
1747 const void *streamData = stream.data();
1748
1749 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001750 if (totalLength > bufSize)
1751 {
1752 if (length)
1753 {
1754 *length = 0;
1755 }
1756
1757 return false;
1758 }
1759
1760 if (binary)
1761 {
1762 char *ptr = (char*) binary;
1763
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001764 memcpy(ptr, streamData, streamLength);
1765 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001766
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001767 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001768 ptr += sizeof(GUID);
1769
daniel@transgaming.comc0ccbd82012-11-28 20:59:37 +00001770 result = mPixelExecutable->getPixelFunction(ptr, &pixelShaderSize);
1771 ASSERT(result);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001772 ptr += pixelShaderSize;
1773
daniel@transgaming.comc0ccbd82012-11-28 20:59:37 +00001774 result = mVertexExecutable->getVertexFunction(ptr, &vertexShaderSize);
1775 ASSERT(result);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001776 ptr += vertexShaderSize;
1777
1778 ASSERT(ptr - totalLength == binary);
1779 }
1780
1781 if (length)
1782 {
1783 *length = totalLength;
1784 }
1785
1786 return true;
1787}
1788
1789GLint ProgramBinary::getLength()
1790{
1791 GLint length;
1792 if (save(NULL, INT_MAX, &length))
1793 {
1794 return length;
1795 }
1796 else
1797 {
1798 return 0;
1799 }
1800}
1801
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001802bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001803{
1804 if (!fragmentShader || !fragmentShader->isCompiled())
1805 {
1806 return false;
1807 }
1808
1809 if (!vertexShader || !vertexShader->isCompiled())
1810 {
1811 return false;
1812 }
1813
1814 std::string pixelHLSL = fragmentShader->getHLSL();
1815 std::string vertexHLSL = vertexShader->getHLSL();
1816
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001817 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001818 {
1819 return false;
1820 }
1821
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001822 bool success = true;
1823 D3DConstantTable *constantTableVS = NULL;
1824 D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001825 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1826 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001827
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001828 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001829 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001830 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001831 constantTableVS = mVertexExecutable->getConstantTable();
1832 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001833 }
1834 else
1835 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001836 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001837 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001838
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001839 delete mVertexExecutable;
1840 mVertexExecutable = NULL;
1841 delete mPixelExecutable;
1842 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001843 }
1844
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001845 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1846 {
1847 success = false;
1848 }
1849
1850 if (constantTableVS && constantTablePS)
1851 {
1852 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1853 {
1854 success = false;
1855 }
1856 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001857
1858 // these uniforms are searched as already-decorated because gl_ and dx_
1859 // are reserved prefixes, and do not receive additional decoration
1860 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1861 mDxDepthLocation = getUniformLocation("dx_Depth");
1862 mDxCoordLocation = getUniformLocation("dx_Coord");
1863 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1864 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1865 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1866
1867 Context *context = getContext();
1868 context->markDxUniformsDirty();
1869
1870 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001871}
1872
1873// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001874bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001875{
1876 unsigned int usedLocations = 0;
1877
1878 // Link attributes that have a binding location
1879 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1880 {
1881 int location = attributeBindings.getAttributeBinding(attribute->name);
1882
1883 if (location != -1) // Set by glBindAttribLocation
1884 {
1885 if (!mLinkedAttribute[location].name.empty())
1886 {
1887 // Multiple active attributes bound to the same location; not an error
1888 }
1889
1890 mLinkedAttribute[location] = *attribute;
1891
1892 int rows = VariableRowCount(attribute->type);
1893
1894 if (rows + location > MAX_VERTEX_ATTRIBS)
1895 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001896 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 +00001897
1898 return false;
1899 }
1900
1901 for (int i = 0; i < rows; i++)
1902 {
1903 usedLocations |= 1 << (location + i);
1904 }
1905 }
1906 }
1907
1908 // Link attributes that don't have a binding location
1909 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1910 {
1911 int location = attributeBindings.getAttributeBinding(attribute->name);
1912
1913 if (location == -1) // Not set by glBindAttribLocation
1914 {
1915 int rows = VariableRowCount(attribute->type);
1916 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1917
1918 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1919 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001920 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001921
1922 return false; // Fail to link
1923 }
1924
1925 mLinkedAttribute[availableIndex] = *attribute;
1926 }
1927 }
1928
1929 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1930 {
1931 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1932 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1933
1934 for (int r = 0; r < rows; r++)
1935 {
1936 mSemanticIndex[attributeIndex++] = index++;
1937 }
1938 }
1939
1940 return true;
1941}
1942
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001943bool ProgramBinary::linkUniforms(InfoLog &infoLog, D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001944{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001945 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001946 {
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001947 const D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001948
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001949 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001950 {
1951 return false;
1952 }
1953 }
1954
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001955 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1956 {
1957 const D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
1958
1959 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1960 {
1961 return false;
1962 }
1963 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001964 return true;
1965}
1966
1967// Adds the description of a constant found in the binary shader to the list of uniforms
1968// Returns true if succesful (uniform not already defined)
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001969bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DConstant *constant, const std::string &name,
1970 D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001971{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001972 if (constant->registerSet == D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001973 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001974 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001975 {
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001976 const D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1977 const D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001978
1979 if (psConstant)
1980 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001981 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001982
1983 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1984 {
1985 mSamplersPS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001986 mSamplersPS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001987 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1988 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1989 }
1990 else
1991 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001992 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001993 return false;
1994 }
1995 }
1996
1997 if (vsConstant)
1998 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001999 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002000
2001 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2002 {
2003 mSamplersVS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002004 mSamplersVS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002005 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2006 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2007 }
2008 else
2009 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002010 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002011 return false;
2012 }
2013 }
2014 }
2015 }
2016
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002017 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002018 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002019 case D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002020 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002021 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002023 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002024 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002025 const D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002026
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002027 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002028
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002029 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002030 {
2031 return false;
2032 }
2033 }
2034 }
2035
2036 return true;
2037 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002038 case D3DConstant::CLASS_SCALAR:
2039 case D3DConstant::CLASS_VECTOR:
2040 case D3DConstant::CLASS_MATRIX_COLUMNS:
2041 case D3DConstant::CLASS_OBJECT:
2042 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002043 default:
2044 UNREACHABLE();
2045 return false;
2046 }
2047}
2048
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002049bool ProgramBinary::defineUniform(GLenum shader, const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002050{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002051 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002052
2053 if(!uniform)
2054 {
2055 return false;
2056 }
2057
2058 // Check if already defined
2059 GLint location = getUniformLocation(uniform->name);
2060 GLenum type = uniform->type;
2061
2062 if (location >= 0)
2063 {
2064 delete uniform;
2065 uniform = mUniforms[mUniformIndex[location].index];
2066 }
2067
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002068 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2069 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002070
2071 if (location >= 0)
2072 {
2073 return uniform->type == type;
2074 }
2075
2076 mUniforms.push_back(uniform);
2077 unsigned int uniformIndex = mUniforms.size() - 1;
2078
2079 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2080 {
2081 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2082 }
2083
2084 return true;
2085}
2086
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002087Uniform *ProgramBinary::createUniform(const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002088{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002089 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002090 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002091 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002092 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002093 case D3DConstant::PT_SAMPLER2D:
2094 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002095 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002096 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002097 default: UNREACHABLE();
2098 }
2099 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002100 case D3DConstant::PT_SAMPLERCUBE:
2101 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002102 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002103 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002104 default: UNREACHABLE();
2105 }
2106 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002107 case D3DConstant::PT_BOOL:
2108 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002109 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002110 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2111 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2112 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2113 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002114 default: UNREACHABLE();
2115 }
2116 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002117 case D3DConstant::PT_INT:
2118 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002119 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002120 case 1: return new Uniform(GL_INT, _name, constant->elements);
2121 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2122 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2123 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002124 default: UNREACHABLE();
2125 }
2126 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002127 case D3DConstant::PT_FLOAT:
2128 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002129 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002130 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2131 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2132 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2133 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002134 default: UNREACHABLE();
2135 }
2136 break;
2137 default:
2138 UNREACHABLE();
2139 }
2140 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002141 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002142 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002143 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002144 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002145 case D3DConstant::PT_FLOAT:
2146 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002147 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002148 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2149 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2150 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002151 default: UNREACHABLE();
2152 }
2153 break;
2154 default: UNREACHABLE();
2155 }
2156 }
2157 else UNREACHABLE();
2158
2159 return 0;
2160}
2161
2162// This method needs to match OutputHLSL::decorate
2163std::string ProgramBinary::decorateAttribute(const std::string &name)
2164{
2165 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2166 {
2167 return "_" + name;
2168 }
2169
2170 return name;
2171}
2172
2173std::string ProgramBinary::undecorateUniform(const std::string &_name)
2174{
2175 std::string name = _name;
2176
2177 // Remove any structure field decoration
2178 size_t pos = 0;
2179 while ((pos = name.find("._", pos)) != std::string::npos)
2180 {
2181 name.replace(pos, 2, ".");
2182 }
2183
2184 // Remove the leading decoration
2185 if (name[0] == '_')
2186 {
2187 return name.substr(1);
2188 }
2189 else if (name.compare(0, 3, "ar_") == 0)
2190 {
2191 return name.substr(3);
2192 }
2193
2194 return name;
2195}
2196
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002197// D3D9_REPLACE begin
2198void ProgramBinary::applyUniformnbv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002199{
2200 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2201 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2202
2203 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2204 {
2205 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2206 for (int i = 0; i < count; i++)
2207 {
2208 for (int j = 0; j < 4; j++)
2209 {
2210 if (j < width)
2211 {
2212 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2213 }
2214 else
2215 {
2216 vector[i * 4 + j] = 0.0f;
2217 }
2218 }
2219 }
2220 }
2221
2222 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2223 {
2224 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2225 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2226 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2227 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2228 for (int i = 0; i < copyCount; i++)
2229 {
2230 boolVector[i] = v[i] != GL_FALSE;
2231 }
2232 }
2233
2234 if (targetUniform->ps.float4Index >= 0)
2235 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002236 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002237 }
2238
2239 if (targetUniform->ps.boolIndex >= 0)
2240 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002241 device->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002242 }
2243
2244 if (targetUniform->vs.float4Index >= 0)
2245 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002246 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002247 }
2248
2249 if (targetUniform->vs.boolIndex >= 0)
2250 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002251 device->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002252 }
2253}
2254
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002255bool ProgramBinary::applyUniformnfv(IDirect3DDevice9 *device, Uniform *targetUniform, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002256{
2257 if (targetUniform->ps.registerCount)
2258 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002259 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002260 }
2261
2262 if (targetUniform->vs.registerCount)
2263 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002264 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002265 }
2266
2267 return true;
2268}
2269
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002270bool ProgramBinary::applyUniform1iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002271{
2272 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002273 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002274
2275 for (int i = 0; i < count; i++)
2276 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002277 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002278 }
2279
2280 if (targetUniform->ps.registerCount)
2281 {
2282 if (targetUniform->ps.samplerIndex >= 0)
2283 {
2284 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2285
2286 for (int i = 0; i < count; i++)
2287 {
2288 unsigned int samplerIndex = firstIndex + i;
2289
2290 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2291 {
2292 ASSERT(mSamplersPS[samplerIndex].active);
2293 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2294 }
2295 }
2296 }
2297 else
2298 {
2299 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002300 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002301 }
2302 }
2303
2304 if (targetUniform->vs.registerCount)
2305 {
2306 if (targetUniform->vs.samplerIndex >= 0)
2307 {
2308 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2309
2310 for (int i = 0; i < count; i++)
2311 {
2312 unsigned int samplerIndex = firstIndex + i;
2313
2314 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2315 {
2316 ASSERT(mSamplersVS[samplerIndex].active);
2317 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2318 }
2319 }
2320 }
2321 else
2322 {
2323 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002324 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002325 }
2326 }
2327
2328 return true;
2329}
2330
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002331bool ProgramBinary::applyUniform2iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002332{
2333 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002334 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002335
2336 for (int i = 0; i < count; i++)
2337 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002338 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002339
2340 v += 2;
2341 }
2342
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002343 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002344
2345 return true;
2346}
2347
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002348bool ProgramBinary::applyUniform3iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002349{
2350 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002351 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002352
2353 for (int i = 0; i < count; i++)
2354 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002355 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002356
2357 v += 3;
2358 }
2359
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002360 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002361
2362 return true;
2363}
2364
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002365bool ProgramBinary::applyUniform4iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002366{
2367 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002368 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002369
2370 for (int i = 0; i < count; i++)
2371 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002372 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002373
2374 v += 4;
2375 }
2376
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002377 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002378
2379 return true;
2380}
2381
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002382void ProgramBinary::applyUniformniv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002383{
2384 if (targetUniform->ps.registerCount)
2385 {
2386 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002387 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002388 }
2389
2390 if (targetUniform->vs.registerCount)
2391 {
2392 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002393 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002394 }
2395}
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002396// D3D9_REPLACE end
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002397
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002398bool ProgramBinary::isValidated() const
2399{
2400 return mValidated;
2401}
2402
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002403void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2404{
2405 // Skip over inactive attributes
2406 unsigned int activeAttribute = 0;
2407 unsigned int attribute;
2408 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2409 {
2410 if (mLinkedAttribute[attribute].name.empty())
2411 {
2412 continue;
2413 }
2414
2415 if (activeAttribute == index)
2416 {
2417 break;
2418 }
2419
2420 activeAttribute++;
2421 }
2422
2423 if (bufsize > 0)
2424 {
2425 const char *string = mLinkedAttribute[attribute].name.c_str();
2426
2427 strncpy(name, string, bufsize);
2428 name[bufsize - 1] = '\0';
2429
2430 if (length)
2431 {
2432 *length = strlen(name);
2433 }
2434 }
2435
2436 *size = 1; // Always a single 'type' instance
2437
2438 *type = mLinkedAttribute[attribute].type;
2439}
2440
2441GLint ProgramBinary::getActiveAttributeCount()
2442{
2443 int count = 0;
2444
2445 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2446 {
2447 if (!mLinkedAttribute[attributeIndex].name.empty())
2448 {
2449 count++;
2450 }
2451 }
2452
2453 return count;
2454}
2455
2456GLint ProgramBinary::getActiveAttributeMaxLength()
2457{
2458 int maxLength = 0;
2459
2460 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2461 {
2462 if (!mLinkedAttribute[attributeIndex].name.empty())
2463 {
2464 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2465 }
2466 }
2467
2468 return maxLength;
2469}
2470
2471void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2472{
2473 // Skip over internal uniforms
2474 unsigned int activeUniform = 0;
2475 unsigned int uniform;
2476 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2477 {
2478 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2479 {
2480 continue;
2481 }
2482
2483 if (activeUniform == index)
2484 {
2485 break;
2486 }
2487
2488 activeUniform++;
2489 }
2490
2491 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2492
2493 if (bufsize > 0)
2494 {
2495 std::string string = mUniforms[uniform]->name;
2496
2497 if (mUniforms[uniform]->isArray())
2498 {
2499 string += "[0]";
2500 }
2501
2502 strncpy(name, string.c_str(), bufsize);
2503 name[bufsize - 1] = '\0';
2504
2505 if (length)
2506 {
2507 *length = strlen(name);
2508 }
2509 }
2510
2511 *size = mUniforms[uniform]->arraySize;
2512
2513 *type = mUniforms[uniform]->type;
2514}
2515
2516GLint ProgramBinary::getActiveUniformCount()
2517{
2518 int count = 0;
2519
2520 unsigned int numUniforms = mUniforms.size();
2521 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2522 {
2523 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2524 {
2525 count++;
2526 }
2527 }
2528
2529 return count;
2530}
2531
2532GLint ProgramBinary::getActiveUniformMaxLength()
2533{
2534 int maxLength = 0;
2535
2536 unsigned int numUniforms = mUniforms.size();
2537 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2538 {
2539 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2540 {
2541 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2542 if (mUniforms[uniformIndex]->isArray())
2543 {
2544 length += 3; // Counting in "[0]".
2545 }
2546 maxLength = std::max(length, maxLength);
2547 }
2548 }
2549
2550 return maxLength;
2551}
2552
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002553void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002554{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002555 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002556 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002557 {
2558 mValidated = false;
2559 }
2560 else
2561 {
2562 mValidated = true;
2563 }
2564}
2565
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002566bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002567{
2568 // if any two active samplers in a program are of different types, but refer to the same
2569 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2570 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2571
2572 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2573 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2574
2575 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2576 {
2577 textureUnitType[i] = TEXTURE_UNKNOWN;
2578 }
2579
2580 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2581 {
2582 if (mSamplersPS[i].active)
2583 {
2584 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2585
2586 if (unit >= maxCombinedTextureImageUnits)
2587 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002588 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002589 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002590 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002591 }
2592
2593 return false;
2594 }
2595
2596 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2597 {
2598 if (mSamplersPS[i].textureType != textureUnitType[unit])
2599 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002600 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002601 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002602 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002603 }
2604
2605 return false;
2606 }
2607 }
2608 else
2609 {
2610 textureUnitType[unit] = mSamplersPS[i].textureType;
2611 }
2612 }
2613 }
2614
2615 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2616 {
2617 if (mSamplersVS[i].active)
2618 {
2619 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2620
2621 if (unit >= maxCombinedTextureImageUnits)
2622 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002623 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002624 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002625 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002626 }
2627
2628 return false;
2629 }
2630
2631 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2632 {
2633 if (mSamplersVS[i].textureType != textureUnitType[unit])
2634 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002635 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002636 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002637 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002638 }
2639
2640 return false;
2641 }
2642 }
2643 else
2644 {
2645 textureUnitType[unit] = mSamplersVS[i].textureType;
2646 }
2647 }
2648 }
2649
2650 return true;
2651}
2652
2653GLint ProgramBinary::getDxDepthRangeLocation() const
2654{
2655 return mDxDepthRangeLocation;
2656}
2657
2658GLint ProgramBinary::getDxDepthLocation() const
2659{
2660 return mDxDepthLocation;
2661}
2662
2663GLint ProgramBinary::getDxCoordLocation() const
2664{
2665 return mDxCoordLocation;
2666}
2667
2668GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2669{
2670 return mDxHalfPixelSizeLocation;
2671}
2672
2673GLint ProgramBinary::getDxFrontCCWLocation() const
2674{
2675 return mDxFrontCCWLocation;
2676}
2677
2678GLint ProgramBinary::getDxPointsOrLinesLocation() const
2679{
2680 return mDxPointsOrLinesLocation;
2681}
2682
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002683ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2684{
2685}
2686
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002687}