blob: f83a6d5fe1ddc4083250542bd91020061480d3b3 [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
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001503 switch (VariableColumnCount(varying->type))
1504 {
1505 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1506 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1507 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1508 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1509 default: UNREACHABLE();
1510 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001511 }
1512 }
1513 }
1514 else UNREACHABLE();
1515 }
1516
1517 pixelHLSL += "\n"
1518 " gl_main();\n"
1519 "\n"
1520 " PS_OUTPUT output;\n"
1521 " output.gl_Color[0] = gl_Color[0];\n"
1522 "\n"
1523 " return output;\n"
1524 "}\n";
1525
1526 return true;
1527}
1528
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001529bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1530{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001531 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001532
1533 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001534 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001535 if (format != GL_PROGRAM_BINARY_ANGLE)
1536 {
1537 infoLog.append("Invalid program binary format.");
1538 return false;
1539 }
1540
1541 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001542 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001543 if (version != BUILD_REVISION)
1544 {
1545 infoLog.append("Invalid program binary version.");
1546 return false;
1547 }
1548
1549 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1550 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001551 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001552 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001553 stream.read(&name);
1554 mLinkedAttribute[i].name = name;
1555 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001556 }
1557
1558 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1559 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001560 stream.read(&mSamplersPS[i].active);
1561 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001562
1563 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001564 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001565 mSamplersPS[i].textureType = (TextureType) textureType;
1566 }
1567
1568 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1569 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001570 stream.read(&mSamplersVS[i].active);
1571 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001572
1573 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001574 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001575 mSamplersVS[i].textureType = (TextureType) textureType;
1576 }
1577
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001578 stream.read(&mUsedVertexSamplerRange);
1579 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001580
1581 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001582 stream.read(&size);
1583 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001584 {
1585 infoLog.append("Invalid program binary.");
1586 return false;
1587 }
1588
1589 mUniforms.resize(size);
1590 for (unsigned int i = 0; i < size; ++i)
1591 {
1592 GLenum type;
1593 std::string _name;
1594 unsigned int arraySize;
1595
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001596 stream.read(&type);
1597 stream.read(&_name);
1598 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001599
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001600 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001601
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001602 stream.read(&mUniforms[i]->ps.float4Index);
1603 stream.read(&mUniforms[i]->ps.samplerIndex);
1604 stream.read(&mUniforms[i]->ps.boolIndex);
1605 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001606
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001607 stream.read(&mUniforms[i]->vs.float4Index);
1608 stream.read(&mUniforms[i]->vs.samplerIndex);
1609 stream.read(&mUniforms[i]->vs.boolIndex);
1610 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001611 }
1612
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001613 stream.read(&size);
1614 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001615 {
1616 infoLog.append("Invalid program binary.");
1617 return false;
1618 }
1619
1620 mUniformIndex.resize(size);
1621 for (unsigned int i = 0; i < size; ++i)
1622 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001623 stream.read(&mUniformIndex[i].name);
1624 stream.read(&mUniformIndex[i].element);
1625 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001626 }
1627
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001628 stream.read(&mDxDepthRangeLocation);
1629 stream.read(&mDxDepthLocation);
1630 stream.read(&mDxCoordLocation);
1631 stream.read(&mDxHalfPixelSizeLocation);
1632 stream.read(&mDxFrontCCWLocation);
1633 stream.read(&mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001634
1635 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001636 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001637
1638 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001639 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001640
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001641 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001642
daniel@transgaming.com36038542012-11-28 20:59:26 +00001643 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001644 ptr += sizeof(GUID);
1645
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001646 GUID identifier = mRenderer->getAdapterIdentifier();
1647 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001648 {
1649 infoLog.append("Invalid program binary.");
1650 return false;
1651 }
1652
1653 const char *pixelShaderFunction = ptr;
1654 ptr += pixelShaderSize;
1655
1656 const char *vertexShaderFunction = ptr;
1657 ptr += vertexShaderSize;
1658
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001659 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1660 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1661 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001662 {
1663 infoLog.append("Could not create pixel shader.");
1664 return false;
1665 }
1666
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001667 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1668 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1669 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001670 {
1671 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001672 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001673 mPixelExecutable = NULL;
1674 return false;
1675 }
1676
1677 return true;
1678}
1679
1680bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1681{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001682 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001683
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001684 stream.write(GL_PROGRAM_BINARY_ANGLE);
1685 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001686
1687 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1688 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001689 stream.write(mLinkedAttribute[i].type);
1690 stream.write(mLinkedAttribute[i].name);
1691 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001692 }
1693
1694 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1695 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001696 stream.write(mSamplersPS[i].active);
1697 stream.write(mSamplersPS[i].logicalTextureUnit);
1698 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001699 }
1700
1701 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1702 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001703 stream.write(mSamplersVS[i].active);
1704 stream.write(mSamplersVS[i].logicalTextureUnit);
1705 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001706 }
1707
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001708 stream.write(mUsedVertexSamplerRange);
1709 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001710
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001711 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001712 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1713 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001714 stream.write(mUniforms[i]->type);
1715 stream.write(mUniforms[i]->_name);
1716 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001717
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001718 stream.write(mUniforms[i]->ps.float4Index);
1719 stream.write(mUniforms[i]->ps.samplerIndex);
1720 stream.write(mUniforms[i]->ps.boolIndex);
1721 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001722
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001723 stream.write(mUniforms[i]->vs.float4Index);
1724 stream.write(mUniforms[i]->vs.samplerIndex);
1725 stream.write(mUniforms[i]->vs.boolIndex);
1726 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001727 }
1728
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001729 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001730 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1731 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001732 stream.write(mUniformIndex[i].name);
1733 stream.write(mUniformIndex[i].element);
1734 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001735 }
1736
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001737 stream.write(mDxDepthRangeLocation);
1738 stream.write(mDxDepthLocation);
1739 stream.write(mDxCoordLocation);
1740 stream.write(mDxHalfPixelSizeLocation);
1741 stream.write(mDxFrontCCWLocation);
1742 stream.write(mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001743
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001744 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001745 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001746
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001747 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001748 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001749
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001750 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001751
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001752 GLsizei streamLength = stream.length();
1753 const void *streamData = stream.data();
1754
1755 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001756 if (totalLength > bufSize)
1757 {
1758 if (length)
1759 {
1760 *length = 0;
1761 }
1762
1763 return false;
1764 }
1765
1766 if (binary)
1767 {
1768 char *ptr = (char*) binary;
1769
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001770 memcpy(ptr, streamData, streamLength);
1771 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001772
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001773 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001774 ptr += sizeof(GUID);
1775
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001776 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001777 ptr += pixelShaderSize;
1778
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001779 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001780 ptr += vertexShaderSize;
1781
1782 ASSERT(ptr - totalLength == binary);
1783 }
1784
1785 if (length)
1786 {
1787 *length = totalLength;
1788 }
1789
1790 return true;
1791}
1792
1793GLint ProgramBinary::getLength()
1794{
1795 GLint length;
1796 if (save(NULL, INT_MAX, &length))
1797 {
1798 return length;
1799 }
1800 else
1801 {
1802 return 0;
1803 }
1804}
1805
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001806bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001807{
1808 if (!fragmentShader || !fragmentShader->isCompiled())
1809 {
1810 return false;
1811 }
1812
1813 if (!vertexShader || !vertexShader->isCompiled())
1814 {
1815 return false;
1816 }
1817
1818 std::string pixelHLSL = fragmentShader->getHLSL();
1819 std::string vertexHLSL = vertexShader->getHLSL();
1820
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001821 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001822 {
1823 return false;
1824 }
1825
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001826 bool success = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001827 rx::D3DConstantTable *constantTableVS = NULL;
1828 rx::D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001829 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1830 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001831
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001832 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001833 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001834 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001835 constantTableVS = mVertexExecutable->getConstantTable();
1836 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001837 }
1838 else
1839 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001840 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001841 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001842
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001843 delete mVertexExecutable;
1844 mVertexExecutable = NULL;
1845 delete mPixelExecutable;
1846 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001847 }
1848
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001849 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1850 {
1851 success = false;
1852 }
1853
1854 if (constantTableVS && constantTablePS)
1855 {
1856 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1857 {
1858 success = false;
1859 }
1860 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001861
1862 // these uniforms are searched as already-decorated because gl_ and dx_
1863 // are reserved prefixes, and do not receive additional decoration
1864 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1865 mDxDepthLocation = getUniformLocation("dx_Depth");
1866 mDxCoordLocation = getUniformLocation("dx_Coord");
1867 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1868 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1869 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1870
1871 Context *context = getContext();
1872 context->markDxUniformsDirty();
1873
1874 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001875}
1876
1877// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001878bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001879{
1880 unsigned int usedLocations = 0;
1881
1882 // Link attributes that have a binding location
1883 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1884 {
1885 int location = attributeBindings.getAttributeBinding(attribute->name);
1886
1887 if (location != -1) // Set by glBindAttribLocation
1888 {
1889 if (!mLinkedAttribute[location].name.empty())
1890 {
1891 // Multiple active attributes bound to the same location; not an error
1892 }
1893
1894 mLinkedAttribute[location] = *attribute;
1895
1896 int rows = VariableRowCount(attribute->type);
1897
1898 if (rows + location > MAX_VERTEX_ATTRIBS)
1899 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001900 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 +00001901
1902 return false;
1903 }
1904
1905 for (int i = 0; i < rows; i++)
1906 {
1907 usedLocations |= 1 << (location + i);
1908 }
1909 }
1910 }
1911
1912 // Link attributes that don't have a binding location
1913 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1914 {
1915 int location = attributeBindings.getAttributeBinding(attribute->name);
1916
1917 if (location == -1) // Not set by glBindAttribLocation
1918 {
1919 int rows = VariableRowCount(attribute->type);
1920 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1921
1922 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1923 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001924 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001925
1926 return false; // Fail to link
1927 }
1928
1929 mLinkedAttribute[availableIndex] = *attribute;
1930 }
1931 }
1932
1933 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1934 {
1935 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1936 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1937
1938 for (int r = 0; r < rows; r++)
1939 {
1940 mSemanticIndex[attributeIndex++] = index++;
1941 }
1942 }
1943
1944 return true;
1945}
1946
daniel@transgaming.com31240482012-11-28 21:06:41 +00001947bool ProgramBinary::linkUniforms(InfoLog &infoLog, rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001948{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001949 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001950 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001951 const rx::D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001952
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001953 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001954 {
1955 return false;
1956 }
1957 }
1958
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001959 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1960 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001961 const rx::D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001962
1963 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1964 {
1965 return false;
1966 }
1967 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001968 return true;
1969}
1970
1971// Adds the description of a constant found in the binary shader to the list of uniforms
1972// Returns true if succesful (uniform not already defined)
daniel@transgaming.com31240482012-11-28 21:06:41 +00001973bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const rx::D3DConstant *constant, const std::string &name,
1974 rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001975{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001976 if (constant->registerSet == rx::D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001977 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001978 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001979 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001980 const rx::D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1981 const rx::D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001982
1983 if (psConstant)
1984 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001985 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001986
1987 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1988 {
1989 mSamplersPS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001990 mSamplersPS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001991 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1992 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1993 }
1994 else
1995 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001996 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001997 return false;
1998 }
1999 }
2000
2001 if (vsConstant)
2002 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002003 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002004
2005 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2006 {
2007 mSamplersVS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002008 mSamplersVS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002009 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2010 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2011 }
2012 else
2013 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002014 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002015 return false;
2016 }
2017 }
2018 }
2019 }
2020
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002021 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002023 case rx::D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002024 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002025 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002026 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002027 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002028 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002029 const rx::D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002030
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002031 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002032
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002033 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002034 {
2035 return false;
2036 }
2037 }
2038 }
2039
2040 return true;
2041 }
daniel@transgaming.com31240482012-11-28 21:06:41 +00002042 case rx::D3DConstant::CLASS_SCALAR:
2043 case rx::D3DConstant::CLASS_VECTOR:
2044 case rx::D3DConstant::CLASS_MATRIX_COLUMNS:
2045 case rx::D3DConstant::CLASS_OBJECT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002046 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002047 default:
2048 UNREACHABLE();
2049 return false;
2050 }
2051}
2052
daniel@transgaming.com31240482012-11-28 21:06:41 +00002053bool ProgramBinary::defineUniform(GLenum shader, const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002054{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002055 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002056
2057 if(!uniform)
2058 {
2059 return false;
2060 }
2061
2062 // Check if already defined
2063 GLint location = getUniformLocation(uniform->name);
2064 GLenum type = uniform->type;
2065
2066 if (location >= 0)
2067 {
2068 delete uniform;
2069 uniform = mUniforms[mUniformIndex[location].index];
2070 }
2071
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002072 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2073 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002074
2075 if (location >= 0)
2076 {
2077 return uniform->type == type;
2078 }
2079
2080 mUniforms.push_back(uniform);
2081 unsigned int uniformIndex = mUniforms.size() - 1;
2082
2083 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2084 {
2085 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2086 }
2087
2088 return true;
2089}
2090
daniel@transgaming.com31240482012-11-28 21:06:41 +00002091Uniform *ProgramBinary::createUniform(const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002092{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002093 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002094 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002095 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002096 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002097 case rx::D3DConstant::PT_SAMPLER2D:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002098 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_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002101 default: UNREACHABLE();
2102 }
2103 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002104 case rx::D3DConstant::PT_SAMPLERCUBE:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002105 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_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002108 default: UNREACHABLE();
2109 }
2110 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002111 case rx::D3DConstant::PT_BOOL:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002112 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002113 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002114 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2115 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2116 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2117 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002118 default: UNREACHABLE();
2119 }
2120 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002121 case rx::D3DConstant::PT_INT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002122 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002123 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002124 case 1: return new Uniform(GL_INT, _name, constant->elements);
2125 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2126 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2127 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002128 default: UNREACHABLE();
2129 }
2130 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002131 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002132 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002133 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002134 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2135 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2136 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2137 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002138 default: UNREACHABLE();
2139 }
2140 break;
2141 default:
2142 UNREACHABLE();
2143 }
2144 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002145 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002146 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002147 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002148 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002149 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002150 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002151 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002152 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2153 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2154 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002155 default: UNREACHABLE();
2156 }
2157 break;
2158 default: UNREACHABLE();
2159 }
2160 }
2161 else UNREACHABLE();
2162
2163 return 0;
2164}
2165
2166// This method needs to match OutputHLSL::decorate
2167std::string ProgramBinary::decorateAttribute(const std::string &name)
2168{
2169 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2170 {
2171 return "_" + name;
2172 }
2173
2174 return name;
2175}
2176
2177std::string ProgramBinary::undecorateUniform(const std::string &_name)
2178{
2179 std::string name = _name;
2180
2181 // Remove any structure field decoration
2182 size_t pos = 0;
2183 while ((pos = name.find("._", pos)) != std::string::npos)
2184 {
2185 name.replace(pos, 2, ".");
2186 }
2187
2188 // Remove the leading decoration
2189 if (name[0] == '_')
2190 {
2191 return name.substr(1);
2192 }
2193 else if (name.compare(0, 3, "ar_") == 0)
2194 {
2195 return name.substr(3);
2196 }
2197
2198 return name;
2199}
2200
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002201// D3D9_REPLACE begin
2202void ProgramBinary::applyUniformnbv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002203{
2204 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2205 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2206
2207 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2208 {
2209 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2210 for (int i = 0; i < count; i++)
2211 {
2212 for (int j = 0; j < 4; j++)
2213 {
2214 if (j < width)
2215 {
2216 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2217 }
2218 else
2219 {
2220 vector[i * 4 + j] = 0.0f;
2221 }
2222 }
2223 }
2224 }
2225
2226 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2227 {
2228 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2229 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2230 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2231 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2232 for (int i = 0; i < copyCount; i++)
2233 {
2234 boolVector[i] = v[i] != GL_FALSE;
2235 }
2236 }
2237
2238 if (targetUniform->ps.float4Index >= 0)
2239 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002240 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002241 }
2242
2243 if (targetUniform->ps.boolIndex >= 0)
2244 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002245 device->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002246 }
2247
2248 if (targetUniform->vs.float4Index >= 0)
2249 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002250 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002251 }
2252
2253 if (targetUniform->vs.boolIndex >= 0)
2254 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002255 device->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002256 }
2257}
2258
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002259bool ProgramBinary::applyUniformnfv(IDirect3DDevice9 *device, Uniform *targetUniform, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002260{
2261 if (targetUniform->ps.registerCount)
2262 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002263 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002264 }
2265
2266 if (targetUniform->vs.registerCount)
2267 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002268 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002269 }
2270
2271 return true;
2272}
2273
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002274bool ProgramBinary::applyUniform1iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002275{
2276 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002277 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002278
2279 for (int i = 0; i < count; i++)
2280 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002281 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002282 }
2283
2284 if (targetUniform->ps.registerCount)
2285 {
2286 if (targetUniform->ps.samplerIndex >= 0)
2287 {
2288 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2289
2290 for (int i = 0; i < count; i++)
2291 {
2292 unsigned int samplerIndex = firstIndex + i;
2293
2294 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2295 {
2296 ASSERT(mSamplersPS[samplerIndex].active);
2297 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2298 }
2299 }
2300 }
2301 else
2302 {
2303 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002304 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002305 }
2306 }
2307
2308 if (targetUniform->vs.registerCount)
2309 {
2310 if (targetUniform->vs.samplerIndex >= 0)
2311 {
2312 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2313
2314 for (int i = 0; i < count; i++)
2315 {
2316 unsigned int samplerIndex = firstIndex + i;
2317
2318 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2319 {
2320 ASSERT(mSamplersVS[samplerIndex].active);
2321 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2322 }
2323 }
2324 }
2325 else
2326 {
2327 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002328 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002329 }
2330 }
2331
2332 return true;
2333}
2334
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002335bool ProgramBinary::applyUniform2iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002336{
2337 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002338 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002339
2340 for (int i = 0; i < count; i++)
2341 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002342 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002343
2344 v += 2;
2345 }
2346
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002347 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002348
2349 return true;
2350}
2351
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002352bool ProgramBinary::applyUniform3iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002353{
2354 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002355 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002356
2357 for (int i = 0; i < count; i++)
2358 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002359 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002360
2361 v += 3;
2362 }
2363
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002364 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002365
2366 return true;
2367}
2368
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002369bool ProgramBinary::applyUniform4iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002370{
2371 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002372 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002373
2374 for (int i = 0; i < count; i++)
2375 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002376 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002377
2378 v += 4;
2379 }
2380
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002381 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002382
2383 return true;
2384}
2385
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002386void ProgramBinary::applyUniformniv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002387{
2388 if (targetUniform->ps.registerCount)
2389 {
2390 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002391 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002392 }
2393
2394 if (targetUniform->vs.registerCount)
2395 {
2396 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002397 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002398 }
2399}
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002400// D3D9_REPLACE end
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002401
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002402bool ProgramBinary::isValidated() const
2403{
2404 return mValidated;
2405}
2406
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002407void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2408{
2409 // Skip over inactive attributes
2410 unsigned int activeAttribute = 0;
2411 unsigned int attribute;
2412 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2413 {
2414 if (mLinkedAttribute[attribute].name.empty())
2415 {
2416 continue;
2417 }
2418
2419 if (activeAttribute == index)
2420 {
2421 break;
2422 }
2423
2424 activeAttribute++;
2425 }
2426
2427 if (bufsize > 0)
2428 {
2429 const char *string = mLinkedAttribute[attribute].name.c_str();
2430
2431 strncpy(name, string, bufsize);
2432 name[bufsize - 1] = '\0';
2433
2434 if (length)
2435 {
2436 *length = strlen(name);
2437 }
2438 }
2439
2440 *size = 1; // Always a single 'type' instance
2441
2442 *type = mLinkedAttribute[attribute].type;
2443}
2444
2445GLint ProgramBinary::getActiveAttributeCount()
2446{
2447 int count = 0;
2448
2449 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2450 {
2451 if (!mLinkedAttribute[attributeIndex].name.empty())
2452 {
2453 count++;
2454 }
2455 }
2456
2457 return count;
2458}
2459
2460GLint ProgramBinary::getActiveAttributeMaxLength()
2461{
2462 int maxLength = 0;
2463
2464 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2465 {
2466 if (!mLinkedAttribute[attributeIndex].name.empty())
2467 {
2468 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2469 }
2470 }
2471
2472 return maxLength;
2473}
2474
2475void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2476{
2477 // Skip over internal uniforms
2478 unsigned int activeUniform = 0;
2479 unsigned int uniform;
2480 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2481 {
2482 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2483 {
2484 continue;
2485 }
2486
2487 if (activeUniform == index)
2488 {
2489 break;
2490 }
2491
2492 activeUniform++;
2493 }
2494
2495 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2496
2497 if (bufsize > 0)
2498 {
2499 std::string string = mUniforms[uniform]->name;
2500
2501 if (mUniforms[uniform]->isArray())
2502 {
2503 string += "[0]";
2504 }
2505
2506 strncpy(name, string.c_str(), bufsize);
2507 name[bufsize - 1] = '\0';
2508
2509 if (length)
2510 {
2511 *length = strlen(name);
2512 }
2513 }
2514
2515 *size = mUniforms[uniform]->arraySize;
2516
2517 *type = mUniforms[uniform]->type;
2518}
2519
2520GLint ProgramBinary::getActiveUniformCount()
2521{
2522 int count = 0;
2523
2524 unsigned int numUniforms = mUniforms.size();
2525 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2526 {
2527 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2528 {
2529 count++;
2530 }
2531 }
2532
2533 return count;
2534}
2535
2536GLint ProgramBinary::getActiveUniformMaxLength()
2537{
2538 int maxLength = 0;
2539
2540 unsigned int numUniforms = mUniforms.size();
2541 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2542 {
2543 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2544 {
2545 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2546 if (mUniforms[uniformIndex]->isArray())
2547 {
2548 length += 3; // Counting in "[0]".
2549 }
2550 maxLength = std::max(length, maxLength);
2551 }
2552 }
2553
2554 return maxLength;
2555}
2556
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002557void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002558{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002559 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002560 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002561 {
2562 mValidated = false;
2563 }
2564 else
2565 {
2566 mValidated = true;
2567 }
2568}
2569
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002570bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002571{
2572 // if any two active samplers in a program are of different types, but refer to the same
2573 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2574 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2575
2576 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2577 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2578
2579 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2580 {
2581 textureUnitType[i] = TEXTURE_UNKNOWN;
2582 }
2583
2584 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2585 {
2586 if (mSamplersPS[i].active)
2587 {
2588 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2589
2590 if (unit >= maxCombinedTextureImageUnits)
2591 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002592 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002593 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002594 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002595 }
2596
2597 return false;
2598 }
2599
2600 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2601 {
2602 if (mSamplersPS[i].textureType != textureUnitType[unit])
2603 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002604 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002605 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002606 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002607 }
2608
2609 return false;
2610 }
2611 }
2612 else
2613 {
2614 textureUnitType[unit] = mSamplersPS[i].textureType;
2615 }
2616 }
2617 }
2618
2619 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2620 {
2621 if (mSamplersVS[i].active)
2622 {
2623 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2624
2625 if (unit >= maxCombinedTextureImageUnits)
2626 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002627 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002628 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002629 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002630 }
2631
2632 return false;
2633 }
2634
2635 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2636 {
2637 if (mSamplersVS[i].textureType != textureUnitType[unit])
2638 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002639 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002640 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002641 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002642 }
2643
2644 return false;
2645 }
2646 }
2647 else
2648 {
2649 textureUnitType[unit] = mSamplersVS[i].textureType;
2650 }
2651 }
2652 }
2653
2654 return true;
2655}
2656
2657GLint ProgramBinary::getDxDepthRangeLocation() const
2658{
2659 return mDxDepthRangeLocation;
2660}
2661
2662GLint ProgramBinary::getDxDepthLocation() const
2663{
2664 return mDxDepthLocation;
2665}
2666
2667GLint ProgramBinary::getDxCoordLocation() const
2668{
2669 return mDxCoordLocation;
2670}
2671
2672GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2673{
2674 return mDxHalfPixelSizeLocation;
2675}
2676
2677GLint ProgramBinary::getDxFrontCCWLocation() const
2678{
2679 return mDxFrontCCWLocation;
2680}
2681
2682GLint ProgramBinary::getDxPointsOrLinesLocation() const
2683{
2684 return mDxPointsOrLinesLocation;
2685}
2686
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002687ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2688{
2689}
2690
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002691}