blob: 0de4ae3ba0cfbc38a86325af1edae533e2d65772 [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
daniel@transgaming.com88853c52012-12-20 20:56:40 +000022#undef near
23#undef far
24
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000025namespace gl
26{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000027std::string str(int i)
28{
29 char buffer[20];
30 snprintf(buffer, sizeof(buffer), "%d", i);
31 return buffer;
32}
33
34Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
35 : type(type), _name(_name), name(ProgramBinary::undecorateUniform(_name)), arraySize(arraySize)
36{
37 int bytes = UniformInternalSize(type) * arraySize;
38 data = new unsigned char[bytes];
39 memset(data, 0, bytes);
40 dirty = true;
41}
42
43Uniform::~Uniform()
44{
45 delete[] data;
46}
47
48bool Uniform::isArray()
49{
daniel@transgaming.com22ba0f72012-09-27 17:46:04 +000050 size_t dot = _name.find_last_of('.');
51 if (dot == std::string::npos) dot = -1;
52
53 return _name.compare(dot + 1, dot + 4, "ar_") == 0;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000054}
55
56UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
57 : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index)
58{
59}
60
daniel@transgaming.come87ca002012-07-24 18:30:43 +000061unsigned int ProgramBinary::mCurrentSerial = 1;
62
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000063ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000064{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000065 mPixelExecutable = NULL;
66 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000067
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000068 mValidated = false;
69
70 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
71 {
72 mSemanticIndex[index] = -1;
73 }
74
75 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
76 {
77 mSamplersPS[index].active = false;
78 }
79
80 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
81 {
82 mSamplersVS[index].active = false;
83 }
84
85 mUsedVertexSamplerRange = 0;
86 mUsedPixelSamplerRange = 0;
87
daniel@transgaming.com593ebc42012-12-20 20:56:46 +000088 mDxDepthRangeRegisterVS = -1;
89 mDxDepthRangeRegisterPS = -1;
90 mDxDepthFrontRegister = -1;
91 mDxCoordRegister = -1;
92 mDxHalfPixelSizeRegister = -1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000093}
94
95ProgramBinary::~ProgramBinary()
96{
daniel@transgaming.com95892412012-11-28 20:59:09 +000097 delete mPixelExecutable;
98 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000099
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000100 while (!mUniforms.empty())
101 {
102 delete mUniforms.back();
103 mUniforms.pop_back();
104 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000105}
106
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000107unsigned int ProgramBinary::getSerial() const
108{
109 return mSerial;
110}
111
112unsigned int ProgramBinary::issueSerial()
113{
114 return mCurrentSerial++;
115}
116
daniel@transgaming.com95892412012-11-28 20:59:09 +0000117rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000118{
119 return mPixelExecutable;
120}
121
daniel@transgaming.com95892412012-11-28 20:59:09 +0000122rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000123{
124 return mVertexExecutable;
125}
126
127GLuint ProgramBinary::getAttributeLocation(const char *name)
128{
129 if (name)
130 {
131 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
132 {
133 if (mLinkedAttribute[index].name == std::string(name))
134 {
135 return index;
136 }
137 }
138 }
139
140 return -1;
141}
142
143int ProgramBinary::getSemanticIndex(int attributeIndex)
144{
145 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
146
147 return mSemanticIndex[attributeIndex];
148}
149
150// Returns one more than the highest sampler index used.
151GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
152{
153 switch (type)
154 {
155 case SAMPLER_PIXEL:
156 return mUsedPixelSamplerRange;
157 case SAMPLER_VERTEX:
158 return mUsedVertexSamplerRange;
159 default:
160 UNREACHABLE();
161 return 0;
162 }
163}
164
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000165bool ProgramBinary::usesPointSize() const
166{
167 return mUsesPointSize;
168}
169
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000170// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
171// index (0-15 for the pixel shader and 0-3 for the vertex shader).
172GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
173{
174 GLint logicalTextureUnit = -1;
175
176 switch (type)
177 {
178 case SAMPLER_PIXEL:
179 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
180
181 if (mSamplersPS[samplerIndex].active)
182 {
183 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
184 }
185 break;
186 case SAMPLER_VERTEX:
187 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
188
189 if (mSamplersVS[samplerIndex].active)
190 {
191 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
192 }
193 break;
194 default: UNREACHABLE();
195 }
196
197 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
198 {
199 return logicalTextureUnit;
200 }
201
202 return -1;
203}
204
205// Returns the texture type for a given Direct3D 9 sampler type and
206// index (0-15 for the pixel shader and 0-3 for the vertex shader).
207TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
208{
209 switch (type)
210 {
211 case SAMPLER_PIXEL:
212 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
213 ASSERT(mSamplersPS[samplerIndex].active);
214 return mSamplersPS[samplerIndex].textureType;
215 case SAMPLER_VERTEX:
216 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
217 ASSERT(mSamplersVS[samplerIndex].active);
218 return mSamplersVS[samplerIndex].textureType;
219 default: UNREACHABLE();
220 }
221
222 return TEXTURE_2D;
223}
224
225GLint ProgramBinary::getUniformLocation(std::string name)
226{
227 unsigned int subscript = 0;
228
229 // Strip any trailing array operator and retrieve the subscript
230 size_t open = name.find_last_of('[');
231 size_t close = name.find_last_of(']');
232 if (open != std::string::npos && close == name.length() - 1)
233 {
234 subscript = atoi(name.substr(open + 1).c_str());
235 name.erase(open);
236 }
237
238 unsigned int numUniforms = mUniformIndex.size();
239 for (unsigned int location = 0; location < numUniforms; location++)
240 {
241 if (mUniformIndex[location].name == name &&
242 mUniformIndex[location].element == subscript)
243 {
244 return location;
245 }
246 }
247
248 return -1;
249}
250
251bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
252{
253 if (location < 0 || location >= (int)mUniformIndex.size())
254 {
255 return false;
256 }
257
258 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
259 targetUniform->dirty = true;
260
261 if (targetUniform->type == GL_FLOAT)
262 {
263 int arraySize = targetUniform->arraySize;
264
265 if (arraySize == 1 && count > 1)
266 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
267
268 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
269
270 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
271
272 for (int i = 0; i < count; i++)
273 {
274 target[0] = v[0];
275 target[1] = 0;
276 target[2] = 0;
277 target[3] = 0;
278 target += 4;
279 v += 1;
280 }
281 }
282 else if (targetUniform->type == GL_BOOL)
283 {
284 int arraySize = targetUniform->arraySize;
285
286 if (arraySize == 1 && count > 1)
287 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
288
289 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
290 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
291
292 for (int i = 0; i < count; ++i)
293 {
294 if (v[i] == 0.0f)
295 {
296 boolParams[i] = GL_FALSE;
297 }
298 else
299 {
300 boolParams[i] = GL_TRUE;
301 }
302 }
303 }
304 else
305 {
306 return false;
307 }
308
309 return true;
310}
311
312bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
313{
314 if (location < 0 || location >= (int)mUniformIndex.size())
315 {
316 return false;
317 }
318
319 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
320 targetUniform->dirty = true;
321
322 if (targetUniform->type == GL_FLOAT_VEC2)
323 {
324 int arraySize = targetUniform->arraySize;
325
326 if (arraySize == 1 && count > 1)
327 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
328
329 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
330
331 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
332
333 for (int i = 0; i < count; i++)
334 {
335 target[0] = v[0];
336 target[1] = v[1];
337 target[2] = 0;
338 target[3] = 0;
339 target += 4;
340 v += 2;
341 }
342 }
343 else if (targetUniform->type == GL_BOOL_VEC2)
344 {
345 int arraySize = targetUniform->arraySize;
346
347 if (arraySize == 1 && count > 1)
348 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
349
350 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
351
352 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
353
354 for (int i = 0; i < count * 2; ++i)
355 {
356 if (v[i] == 0.0f)
357 {
358 boolParams[i] = GL_FALSE;
359 }
360 else
361 {
362 boolParams[i] = GL_TRUE;
363 }
364 }
365 }
366 else
367 {
368 return false;
369 }
370
371 return true;
372}
373
374bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
375{
376 if (location < 0 || location >= (int)mUniformIndex.size())
377 {
378 return false;
379 }
380
381 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
382 targetUniform->dirty = true;
383
384 if (targetUniform->type == GL_FLOAT_VEC3)
385 {
386 int arraySize = targetUniform->arraySize;
387
388 if (arraySize == 1 && count > 1)
389 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
390
391 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
392
393 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
394
395 for (int i = 0; i < count; i++)
396 {
397 target[0] = v[0];
398 target[1] = v[1];
399 target[2] = v[2];
400 target[3] = 0;
401 target += 4;
402 v += 3;
403 }
404 }
405 else if (targetUniform->type == GL_BOOL_VEC3)
406 {
407 int arraySize = targetUniform->arraySize;
408
409 if (arraySize == 1 && count > 1)
410 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
411
412 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
413 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
414
415 for (int i = 0; i < count * 3; ++i)
416 {
417 if (v[i] == 0.0f)
418 {
419 boolParams[i] = GL_FALSE;
420 }
421 else
422 {
423 boolParams[i] = GL_TRUE;
424 }
425 }
426 }
427 else
428 {
429 return false;
430 }
431
432 return true;
433}
434
435bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
436{
437 if (location < 0 || location >= (int)mUniformIndex.size())
438 {
439 return false;
440 }
441
442 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
443 targetUniform->dirty = true;
444
445 if (targetUniform->type == GL_FLOAT_VEC4)
446 {
447 int arraySize = targetUniform->arraySize;
448
449 if (arraySize == 1 && count > 1)
450 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
451
452 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
453
454 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
455 v, 4 * sizeof(GLfloat) * count);
456 }
457 else if (targetUniform->type == GL_BOOL_VEC4)
458 {
459 int arraySize = targetUniform->arraySize;
460
461 if (arraySize == 1 && count > 1)
462 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
463
464 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
465 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
466
467 for (int i = 0; i < count * 4; ++i)
468 {
469 if (v[i] == 0.0f)
470 {
471 boolParams[i] = GL_FALSE;
472 }
473 else
474 {
475 boolParams[i] = GL_TRUE;
476 }
477 }
478 }
479 else
480 {
481 return false;
482 }
483
484 return true;
485}
486
487template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
488void transposeMatrix(T *target, const GLfloat *value)
489{
490 int copyWidth = std::min(targetWidth, srcWidth);
491 int copyHeight = std::min(targetHeight, srcHeight);
492
493 for (int x = 0; x < copyWidth; x++)
494 {
495 for (int y = 0; y < copyHeight; y++)
496 {
497 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
498 }
499 }
500 // clear unfilled right side
501 for (int y = 0; y < copyHeight; y++)
502 {
503 for (int x = srcWidth; x < targetWidth; x++)
504 {
505 target[y * targetWidth + x] = (T)0;
506 }
507 }
508 // clear unfilled bottom.
509 for (int y = srcHeight; y < targetHeight; y++)
510 {
511 for (int x = 0; x < targetWidth; x++)
512 {
513 target[y * targetWidth + x] = (T)0;
514 }
515 }
516}
517
518bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
519{
520 if (location < 0 || location >= (int)mUniformIndex.size())
521 {
522 return false;
523 }
524
525 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
526 targetUniform->dirty = true;
527
528 if (targetUniform->type != GL_FLOAT_MAT2)
529 {
530 return false;
531 }
532
533 int arraySize = targetUniform->arraySize;
534
535 if (arraySize == 1 && count > 1)
536 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
537
538 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
539
540 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
541 for (int i = 0; i < count; i++)
542 {
543 transposeMatrix<GLfloat,4,2,2,2>(target, value);
544 target += 8;
545 value += 4;
546 }
547
548 return true;
549}
550
551bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
552{
553 if (location < 0 || location >= (int)mUniformIndex.size())
554 {
555 return false;
556 }
557
558 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
559 targetUniform->dirty = true;
560
561 if (targetUniform->type != GL_FLOAT_MAT3)
562 {
563 return false;
564 }
565
566 int arraySize = targetUniform->arraySize;
567
568 if (arraySize == 1 && count > 1)
569 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
570
571 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
572
573 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
574 for (int i = 0; i < count; i++)
575 {
576 transposeMatrix<GLfloat,4,3,3,3>(target, value);
577 target += 12;
578 value += 9;
579 }
580
581 return true;
582}
583
584
585bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
586{
587 if (location < 0 || location >= (int)mUniformIndex.size())
588 {
589 return false;
590 }
591
592 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
593 targetUniform->dirty = true;
594
595 if (targetUniform->type != GL_FLOAT_MAT4)
596 {
597 return false;
598 }
599
600 int arraySize = targetUniform->arraySize;
601
602 if (arraySize == 1 && count > 1)
603 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
604
605 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
606
607 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
608 for (int i = 0; i < count; i++)
609 {
610 transposeMatrix<GLfloat,4,4,4,4>(target, value);
611 target += 16;
612 value += 16;
613 }
614
615 return true;
616}
617
618bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
619{
620 if (location < 0 || location >= (int)mUniformIndex.size())
621 {
622 return false;
623 }
624
625 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
626 targetUniform->dirty = true;
627
628 if (targetUniform->type == GL_INT ||
629 targetUniform->type == GL_SAMPLER_2D ||
630 targetUniform->type == GL_SAMPLER_CUBE)
631 {
632 int arraySize = targetUniform->arraySize;
633
634 if (arraySize == 1 && count > 1)
635 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
636
637 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
638
639 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
640 v, sizeof(GLint) * count);
641 }
642 else if (targetUniform->type == GL_BOOL)
643 {
644 int arraySize = targetUniform->arraySize;
645
646 if (arraySize == 1 && count > 1)
647 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
648
649 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
650 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
651
652 for (int i = 0; i < count; ++i)
653 {
654 if (v[i] == 0)
655 {
656 boolParams[i] = GL_FALSE;
657 }
658 else
659 {
660 boolParams[i] = GL_TRUE;
661 }
662 }
663 }
664 else
665 {
666 return false;
667 }
668
669 return true;
670}
671
672bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
673{
674 if (location < 0 || location >= (int)mUniformIndex.size())
675 {
676 return false;
677 }
678
679 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
680 targetUniform->dirty = true;
681
682 if (targetUniform->type == GL_INT_VEC2)
683 {
684 int arraySize = targetUniform->arraySize;
685
686 if (arraySize == 1 && count > 1)
687 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
688
689 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
690
691 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
692 v, 2 * sizeof(GLint) * count);
693 }
694 else if (targetUniform->type == GL_BOOL_VEC2)
695 {
696 int arraySize = targetUniform->arraySize;
697
698 if (arraySize == 1 && count > 1)
699 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
700
701 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
702 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
703
704 for (int i = 0; i < count * 2; ++i)
705 {
706 if (v[i] == 0)
707 {
708 boolParams[i] = GL_FALSE;
709 }
710 else
711 {
712 boolParams[i] = GL_TRUE;
713 }
714 }
715 }
716 else
717 {
718 return false;
719 }
720
721 return true;
722}
723
724bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
725{
726 if (location < 0 || location >= (int)mUniformIndex.size())
727 {
728 return false;
729 }
730
731 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
732 targetUniform->dirty = true;
733
734 if (targetUniform->type == GL_INT_VEC3)
735 {
736 int arraySize = targetUniform->arraySize;
737
738 if (arraySize == 1 && count > 1)
739 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
740
741 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
742
743 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
744 v, 3 * sizeof(GLint) * count);
745 }
746 else if (targetUniform->type == GL_BOOL_VEC3)
747 {
748 int arraySize = targetUniform->arraySize;
749
750 if (arraySize == 1 && count > 1)
751 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
752
753 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
754 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
755
756 for (int i = 0; i < count * 3; ++i)
757 {
758 if (v[i] == 0)
759 {
760 boolParams[i] = GL_FALSE;
761 }
762 else
763 {
764 boolParams[i] = GL_TRUE;
765 }
766 }
767 }
768 else
769 {
770 return false;
771 }
772
773 return true;
774}
775
776bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
777{
778 if (location < 0 || location >= (int)mUniformIndex.size())
779 {
780 return false;
781 }
782
783 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
784 targetUniform->dirty = true;
785
786 if (targetUniform->type == GL_INT_VEC4)
787 {
788 int arraySize = targetUniform->arraySize;
789
790 if (arraySize == 1 && count > 1)
791 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
792
793 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
794
795 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
796 v, 4 * sizeof(GLint) * count);
797 }
798 else if (targetUniform->type == GL_BOOL_VEC4)
799 {
800 int arraySize = targetUniform->arraySize;
801
802 if (arraySize == 1 && count > 1)
803 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
804
805 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
806 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
807
808 for (int i = 0; i < count * 4; ++i)
809 {
810 if (v[i] == 0)
811 {
812 boolParams[i] = GL_FALSE;
813 }
814 else
815 {
816 boolParams[i] = GL_TRUE;
817 }
818 }
819 }
820 else
821 {
822 return false;
823 }
824
825 return true;
826}
827
828bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
829{
830 if (location < 0 || location >= (int)mUniformIndex.size())
831 {
832 return false;
833 }
834
835 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
836
837 // sized queries -- ensure the provided buffer is large enough
838 if (bufSize)
839 {
840 int requiredBytes = UniformExternalSize(targetUniform->type);
841 if (*bufSize < requiredBytes)
842 {
843 return false;
844 }
845 }
846
847 switch (targetUniform->type)
848 {
849 case GL_FLOAT_MAT2:
850 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
851 break;
852 case GL_FLOAT_MAT3:
853 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
854 break;
855 case GL_FLOAT_MAT4:
856 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
857 break;
858 default:
859 {
860 unsigned int count = UniformExternalComponentCount(targetUniform->type);
861 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
862
863 switch (UniformComponentType(targetUniform->type))
864 {
865 case GL_BOOL:
866 {
867 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
868
869 for (unsigned int i = 0; i < count; ++i)
870 {
871 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
872 }
873 }
874 break;
875 case GL_FLOAT:
876 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
877 count * sizeof(GLfloat));
878 break;
879 case GL_INT:
880 {
881 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
882
883 for (unsigned int i = 0; i < count; ++i)
884 {
885 params[i] = (float)intParams[i];
886 }
887 }
888 break;
889 default: UNREACHABLE();
890 }
891 }
892 }
893
894 return true;
895}
896
897bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
898{
899 if (location < 0 || location >= (int)mUniformIndex.size())
900 {
901 return false;
902 }
903
904 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
905
906 // sized queries -- ensure the provided buffer is large enough
907 if (bufSize)
908 {
909 int requiredBytes = UniformExternalSize(targetUniform->type);
910 if (*bufSize < requiredBytes)
911 {
912 return false;
913 }
914 }
915
916 switch (targetUniform->type)
917 {
918 case GL_FLOAT_MAT2:
919 {
920 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
921 }
922 break;
923 case GL_FLOAT_MAT3:
924 {
925 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
926 }
927 break;
928 case GL_FLOAT_MAT4:
929 {
930 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
931 }
932 break;
933 default:
934 {
935 unsigned int count = UniformExternalComponentCount(targetUniform->type);
936 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
937
938 switch (UniformComponentType(targetUniform->type))
939 {
940 case GL_BOOL:
941 {
942 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
943
944 for (unsigned int i = 0; i < count; ++i)
945 {
946 params[i] = (GLint)boolParams[i];
947 }
948 }
949 break;
950 case GL_FLOAT:
951 {
952 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
953
954 for (unsigned int i = 0; i < count; ++i)
955 {
956 params[i] = (GLint)floatParams[i];
957 }
958 }
959 break;
960 case GL_INT:
961 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
962 count * sizeof(GLint));
963 break;
964 default: UNREACHABLE();
965 }
966 }
967 }
968
969 return true;
970}
971
972void ProgramBinary::dirtyAllUniforms()
973{
974 unsigned int numUniforms = mUniforms.size();
975 for (unsigned int index = 0; index < numUniforms; index++)
976 {
977 mUniforms[index]->dirty = true;
978 }
979}
980
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000981// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000982void ProgramBinary::applyUniforms()
983{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000984 // Retrieve sampler uniform values
985 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
986 {
987 Uniform *targetUniform = *ub;
988
989 if (targetUniform->dirty)
990 {
991 int count = targetUniform->arraySize;
992 GLint *v = (GLint*)targetUniform->data;
993
994 switch (targetUniform->type)
995 {
996 case GL_SAMPLER_2D:
997 case GL_SAMPLER_CUBE:
998 {
999 if (targetUniform->ps.registerCount)
1000 {
1001 if (targetUniform->ps.samplerIndex >= 0)
1002 {
1003 unsigned int firstIndex = targetUniform->ps.samplerIndex;
1004
1005 for (int i = 0; i < count; i++)
1006 {
1007 unsigned int samplerIndex = firstIndex + i;
1008
1009 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1010 {
1011 ASSERT(mSamplersPS[samplerIndex].active);
1012 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
1013 }
1014 }
1015 }
1016 }
1017
1018 if (targetUniform->vs.registerCount)
1019 {
1020 if (targetUniform->vs.samplerIndex >= 0)
1021 {
1022 unsigned int firstIndex = targetUniform->vs.samplerIndex;
1023
1024 for (int i = 0; i < count; i++)
1025 {
1026 unsigned int samplerIndex = firstIndex + i;
1027
1028 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
1029 {
1030 ASSERT(mSamplersVS[samplerIndex].active);
1031 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
1032 }
1033 }
1034 }
1035 }
1036 }
1037 break;
1038 }
1039 }
1040 }
1041
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001042 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) // D3D9_REPLACE
1043 {
1044 return; // UNIMPLEMENTED
1045 }
1046
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001047 // Set all other uniforms
1048 rx::Renderer9::makeRenderer9(mRenderer)->applyUniforms(&mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001049}
1050
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001051// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1052// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001053int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001054{
1055 Context *context = getContext();
1056 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1057
1058 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1059 {
1060 int n = VariableRowCount(varying->type) * varying->size;
1061 int m = VariableColumnCount(varying->type);
1062 bool success = false;
1063
1064 if (m == 2 || m == 3 || m == 4)
1065 {
1066 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1067 {
1068 bool available = true;
1069
1070 for (int y = 0; y < n && available; y++)
1071 {
1072 for (int x = 0; x < m && available; x++)
1073 {
1074 if (packing[r + y][x])
1075 {
1076 available = false;
1077 }
1078 }
1079 }
1080
1081 if (available)
1082 {
1083 varying->reg = r;
1084 varying->col = 0;
1085
1086 for (int y = 0; y < n; y++)
1087 {
1088 for (int x = 0; x < m; x++)
1089 {
1090 packing[r + y][x] = &*varying;
1091 }
1092 }
1093
1094 success = true;
1095 }
1096 }
1097
1098 if (!success && m == 2)
1099 {
1100 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1101 {
1102 bool available = true;
1103
1104 for (int y = 0; y < n && available; y++)
1105 {
1106 for (int x = 2; x < 4 && available; x++)
1107 {
1108 if (packing[r + y][x])
1109 {
1110 available = false;
1111 }
1112 }
1113 }
1114
1115 if (available)
1116 {
1117 varying->reg = r;
1118 varying->col = 2;
1119
1120 for (int y = 0; y < n; y++)
1121 {
1122 for (int x = 2; x < 4; x++)
1123 {
1124 packing[r + y][x] = &*varying;
1125 }
1126 }
1127
1128 success = true;
1129 }
1130 }
1131 }
1132 }
1133 else if (m == 1)
1134 {
1135 int space[4] = {0};
1136
1137 for (int y = 0; y < maxVaryingVectors; y++)
1138 {
1139 for (int x = 0; x < 4; x++)
1140 {
1141 space[x] += packing[y][x] ? 0 : 1;
1142 }
1143 }
1144
1145 int column = 0;
1146
1147 for (int x = 0; x < 4; x++)
1148 {
1149 if (space[x] >= n && space[x] < space[column])
1150 {
1151 column = x;
1152 }
1153 }
1154
1155 if (space[column] >= n)
1156 {
1157 for (int r = 0; r < maxVaryingVectors; r++)
1158 {
1159 if (!packing[r][column])
1160 {
1161 varying->reg = r;
1162
1163 for (int y = r; y < r + n; y++)
1164 {
1165 packing[y][column] = &*varying;
1166 }
1167
1168 break;
1169 }
1170 }
1171
1172 varying->col = column;
1173
1174 success = true;
1175 }
1176 }
1177 else UNREACHABLE();
1178
1179 if (!success)
1180 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001181 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001182
1183 return -1;
1184 }
1185 }
1186
1187 // Return the number of used registers
1188 int registers = 0;
1189
1190 for (int r = 0; r < maxVaryingVectors; r++)
1191 {
1192 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1193 {
1194 registers++;
1195 }
1196 }
1197
1198 return registers;
1199}
1200
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001201bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001202{
1203 if (pixelHLSL.empty() || vertexHLSL.empty())
1204 {
1205 return false;
1206 }
1207
1208 // Reset the varying register assignments
1209 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1210 {
1211 fragVar->reg = -1;
1212 fragVar->col = -1;
1213 }
1214
1215 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1216 {
1217 vtxVar->reg = -1;
1218 vtxVar->col = -1;
1219 }
1220
1221 // Map the varyings to the register file
1222 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001223 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001224
1225 if (registers < 0)
1226 {
1227 return false;
1228 }
1229
1230 // Write the HLSL input/output declarations
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001231 const bool sm3 = (mRenderer->getMajorShaderModel() >= 3);
1232 const bool sm4 = (mRenderer->getMajorShaderModel() >= 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001233 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001234 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1235
1236 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1237 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001238 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001239
1240 return false;
1241 }
1242
1243 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1244 {
1245 bool matched = false;
1246
1247 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1248 {
1249 if (output->name == input->name)
1250 {
1251 if (output->type != input->type || output->size != input->size)
1252 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001253 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 +00001254
1255 return false;
1256 }
1257
1258 output->reg = input->reg;
1259 output->col = input->col;
1260
1261 matched = true;
1262 break;
1263 }
1264 }
1265
1266 if (!matched)
1267 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001268 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001269
1270 return false;
1271 }
1272 }
1273
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001274 mUsesPointSize = vertexShader->mUsesPointSize;
1275 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001276 std::string targetSemantic = sm4 ? "SV_Target" : "COLOR";
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001277 std::string positionSemantic = sm4 ? "SV_POSITION" : "POSITION";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001278
1279 vertexHLSL += "struct VS_INPUT\n"
1280 "{\n";
1281
1282 int semanticIndex = 0;
1283 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1284 {
1285 switch (attribute->type)
1286 {
1287 case GL_FLOAT: vertexHLSL += " float "; break;
1288 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1289 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1290 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1291 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1292 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1293 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1294 default: UNREACHABLE();
1295 }
1296
1297 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1298
1299 semanticIndex += VariableRowCount(attribute->type);
1300 }
1301
1302 vertexHLSL += "};\n"
1303 "\n"
1304 "struct VS_OUTPUT\n"
1305 "{\n"
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001306 " float4 gl_Position : " + positionSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001307
1308 for (int r = 0; r < registers; r++)
1309 {
1310 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1311
1312 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1313 }
1314
1315 if (fragmentShader->mUsesFragCoord)
1316 {
1317 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1318 }
1319
1320 if (vertexShader->mUsesPointSize && sm3)
1321 {
1322 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1323 }
1324
1325 vertexHLSL += "};\n"
1326 "\n"
1327 "VS_OUTPUT main(VS_INPUT input)\n"
1328 "{\n";
1329
1330 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1331 {
1332 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1333
1334 if (VariableRowCount(attribute->type) > 1) // Matrix
1335 {
1336 vertexHLSL += "transpose";
1337 }
1338
1339 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1340 }
1341
1342 vertexHLSL += "\n"
1343 " gl_main();\n"
1344 "\n"
1345 " VS_OUTPUT output;\n"
1346 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001347 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001348 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1349 " output.gl_Position.w = gl_Position.w;\n";
1350
1351 if (vertexShader->mUsesPointSize && sm3)
1352 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001353 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001354 }
1355
1356 if (fragmentShader->mUsesFragCoord)
1357 {
1358 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1359 }
1360
1361 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1362 {
1363 if (varying->reg >= 0)
1364 {
1365 for (int i = 0; i < varying->size; i++)
1366 {
1367 int rows = VariableRowCount(varying->type);
1368
1369 for (int j = 0; j < rows; j++)
1370 {
1371 int r = varying->reg + i * rows + j;
1372 vertexHLSL += " output.v" + str(r);
1373
1374 bool sharedRegister = false; // Register used by multiple varyings
1375
1376 for (int x = 0; x < 4; x++)
1377 {
1378 if (packing[r][x] && packing[r][x] != packing[r][0])
1379 {
1380 sharedRegister = true;
1381 break;
1382 }
1383 }
1384
1385 if(sharedRegister)
1386 {
1387 vertexHLSL += ".";
1388
1389 for (int x = 0; x < 4; x++)
1390 {
1391 if (packing[r][x] == &*varying)
1392 {
1393 switch(x)
1394 {
1395 case 0: vertexHLSL += "x"; break;
1396 case 1: vertexHLSL += "y"; break;
1397 case 2: vertexHLSL += "z"; break;
1398 case 3: vertexHLSL += "w"; break;
1399 }
1400 }
1401 }
1402 }
1403
1404 vertexHLSL += " = " + varying->name;
1405
1406 if (varying->array)
1407 {
1408 vertexHLSL += "[" + str(i) + "]";
1409 }
1410
1411 if (rows > 1)
1412 {
1413 vertexHLSL += "[" + str(j) + "]";
1414 }
1415
1416 vertexHLSL += ";\n";
1417 }
1418 }
1419 }
1420 }
1421
1422 vertexHLSL += "\n"
1423 " return output;\n"
1424 "}\n";
1425
1426 pixelHLSL += "struct PS_INPUT\n"
1427 "{\n";
1428
1429 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1430 {
1431 if (varying->reg >= 0)
1432 {
1433 for (int i = 0; i < varying->size; i++)
1434 {
1435 int rows = VariableRowCount(varying->type);
1436 for (int j = 0; j < rows; j++)
1437 {
1438 std::string n = str(varying->reg + i * rows + j);
1439 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1440 }
1441 }
1442 }
1443 else UNREACHABLE();
1444 }
1445
1446 if (fragmentShader->mUsesFragCoord)
1447 {
1448 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1449 if (sm3) {
1450 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1451 }
1452 }
1453
1454 if (fragmentShader->mUsesPointCoord && sm3)
1455 {
1456 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1457 }
1458
1459 if (fragmentShader->mUsesFrontFacing)
1460 {
1461 pixelHLSL += " float vFace : VFACE;\n";
1462 }
1463
1464 pixelHLSL += "};\n"
1465 "\n"
1466 "struct PS_OUTPUT\n"
1467 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001468 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001469 "};\n"
1470 "\n"
1471 "PS_OUTPUT main(PS_INPUT input)\n"
1472 "{\n";
1473
1474 if (fragmentShader->mUsesFragCoord)
1475 {
1476 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1477
1478 if (sm3)
1479 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001480 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001481 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001482 }
1483 else
1484 {
1485 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1486 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001487 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001488 }
1489
daniel@transgaming.com12985182012-12-20 20:56:31 +00001490 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001491 " gl_FragCoord.w = rhw;\n";
1492 }
1493
1494 if (fragmentShader->mUsesPointCoord && sm3)
1495 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001496 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1497 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001498 }
1499
1500 if (fragmentShader->mUsesFrontFacing)
1501 {
daniel@transgaming.com12985182012-12-20 20:56:31 +00001502 pixelHLSL += " gl_FrontFacing = (input.vFace * dx_DepthFront.z >= 0.0);\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001503 }
1504
1505 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1506 {
1507 if (varying->reg >= 0)
1508 {
1509 for (int i = 0; i < varying->size; i++)
1510 {
1511 int rows = VariableRowCount(varying->type);
1512 for (int j = 0; j < rows; j++)
1513 {
1514 std::string n = str(varying->reg + i * rows + j);
1515 pixelHLSL += " " + varying->name;
1516
1517 if (varying->array)
1518 {
1519 pixelHLSL += "[" + str(i) + "]";
1520 }
1521
1522 if (rows > 1)
1523 {
1524 pixelHLSL += "[" + str(j) + "]";
1525 }
1526
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001527 switch (VariableColumnCount(varying->type))
1528 {
1529 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1530 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1531 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1532 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1533 default: UNREACHABLE();
1534 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001535 }
1536 }
1537 }
1538 else UNREACHABLE();
1539 }
1540
1541 pixelHLSL += "\n"
1542 " gl_main();\n"
1543 "\n"
1544 " PS_OUTPUT output;\n"
1545 " output.gl_Color[0] = gl_Color[0];\n"
1546 "\n"
1547 " return output;\n"
1548 "}\n";
1549
1550 return true;
1551}
1552
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001553bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1554{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001555 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001556
1557 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001558 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001559 if (format != GL_PROGRAM_BINARY_ANGLE)
1560 {
1561 infoLog.append("Invalid program binary format.");
1562 return false;
1563 }
1564
1565 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001566 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001567 if (version != BUILD_REVISION)
1568 {
1569 infoLog.append("Invalid program binary version.");
1570 return false;
1571 }
1572
1573 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1574 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001575 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001576 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001577 stream.read(&name);
1578 mLinkedAttribute[i].name = name;
1579 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001580 }
1581
1582 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1583 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001584 stream.read(&mSamplersPS[i].active);
1585 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001586
1587 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001588 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001589 mSamplersPS[i].textureType = (TextureType) textureType;
1590 }
1591
1592 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1593 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001594 stream.read(&mSamplersVS[i].active);
1595 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001596
1597 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001598 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001599 mSamplersVS[i].textureType = (TextureType) textureType;
1600 }
1601
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001602 stream.read(&mUsedVertexSamplerRange);
1603 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001604
1605 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001606 stream.read(&size);
1607 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001608 {
1609 infoLog.append("Invalid program binary.");
1610 return false;
1611 }
1612
1613 mUniforms.resize(size);
1614 for (unsigned int i = 0; i < size; ++i)
1615 {
1616 GLenum type;
1617 std::string _name;
1618 unsigned int arraySize;
1619
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001620 stream.read(&type);
1621 stream.read(&_name);
1622 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001623
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001624 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001625
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001626 stream.read(&mUniforms[i]->ps.float4Index);
1627 stream.read(&mUniforms[i]->ps.samplerIndex);
1628 stream.read(&mUniforms[i]->ps.boolIndex);
1629 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001630
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001631 stream.read(&mUniforms[i]->vs.float4Index);
1632 stream.read(&mUniforms[i]->vs.samplerIndex);
1633 stream.read(&mUniforms[i]->vs.boolIndex);
1634 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001635 }
1636
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001637 stream.read(&size);
1638 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001639 {
1640 infoLog.append("Invalid program binary.");
1641 return false;
1642 }
1643
1644 mUniformIndex.resize(size);
1645 for (unsigned int i = 0; i < size; ++i)
1646 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001647 stream.read(&mUniformIndex[i].name);
1648 stream.read(&mUniformIndex[i].element);
1649 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001650 }
1651
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001652 stream.read(&mDxDepthRangeRegisterVS);
1653 stream.read(&mDxDepthRangeRegisterPS);
1654 stream.read(&mDxDepthFrontRegister);
1655 stream.read(&mDxCoordRegister);
1656 stream.read(&mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001657
1658 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001659 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001660
1661 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001662 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001663
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001664 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001665
daniel@transgaming.com36038542012-11-28 20:59:26 +00001666 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001667 ptr += sizeof(GUID);
1668
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001669 GUID identifier = mRenderer->getAdapterIdentifier();
1670 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001671 {
1672 infoLog.append("Invalid program binary.");
1673 return false;
1674 }
1675
1676 const char *pixelShaderFunction = ptr;
1677 ptr += pixelShaderSize;
1678
1679 const char *vertexShaderFunction = ptr;
1680 ptr += vertexShaderSize;
1681
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001682 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1683 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1684 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001685 {
1686 infoLog.append("Could not create pixel shader.");
1687 return false;
1688 }
1689
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001690 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1691 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1692 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001693 {
1694 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001695 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001696 mPixelExecutable = NULL;
1697 return false;
1698 }
1699
1700 return true;
1701}
1702
1703bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1704{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001705 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001706
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001707 stream.write(GL_PROGRAM_BINARY_ANGLE);
1708 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001709
1710 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1711 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001712 stream.write(mLinkedAttribute[i].type);
1713 stream.write(mLinkedAttribute[i].name);
1714 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001715 }
1716
1717 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1718 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001719 stream.write(mSamplersPS[i].active);
1720 stream.write(mSamplersPS[i].logicalTextureUnit);
1721 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001722 }
1723
1724 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1725 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001726 stream.write(mSamplersVS[i].active);
1727 stream.write(mSamplersVS[i].logicalTextureUnit);
1728 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001729 }
1730
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001731 stream.write(mUsedVertexSamplerRange);
1732 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001733
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001734 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001735 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1736 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001737 stream.write(mUniforms[i]->type);
1738 stream.write(mUniforms[i]->_name);
1739 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001740
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001741 stream.write(mUniforms[i]->ps.float4Index);
1742 stream.write(mUniforms[i]->ps.samplerIndex);
1743 stream.write(mUniforms[i]->ps.boolIndex);
1744 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001745
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001746 stream.write(mUniforms[i]->vs.float4Index);
1747 stream.write(mUniforms[i]->vs.samplerIndex);
1748 stream.write(mUniforms[i]->vs.boolIndex);
1749 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001750 }
1751
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001752 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001753 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1754 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001755 stream.write(mUniformIndex[i].name);
1756 stream.write(mUniformIndex[i].element);
1757 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001758 }
1759
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001760 stream.write(mDxDepthRangeRegisterVS);
1761 stream.write(mDxDepthRangeRegisterPS);
1762 stream.write(mDxDepthFrontRegister);
1763 stream.write(mDxCoordRegister);
1764 stream.write(mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001765
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001766 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001767 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001768
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001769 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001770 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001771
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001772 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001773
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001774 GLsizei streamLength = stream.length();
1775 const void *streamData = stream.data();
1776
1777 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001778 if (totalLength > bufSize)
1779 {
1780 if (length)
1781 {
1782 *length = 0;
1783 }
1784
1785 return false;
1786 }
1787
1788 if (binary)
1789 {
1790 char *ptr = (char*) binary;
1791
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001792 memcpy(ptr, streamData, streamLength);
1793 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001794
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001795 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001796 ptr += sizeof(GUID);
1797
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001798 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001799 ptr += pixelShaderSize;
1800
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001801 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001802 ptr += vertexShaderSize;
1803
1804 ASSERT(ptr - totalLength == binary);
1805 }
1806
1807 if (length)
1808 {
1809 *length = totalLength;
1810 }
1811
1812 return true;
1813}
1814
1815GLint ProgramBinary::getLength()
1816{
1817 GLint length;
1818 if (save(NULL, INT_MAX, &length))
1819 {
1820 return length;
1821 }
1822 else
1823 {
1824 return 0;
1825 }
1826}
1827
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001828bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001829{
1830 if (!fragmentShader || !fragmentShader->isCompiled())
1831 {
1832 return false;
1833 }
1834
1835 if (!vertexShader || !vertexShader->isCompiled())
1836 {
1837 return false;
1838 }
1839
1840 std::string pixelHLSL = fragmentShader->getHLSL();
1841 std::string vertexHLSL = vertexShader->getHLSL();
1842
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001843 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001844 {
1845 return false;
1846 }
1847
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001848 bool success = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001849 rx::D3DConstantTable *constantTableVS = NULL;
1850 rx::D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001851 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1852 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001853
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001854 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001855 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001856 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001857 constantTableVS = mVertexExecutable->getConstantTable();
1858 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001859 }
1860 else
1861 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001862 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001863 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001864
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001865 delete mVertexExecutable;
1866 mVertexExecutable = NULL;
1867 delete mPixelExecutable;
1868 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001869 }
1870
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001871 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1872 {
1873 success = false;
1874 }
1875
1876 if (constantTableVS && constantTablePS)
1877 {
1878 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1879 {
1880 success = false;
1881 }
1882 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001883
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001884 Context *context = getContext();
1885 context->markDxUniformsDirty();
1886
1887 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001888}
1889
1890// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001891bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001892{
1893 unsigned int usedLocations = 0;
1894
1895 // Link attributes that have a binding location
1896 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1897 {
1898 int location = attributeBindings.getAttributeBinding(attribute->name);
1899
1900 if (location != -1) // Set by glBindAttribLocation
1901 {
1902 if (!mLinkedAttribute[location].name.empty())
1903 {
1904 // Multiple active attributes bound to the same location; not an error
1905 }
1906
1907 mLinkedAttribute[location] = *attribute;
1908
1909 int rows = VariableRowCount(attribute->type);
1910
1911 if (rows + location > MAX_VERTEX_ATTRIBS)
1912 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001913 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 +00001914
1915 return false;
1916 }
1917
1918 for (int i = 0; i < rows; i++)
1919 {
1920 usedLocations |= 1 << (location + i);
1921 }
1922 }
1923 }
1924
1925 // Link attributes that don't have a binding location
1926 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1927 {
1928 int location = attributeBindings.getAttributeBinding(attribute->name);
1929
1930 if (location == -1) // Not set by glBindAttribLocation
1931 {
1932 int rows = VariableRowCount(attribute->type);
1933 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1934
1935 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1936 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001937 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001938
1939 return false; // Fail to link
1940 }
1941
1942 mLinkedAttribute[availableIndex] = *attribute;
1943 }
1944 }
1945
1946 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1947 {
1948 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1949 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1950
1951 for (int r = 0; r < rows; r++)
1952 {
1953 mSemanticIndex[attributeIndex++] = index++;
1954 }
1955 }
1956
1957 return true;
1958}
1959
daniel@transgaming.com31240482012-11-28 21:06:41 +00001960bool ProgramBinary::linkUniforms(InfoLog &infoLog, rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001961{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001962 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001963 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001964 const rx::D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001965
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001966 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001967 {
1968 return false;
1969 }
1970 }
1971
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001972 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1973 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001974 const rx::D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001975
1976 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1977 {
1978 return false;
1979 }
1980 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001981 return true;
1982}
1983
1984// Adds the description of a constant found in the binary shader to the list of uniforms
1985// Returns true if succesful (uniform not already defined)
daniel@transgaming.com31240482012-11-28 21:06:41 +00001986bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const rx::D3DConstant *constant, const std::string &name,
1987 rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001988{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001989 if (constant->registerSet == rx::D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001990 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001991 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001992 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001993 const rx::D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1994 const rx::D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001995
1996 if (psConstant)
1997 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001998 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001999
2000 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2001 {
2002 mSamplersPS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002003 mSamplersPS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002004 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2005 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2006 }
2007 else
2008 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002009 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002010 return false;
2011 }
2012 }
2013
2014 if (vsConstant)
2015 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002016 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002017
2018 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2019 {
2020 mSamplersVS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002021 mSamplersVS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2023 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2024 }
2025 else
2026 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002027 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002028 return false;
2029 }
2030 }
2031 }
2032 }
2033
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002034 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002035 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002036 case rx::D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002037 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002038 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002039 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002040 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002041 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002042 const rx::D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002043
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002044 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002045
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002046 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002047 {
2048 return false;
2049 }
2050 }
2051 }
2052
2053 return true;
2054 }
daniel@transgaming.com31240482012-11-28 21:06:41 +00002055 case rx::D3DConstant::CLASS_SCALAR:
2056 case rx::D3DConstant::CLASS_VECTOR:
2057 case rx::D3DConstant::CLASS_MATRIX_COLUMNS:
2058 case rx::D3DConstant::CLASS_OBJECT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002059 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002060 default:
2061 UNREACHABLE();
2062 return false;
2063 }
2064}
2065
daniel@transgaming.com31240482012-11-28 21:06:41 +00002066bool ProgramBinary::defineUniform(GLenum shader, const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002067{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002068 if (_name == "dx_DepthRange")
2069 {
2070 if (shader == GL_VERTEX_SHADER) mDxDepthRangeRegisterVS = constant->registerIndex;
2071 if (shader == GL_FRAGMENT_SHADER) mDxDepthRangeRegisterPS = constant->registerIndex;
2072 return true;
2073 }
2074
2075 if (_name == "dx_DepthFront")
2076 {
2077 mDxDepthFrontRegister = constant->registerIndex;
2078 return true;
2079 }
2080
2081 if (_name == "dx_Coord")
2082 {
2083 mDxCoordRegister = constant->registerIndex;
2084 return true;
2085 }
2086
2087 if (_name == "dx_HalfPixelSize")
2088 {
2089 mDxHalfPixelSizeRegister = constant->registerIndex;
2090 return true;
2091 }
2092
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002093 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002094
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002095 if (!uniform)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002096 {
2097 return false;
2098 }
2099
2100 // Check if already defined
2101 GLint location = getUniformLocation(uniform->name);
2102 GLenum type = uniform->type;
2103
2104 if (location >= 0)
2105 {
2106 delete uniform;
2107 uniform = mUniforms[mUniformIndex[location].index];
2108 }
2109
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002110 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2111 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002112
2113 if (location >= 0)
2114 {
2115 return uniform->type == type;
2116 }
2117
2118 mUniforms.push_back(uniform);
2119 unsigned int uniformIndex = mUniforms.size() - 1;
2120
2121 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2122 {
2123 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2124 }
2125
2126 return true;
2127}
2128
daniel@transgaming.com31240482012-11-28 21:06:41 +00002129Uniform *ProgramBinary::createUniform(const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002130{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002131 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002132 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002133 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002134 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002135 case rx::D3DConstant::PT_SAMPLER2D:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002136 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002137 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002138 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002139 default: UNREACHABLE();
2140 }
2141 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002142 case rx::D3DConstant::PT_SAMPLERCUBE:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002143 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002144 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002145 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002146 default: UNREACHABLE();
2147 }
2148 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002149 case rx::D3DConstant::PT_BOOL:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002150 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002151 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002152 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2153 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2154 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2155 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002156 default: UNREACHABLE();
2157 }
2158 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002159 case rx::D3DConstant::PT_INT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002160 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002161 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002162 case 1: return new Uniform(GL_INT, _name, constant->elements);
2163 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2164 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2165 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002166 default: UNREACHABLE();
2167 }
2168 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002169 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002170 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002171 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002172 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2173 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2174 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2175 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002176 default: UNREACHABLE();
2177 }
2178 break;
2179 default:
2180 UNREACHABLE();
2181 }
2182 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002183 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002184 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002185 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002186 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002187 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002188 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002189 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002190 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2191 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2192 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002193 default: UNREACHABLE();
2194 }
2195 break;
2196 default: UNREACHABLE();
2197 }
2198 }
2199 else UNREACHABLE();
2200
2201 return 0;
2202}
2203
2204// This method needs to match OutputHLSL::decorate
2205std::string ProgramBinary::decorateAttribute(const std::string &name)
2206{
2207 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2208 {
2209 return "_" + name;
2210 }
2211
2212 return name;
2213}
2214
2215std::string ProgramBinary::undecorateUniform(const std::string &_name)
2216{
2217 std::string name = _name;
2218
2219 // Remove any structure field decoration
2220 size_t pos = 0;
2221 while ((pos = name.find("._", pos)) != std::string::npos)
2222 {
2223 name.replace(pos, 2, ".");
2224 }
2225
2226 // Remove the leading decoration
2227 if (name[0] == '_')
2228 {
2229 return name.substr(1);
2230 }
2231 else if (name.compare(0, 3, "ar_") == 0)
2232 {
2233 return name.substr(3);
2234 }
2235
2236 return name;
2237}
2238
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002239bool ProgramBinary::isValidated() const
2240{
2241 return mValidated;
2242}
2243
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002244void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2245{
2246 // Skip over inactive attributes
2247 unsigned int activeAttribute = 0;
2248 unsigned int attribute;
2249 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2250 {
2251 if (mLinkedAttribute[attribute].name.empty())
2252 {
2253 continue;
2254 }
2255
2256 if (activeAttribute == index)
2257 {
2258 break;
2259 }
2260
2261 activeAttribute++;
2262 }
2263
2264 if (bufsize > 0)
2265 {
2266 const char *string = mLinkedAttribute[attribute].name.c_str();
2267
2268 strncpy(name, string, bufsize);
2269 name[bufsize - 1] = '\0';
2270
2271 if (length)
2272 {
2273 *length = strlen(name);
2274 }
2275 }
2276
2277 *size = 1; // Always a single 'type' instance
2278
2279 *type = mLinkedAttribute[attribute].type;
2280}
2281
2282GLint ProgramBinary::getActiveAttributeCount()
2283{
2284 int count = 0;
2285
2286 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2287 {
2288 if (!mLinkedAttribute[attributeIndex].name.empty())
2289 {
2290 count++;
2291 }
2292 }
2293
2294 return count;
2295}
2296
2297GLint ProgramBinary::getActiveAttributeMaxLength()
2298{
2299 int maxLength = 0;
2300
2301 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2302 {
2303 if (!mLinkedAttribute[attributeIndex].name.empty())
2304 {
2305 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2306 }
2307 }
2308
2309 return maxLength;
2310}
2311
2312void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2313{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002314 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002315
2316 if (bufsize > 0)
2317 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002318 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002319
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002320 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002321 {
2322 string += "[0]";
2323 }
2324
2325 strncpy(name, string.c_str(), bufsize);
2326 name[bufsize - 1] = '\0';
2327
2328 if (length)
2329 {
2330 *length = strlen(name);
2331 }
2332 }
2333
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002334 *size = mUniforms[index]->arraySize;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002335
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002336 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002337}
2338
2339GLint ProgramBinary::getActiveUniformCount()
2340{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002341 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002342}
2343
2344GLint ProgramBinary::getActiveUniformMaxLength()
2345{
2346 int maxLength = 0;
2347
2348 unsigned int numUniforms = mUniforms.size();
2349 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2350 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002351 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002352 {
2353 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2354 if (mUniforms[uniformIndex]->isArray())
2355 {
2356 length += 3; // Counting in "[0]".
2357 }
2358 maxLength = std::max(length, maxLength);
2359 }
2360 }
2361
2362 return maxLength;
2363}
2364
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002365void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002366{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002367 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002368 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002369 {
2370 mValidated = false;
2371 }
2372 else
2373 {
2374 mValidated = true;
2375 }
2376}
2377
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002378bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002379{
2380 // if any two active samplers in a program are of different types, but refer to the same
2381 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2382 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2383
2384 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2385 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2386
2387 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2388 {
2389 textureUnitType[i] = TEXTURE_UNKNOWN;
2390 }
2391
2392 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2393 {
2394 if (mSamplersPS[i].active)
2395 {
2396 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2397
2398 if (unit >= maxCombinedTextureImageUnits)
2399 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002400 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002401 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002402 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002403 }
2404
2405 return false;
2406 }
2407
2408 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2409 {
2410 if (mSamplersPS[i].textureType != textureUnitType[unit])
2411 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002412 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002413 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002414 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002415 }
2416
2417 return false;
2418 }
2419 }
2420 else
2421 {
2422 textureUnitType[unit] = mSamplersPS[i].textureType;
2423 }
2424 }
2425 }
2426
2427 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2428 {
2429 if (mSamplersVS[i].active)
2430 {
2431 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2432
2433 if (unit >= maxCombinedTextureImageUnits)
2434 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002435 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002436 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002437 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002438 }
2439
2440 return false;
2441 }
2442
2443 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2444 {
2445 if (mSamplersVS[i].textureType != textureUnitType[unit])
2446 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002447 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002448 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002449 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002450 }
2451
2452 return false;
2453 }
2454 }
2455 else
2456 {
2457 textureUnitType[unit] = mSamplersVS[i].textureType;
2458 }
2459 }
2460 }
2461
2462 return true;
2463}
2464
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002465void ProgramBinary::applyDxDepthRange(float near, float far, float diff)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002466{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002467 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2468 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2469
2470 if (mDxDepthRangeRegisterVS >= 0)
2471 {
2472 float nearFarDiff[4] = {near, far, diff};
2473 device->SetVertexShaderConstantF(mDxDepthRangeRegisterVS, nearFarDiff, 1);
2474 }
2475
2476 if (mDxDepthRangeRegisterPS >= 0)
2477 {
2478 float nearFarDiff[4] = {near, far, diff};
2479 device->SetPixelShaderConstantF(mDxDepthRangeRegisterPS, nearFarDiff, 1);
2480 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002481}
2482
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002483void ProgramBinary::applyDxDepthFront(float range, float start, float frontCCW)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002484{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002485 if (mDxDepthFrontRegister >= 0)
2486 {
2487 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2488 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2489
2490 GLfloat dz[4] = {range, start, frontCCW};
2491 device->SetPixelShaderConstantF(mDxDepthFrontRegister, dz, 1);
2492 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002493}
2494
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002495void ProgramBinary::applyDxCoord(float halfWidth, float halfHeight, float x0, float y0)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002496{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002497 if (mDxCoordRegister >= 0)
2498 {
2499 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2500 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2501
2502 GLfloat whxy[4] = {halfWidth,halfHeight, x0, y0};
2503 device->SetPixelShaderConstantF(mDxCoordRegister, whxy, 1);
2504 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002505}
2506
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002507void ProgramBinary::applyDxHalfPixelSize(float width, float height)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002508{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002509 if (mDxHalfPixelSizeRegister >= 0)
2510 {
2511 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2512 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2513
2514 GLfloat xy[4] = {width, height};
2515 device->SetVertexShaderConstantF(mDxHalfPixelSizeRegister, xy, 1);
2516 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002517}
2518
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002519ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2520{
2521}
2522
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002523}