blob: a3b3b00f4d3ec52cb577d13d4b529272f9e921a5 [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";
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001253 std::string positionSemantic = sm4 ? "SV_POSITION" : "POSITION";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001254
1255 vertexHLSL += "struct VS_INPUT\n"
1256 "{\n";
1257
1258 int semanticIndex = 0;
1259 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1260 {
1261 switch (attribute->type)
1262 {
1263 case GL_FLOAT: vertexHLSL += " float "; break;
1264 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1265 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1266 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1267 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1268 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1269 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1270 default: UNREACHABLE();
1271 }
1272
1273 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1274
1275 semanticIndex += VariableRowCount(attribute->type);
1276 }
1277
1278 vertexHLSL += "};\n"
1279 "\n"
1280 "struct VS_OUTPUT\n"
1281 "{\n"
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001282 " float4 gl_Position : " + positionSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001283
1284 for (int r = 0; r < registers; r++)
1285 {
1286 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1287
1288 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1289 }
1290
1291 if (fragmentShader->mUsesFragCoord)
1292 {
1293 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1294 }
1295
1296 if (vertexShader->mUsesPointSize && sm3)
1297 {
1298 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1299 }
1300
1301 vertexHLSL += "};\n"
1302 "\n"
1303 "VS_OUTPUT main(VS_INPUT input)\n"
1304 "{\n";
1305
1306 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1307 {
1308 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1309
1310 if (VariableRowCount(attribute->type) > 1) // Matrix
1311 {
1312 vertexHLSL += "transpose";
1313 }
1314
1315 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1316 }
1317
1318 vertexHLSL += "\n"
1319 " gl_main();\n"
1320 "\n"
1321 " VS_OUTPUT output;\n"
1322 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001323 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001324 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1325 " output.gl_Position.w = gl_Position.w;\n";
1326
1327 if (vertexShader->mUsesPointSize && sm3)
1328 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001329 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001330 }
1331
1332 if (fragmentShader->mUsesFragCoord)
1333 {
1334 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1335 }
1336
1337 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1338 {
1339 if (varying->reg >= 0)
1340 {
1341 for (int i = 0; i < varying->size; i++)
1342 {
1343 int rows = VariableRowCount(varying->type);
1344
1345 for (int j = 0; j < rows; j++)
1346 {
1347 int r = varying->reg + i * rows + j;
1348 vertexHLSL += " output.v" + str(r);
1349
1350 bool sharedRegister = false; // Register used by multiple varyings
1351
1352 for (int x = 0; x < 4; x++)
1353 {
1354 if (packing[r][x] && packing[r][x] != packing[r][0])
1355 {
1356 sharedRegister = true;
1357 break;
1358 }
1359 }
1360
1361 if(sharedRegister)
1362 {
1363 vertexHLSL += ".";
1364
1365 for (int x = 0; x < 4; x++)
1366 {
1367 if (packing[r][x] == &*varying)
1368 {
1369 switch(x)
1370 {
1371 case 0: vertexHLSL += "x"; break;
1372 case 1: vertexHLSL += "y"; break;
1373 case 2: vertexHLSL += "z"; break;
1374 case 3: vertexHLSL += "w"; break;
1375 }
1376 }
1377 }
1378 }
1379
1380 vertexHLSL += " = " + varying->name;
1381
1382 if (varying->array)
1383 {
1384 vertexHLSL += "[" + str(i) + "]";
1385 }
1386
1387 if (rows > 1)
1388 {
1389 vertexHLSL += "[" + str(j) + "]";
1390 }
1391
1392 vertexHLSL += ";\n";
1393 }
1394 }
1395 }
1396 }
1397
1398 vertexHLSL += "\n"
1399 " return output;\n"
1400 "}\n";
1401
1402 pixelHLSL += "struct PS_INPUT\n"
1403 "{\n";
1404
1405 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1406 {
1407 if (varying->reg >= 0)
1408 {
1409 for (int i = 0; i < varying->size; i++)
1410 {
1411 int rows = VariableRowCount(varying->type);
1412 for (int j = 0; j < rows; j++)
1413 {
1414 std::string n = str(varying->reg + i * rows + j);
1415 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1416 }
1417 }
1418 }
1419 else UNREACHABLE();
1420 }
1421
1422 if (fragmentShader->mUsesFragCoord)
1423 {
1424 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1425 if (sm3) {
1426 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1427 }
1428 }
1429
1430 if (fragmentShader->mUsesPointCoord && sm3)
1431 {
1432 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1433 }
1434
1435 if (fragmentShader->mUsesFrontFacing)
1436 {
1437 pixelHLSL += " float vFace : VFACE;\n";
1438 }
1439
1440 pixelHLSL += "};\n"
1441 "\n"
1442 "struct PS_OUTPUT\n"
1443 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001444 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001445 "};\n"
1446 "\n"
1447 "PS_OUTPUT main(PS_INPUT input)\n"
1448 "{\n";
1449
1450 if (fragmentShader->mUsesFragCoord)
1451 {
1452 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1453
1454 if (sm3)
1455 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001456 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001457 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001458 }
1459 else
1460 {
1461 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1462 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001463 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001464 }
1465
1466 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n"
1467 " gl_FragCoord.w = rhw;\n";
1468 }
1469
1470 if (fragmentShader->mUsesPointCoord && sm3)
1471 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001472 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1473 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001474 }
1475
1476 if (fragmentShader->mUsesFrontFacing)
1477 {
1478 pixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n";
1479 }
1480
1481 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1482 {
1483 if (varying->reg >= 0)
1484 {
1485 for (int i = 0; i < varying->size; i++)
1486 {
1487 int rows = VariableRowCount(varying->type);
1488 for (int j = 0; j < rows; j++)
1489 {
1490 std::string n = str(varying->reg + i * rows + j);
1491 pixelHLSL += " " + varying->name;
1492
1493 if (varying->array)
1494 {
1495 pixelHLSL += "[" + str(i) + "]";
1496 }
1497
1498 if (rows > 1)
1499 {
1500 pixelHLSL += "[" + str(j) + "]";
1501 }
1502
1503 pixelHLSL += " = input.v" + n + ";\n";
1504 }
1505 }
1506 }
1507 else UNREACHABLE();
1508 }
1509
1510 pixelHLSL += "\n"
1511 " gl_main();\n"
1512 "\n"
1513 " PS_OUTPUT output;\n"
1514 " output.gl_Color[0] = gl_Color[0];\n"
1515 "\n"
1516 " return output;\n"
1517 "}\n";
1518
1519 return true;
1520}
1521
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001522bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1523{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001524 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001525
1526 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001527 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001528 if (format != GL_PROGRAM_BINARY_ANGLE)
1529 {
1530 infoLog.append("Invalid program binary format.");
1531 return false;
1532 }
1533
1534 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001535 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001536 if (version != BUILD_REVISION)
1537 {
1538 infoLog.append("Invalid program binary version.");
1539 return false;
1540 }
1541
1542 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1543 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001544 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001545 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001546 stream.read(&name);
1547 mLinkedAttribute[i].name = name;
1548 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001549 }
1550
1551 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1552 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001553 stream.read(&mSamplersPS[i].active);
1554 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001555
1556 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001557 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001558 mSamplersPS[i].textureType = (TextureType) textureType;
1559 }
1560
1561 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1562 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001563 stream.read(&mSamplersVS[i].active);
1564 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001565
1566 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001567 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001568 mSamplersVS[i].textureType = (TextureType) textureType;
1569 }
1570
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001571 stream.read(&mUsedVertexSamplerRange);
1572 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001573
1574 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001575 stream.read(&size);
1576 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001577 {
1578 infoLog.append("Invalid program binary.");
1579 return false;
1580 }
1581
1582 mUniforms.resize(size);
1583 for (unsigned int i = 0; i < size; ++i)
1584 {
1585 GLenum type;
1586 std::string _name;
1587 unsigned int arraySize;
1588
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001589 stream.read(&type);
1590 stream.read(&_name);
1591 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001592
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001593 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001594
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001595 stream.read(&mUniforms[i]->ps.float4Index);
1596 stream.read(&mUniforms[i]->ps.samplerIndex);
1597 stream.read(&mUniforms[i]->ps.boolIndex);
1598 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001599
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001600 stream.read(&mUniforms[i]->vs.float4Index);
1601 stream.read(&mUniforms[i]->vs.samplerIndex);
1602 stream.read(&mUniforms[i]->vs.boolIndex);
1603 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001604 }
1605
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001606 stream.read(&size);
1607 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001608 {
1609 infoLog.append("Invalid program binary.");
1610 return false;
1611 }
1612
1613 mUniformIndex.resize(size);
1614 for (unsigned int i = 0; i < size; ++i)
1615 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001616 stream.read(&mUniformIndex[i].name);
1617 stream.read(&mUniformIndex[i].element);
1618 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001619 }
1620
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001621 stream.read(&mDxDepthRangeLocation);
1622 stream.read(&mDxDepthLocation);
1623 stream.read(&mDxCoordLocation);
1624 stream.read(&mDxHalfPixelSizeLocation);
1625 stream.read(&mDxFrontCCWLocation);
1626 stream.read(&mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001627
1628 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001629 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001630
1631 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001632 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001633
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001634 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001635
daniel@transgaming.com36038542012-11-28 20:59:26 +00001636 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001637 ptr += sizeof(GUID);
1638
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001639 GUID identifier = mRenderer->getAdapterIdentifier();
1640 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001641 {
1642 infoLog.append("Invalid program binary.");
1643 return false;
1644 }
1645
1646 const char *pixelShaderFunction = ptr;
1647 ptr += pixelShaderSize;
1648
1649 const char *vertexShaderFunction = ptr;
1650 ptr += vertexShaderSize;
1651
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001652 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1653 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1654 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001655 {
1656 infoLog.append("Could not create pixel shader.");
1657 return false;
1658 }
1659
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001660 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1661 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1662 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001663 {
1664 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001665 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001666 mPixelExecutable = NULL;
1667 return false;
1668 }
1669
1670 return true;
1671}
1672
1673bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1674{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001675 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001676
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001677 stream.write(GL_PROGRAM_BINARY_ANGLE);
1678 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001679
1680 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1681 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001682 stream.write(mLinkedAttribute[i].type);
1683 stream.write(mLinkedAttribute[i].name);
1684 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001685 }
1686
1687 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1688 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001689 stream.write(mSamplersPS[i].active);
1690 stream.write(mSamplersPS[i].logicalTextureUnit);
1691 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001692 }
1693
1694 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1695 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001696 stream.write(mSamplersVS[i].active);
1697 stream.write(mSamplersVS[i].logicalTextureUnit);
1698 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001699 }
1700
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001701 stream.write(mUsedVertexSamplerRange);
1702 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001703
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001704 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001705 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1706 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001707 stream.write(mUniforms[i]->type);
1708 stream.write(mUniforms[i]->_name);
1709 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001710
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001711 stream.write(mUniforms[i]->ps.float4Index);
1712 stream.write(mUniforms[i]->ps.samplerIndex);
1713 stream.write(mUniforms[i]->ps.boolIndex);
1714 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001715
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001716 stream.write(mUniforms[i]->vs.float4Index);
1717 stream.write(mUniforms[i]->vs.samplerIndex);
1718 stream.write(mUniforms[i]->vs.boolIndex);
1719 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001720 }
1721
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001722 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001723 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1724 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001725 stream.write(mUniformIndex[i].name);
1726 stream.write(mUniformIndex[i].element);
1727 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001728 }
1729
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001730 stream.write(mDxDepthRangeLocation);
1731 stream.write(mDxDepthLocation);
1732 stream.write(mDxCoordLocation);
1733 stream.write(mDxHalfPixelSizeLocation);
1734 stream.write(mDxFrontCCWLocation);
1735 stream.write(mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001736
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001737 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001738 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001739
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001740 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001741 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001742
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001743 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001744
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001745 GLsizei streamLength = stream.length();
1746 const void *streamData = stream.data();
1747
1748 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001749 if (totalLength > bufSize)
1750 {
1751 if (length)
1752 {
1753 *length = 0;
1754 }
1755
1756 return false;
1757 }
1758
1759 if (binary)
1760 {
1761 char *ptr = (char*) binary;
1762
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001763 memcpy(ptr, streamData, streamLength);
1764 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001765
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001766 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001767 ptr += sizeof(GUID);
1768
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001769 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001770 ptr += pixelShaderSize;
1771
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001772 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001773 ptr += vertexShaderSize;
1774
1775 ASSERT(ptr - totalLength == binary);
1776 }
1777
1778 if (length)
1779 {
1780 *length = totalLength;
1781 }
1782
1783 return true;
1784}
1785
1786GLint ProgramBinary::getLength()
1787{
1788 GLint length;
1789 if (save(NULL, INT_MAX, &length))
1790 {
1791 return length;
1792 }
1793 else
1794 {
1795 return 0;
1796 }
1797}
1798
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001799bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001800{
1801 if (!fragmentShader || !fragmentShader->isCompiled())
1802 {
1803 return false;
1804 }
1805
1806 if (!vertexShader || !vertexShader->isCompiled())
1807 {
1808 return false;
1809 }
1810
1811 std::string pixelHLSL = fragmentShader->getHLSL();
1812 std::string vertexHLSL = vertexShader->getHLSL();
1813
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001814 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001815 {
1816 return false;
1817 }
1818
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001819 bool success = true;
1820 D3DConstantTable *constantTableVS = NULL;
1821 D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001822 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1823 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001824
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001825 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001826 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001827 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001828 constantTableVS = mVertexExecutable->getConstantTable();
1829 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001830 }
1831 else
1832 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001833 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001834 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001835
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001836 delete mVertexExecutable;
1837 mVertexExecutable = NULL;
1838 delete mPixelExecutable;
1839 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001840 }
1841
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001842 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1843 {
1844 success = false;
1845 }
1846
1847 if (constantTableVS && constantTablePS)
1848 {
1849 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1850 {
1851 success = false;
1852 }
1853 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001854
1855 // these uniforms are searched as already-decorated because gl_ and dx_
1856 // are reserved prefixes, and do not receive additional decoration
1857 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1858 mDxDepthLocation = getUniformLocation("dx_Depth");
1859 mDxCoordLocation = getUniformLocation("dx_Coord");
1860 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1861 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1862 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1863
1864 Context *context = getContext();
1865 context->markDxUniformsDirty();
1866
1867 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001868}
1869
1870// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001871bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001872{
1873 unsigned int usedLocations = 0;
1874
1875 // Link attributes that have a binding location
1876 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1877 {
1878 int location = attributeBindings.getAttributeBinding(attribute->name);
1879
1880 if (location != -1) // Set by glBindAttribLocation
1881 {
1882 if (!mLinkedAttribute[location].name.empty())
1883 {
1884 // Multiple active attributes bound to the same location; not an error
1885 }
1886
1887 mLinkedAttribute[location] = *attribute;
1888
1889 int rows = VariableRowCount(attribute->type);
1890
1891 if (rows + location > MAX_VERTEX_ATTRIBS)
1892 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001893 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 +00001894
1895 return false;
1896 }
1897
1898 for (int i = 0; i < rows; i++)
1899 {
1900 usedLocations |= 1 << (location + i);
1901 }
1902 }
1903 }
1904
1905 // Link attributes that don't have a binding location
1906 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1907 {
1908 int location = attributeBindings.getAttributeBinding(attribute->name);
1909
1910 if (location == -1) // Not set by glBindAttribLocation
1911 {
1912 int rows = VariableRowCount(attribute->type);
1913 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1914
1915 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1916 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001917 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001918
1919 return false; // Fail to link
1920 }
1921
1922 mLinkedAttribute[availableIndex] = *attribute;
1923 }
1924 }
1925
1926 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1927 {
1928 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1929 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1930
1931 for (int r = 0; r < rows; r++)
1932 {
1933 mSemanticIndex[attributeIndex++] = index++;
1934 }
1935 }
1936
1937 return true;
1938}
1939
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001940bool ProgramBinary::linkUniforms(InfoLog &infoLog, D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001941{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001942 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001943 {
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001944 const D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001945
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001946 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001947 {
1948 return false;
1949 }
1950 }
1951
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001952 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1953 {
1954 const D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
1955
1956 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1957 {
1958 return false;
1959 }
1960 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001961 return true;
1962}
1963
1964// Adds the description of a constant found in the binary shader to the list of uniforms
1965// Returns true if succesful (uniform not already defined)
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001966bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DConstant *constant, const std::string &name,
1967 D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001968{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001969 if (constant->registerSet == D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001970 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001971 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001972 {
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001973 const D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1974 const D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001975
1976 if (psConstant)
1977 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001978 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001979
1980 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1981 {
1982 mSamplersPS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001983 mSamplersPS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001984 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1985 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1986 }
1987 else
1988 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001989 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001990 return false;
1991 }
1992 }
1993
1994 if (vsConstant)
1995 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001996 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001997
1998 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
1999 {
2000 mSamplersVS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002001 mSamplersVS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002002 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2003 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2004 }
2005 else
2006 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002007 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002008 return false;
2009 }
2010 }
2011 }
2012 }
2013
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002014 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002015 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002016 case D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002017 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002018 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002019 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002020 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002021 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002022 const D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002023
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002024 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002025
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002026 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002027 {
2028 return false;
2029 }
2030 }
2031 }
2032
2033 return true;
2034 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002035 case D3DConstant::CLASS_SCALAR:
2036 case D3DConstant::CLASS_VECTOR:
2037 case D3DConstant::CLASS_MATRIX_COLUMNS:
2038 case D3DConstant::CLASS_OBJECT:
2039 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002040 default:
2041 UNREACHABLE();
2042 return false;
2043 }
2044}
2045
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002046bool ProgramBinary::defineUniform(GLenum shader, const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002047{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002048 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002049
2050 if(!uniform)
2051 {
2052 return false;
2053 }
2054
2055 // Check if already defined
2056 GLint location = getUniformLocation(uniform->name);
2057 GLenum type = uniform->type;
2058
2059 if (location >= 0)
2060 {
2061 delete uniform;
2062 uniform = mUniforms[mUniformIndex[location].index];
2063 }
2064
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002065 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2066 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002067
2068 if (location >= 0)
2069 {
2070 return uniform->type == type;
2071 }
2072
2073 mUniforms.push_back(uniform);
2074 unsigned int uniformIndex = mUniforms.size() - 1;
2075
2076 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2077 {
2078 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2079 }
2080
2081 return true;
2082}
2083
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002084Uniform *ProgramBinary::createUniform(const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002085{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002086 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002087 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002088 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002089 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002090 case D3DConstant::PT_SAMPLER2D:
2091 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002092 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002093 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002094 default: UNREACHABLE();
2095 }
2096 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002097 case D3DConstant::PT_SAMPLERCUBE:
2098 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002099 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002100 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002101 default: UNREACHABLE();
2102 }
2103 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002104 case D3DConstant::PT_BOOL:
2105 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002106 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002107 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2108 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2109 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2110 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002111 default: UNREACHABLE();
2112 }
2113 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002114 case D3DConstant::PT_INT:
2115 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002116 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002117 case 1: return new Uniform(GL_INT, _name, constant->elements);
2118 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2119 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2120 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002121 default: UNREACHABLE();
2122 }
2123 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002124 case D3DConstant::PT_FLOAT:
2125 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002126 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002127 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2128 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2129 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2130 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002131 default: UNREACHABLE();
2132 }
2133 break;
2134 default:
2135 UNREACHABLE();
2136 }
2137 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002138 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002139 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002140 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002141 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002142 case D3DConstant::PT_FLOAT:
2143 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002144 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002145 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2146 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2147 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002148 default: UNREACHABLE();
2149 }
2150 break;
2151 default: UNREACHABLE();
2152 }
2153 }
2154 else UNREACHABLE();
2155
2156 return 0;
2157}
2158
2159// This method needs to match OutputHLSL::decorate
2160std::string ProgramBinary::decorateAttribute(const std::string &name)
2161{
2162 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2163 {
2164 return "_" + name;
2165 }
2166
2167 return name;
2168}
2169
2170std::string ProgramBinary::undecorateUniform(const std::string &_name)
2171{
2172 std::string name = _name;
2173
2174 // Remove any structure field decoration
2175 size_t pos = 0;
2176 while ((pos = name.find("._", pos)) != std::string::npos)
2177 {
2178 name.replace(pos, 2, ".");
2179 }
2180
2181 // Remove the leading decoration
2182 if (name[0] == '_')
2183 {
2184 return name.substr(1);
2185 }
2186 else if (name.compare(0, 3, "ar_") == 0)
2187 {
2188 return name.substr(3);
2189 }
2190
2191 return name;
2192}
2193
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002194// D3D9_REPLACE begin
2195void ProgramBinary::applyUniformnbv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002196{
2197 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2198 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2199
2200 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2201 {
2202 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2203 for (int i = 0; i < count; i++)
2204 {
2205 for (int j = 0; j < 4; j++)
2206 {
2207 if (j < width)
2208 {
2209 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2210 }
2211 else
2212 {
2213 vector[i * 4 + j] = 0.0f;
2214 }
2215 }
2216 }
2217 }
2218
2219 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2220 {
2221 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2222 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2223 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2224 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2225 for (int i = 0; i < copyCount; i++)
2226 {
2227 boolVector[i] = v[i] != GL_FALSE;
2228 }
2229 }
2230
2231 if (targetUniform->ps.float4Index >= 0)
2232 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002233 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002234 }
2235
2236 if (targetUniform->ps.boolIndex >= 0)
2237 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002238 device->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002239 }
2240
2241 if (targetUniform->vs.float4Index >= 0)
2242 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002243 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002244 }
2245
2246 if (targetUniform->vs.boolIndex >= 0)
2247 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002248 device->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002249 }
2250}
2251
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002252bool ProgramBinary::applyUniformnfv(IDirect3DDevice9 *device, Uniform *targetUniform, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002253{
2254 if (targetUniform->ps.registerCount)
2255 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002256 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002257 }
2258
2259 if (targetUniform->vs.registerCount)
2260 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002261 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002262 }
2263
2264 return true;
2265}
2266
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002267bool ProgramBinary::applyUniform1iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002268{
2269 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002270 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002271
2272 for (int i = 0; i < count; i++)
2273 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002274 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002275 }
2276
2277 if (targetUniform->ps.registerCount)
2278 {
2279 if (targetUniform->ps.samplerIndex >= 0)
2280 {
2281 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2282
2283 for (int i = 0; i < count; i++)
2284 {
2285 unsigned int samplerIndex = firstIndex + i;
2286
2287 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2288 {
2289 ASSERT(mSamplersPS[samplerIndex].active);
2290 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2291 }
2292 }
2293 }
2294 else
2295 {
2296 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002297 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002298 }
2299 }
2300
2301 if (targetUniform->vs.registerCount)
2302 {
2303 if (targetUniform->vs.samplerIndex >= 0)
2304 {
2305 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2306
2307 for (int i = 0; i < count; i++)
2308 {
2309 unsigned int samplerIndex = firstIndex + i;
2310
2311 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2312 {
2313 ASSERT(mSamplersVS[samplerIndex].active);
2314 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2315 }
2316 }
2317 }
2318 else
2319 {
2320 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002321 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002322 }
2323 }
2324
2325 return true;
2326}
2327
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002328bool ProgramBinary::applyUniform2iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002329{
2330 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002331 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002332
2333 for (int i = 0; i < count; i++)
2334 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002335 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002336
2337 v += 2;
2338 }
2339
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002340 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002341
2342 return true;
2343}
2344
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002345bool ProgramBinary::applyUniform3iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002346{
2347 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002348 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002349
2350 for (int i = 0; i < count; i++)
2351 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002352 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002353
2354 v += 3;
2355 }
2356
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002357 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002358
2359 return true;
2360}
2361
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002362bool ProgramBinary::applyUniform4iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002363{
2364 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002365 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002366
2367 for (int i = 0; i < count; i++)
2368 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002369 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002370
2371 v += 4;
2372 }
2373
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002374 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002375
2376 return true;
2377}
2378
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002379void ProgramBinary::applyUniformniv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002380{
2381 if (targetUniform->ps.registerCount)
2382 {
2383 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002384 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002385 }
2386
2387 if (targetUniform->vs.registerCount)
2388 {
2389 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002390 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002391 }
2392}
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002393// D3D9_REPLACE end
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002394
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002395bool ProgramBinary::isValidated() const
2396{
2397 return mValidated;
2398}
2399
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002400void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2401{
2402 // Skip over inactive attributes
2403 unsigned int activeAttribute = 0;
2404 unsigned int attribute;
2405 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2406 {
2407 if (mLinkedAttribute[attribute].name.empty())
2408 {
2409 continue;
2410 }
2411
2412 if (activeAttribute == index)
2413 {
2414 break;
2415 }
2416
2417 activeAttribute++;
2418 }
2419
2420 if (bufsize > 0)
2421 {
2422 const char *string = mLinkedAttribute[attribute].name.c_str();
2423
2424 strncpy(name, string, bufsize);
2425 name[bufsize - 1] = '\0';
2426
2427 if (length)
2428 {
2429 *length = strlen(name);
2430 }
2431 }
2432
2433 *size = 1; // Always a single 'type' instance
2434
2435 *type = mLinkedAttribute[attribute].type;
2436}
2437
2438GLint ProgramBinary::getActiveAttributeCount()
2439{
2440 int count = 0;
2441
2442 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2443 {
2444 if (!mLinkedAttribute[attributeIndex].name.empty())
2445 {
2446 count++;
2447 }
2448 }
2449
2450 return count;
2451}
2452
2453GLint ProgramBinary::getActiveAttributeMaxLength()
2454{
2455 int maxLength = 0;
2456
2457 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2458 {
2459 if (!mLinkedAttribute[attributeIndex].name.empty())
2460 {
2461 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2462 }
2463 }
2464
2465 return maxLength;
2466}
2467
2468void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2469{
2470 // Skip over internal uniforms
2471 unsigned int activeUniform = 0;
2472 unsigned int uniform;
2473 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2474 {
2475 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2476 {
2477 continue;
2478 }
2479
2480 if (activeUniform == index)
2481 {
2482 break;
2483 }
2484
2485 activeUniform++;
2486 }
2487
2488 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2489
2490 if (bufsize > 0)
2491 {
2492 std::string string = mUniforms[uniform]->name;
2493
2494 if (mUniforms[uniform]->isArray())
2495 {
2496 string += "[0]";
2497 }
2498
2499 strncpy(name, string.c_str(), bufsize);
2500 name[bufsize - 1] = '\0';
2501
2502 if (length)
2503 {
2504 *length = strlen(name);
2505 }
2506 }
2507
2508 *size = mUniforms[uniform]->arraySize;
2509
2510 *type = mUniforms[uniform]->type;
2511}
2512
2513GLint ProgramBinary::getActiveUniformCount()
2514{
2515 int count = 0;
2516
2517 unsigned int numUniforms = mUniforms.size();
2518 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2519 {
2520 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2521 {
2522 count++;
2523 }
2524 }
2525
2526 return count;
2527}
2528
2529GLint ProgramBinary::getActiveUniformMaxLength()
2530{
2531 int maxLength = 0;
2532
2533 unsigned int numUniforms = mUniforms.size();
2534 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2535 {
2536 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2537 {
2538 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2539 if (mUniforms[uniformIndex]->isArray())
2540 {
2541 length += 3; // Counting in "[0]".
2542 }
2543 maxLength = std::max(length, maxLength);
2544 }
2545 }
2546
2547 return maxLength;
2548}
2549
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002550void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002551{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002552 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002553 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002554 {
2555 mValidated = false;
2556 }
2557 else
2558 {
2559 mValidated = true;
2560 }
2561}
2562
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002563bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002564{
2565 // if any two active samplers in a program are of different types, but refer to the same
2566 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2567 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2568
2569 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2570 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2571
2572 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2573 {
2574 textureUnitType[i] = TEXTURE_UNKNOWN;
2575 }
2576
2577 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2578 {
2579 if (mSamplersPS[i].active)
2580 {
2581 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2582
2583 if (unit >= maxCombinedTextureImageUnits)
2584 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002585 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002586 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002587 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002588 }
2589
2590 return false;
2591 }
2592
2593 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2594 {
2595 if (mSamplersPS[i].textureType != textureUnitType[unit])
2596 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002597 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002598 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002599 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002600 }
2601
2602 return false;
2603 }
2604 }
2605 else
2606 {
2607 textureUnitType[unit] = mSamplersPS[i].textureType;
2608 }
2609 }
2610 }
2611
2612 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2613 {
2614 if (mSamplersVS[i].active)
2615 {
2616 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2617
2618 if (unit >= maxCombinedTextureImageUnits)
2619 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002620 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002621 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002622 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002623 }
2624
2625 return false;
2626 }
2627
2628 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2629 {
2630 if (mSamplersVS[i].textureType != textureUnitType[unit])
2631 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002632 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002633 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002634 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002635 }
2636
2637 return false;
2638 }
2639 }
2640 else
2641 {
2642 textureUnitType[unit] = mSamplersVS[i].textureType;
2643 }
2644 }
2645 }
2646
2647 return true;
2648}
2649
2650GLint ProgramBinary::getDxDepthRangeLocation() const
2651{
2652 return mDxDepthRangeLocation;
2653}
2654
2655GLint ProgramBinary::getDxDepthLocation() const
2656{
2657 return mDxDepthLocation;
2658}
2659
2660GLint ProgramBinary::getDxCoordLocation() const
2661{
2662 return mDxCoordLocation;
2663}
2664
2665GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2666{
2667 return mDxHalfPixelSizeLocation;
2668}
2669
2670GLint ProgramBinary::getDxFrontCCWLocation() const
2671{
2672 return mDxFrontCCWLocation;
2673}
2674
2675GLint ProgramBinary::getDxPointsOrLinesLocation() const
2676{
2677 return mDxPointsOrLinesLocation;
2678}
2679
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002680ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2681{
2682}
2683
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002684}