blob: f5f10984923259ab4688b74f082f233b9fb8f69c [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
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001736 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001737 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001738
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001739 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001740 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001741
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001742 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001743
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001744 GLsizei streamLength = stream.length();
1745 const void *streamData = stream.data();
1746
1747 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001748 if (totalLength > bufSize)
1749 {
1750 if (length)
1751 {
1752 *length = 0;
1753 }
1754
1755 return false;
1756 }
1757
1758 if (binary)
1759 {
1760 char *ptr = (char*) binary;
1761
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001762 memcpy(ptr, streamData, streamLength);
1763 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001764
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001765 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001766 ptr += sizeof(GUID);
1767
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001768 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001769 ptr += pixelShaderSize;
1770
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001771 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001772 ptr += vertexShaderSize;
1773
1774 ASSERT(ptr - totalLength == binary);
1775 }
1776
1777 if (length)
1778 {
1779 *length = totalLength;
1780 }
1781
1782 return true;
1783}
1784
1785GLint ProgramBinary::getLength()
1786{
1787 GLint length;
1788 if (save(NULL, INT_MAX, &length))
1789 {
1790 return length;
1791 }
1792 else
1793 {
1794 return 0;
1795 }
1796}
1797
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001798bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001799{
1800 if (!fragmentShader || !fragmentShader->isCompiled())
1801 {
1802 return false;
1803 }
1804
1805 if (!vertexShader || !vertexShader->isCompiled())
1806 {
1807 return false;
1808 }
1809
1810 std::string pixelHLSL = fragmentShader->getHLSL();
1811 std::string vertexHLSL = vertexShader->getHLSL();
1812
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001813 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001814 {
1815 return false;
1816 }
1817
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001818 bool success = true;
1819 D3DConstantTable *constantTableVS = NULL;
1820 D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001821 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1822 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001823
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001824 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001825 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001826 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001827 constantTableVS = mVertexExecutable->getConstantTable();
1828 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001829 }
1830 else
1831 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001832 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001833 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001834
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001835 delete mVertexExecutable;
1836 mVertexExecutable = NULL;
1837 delete mPixelExecutable;
1838 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001839 }
1840
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001841 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1842 {
1843 success = false;
1844 }
1845
1846 if (constantTableVS && constantTablePS)
1847 {
1848 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1849 {
1850 success = false;
1851 }
1852 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001853
1854 // these uniforms are searched as already-decorated because gl_ and dx_
1855 // are reserved prefixes, and do not receive additional decoration
1856 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1857 mDxDepthLocation = getUniformLocation("dx_Depth");
1858 mDxCoordLocation = getUniformLocation("dx_Coord");
1859 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1860 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1861 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1862
1863 Context *context = getContext();
1864 context->markDxUniformsDirty();
1865
1866 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001867}
1868
1869// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001870bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001871{
1872 unsigned int usedLocations = 0;
1873
1874 // Link attributes that have a binding location
1875 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1876 {
1877 int location = attributeBindings.getAttributeBinding(attribute->name);
1878
1879 if (location != -1) // Set by glBindAttribLocation
1880 {
1881 if (!mLinkedAttribute[location].name.empty())
1882 {
1883 // Multiple active attributes bound to the same location; not an error
1884 }
1885
1886 mLinkedAttribute[location] = *attribute;
1887
1888 int rows = VariableRowCount(attribute->type);
1889
1890 if (rows + location > MAX_VERTEX_ATTRIBS)
1891 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001892 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 +00001893
1894 return false;
1895 }
1896
1897 for (int i = 0; i < rows; i++)
1898 {
1899 usedLocations |= 1 << (location + i);
1900 }
1901 }
1902 }
1903
1904 // Link attributes that don't have a binding location
1905 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1906 {
1907 int location = attributeBindings.getAttributeBinding(attribute->name);
1908
1909 if (location == -1) // Not set by glBindAttribLocation
1910 {
1911 int rows = VariableRowCount(attribute->type);
1912 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1913
1914 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1915 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001916 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001917
1918 return false; // Fail to link
1919 }
1920
1921 mLinkedAttribute[availableIndex] = *attribute;
1922 }
1923 }
1924
1925 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1926 {
1927 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1928 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1929
1930 for (int r = 0; r < rows; r++)
1931 {
1932 mSemanticIndex[attributeIndex++] = index++;
1933 }
1934 }
1935
1936 return true;
1937}
1938
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001939bool ProgramBinary::linkUniforms(InfoLog &infoLog, D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001940{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001941 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001942 {
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001943 const D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001944
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001945 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001946 {
1947 return false;
1948 }
1949 }
1950
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001951 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1952 {
1953 const D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
1954
1955 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1956 {
1957 return false;
1958 }
1959 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001960 return true;
1961}
1962
1963// Adds the description of a constant found in the binary shader to the list of uniforms
1964// Returns true if succesful (uniform not already defined)
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001965bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DConstant *constant, const std::string &name,
1966 D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001967{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001968 if (constant->registerSet == D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001969 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001970 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001971 {
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001972 const D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1973 const D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001974
1975 if (psConstant)
1976 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001977 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001978
1979 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1980 {
1981 mSamplersPS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001982 mSamplersPS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001983 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1984 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1985 }
1986 else
1987 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001988 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001989 return false;
1990 }
1991 }
1992
1993 if (vsConstant)
1994 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001995 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001996
1997 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
1998 {
1999 mSamplersVS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002000 mSamplersVS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002001 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2002 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2003 }
2004 else
2005 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002006 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002007 return false;
2008 }
2009 }
2010 }
2011 }
2012
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002013 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002014 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002015 case D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002016 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002017 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002018 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002019 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002020 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002021 const D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002023 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002024
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002025 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002026 {
2027 return false;
2028 }
2029 }
2030 }
2031
2032 return true;
2033 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002034 case D3DConstant::CLASS_SCALAR:
2035 case D3DConstant::CLASS_VECTOR:
2036 case D3DConstant::CLASS_MATRIX_COLUMNS:
2037 case D3DConstant::CLASS_OBJECT:
2038 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002039 default:
2040 UNREACHABLE();
2041 return false;
2042 }
2043}
2044
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002045bool ProgramBinary::defineUniform(GLenum shader, const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002046{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002047 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002048
2049 if(!uniform)
2050 {
2051 return false;
2052 }
2053
2054 // Check if already defined
2055 GLint location = getUniformLocation(uniform->name);
2056 GLenum type = uniform->type;
2057
2058 if (location >= 0)
2059 {
2060 delete uniform;
2061 uniform = mUniforms[mUniformIndex[location].index];
2062 }
2063
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002064 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2065 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002066
2067 if (location >= 0)
2068 {
2069 return uniform->type == type;
2070 }
2071
2072 mUniforms.push_back(uniform);
2073 unsigned int uniformIndex = mUniforms.size() - 1;
2074
2075 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2076 {
2077 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2078 }
2079
2080 return true;
2081}
2082
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002083Uniform *ProgramBinary::createUniform(const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002084{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002085 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002086 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002087 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002088 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002089 case D3DConstant::PT_SAMPLER2D:
2090 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002091 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002092 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002093 default: UNREACHABLE();
2094 }
2095 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002096 case D3DConstant::PT_SAMPLERCUBE:
2097 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002098 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002099 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002100 default: UNREACHABLE();
2101 }
2102 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002103 case D3DConstant::PT_BOOL:
2104 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002105 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002106 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2107 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2108 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2109 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002110 default: UNREACHABLE();
2111 }
2112 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002113 case D3DConstant::PT_INT:
2114 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002115 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002116 case 1: return new Uniform(GL_INT, _name, constant->elements);
2117 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2118 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2119 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002120 default: UNREACHABLE();
2121 }
2122 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002123 case D3DConstant::PT_FLOAT:
2124 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002125 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002126 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2127 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2128 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2129 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002130 default: UNREACHABLE();
2131 }
2132 break;
2133 default:
2134 UNREACHABLE();
2135 }
2136 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002137 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002138 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002139 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002140 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002141 case D3DConstant::PT_FLOAT:
2142 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002143 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002144 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2145 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2146 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002147 default: UNREACHABLE();
2148 }
2149 break;
2150 default: UNREACHABLE();
2151 }
2152 }
2153 else UNREACHABLE();
2154
2155 return 0;
2156}
2157
2158// This method needs to match OutputHLSL::decorate
2159std::string ProgramBinary::decorateAttribute(const std::string &name)
2160{
2161 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2162 {
2163 return "_" + name;
2164 }
2165
2166 return name;
2167}
2168
2169std::string ProgramBinary::undecorateUniform(const std::string &_name)
2170{
2171 std::string name = _name;
2172
2173 // Remove any structure field decoration
2174 size_t pos = 0;
2175 while ((pos = name.find("._", pos)) != std::string::npos)
2176 {
2177 name.replace(pos, 2, ".");
2178 }
2179
2180 // Remove the leading decoration
2181 if (name[0] == '_')
2182 {
2183 return name.substr(1);
2184 }
2185 else if (name.compare(0, 3, "ar_") == 0)
2186 {
2187 return name.substr(3);
2188 }
2189
2190 return name;
2191}
2192
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002193// D3D9_REPLACE begin
2194void ProgramBinary::applyUniformnbv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002195{
2196 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2197 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2198
2199 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2200 {
2201 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2202 for (int i = 0; i < count; i++)
2203 {
2204 for (int j = 0; j < 4; j++)
2205 {
2206 if (j < width)
2207 {
2208 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2209 }
2210 else
2211 {
2212 vector[i * 4 + j] = 0.0f;
2213 }
2214 }
2215 }
2216 }
2217
2218 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2219 {
2220 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2221 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2222 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2223 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2224 for (int i = 0; i < copyCount; i++)
2225 {
2226 boolVector[i] = v[i] != GL_FALSE;
2227 }
2228 }
2229
2230 if (targetUniform->ps.float4Index >= 0)
2231 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002232 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002233 }
2234
2235 if (targetUniform->ps.boolIndex >= 0)
2236 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002237 device->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002238 }
2239
2240 if (targetUniform->vs.float4Index >= 0)
2241 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002242 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002243 }
2244
2245 if (targetUniform->vs.boolIndex >= 0)
2246 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002247 device->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002248 }
2249}
2250
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002251bool ProgramBinary::applyUniformnfv(IDirect3DDevice9 *device, Uniform *targetUniform, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002252{
2253 if (targetUniform->ps.registerCount)
2254 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002255 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002256 }
2257
2258 if (targetUniform->vs.registerCount)
2259 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002260 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002261 }
2262
2263 return true;
2264}
2265
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002266bool ProgramBinary::applyUniform1iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002267{
2268 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002269 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002270
2271 for (int i = 0; i < count; i++)
2272 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002273 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002274 }
2275
2276 if (targetUniform->ps.registerCount)
2277 {
2278 if (targetUniform->ps.samplerIndex >= 0)
2279 {
2280 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2281
2282 for (int i = 0; i < count; i++)
2283 {
2284 unsigned int samplerIndex = firstIndex + i;
2285
2286 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2287 {
2288 ASSERT(mSamplersPS[samplerIndex].active);
2289 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2290 }
2291 }
2292 }
2293 else
2294 {
2295 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002296 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002297 }
2298 }
2299
2300 if (targetUniform->vs.registerCount)
2301 {
2302 if (targetUniform->vs.samplerIndex >= 0)
2303 {
2304 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2305
2306 for (int i = 0; i < count; i++)
2307 {
2308 unsigned int samplerIndex = firstIndex + i;
2309
2310 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2311 {
2312 ASSERT(mSamplersVS[samplerIndex].active);
2313 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2314 }
2315 }
2316 }
2317 else
2318 {
2319 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002320 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002321 }
2322 }
2323
2324 return true;
2325}
2326
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002327bool ProgramBinary::applyUniform2iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002328{
2329 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002330 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002331
2332 for (int i = 0; i < count; i++)
2333 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002334 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002335
2336 v += 2;
2337 }
2338
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002339 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002340
2341 return true;
2342}
2343
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002344bool ProgramBinary::applyUniform3iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002345{
2346 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002347 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002348
2349 for (int i = 0; i < count; i++)
2350 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002351 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002352
2353 v += 3;
2354 }
2355
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002356 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002357
2358 return true;
2359}
2360
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002361bool ProgramBinary::applyUniform4iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002362{
2363 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002364 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002365
2366 for (int i = 0; i < count; i++)
2367 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002368 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002369
2370 v += 4;
2371 }
2372
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002373 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002374
2375 return true;
2376}
2377
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002378void ProgramBinary::applyUniformniv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002379{
2380 if (targetUniform->ps.registerCount)
2381 {
2382 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002383 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002384 }
2385
2386 if (targetUniform->vs.registerCount)
2387 {
2388 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002389 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002390 }
2391}
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002392// D3D9_REPLACE end
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002393
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002394bool ProgramBinary::isValidated() const
2395{
2396 return mValidated;
2397}
2398
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002399void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2400{
2401 // Skip over inactive attributes
2402 unsigned int activeAttribute = 0;
2403 unsigned int attribute;
2404 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2405 {
2406 if (mLinkedAttribute[attribute].name.empty())
2407 {
2408 continue;
2409 }
2410
2411 if (activeAttribute == index)
2412 {
2413 break;
2414 }
2415
2416 activeAttribute++;
2417 }
2418
2419 if (bufsize > 0)
2420 {
2421 const char *string = mLinkedAttribute[attribute].name.c_str();
2422
2423 strncpy(name, string, bufsize);
2424 name[bufsize - 1] = '\0';
2425
2426 if (length)
2427 {
2428 *length = strlen(name);
2429 }
2430 }
2431
2432 *size = 1; // Always a single 'type' instance
2433
2434 *type = mLinkedAttribute[attribute].type;
2435}
2436
2437GLint ProgramBinary::getActiveAttributeCount()
2438{
2439 int count = 0;
2440
2441 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2442 {
2443 if (!mLinkedAttribute[attributeIndex].name.empty())
2444 {
2445 count++;
2446 }
2447 }
2448
2449 return count;
2450}
2451
2452GLint ProgramBinary::getActiveAttributeMaxLength()
2453{
2454 int maxLength = 0;
2455
2456 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2457 {
2458 if (!mLinkedAttribute[attributeIndex].name.empty())
2459 {
2460 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2461 }
2462 }
2463
2464 return maxLength;
2465}
2466
2467void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2468{
2469 // Skip over internal uniforms
2470 unsigned int activeUniform = 0;
2471 unsigned int uniform;
2472 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2473 {
2474 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2475 {
2476 continue;
2477 }
2478
2479 if (activeUniform == index)
2480 {
2481 break;
2482 }
2483
2484 activeUniform++;
2485 }
2486
2487 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2488
2489 if (bufsize > 0)
2490 {
2491 std::string string = mUniforms[uniform]->name;
2492
2493 if (mUniforms[uniform]->isArray())
2494 {
2495 string += "[0]";
2496 }
2497
2498 strncpy(name, string.c_str(), bufsize);
2499 name[bufsize - 1] = '\0';
2500
2501 if (length)
2502 {
2503 *length = strlen(name);
2504 }
2505 }
2506
2507 *size = mUniforms[uniform]->arraySize;
2508
2509 *type = mUniforms[uniform]->type;
2510}
2511
2512GLint ProgramBinary::getActiveUniformCount()
2513{
2514 int count = 0;
2515
2516 unsigned int numUniforms = mUniforms.size();
2517 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2518 {
2519 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2520 {
2521 count++;
2522 }
2523 }
2524
2525 return count;
2526}
2527
2528GLint ProgramBinary::getActiveUniformMaxLength()
2529{
2530 int maxLength = 0;
2531
2532 unsigned int numUniforms = mUniforms.size();
2533 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2534 {
2535 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2536 {
2537 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2538 if (mUniforms[uniformIndex]->isArray())
2539 {
2540 length += 3; // Counting in "[0]".
2541 }
2542 maxLength = std::max(length, maxLength);
2543 }
2544 }
2545
2546 return maxLength;
2547}
2548
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002549void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002550{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002551 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002552 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002553 {
2554 mValidated = false;
2555 }
2556 else
2557 {
2558 mValidated = true;
2559 }
2560}
2561
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002562bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002563{
2564 // if any two active samplers in a program are of different types, but refer to the same
2565 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2566 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2567
2568 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2569 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2570
2571 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2572 {
2573 textureUnitType[i] = TEXTURE_UNKNOWN;
2574 }
2575
2576 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2577 {
2578 if (mSamplersPS[i].active)
2579 {
2580 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2581
2582 if (unit >= maxCombinedTextureImageUnits)
2583 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002584 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002585 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002586 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002587 }
2588
2589 return false;
2590 }
2591
2592 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2593 {
2594 if (mSamplersPS[i].textureType != textureUnitType[unit])
2595 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002596 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002597 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002598 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002599 }
2600
2601 return false;
2602 }
2603 }
2604 else
2605 {
2606 textureUnitType[unit] = mSamplersPS[i].textureType;
2607 }
2608 }
2609 }
2610
2611 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2612 {
2613 if (mSamplersVS[i].active)
2614 {
2615 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2616
2617 if (unit >= maxCombinedTextureImageUnits)
2618 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002619 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002620 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002621 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002622 }
2623
2624 return false;
2625 }
2626
2627 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2628 {
2629 if (mSamplersVS[i].textureType != textureUnitType[unit])
2630 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002631 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002632 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002633 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002634 }
2635
2636 return false;
2637 }
2638 }
2639 else
2640 {
2641 textureUnitType[unit] = mSamplersVS[i].textureType;
2642 }
2643 }
2644 }
2645
2646 return true;
2647}
2648
2649GLint ProgramBinary::getDxDepthRangeLocation() const
2650{
2651 return mDxDepthRangeLocation;
2652}
2653
2654GLint ProgramBinary::getDxDepthLocation() const
2655{
2656 return mDxDepthLocation;
2657}
2658
2659GLint ProgramBinary::getDxCoordLocation() const
2660{
2661 return mDxCoordLocation;
2662}
2663
2664GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2665{
2666 return mDxHalfPixelSizeLocation;
2667}
2668
2669GLint ProgramBinary::getDxFrontCCWLocation() const
2670{
2671 return mDxFrontCCWLocation;
2672}
2673
2674GLint ProgramBinary::getDxPointsOrLinesLocation() const
2675{
2676 return mDxPointsOrLinesLocation;
2677}
2678
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002679ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2680{
2681}
2682
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002683}