blob: e73f7c7d2e159cab8369fc385526784f94f645ac [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.comc5693152012-11-28 21:03:02 +00001207 const bool sm3 = (mRenderer->getMajorShaderModel() >= 3);
1208 const bool sm4 = (mRenderer->getMajorShaderModel() >= 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001209 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001210 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1211
1212 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1213 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001214 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001215
1216 return false;
1217 }
1218
1219 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1220 {
1221 bool matched = false;
1222
1223 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1224 {
1225 if (output->name == input->name)
1226 {
1227 if (output->type != input->type || output->size != input->size)
1228 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001229 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 +00001230
1231 return false;
1232 }
1233
1234 output->reg = input->reg;
1235 output->col = input->col;
1236
1237 matched = true;
1238 break;
1239 }
1240 }
1241
1242 if (!matched)
1243 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001244 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001245
1246 return false;
1247 }
1248 }
1249
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001250 mUsesPointSize = vertexShader->mUsesPointSize;
1251 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001252 std::string targetSemantic = sm4 ? "SV_Target" : "COLOR";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001253
1254 vertexHLSL += "struct VS_INPUT\n"
1255 "{\n";
1256
1257 int semanticIndex = 0;
1258 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1259 {
1260 switch (attribute->type)
1261 {
1262 case GL_FLOAT: vertexHLSL += " float "; break;
1263 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1264 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1265 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1266 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1267 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1268 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1269 default: UNREACHABLE();
1270 }
1271
1272 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1273
1274 semanticIndex += VariableRowCount(attribute->type);
1275 }
1276
1277 vertexHLSL += "};\n"
1278 "\n"
1279 "struct VS_OUTPUT\n"
1280 "{\n"
1281 " float4 gl_Position : POSITION;\n";
1282
1283 for (int r = 0; r < registers; r++)
1284 {
1285 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1286
1287 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1288 }
1289
1290 if (fragmentShader->mUsesFragCoord)
1291 {
1292 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1293 }
1294
1295 if (vertexShader->mUsesPointSize && sm3)
1296 {
1297 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1298 }
1299
1300 vertexHLSL += "};\n"
1301 "\n"
1302 "VS_OUTPUT main(VS_INPUT input)\n"
1303 "{\n";
1304
1305 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1306 {
1307 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1308
1309 if (VariableRowCount(attribute->type) > 1) // Matrix
1310 {
1311 vertexHLSL += "transpose";
1312 }
1313
1314 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1315 }
1316
1317 vertexHLSL += "\n"
1318 " gl_main();\n"
1319 "\n"
1320 " VS_OUTPUT output;\n"
1321 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001322 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001323 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1324 " output.gl_Position.w = gl_Position.w;\n";
1325
1326 if (vertexShader->mUsesPointSize && sm3)
1327 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001328 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001329 }
1330
1331 if (fragmentShader->mUsesFragCoord)
1332 {
1333 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1334 }
1335
1336 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1337 {
1338 if (varying->reg >= 0)
1339 {
1340 for (int i = 0; i < varying->size; i++)
1341 {
1342 int rows = VariableRowCount(varying->type);
1343
1344 for (int j = 0; j < rows; j++)
1345 {
1346 int r = varying->reg + i * rows + j;
1347 vertexHLSL += " output.v" + str(r);
1348
1349 bool sharedRegister = false; // Register used by multiple varyings
1350
1351 for (int x = 0; x < 4; x++)
1352 {
1353 if (packing[r][x] && packing[r][x] != packing[r][0])
1354 {
1355 sharedRegister = true;
1356 break;
1357 }
1358 }
1359
1360 if(sharedRegister)
1361 {
1362 vertexHLSL += ".";
1363
1364 for (int x = 0; x < 4; x++)
1365 {
1366 if (packing[r][x] == &*varying)
1367 {
1368 switch(x)
1369 {
1370 case 0: vertexHLSL += "x"; break;
1371 case 1: vertexHLSL += "y"; break;
1372 case 2: vertexHLSL += "z"; break;
1373 case 3: vertexHLSL += "w"; break;
1374 }
1375 }
1376 }
1377 }
1378
1379 vertexHLSL += " = " + varying->name;
1380
1381 if (varying->array)
1382 {
1383 vertexHLSL += "[" + str(i) + "]";
1384 }
1385
1386 if (rows > 1)
1387 {
1388 vertexHLSL += "[" + str(j) + "]";
1389 }
1390
1391 vertexHLSL += ";\n";
1392 }
1393 }
1394 }
1395 }
1396
1397 vertexHLSL += "\n"
1398 " return output;\n"
1399 "}\n";
1400
1401 pixelHLSL += "struct PS_INPUT\n"
1402 "{\n";
1403
1404 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1405 {
1406 if (varying->reg >= 0)
1407 {
1408 for (int i = 0; i < varying->size; i++)
1409 {
1410 int rows = VariableRowCount(varying->type);
1411 for (int j = 0; j < rows; j++)
1412 {
1413 std::string n = str(varying->reg + i * rows + j);
1414 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1415 }
1416 }
1417 }
1418 else UNREACHABLE();
1419 }
1420
1421 if (fragmentShader->mUsesFragCoord)
1422 {
1423 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1424 if (sm3) {
1425 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1426 }
1427 }
1428
1429 if (fragmentShader->mUsesPointCoord && sm3)
1430 {
1431 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1432 }
1433
1434 if (fragmentShader->mUsesFrontFacing)
1435 {
1436 pixelHLSL += " float vFace : VFACE;\n";
1437 }
1438
1439 pixelHLSL += "};\n"
1440 "\n"
1441 "struct PS_OUTPUT\n"
1442 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001443 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001444 "};\n"
1445 "\n"
1446 "PS_OUTPUT main(PS_INPUT input)\n"
1447 "{\n";
1448
1449 if (fragmentShader->mUsesFragCoord)
1450 {
1451 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1452
1453 if (sm3)
1454 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001455 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001456 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001457 }
1458 else
1459 {
1460 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1461 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001462 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001463 }
1464
1465 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n"
1466 " gl_FragCoord.w = rhw;\n";
1467 }
1468
1469 if (fragmentShader->mUsesPointCoord && sm3)
1470 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001471 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1472 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001473 }
1474
1475 if (fragmentShader->mUsesFrontFacing)
1476 {
1477 pixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n";
1478 }
1479
1480 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1481 {
1482 if (varying->reg >= 0)
1483 {
1484 for (int i = 0; i < varying->size; i++)
1485 {
1486 int rows = VariableRowCount(varying->type);
1487 for (int j = 0; j < rows; j++)
1488 {
1489 std::string n = str(varying->reg + i * rows + j);
1490 pixelHLSL += " " + varying->name;
1491
1492 if (varying->array)
1493 {
1494 pixelHLSL += "[" + str(i) + "]";
1495 }
1496
1497 if (rows > 1)
1498 {
1499 pixelHLSL += "[" + str(j) + "]";
1500 }
1501
1502 pixelHLSL += " = input.v" + n + ";\n";
1503 }
1504 }
1505 }
1506 else UNREACHABLE();
1507 }
1508
1509 pixelHLSL += "\n"
1510 " gl_main();\n"
1511 "\n"
1512 " PS_OUTPUT output;\n"
1513 " output.gl_Color[0] = gl_Color[0];\n"
1514 "\n"
1515 " return output;\n"
1516 "}\n";
1517
1518 return true;
1519}
1520
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001521bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1522{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001523 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001524
1525 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001526 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001527 if (format != GL_PROGRAM_BINARY_ANGLE)
1528 {
1529 infoLog.append("Invalid program binary format.");
1530 return false;
1531 }
1532
1533 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001534 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001535 if (version != BUILD_REVISION)
1536 {
1537 infoLog.append("Invalid program binary version.");
1538 return false;
1539 }
1540
1541 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1542 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001543 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001544 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001545 stream.read(&name);
1546 mLinkedAttribute[i].name = name;
1547 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001548 }
1549
1550 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1551 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001552 stream.read(&mSamplersPS[i].active);
1553 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001554
1555 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001556 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001557 mSamplersPS[i].textureType = (TextureType) textureType;
1558 }
1559
1560 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1561 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001562 stream.read(&mSamplersVS[i].active);
1563 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001564
1565 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001566 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001567 mSamplersVS[i].textureType = (TextureType) textureType;
1568 }
1569
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001570 stream.read(&mUsedVertexSamplerRange);
1571 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001572
1573 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001574 stream.read(&size);
1575 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001576 {
1577 infoLog.append("Invalid program binary.");
1578 return false;
1579 }
1580
1581 mUniforms.resize(size);
1582 for (unsigned int i = 0; i < size; ++i)
1583 {
1584 GLenum type;
1585 std::string _name;
1586 unsigned int arraySize;
1587
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001588 stream.read(&type);
1589 stream.read(&_name);
1590 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001591
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001592 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001593
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001594 stream.read(&mUniforms[i]->ps.float4Index);
1595 stream.read(&mUniforms[i]->ps.samplerIndex);
1596 stream.read(&mUniforms[i]->ps.boolIndex);
1597 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001598
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001599 stream.read(&mUniforms[i]->vs.float4Index);
1600 stream.read(&mUniforms[i]->vs.samplerIndex);
1601 stream.read(&mUniforms[i]->vs.boolIndex);
1602 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001603 }
1604
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001605 stream.read(&size);
1606 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001607 {
1608 infoLog.append("Invalid program binary.");
1609 return false;
1610 }
1611
1612 mUniformIndex.resize(size);
1613 for (unsigned int i = 0; i < size; ++i)
1614 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001615 stream.read(&mUniformIndex[i].name);
1616 stream.read(&mUniformIndex[i].element);
1617 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001618 }
1619
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001620 stream.read(&mDxDepthRangeLocation);
1621 stream.read(&mDxDepthLocation);
1622 stream.read(&mDxCoordLocation);
1623 stream.read(&mDxHalfPixelSizeLocation);
1624 stream.read(&mDxFrontCCWLocation);
1625 stream.read(&mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001626
1627 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001628 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001629
1630 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001631 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001632
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001633 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001634
daniel@transgaming.com36038542012-11-28 20:59:26 +00001635 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001636 ptr += sizeof(GUID);
1637
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001638 GUID identifier = mRenderer->getAdapterIdentifier();
1639 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001640 {
1641 infoLog.append("Invalid program binary.");
1642 return false;
1643 }
1644
1645 const char *pixelShaderFunction = ptr;
1646 ptr += pixelShaderSize;
1647
1648 const char *vertexShaderFunction = ptr;
1649 ptr += vertexShaderSize;
1650
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001651 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1652 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1653 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001654 {
1655 infoLog.append("Could not create pixel shader.");
1656 return false;
1657 }
1658
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001659 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1660 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1661 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001662 {
1663 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001664 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001665 mPixelExecutable = NULL;
1666 return false;
1667 }
1668
1669 return true;
1670}
1671
1672bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1673{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001674 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001675
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001676 stream.write(GL_PROGRAM_BINARY_ANGLE);
1677 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001678
1679 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1680 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001681 stream.write(mLinkedAttribute[i].type);
1682 stream.write(mLinkedAttribute[i].name);
1683 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001684 }
1685
1686 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1687 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001688 stream.write(mSamplersPS[i].active);
1689 stream.write(mSamplersPS[i].logicalTextureUnit);
1690 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001691 }
1692
1693 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1694 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001695 stream.write(mSamplersVS[i].active);
1696 stream.write(mSamplersVS[i].logicalTextureUnit);
1697 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001698 }
1699
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001700 stream.write(mUsedVertexSamplerRange);
1701 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001702
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001703 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001704 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1705 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001706 stream.write(mUniforms[i]->type);
1707 stream.write(mUniforms[i]->_name);
1708 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001709
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001710 stream.write(mUniforms[i]->ps.float4Index);
1711 stream.write(mUniforms[i]->ps.samplerIndex);
1712 stream.write(mUniforms[i]->ps.boolIndex);
1713 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001714
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001715 stream.write(mUniforms[i]->vs.float4Index);
1716 stream.write(mUniforms[i]->vs.samplerIndex);
1717 stream.write(mUniforms[i]->vs.boolIndex);
1718 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001719 }
1720
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001721 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001722 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1723 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001724 stream.write(mUniformIndex[i].name);
1725 stream.write(mUniformIndex[i].element);
1726 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001727 }
1728
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001729 stream.write(mDxDepthRangeLocation);
1730 stream.write(mDxDepthLocation);
1731 stream.write(mDxCoordLocation);
1732 stream.write(mDxHalfPixelSizeLocation);
1733 stream.write(mDxFrontCCWLocation);
1734 stream.write(mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001735
1736 UINT pixelShaderSize;
daniel@transgaming.comc0ccbd82012-11-28 20:59:37 +00001737 bool result = mPixelExecutable->getPixelFunction(NULL, &pixelShaderSize);
1738 ASSERT(result);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001739 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001740
1741 UINT vertexShaderSize;
daniel@transgaming.comc0ccbd82012-11-28 20:59:37 +00001742 result = mVertexExecutable->getVertexFunction(NULL, &vertexShaderSize);
1743 ASSERT(result);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001744 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001745
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001746 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001747
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001748 GLsizei streamLength = stream.length();
1749 const void *streamData = stream.data();
1750
1751 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001752 if (totalLength > bufSize)
1753 {
1754 if (length)
1755 {
1756 *length = 0;
1757 }
1758
1759 return false;
1760 }
1761
1762 if (binary)
1763 {
1764 char *ptr = (char*) binary;
1765
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001766 memcpy(ptr, streamData, streamLength);
1767 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001768
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001769 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001770 ptr += sizeof(GUID);
1771
daniel@transgaming.comc0ccbd82012-11-28 20:59:37 +00001772 result = mPixelExecutable->getPixelFunction(ptr, &pixelShaderSize);
1773 ASSERT(result);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001774 ptr += pixelShaderSize;
1775
daniel@transgaming.comc0ccbd82012-11-28 20:59:37 +00001776 result = mVertexExecutable->getVertexFunction(ptr, &vertexShaderSize);
1777 ASSERT(result);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001778 ptr += vertexShaderSize;
1779
1780 ASSERT(ptr - totalLength == binary);
1781 }
1782
1783 if (length)
1784 {
1785 *length = totalLength;
1786 }
1787
1788 return true;
1789}
1790
1791GLint ProgramBinary::getLength()
1792{
1793 GLint length;
1794 if (save(NULL, INT_MAX, &length))
1795 {
1796 return length;
1797 }
1798 else
1799 {
1800 return 0;
1801 }
1802}
1803
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001804bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001805{
1806 if (!fragmentShader || !fragmentShader->isCompiled())
1807 {
1808 return false;
1809 }
1810
1811 if (!vertexShader || !vertexShader->isCompiled())
1812 {
1813 return false;
1814 }
1815
1816 std::string pixelHLSL = fragmentShader->getHLSL();
1817 std::string vertexHLSL = vertexShader->getHLSL();
1818
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001819 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001820 {
1821 return false;
1822 }
1823
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001824 bool success = true;
1825 D3DConstantTable *constantTableVS = NULL;
1826 D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001827 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1828 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001829
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001830 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001831 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001832 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001833 constantTableVS = mVertexExecutable->getConstantTable();
1834 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001835 }
1836 else
1837 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001838 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001839 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001840
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001841 delete mVertexExecutable;
1842 mVertexExecutable = NULL;
1843 delete mPixelExecutable;
1844 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001845 }
1846
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001847 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1848 {
1849 success = false;
1850 }
1851
1852 if (constantTableVS && constantTablePS)
1853 {
1854 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1855 {
1856 success = false;
1857 }
1858 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001859
1860 // these uniforms are searched as already-decorated because gl_ and dx_
1861 // are reserved prefixes, and do not receive additional decoration
1862 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1863 mDxDepthLocation = getUniformLocation("dx_Depth");
1864 mDxCoordLocation = getUniformLocation("dx_Coord");
1865 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1866 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1867 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1868
1869 Context *context = getContext();
1870 context->markDxUniformsDirty();
1871
1872 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001873}
1874
1875// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001876bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001877{
1878 unsigned int usedLocations = 0;
1879
1880 // Link attributes that have a binding location
1881 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1882 {
1883 int location = attributeBindings.getAttributeBinding(attribute->name);
1884
1885 if (location != -1) // Set by glBindAttribLocation
1886 {
1887 if (!mLinkedAttribute[location].name.empty())
1888 {
1889 // Multiple active attributes bound to the same location; not an error
1890 }
1891
1892 mLinkedAttribute[location] = *attribute;
1893
1894 int rows = VariableRowCount(attribute->type);
1895
1896 if (rows + location > MAX_VERTEX_ATTRIBS)
1897 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001898 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 +00001899
1900 return false;
1901 }
1902
1903 for (int i = 0; i < rows; i++)
1904 {
1905 usedLocations |= 1 << (location + i);
1906 }
1907 }
1908 }
1909
1910 // Link attributes that don't have a binding location
1911 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1912 {
1913 int location = attributeBindings.getAttributeBinding(attribute->name);
1914
1915 if (location == -1) // Not set by glBindAttribLocation
1916 {
1917 int rows = VariableRowCount(attribute->type);
1918 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1919
1920 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1921 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001922 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001923
1924 return false; // Fail to link
1925 }
1926
1927 mLinkedAttribute[availableIndex] = *attribute;
1928 }
1929 }
1930
1931 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1932 {
1933 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1934 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1935
1936 for (int r = 0; r < rows; r++)
1937 {
1938 mSemanticIndex[attributeIndex++] = index++;
1939 }
1940 }
1941
1942 return true;
1943}
1944
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001945bool ProgramBinary::linkUniforms(InfoLog &infoLog, D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001946{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001947 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001948 {
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001949 const D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001950
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001951 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001952 {
1953 return false;
1954 }
1955 }
1956
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001957 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1958 {
1959 const D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
1960
1961 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1962 {
1963 return false;
1964 }
1965 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001966 return true;
1967}
1968
1969// Adds the description of a constant found in the binary shader to the list of uniforms
1970// Returns true if succesful (uniform not already defined)
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001971bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DConstant *constant, const std::string &name,
1972 D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001973{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001974 if (constant->registerSet == D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001975 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001976 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001977 {
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001978 const D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1979 const D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001980
1981 if (psConstant)
1982 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001983 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001984
1985 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1986 {
1987 mSamplersPS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001988 mSamplersPS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001989 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1990 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1991 }
1992 else
1993 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001994 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001995 return false;
1996 }
1997 }
1998
1999 if (vsConstant)
2000 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002001 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002002
2003 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2004 {
2005 mSamplersVS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002006 mSamplersVS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002007 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2008 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2009 }
2010 else
2011 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002012 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002013 return false;
2014 }
2015 }
2016 }
2017 }
2018
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002019 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002020 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002021 case D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002023 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002024 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002025 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002026 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002027 const D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002028
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002029 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002030
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002031 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002032 {
2033 return false;
2034 }
2035 }
2036 }
2037
2038 return true;
2039 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002040 case D3DConstant::CLASS_SCALAR:
2041 case D3DConstant::CLASS_VECTOR:
2042 case D3DConstant::CLASS_MATRIX_COLUMNS:
2043 case D3DConstant::CLASS_OBJECT:
2044 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002045 default:
2046 UNREACHABLE();
2047 return false;
2048 }
2049}
2050
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002051bool ProgramBinary::defineUniform(GLenum shader, const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002052{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002053 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002054
2055 if(!uniform)
2056 {
2057 return false;
2058 }
2059
2060 // Check if already defined
2061 GLint location = getUniformLocation(uniform->name);
2062 GLenum type = uniform->type;
2063
2064 if (location >= 0)
2065 {
2066 delete uniform;
2067 uniform = mUniforms[mUniformIndex[location].index];
2068 }
2069
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002070 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2071 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002072
2073 if (location >= 0)
2074 {
2075 return uniform->type == type;
2076 }
2077
2078 mUniforms.push_back(uniform);
2079 unsigned int uniformIndex = mUniforms.size() - 1;
2080
2081 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2082 {
2083 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2084 }
2085
2086 return true;
2087}
2088
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002089Uniform *ProgramBinary::createUniform(const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002090{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002091 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002092 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002093 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002094 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002095 case D3DConstant::PT_SAMPLER2D:
2096 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002097 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002098 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002099 default: UNREACHABLE();
2100 }
2101 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002102 case D3DConstant::PT_SAMPLERCUBE:
2103 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002104 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002105 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002106 default: UNREACHABLE();
2107 }
2108 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002109 case D3DConstant::PT_BOOL:
2110 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002111 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002112 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2113 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2114 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2115 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002116 default: UNREACHABLE();
2117 }
2118 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002119 case D3DConstant::PT_INT:
2120 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002121 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002122 case 1: return new Uniform(GL_INT, _name, constant->elements);
2123 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2124 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2125 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002126 default: UNREACHABLE();
2127 }
2128 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002129 case D3DConstant::PT_FLOAT:
2130 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002131 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002132 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2133 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2134 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2135 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002136 default: UNREACHABLE();
2137 }
2138 break;
2139 default:
2140 UNREACHABLE();
2141 }
2142 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002143 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002144 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002145 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002146 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002147 case D3DConstant::PT_FLOAT:
2148 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002149 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002150 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2151 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2152 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002153 default: UNREACHABLE();
2154 }
2155 break;
2156 default: UNREACHABLE();
2157 }
2158 }
2159 else UNREACHABLE();
2160
2161 return 0;
2162}
2163
2164// This method needs to match OutputHLSL::decorate
2165std::string ProgramBinary::decorateAttribute(const std::string &name)
2166{
2167 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2168 {
2169 return "_" + name;
2170 }
2171
2172 return name;
2173}
2174
2175std::string ProgramBinary::undecorateUniform(const std::string &_name)
2176{
2177 std::string name = _name;
2178
2179 // Remove any structure field decoration
2180 size_t pos = 0;
2181 while ((pos = name.find("._", pos)) != std::string::npos)
2182 {
2183 name.replace(pos, 2, ".");
2184 }
2185
2186 // Remove the leading decoration
2187 if (name[0] == '_')
2188 {
2189 return name.substr(1);
2190 }
2191 else if (name.compare(0, 3, "ar_") == 0)
2192 {
2193 return name.substr(3);
2194 }
2195
2196 return name;
2197}
2198
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002199// D3D9_REPLACE begin
2200void ProgramBinary::applyUniformnbv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002201{
2202 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2203 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2204
2205 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2206 {
2207 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2208 for (int i = 0; i < count; i++)
2209 {
2210 for (int j = 0; j < 4; j++)
2211 {
2212 if (j < width)
2213 {
2214 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2215 }
2216 else
2217 {
2218 vector[i * 4 + j] = 0.0f;
2219 }
2220 }
2221 }
2222 }
2223
2224 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2225 {
2226 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2227 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2228 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2229 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2230 for (int i = 0; i < copyCount; i++)
2231 {
2232 boolVector[i] = v[i] != GL_FALSE;
2233 }
2234 }
2235
2236 if (targetUniform->ps.float4Index >= 0)
2237 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002238 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002239 }
2240
2241 if (targetUniform->ps.boolIndex >= 0)
2242 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002243 device->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002244 }
2245
2246 if (targetUniform->vs.float4Index >= 0)
2247 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002248 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002249 }
2250
2251 if (targetUniform->vs.boolIndex >= 0)
2252 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002253 device->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002254 }
2255}
2256
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002257bool ProgramBinary::applyUniformnfv(IDirect3DDevice9 *device, Uniform *targetUniform, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002258{
2259 if (targetUniform->ps.registerCount)
2260 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002261 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002262 }
2263
2264 if (targetUniform->vs.registerCount)
2265 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002266 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002267 }
2268
2269 return true;
2270}
2271
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002272bool ProgramBinary::applyUniform1iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002273{
2274 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002275 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002276
2277 for (int i = 0; i < count; i++)
2278 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002279 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002280 }
2281
2282 if (targetUniform->ps.registerCount)
2283 {
2284 if (targetUniform->ps.samplerIndex >= 0)
2285 {
2286 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2287
2288 for (int i = 0; i < count; i++)
2289 {
2290 unsigned int samplerIndex = firstIndex + i;
2291
2292 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2293 {
2294 ASSERT(mSamplersPS[samplerIndex].active);
2295 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2296 }
2297 }
2298 }
2299 else
2300 {
2301 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002302 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002303 }
2304 }
2305
2306 if (targetUniform->vs.registerCount)
2307 {
2308 if (targetUniform->vs.samplerIndex >= 0)
2309 {
2310 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2311
2312 for (int i = 0; i < count; i++)
2313 {
2314 unsigned int samplerIndex = firstIndex + i;
2315
2316 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2317 {
2318 ASSERT(mSamplersVS[samplerIndex].active);
2319 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2320 }
2321 }
2322 }
2323 else
2324 {
2325 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002326 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002327 }
2328 }
2329
2330 return true;
2331}
2332
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002333bool ProgramBinary::applyUniform2iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002334{
2335 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002336 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002337
2338 for (int i = 0; i < count; i++)
2339 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002340 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002341
2342 v += 2;
2343 }
2344
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002345 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002346
2347 return true;
2348}
2349
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002350bool ProgramBinary::applyUniform3iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002351{
2352 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002353 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002354
2355 for (int i = 0; i < count; i++)
2356 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002357 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002358
2359 v += 3;
2360 }
2361
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002362 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002363
2364 return true;
2365}
2366
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002367bool ProgramBinary::applyUniform4iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002368{
2369 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002370 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002371
2372 for (int i = 0; i < count; i++)
2373 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002374 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002375
2376 v += 4;
2377 }
2378
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002379 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002380
2381 return true;
2382}
2383
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002384void ProgramBinary::applyUniformniv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002385{
2386 if (targetUniform->ps.registerCount)
2387 {
2388 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002389 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002390 }
2391
2392 if (targetUniform->vs.registerCount)
2393 {
2394 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002395 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002396 }
2397}
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002398// D3D9_REPLACE end
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002399
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002400bool ProgramBinary::isValidated() const
2401{
2402 return mValidated;
2403}
2404
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002405void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2406{
2407 // Skip over inactive attributes
2408 unsigned int activeAttribute = 0;
2409 unsigned int attribute;
2410 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2411 {
2412 if (mLinkedAttribute[attribute].name.empty())
2413 {
2414 continue;
2415 }
2416
2417 if (activeAttribute == index)
2418 {
2419 break;
2420 }
2421
2422 activeAttribute++;
2423 }
2424
2425 if (bufsize > 0)
2426 {
2427 const char *string = mLinkedAttribute[attribute].name.c_str();
2428
2429 strncpy(name, string, bufsize);
2430 name[bufsize - 1] = '\0';
2431
2432 if (length)
2433 {
2434 *length = strlen(name);
2435 }
2436 }
2437
2438 *size = 1; // Always a single 'type' instance
2439
2440 *type = mLinkedAttribute[attribute].type;
2441}
2442
2443GLint ProgramBinary::getActiveAttributeCount()
2444{
2445 int count = 0;
2446
2447 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2448 {
2449 if (!mLinkedAttribute[attributeIndex].name.empty())
2450 {
2451 count++;
2452 }
2453 }
2454
2455 return count;
2456}
2457
2458GLint ProgramBinary::getActiveAttributeMaxLength()
2459{
2460 int maxLength = 0;
2461
2462 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2463 {
2464 if (!mLinkedAttribute[attributeIndex].name.empty())
2465 {
2466 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2467 }
2468 }
2469
2470 return maxLength;
2471}
2472
2473void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2474{
2475 // Skip over internal uniforms
2476 unsigned int activeUniform = 0;
2477 unsigned int uniform;
2478 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2479 {
2480 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2481 {
2482 continue;
2483 }
2484
2485 if (activeUniform == index)
2486 {
2487 break;
2488 }
2489
2490 activeUniform++;
2491 }
2492
2493 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2494
2495 if (bufsize > 0)
2496 {
2497 std::string string = mUniforms[uniform]->name;
2498
2499 if (mUniforms[uniform]->isArray())
2500 {
2501 string += "[0]";
2502 }
2503
2504 strncpy(name, string.c_str(), bufsize);
2505 name[bufsize - 1] = '\0';
2506
2507 if (length)
2508 {
2509 *length = strlen(name);
2510 }
2511 }
2512
2513 *size = mUniforms[uniform]->arraySize;
2514
2515 *type = mUniforms[uniform]->type;
2516}
2517
2518GLint ProgramBinary::getActiveUniformCount()
2519{
2520 int count = 0;
2521
2522 unsigned int numUniforms = mUniforms.size();
2523 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2524 {
2525 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2526 {
2527 count++;
2528 }
2529 }
2530
2531 return count;
2532}
2533
2534GLint ProgramBinary::getActiveUniformMaxLength()
2535{
2536 int maxLength = 0;
2537
2538 unsigned int numUniforms = mUniforms.size();
2539 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2540 {
2541 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2542 {
2543 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2544 if (mUniforms[uniformIndex]->isArray())
2545 {
2546 length += 3; // Counting in "[0]".
2547 }
2548 maxLength = std::max(length, maxLength);
2549 }
2550 }
2551
2552 return maxLength;
2553}
2554
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002555void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002556{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002557 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002558 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002559 {
2560 mValidated = false;
2561 }
2562 else
2563 {
2564 mValidated = true;
2565 }
2566}
2567
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002568bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002569{
2570 // if any two active samplers in a program are of different types, but refer to the same
2571 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2572 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2573
2574 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2575 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2576
2577 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2578 {
2579 textureUnitType[i] = TEXTURE_UNKNOWN;
2580 }
2581
2582 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2583 {
2584 if (mSamplersPS[i].active)
2585 {
2586 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2587
2588 if (unit >= maxCombinedTextureImageUnits)
2589 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002590 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002591 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002592 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002593 }
2594
2595 return false;
2596 }
2597
2598 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2599 {
2600 if (mSamplersPS[i].textureType != textureUnitType[unit])
2601 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002602 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002603 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002604 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002605 }
2606
2607 return false;
2608 }
2609 }
2610 else
2611 {
2612 textureUnitType[unit] = mSamplersPS[i].textureType;
2613 }
2614 }
2615 }
2616
2617 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2618 {
2619 if (mSamplersVS[i].active)
2620 {
2621 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2622
2623 if (unit >= maxCombinedTextureImageUnits)
2624 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002625 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002626 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002627 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002628 }
2629
2630 return false;
2631 }
2632
2633 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2634 {
2635 if (mSamplersVS[i].textureType != textureUnitType[unit])
2636 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002637 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002638 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002639 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002640 }
2641
2642 return false;
2643 }
2644 }
2645 else
2646 {
2647 textureUnitType[unit] = mSamplersVS[i].textureType;
2648 }
2649 }
2650 }
2651
2652 return true;
2653}
2654
2655GLint ProgramBinary::getDxDepthRangeLocation() const
2656{
2657 return mDxDepthRangeLocation;
2658}
2659
2660GLint ProgramBinary::getDxDepthLocation() const
2661{
2662 return mDxDepthLocation;
2663}
2664
2665GLint ProgramBinary::getDxCoordLocation() const
2666{
2667 return mDxCoordLocation;
2668}
2669
2670GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2671{
2672 return mDxHalfPixelSizeLocation;
2673}
2674
2675GLint ProgramBinary::getDxFrontCCWLocation() const
2676{
2677 return mDxFrontCCWLocation;
2678}
2679
2680GLint ProgramBinary::getDxPointsOrLinesLocation() const
2681{
2682 return mDxPointsOrLinesLocation;
2683}
2684
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002685ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2686{
2687}
2688
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002689}