blob: 42ff8b4034f2551eb6b6f8e62eb6799d7e5c2630 [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
88 mDxDepthRangeLocation = -1;
daniel@transgaming.com12985182012-12-20 20:56:31 +000089 mDxDepthFrontLocation = -1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000090 mDxCoordLocation = -1;
91 mDxHalfPixelSizeLocation = -1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000092}
93
94ProgramBinary::~ProgramBinary()
95{
daniel@transgaming.com95892412012-11-28 20:59:09 +000096 delete mPixelExecutable;
97 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000098
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000099 while (!mUniforms.empty())
100 {
101 delete mUniforms.back();
102 mUniforms.pop_back();
103 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000104}
105
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000106unsigned int ProgramBinary::getSerial() const
107{
108 return mSerial;
109}
110
111unsigned int ProgramBinary::issueSerial()
112{
113 return mCurrentSerial++;
114}
115
daniel@transgaming.com95892412012-11-28 20:59:09 +0000116rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000117{
118 return mPixelExecutable;
119}
120
daniel@transgaming.com95892412012-11-28 20:59:09 +0000121rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000122{
123 return mVertexExecutable;
124}
125
126GLuint ProgramBinary::getAttributeLocation(const char *name)
127{
128 if (name)
129 {
130 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
131 {
132 if (mLinkedAttribute[index].name == std::string(name))
133 {
134 return index;
135 }
136 }
137 }
138
139 return -1;
140}
141
142int ProgramBinary::getSemanticIndex(int attributeIndex)
143{
144 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
145
146 return mSemanticIndex[attributeIndex];
147}
148
149// Returns one more than the highest sampler index used.
150GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
151{
152 switch (type)
153 {
154 case SAMPLER_PIXEL:
155 return mUsedPixelSamplerRange;
156 case SAMPLER_VERTEX:
157 return mUsedVertexSamplerRange;
158 default:
159 UNREACHABLE();
160 return 0;
161 }
162}
163
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000164bool ProgramBinary::usesPointSize() const
165{
166 return mUsesPointSize;
167}
168
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000169// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
170// index (0-15 for the pixel shader and 0-3 for the vertex shader).
171GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
172{
173 GLint logicalTextureUnit = -1;
174
175 switch (type)
176 {
177 case SAMPLER_PIXEL:
178 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
179
180 if (mSamplersPS[samplerIndex].active)
181 {
182 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
183 }
184 break;
185 case SAMPLER_VERTEX:
186 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
187
188 if (mSamplersVS[samplerIndex].active)
189 {
190 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
191 }
192 break;
193 default: UNREACHABLE();
194 }
195
196 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
197 {
198 return logicalTextureUnit;
199 }
200
201 return -1;
202}
203
204// Returns the texture type for a given Direct3D 9 sampler type and
205// index (0-15 for the pixel shader and 0-3 for the vertex shader).
206TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
207{
208 switch (type)
209 {
210 case SAMPLER_PIXEL:
211 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
212 ASSERT(mSamplersPS[samplerIndex].active);
213 return mSamplersPS[samplerIndex].textureType;
214 case SAMPLER_VERTEX:
215 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
216 ASSERT(mSamplersVS[samplerIndex].active);
217 return mSamplersVS[samplerIndex].textureType;
218 default: UNREACHABLE();
219 }
220
221 return TEXTURE_2D;
222}
223
224GLint ProgramBinary::getUniformLocation(std::string name)
225{
226 unsigned int subscript = 0;
227
228 // Strip any trailing array operator and retrieve the subscript
229 size_t open = name.find_last_of('[');
230 size_t close = name.find_last_of(']');
231 if (open != std::string::npos && close == name.length() - 1)
232 {
233 subscript = atoi(name.substr(open + 1).c_str());
234 name.erase(open);
235 }
236
237 unsigned int numUniforms = mUniformIndex.size();
238 for (unsigned int location = 0; location < numUniforms; location++)
239 {
240 if (mUniformIndex[location].name == name &&
241 mUniformIndex[location].element == subscript)
242 {
243 return location;
244 }
245 }
246
247 return -1;
248}
249
250bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
251{
252 if (location < 0 || location >= (int)mUniformIndex.size())
253 {
254 return false;
255 }
256
257 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
258 targetUniform->dirty = true;
259
260 if (targetUniform->type == GL_FLOAT)
261 {
262 int arraySize = targetUniform->arraySize;
263
264 if (arraySize == 1 && count > 1)
265 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
266
267 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
268
269 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
270
271 for (int i = 0; i < count; i++)
272 {
273 target[0] = v[0];
274 target[1] = 0;
275 target[2] = 0;
276 target[3] = 0;
277 target += 4;
278 v += 1;
279 }
280 }
281 else if (targetUniform->type == GL_BOOL)
282 {
283 int arraySize = targetUniform->arraySize;
284
285 if (arraySize == 1 && count > 1)
286 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
287
288 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
289 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
290
291 for (int i = 0; i < count; ++i)
292 {
293 if (v[i] == 0.0f)
294 {
295 boolParams[i] = GL_FALSE;
296 }
297 else
298 {
299 boolParams[i] = GL_TRUE;
300 }
301 }
302 }
303 else
304 {
305 return false;
306 }
307
308 return true;
309}
310
311bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
312{
313 if (location < 0 || location >= (int)mUniformIndex.size())
314 {
315 return false;
316 }
317
318 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
319 targetUniform->dirty = true;
320
321 if (targetUniform->type == GL_FLOAT_VEC2)
322 {
323 int arraySize = targetUniform->arraySize;
324
325 if (arraySize == 1 && count > 1)
326 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
327
328 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
329
330 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
331
332 for (int i = 0; i < count; i++)
333 {
334 target[0] = v[0];
335 target[1] = v[1];
336 target[2] = 0;
337 target[3] = 0;
338 target += 4;
339 v += 2;
340 }
341 }
342 else if (targetUniform->type == GL_BOOL_VEC2)
343 {
344 int arraySize = targetUniform->arraySize;
345
346 if (arraySize == 1 && count > 1)
347 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
348
349 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
350
351 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
352
353 for (int i = 0; i < count * 2; ++i)
354 {
355 if (v[i] == 0.0f)
356 {
357 boolParams[i] = GL_FALSE;
358 }
359 else
360 {
361 boolParams[i] = GL_TRUE;
362 }
363 }
364 }
365 else
366 {
367 return false;
368 }
369
370 return true;
371}
372
373bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
374{
375 if (location < 0 || location >= (int)mUniformIndex.size())
376 {
377 return false;
378 }
379
380 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
381 targetUniform->dirty = true;
382
383 if (targetUniform->type == GL_FLOAT_VEC3)
384 {
385 int arraySize = targetUniform->arraySize;
386
387 if (arraySize == 1 && count > 1)
388 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
389
390 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
391
392 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
393
394 for (int i = 0; i < count; i++)
395 {
396 target[0] = v[0];
397 target[1] = v[1];
398 target[2] = v[2];
399 target[3] = 0;
400 target += 4;
401 v += 3;
402 }
403 }
404 else if (targetUniform->type == GL_BOOL_VEC3)
405 {
406 int arraySize = targetUniform->arraySize;
407
408 if (arraySize == 1 && count > 1)
409 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
410
411 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
412 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
413
414 for (int i = 0; i < count * 3; ++i)
415 {
416 if (v[i] == 0.0f)
417 {
418 boolParams[i] = GL_FALSE;
419 }
420 else
421 {
422 boolParams[i] = GL_TRUE;
423 }
424 }
425 }
426 else
427 {
428 return false;
429 }
430
431 return true;
432}
433
434bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
435{
436 if (location < 0 || location >= (int)mUniformIndex.size())
437 {
438 return false;
439 }
440
441 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
442 targetUniform->dirty = true;
443
444 if (targetUniform->type == GL_FLOAT_VEC4)
445 {
446 int arraySize = targetUniform->arraySize;
447
448 if (arraySize == 1 && count > 1)
449 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
450
451 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
452
453 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
454 v, 4 * sizeof(GLfloat) * count);
455 }
456 else if (targetUniform->type == GL_BOOL_VEC4)
457 {
458 int arraySize = targetUniform->arraySize;
459
460 if (arraySize == 1 && count > 1)
461 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
462
463 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
464 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
465
466 for (int i = 0; i < count * 4; ++i)
467 {
468 if (v[i] == 0.0f)
469 {
470 boolParams[i] = GL_FALSE;
471 }
472 else
473 {
474 boolParams[i] = GL_TRUE;
475 }
476 }
477 }
478 else
479 {
480 return false;
481 }
482
483 return true;
484}
485
486template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
487void transposeMatrix(T *target, const GLfloat *value)
488{
489 int copyWidth = std::min(targetWidth, srcWidth);
490 int copyHeight = std::min(targetHeight, srcHeight);
491
492 for (int x = 0; x < copyWidth; x++)
493 {
494 for (int y = 0; y < copyHeight; y++)
495 {
496 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
497 }
498 }
499 // clear unfilled right side
500 for (int y = 0; y < copyHeight; y++)
501 {
502 for (int x = srcWidth; x < targetWidth; x++)
503 {
504 target[y * targetWidth + x] = (T)0;
505 }
506 }
507 // clear unfilled bottom.
508 for (int y = srcHeight; y < targetHeight; y++)
509 {
510 for (int x = 0; x < targetWidth; x++)
511 {
512 target[y * targetWidth + x] = (T)0;
513 }
514 }
515}
516
517bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
518{
519 if (location < 0 || location >= (int)mUniformIndex.size())
520 {
521 return false;
522 }
523
524 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
525 targetUniform->dirty = true;
526
527 if (targetUniform->type != GL_FLOAT_MAT2)
528 {
529 return false;
530 }
531
532 int arraySize = targetUniform->arraySize;
533
534 if (arraySize == 1 && count > 1)
535 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
536
537 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
538
539 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
540 for (int i = 0; i < count; i++)
541 {
542 transposeMatrix<GLfloat,4,2,2,2>(target, value);
543 target += 8;
544 value += 4;
545 }
546
547 return true;
548}
549
550bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
551{
552 if (location < 0 || location >= (int)mUniformIndex.size())
553 {
554 return false;
555 }
556
557 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
558 targetUniform->dirty = true;
559
560 if (targetUniform->type != GL_FLOAT_MAT3)
561 {
562 return false;
563 }
564
565 int arraySize = targetUniform->arraySize;
566
567 if (arraySize == 1 && count > 1)
568 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
569
570 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
571
572 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
573 for (int i = 0; i < count; i++)
574 {
575 transposeMatrix<GLfloat,4,3,3,3>(target, value);
576 target += 12;
577 value += 9;
578 }
579
580 return true;
581}
582
583
584bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
585{
586 if (location < 0 || location >= (int)mUniformIndex.size())
587 {
588 return false;
589 }
590
591 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
592 targetUniform->dirty = true;
593
594 if (targetUniform->type != GL_FLOAT_MAT4)
595 {
596 return false;
597 }
598
599 int arraySize = targetUniform->arraySize;
600
601 if (arraySize == 1 && count > 1)
602 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
603
604 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
605
606 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
607 for (int i = 0; i < count; i++)
608 {
609 transposeMatrix<GLfloat,4,4,4,4>(target, value);
610 target += 16;
611 value += 16;
612 }
613
614 return true;
615}
616
617bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
618{
619 if (location < 0 || location >= (int)mUniformIndex.size())
620 {
621 return false;
622 }
623
624 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
625 targetUniform->dirty = true;
626
627 if (targetUniform->type == GL_INT ||
628 targetUniform->type == GL_SAMPLER_2D ||
629 targetUniform->type == GL_SAMPLER_CUBE)
630 {
631 int arraySize = targetUniform->arraySize;
632
633 if (arraySize == 1 && count > 1)
634 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
635
636 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
637
638 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
639 v, sizeof(GLint) * count);
640 }
641 else if (targetUniform->type == GL_BOOL)
642 {
643 int arraySize = targetUniform->arraySize;
644
645 if (arraySize == 1 && count > 1)
646 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
647
648 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
649 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
650
651 for (int i = 0; i < count; ++i)
652 {
653 if (v[i] == 0)
654 {
655 boolParams[i] = GL_FALSE;
656 }
657 else
658 {
659 boolParams[i] = GL_TRUE;
660 }
661 }
662 }
663 else
664 {
665 return false;
666 }
667
668 return true;
669}
670
671bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
672{
673 if (location < 0 || location >= (int)mUniformIndex.size())
674 {
675 return false;
676 }
677
678 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
679 targetUniform->dirty = true;
680
681 if (targetUniform->type == GL_INT_VEC2)
682 {
683 int arraySize = targetUniform->arraySize;
684
685 if (arraySize == 1 && count > 1)
686 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
687
688 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
689
690 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
691 v, 2 * sizeof(GLint) * count);
692 }
693 else if (targetUniform->type == GL_BOOL_VEC2)
694 {
695 int arraySize = targetUniform->arraySize;
696
697 if (arraySize == 1 && count > 1)
698 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
699
700 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
701 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
702
703 for (int i = 0; i < count * 2; ++i)
704 {
705 if (v[i] == 0)
706 {
707 boolParams[i] = GL_FALSE;
708 }
709 else
710 {
711 boolParams[i] = GL_TRUE;
712 }
713 }
714 }
715 else
716 {
717 return false;
718 }
719
720 return true;
721}
722
723bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
724{
725 if (location < 0 || location >= (int)mUniformIndex.size())
726 {
727 return false;
728 }
729
730 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
731 targetUniform->dirty = true;
732
733 if (targetUniform->type == GL_INT_VEC3)
734 {
735 int arraySize = targetUniform->arraySize;
736
737 if (arraySize == 1 && count > 1)
738 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
739
740 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
741
742 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
743 v, 3 * sizeof(GLint) * count);
744 }
745 else if (targetUniform->type == GL_BOOL_VEC3)
746 {
747 int arraySize = targetUniform->arraySize;
748
749 if (arraySize == 1 && count > 1)
750 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
751
752 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
753 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
754
755 for (int i = 0; i < count * 3; ++i)
756 {
757 if (v[i] == 0)
758 {
759 boolParams[i] = GL_FALSE;
760 }
761 else
762 {
763 boolParams[i] = GL_TRUE;
764 }
765 }
766 }
767 else
768 {
769 return false;
770 }
771
772 return true;
773}
774
775bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
776{
777 if (location < 0 || location >= (int)mUniformIndex.size())
778 {
779 return false;
780 }
781
782 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
783 targetUniform->dirty = true;
784
785 if (targetUniform->type == GL_INT_VEC4)
786 {
787 int arraySize = targetUniform->arraySize;
788
789 if (arraySize == 1 && count > 1)
790 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
791
792 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
793
794 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
795 v, 4 * sizeof(GLint) * count);
796 }
797 else if (targetUniform->type == GL_BOOL_VEC4)
798 {
799 int arraySize = targetUniform->arraySize;
800
801 if (arraySize == 1 && count > 1)
802 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
803
804 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
805 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
806
807 for (int i = 0; i < count * 4; ++i)
808 {
809 if (v[i] == 0)
810 {
811 boolParams[i] = GL_FALSE;
812 }
813 else
814 {
815 boolParams[i] = GL_TRUE;
816 }
817 }
818 }
819 else
820 {
821 return false;
822 }
823
824 return true;
825}
826
827bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
828{
829 if (location < 0 || location >= (int)mUniformIndex.size())
830 {
831 return false;
832 }
833
834 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
835
836 // sized queries -- ensure the provided buffer is large enough
837 if (bufSize)
838 {
839 int requiredBytes = UniformExternalSize(targetUniform->type);
840 if (*bufSize < requiredBytes)
841 {
842 return false;
843 }
844 }
845
846 switch (targetUniform->type)
847 {
848 case GL_FLOAT_MAT2:
849 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
850 break;
851 case GL_FLOAT_MAT3:
852 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
853 break;
854 case GL_FLOAT_MAT4:
855 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
856 break;
857 default:
858 {
859 unsigned int count = UniformExternalComponentCount(targetUniform->type);
860 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
861
862 switch (UniformComponentType(targetUniform->type))
863 {
864 case GL_BOOL:
865 {
866 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
867
868 for (unsigned int i = 0; i < count; ++i)
869 {
870 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
871 }
872 }
873 break;
874 case GL_FLOAT:
875 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
876 count * sizeof(GLfloat));
877 break;
878 case GL_INT:
879 {
880 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
881
882 for (unsigned int i = 0; i < count; ++i)
883 {
884 params[i] = (float)intParams[i];
885 }
886 }
887 break;
888 default: UNREACHABLE();
889 }
890 }
891 }
892
893 return true;
894}
895
896bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
897{
898 if (location < 0 || location >= (int)mUniformIndex.size())
899 {
900 return false;
901 }
902
903 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
904
905 // sized queries -- ensure the provided buffer is large enough
906 if (bufSize)
907 {
908 int requiredBytes = UniformExternalSize(targetUniform->type);
909 if (*bufSize < requiredBytes)
910 {
911 return false;
912 }
913 }
914
915 switch (targetUniform->type)
916 {
917 case GL_FLOAT_MAT2:
918 {
919 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
920 }
921 break;
922 case GL_FLOAT_MAT3:
923 {
924 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
925 }
926 break;
927 case GL_FLOAT_MAT4:
928 {
929 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
930 }
931 break;
932 default:
933 {
934 unsigned int count = UniformExternalComponentCount(targetUniform->type);
935 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
936
937 switch (UniformComponentType(targetUniform->type))
938 {
939 case GL_BOOL:
940 {
941 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
942
943 for (unsigned int i = 0; i < count; ++i)
944 {
945 params[i] = (GLint)boolParams[i];
946 }
947 }
948 break;
949 case GL_FLOAT:
950 {
951 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
952
953 for (unsigned int i = 0; i < count; ++i)
954 {
955 params[i] = (GLint)floatParams[i];
956 }
957 }
958 break;
959 case GL_INT:
960 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
961 count * sizeof(GLint));
962 break;
963 default: UNREACHABLE();
964 }
965 }
966 }
967
968 return true;
969}
970
971void ProgramBinary::dirtyAllUniforms()
972{
973 unsigned int numUniforms = mUniforms.size();
974 for (unsigned int index = 0; index < numUniforms; index++)
975 {
976 mUniforms[index]->dirty = true;
977 }
978}
979
980// Applies all the uniforms set for this program object to the Direct3D 9 device
981void ProgramBinary::applyUniforms()
982{
daniel@transgaming.com77fbf972012-11-28 21:02:55 +0000983 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) // D3D9_REPLACE
984 {
985 return; // UNIMPLEMENTED
986 }
987
988 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
989
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000990 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) {
991 Uniform *targetUniform = *ub;
992
993 if (targetUniform->dirty)
994 {
995 int arraySize = targetUniform->arraySize;
996 GLfloat *f = (GLfloat*)targetUniform->data;
997 GLint *i = (GLint*)targetUniform->data;
998 GLboolean *b = (GLboolean*)targetUniform->data;
999
1000 switch (targetUniform->type)
1001 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001002 case GL_BOOL: applyUniformnbv(device, targetUniform, arraySize, 1, b); break;
1003 case GL_BOOL_VEC2: applyUniformnbv(device, targetUniform, arraySize, 2, b); break;
1004 case GL_BOOL_VEC3: applyUniformnbv(device, targetUniform, arraySize, 3, b); break;
1005 case GL_BOOL_VEC4: applyUniformnbv(device, targetUniform, arraySize, 4, b); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001006 case GL_FLOAT:
1007 case GL_FLOAT_VEC2:
1008 case GL_FLOAT_VEC3:
1009 case GL_FLOAT_VEC4:
1010 case GL_FLOAT_MAT2:
1011 case GL_FLOAT_MAT3:
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001012 case GL_FLOAT_MAT4: applyUniformnfv(device, targetUniform, f); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001013 case GL_SAMPLER_2D:
1014 case GL_SAMPLER_CUBE:
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001015 case GL_INT: applyUniform1iv(device, targetUniform, arraySize, i); break;
1016 case GL_INT_VEC2: applyUniform2iv(device, targetUniform, arraySize, i); break;
1017 case GL_INT_VEC3: applyUniform3iv(device, targetUniform, arraySize, i); break;
1018 case GL_INT_VEC4: applyUniform4iv(device, targetUniform, arraySize, i); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001019 default:
1020 UNREACHABLE();
1021 }
1022
1023 targetUniform->dirty = false;
1024 }
1025 }
1026}
1027
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001028// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1029// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001030int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001031{
1032 Context *context = getContext();
1033 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1034
1035 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1036 {
1037 int n = VariableRowCount(varying->type) * varying->size;
1038 int m = VariableColumnCount(varying->type);
1039 bool success = false;
1040
1041 if (m == 2 || m == 3 || m == 4)
1042 {
1043 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1044 {
1045 bool available = true;
1046
1047 for (int y = 0; y < n && available; y++)
1048 {
1049 for (int x = 0; x < m && available; x++)
1050 {
1051 if (packing[r + y][x])
1052 {
1053 available = false;
1054 }
1055 }
1056 }
1057
1058 if (available)
1059 {
1060 varying->reg = r;
1061 varying->col = 0;
1062
1063 for (int y = 0; y < n; y++)
1064 {
1065 for (int x = 0; x < m; x++)
1066 {
1067 packing[r + y][x] = &*varying;
1068 }
1069 }
1070
1071 success = true;
1072 }
1073 }
1074
1075 if (!success && m == 2)
1076 {
1077 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1078 {
1079 bool available = true;
1080
1081 for (int y = 0; y < n && available; y++)
1082 {
1083 for (int x = 2; x < 4 && available; x++)
1084 {
1085 if (packing[r + y][x])
1086 {
1087 available = false;
1088 }
1089 }
1090 }
1091
1092 if (available)
1093 {
1094 varying->reg = r;
1095 varying->col = 2;
1096
1097 for (int y = 0; y < n; y++)
1098 {
1099 for (int x = 2; x < 4; x++)
1100 {
1101 packing[r + y][x] = &*varying;
1102 }
1103 }
1104
1105 success = true;
1106 }
1107 }
1108 }
1109 }
1110 else if (m == 1)
1111 {
1112 int space[4] = {0};
1113
1114 for (int y = 0; y < maxVaryingVectors; y++)
1115 {
1116 for (int x = 0; x < 4; x++)
1117 {
1118 space[x] += packing[y][x] ? 0 : 1;
1119 }
1120 }
1121
1122 int column = 0;
1123
1124 for (int x = 0; x < 4; x++)
1125 {
1126 if (space[x] >= n && space[x] < space[column])
1127 {
1128 column = x;
1129 }
1130 }
1131
1132 if (space[column] >= n)
1133 {
1134 for (int r = 0; r < maxVaryingVectors; r++)
1135 {
1136 if (!packing[r][column])
1137 {
1138 varying->reg = r;
1139
1140 for (int y = r; y < r + n; y++)
1141 {
1142 packing[y][column] = &*varying;
1143 }
1144
1145 break;
1146 }
1147 }
1148
1149 varying->col = column;
1150
1151 success = true;
1152 }
1153 }
1154 else UNREACHABLE();
1155
1156 if (!success)
1157 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001158 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001159
1160 return -1;
1161 }
1162 }
1163
1164 // Return the number of used registers
1165 int registers = 0;
1166
1167 for (int r = 0; r < maxVaryingVectors; r++)
1168 {
1169 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1170 {
1171 registers++;
1172 }
1173 }
1174
1175 return registers;
1176}
1177
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001178bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001179{
1180 if (pixelHLSL.empty() || vertexHLSL.empty())
1181 {
1182 return false;
1183 }
1184
1185 // Reset the varying register assignments
1186 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1187 {
1188 fragVar->reg = -1;
1189 fragVar->col = -1;
1190 }
1191
1192 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1193 {
1194 vtxVar->reg = -1;
1195 vtxVar->col = -1;
1196 }
1197
1198 // Map the varyings to the register file
1199 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001200 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001201
1202 if (registers < 0)
1203 {
1204 return false;
1205 }
1206
1207 // Write the HLSL input/output declarations
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001208 const bool sm3 = (mRenderer->getMajorShaderModel() >= 3);
1209 const bool sm4 = (mRenderer->getMajorShaderModel() >= 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001210 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001211 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1212
1213 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1214 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001215 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001216
1217 return false;
1218 }
1219
1220 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1221 {
1222 bool matched = false;
1223
1224 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1225 {
1226 if (output->name == input->name)
1227 {
1228 if (output->type != input->type || output->size != input->size)
1229 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001230 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 +00001231
1232 return false;
1233 }
1234
1235 output->reg = input->reg;
1236 output->col = input->col;
1237
1238 matched = true;
1239 break;
1240 }
1241 }
1242
1243 if (!matched)
1244 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001245 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001246
1247 return false;
1248 }
1249 }
1250
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001251 mUsesPointSize = vertexShader->mUsesPointSize;
1252 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001253 std::string targetSemantic = sm4 ? "SV_Target" : "COLOR";
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001254 std::string positionSemantic = sm4 ? "SV_POSITION" : "POSITION";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001255
1256 vertexHLSL += "struct VS_INPUT\n"
1257 "{\n";
1258
1259 int semanticIndex = 0;
1260 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1261 {
1262 switch (attribute->type)
1263 {
1264 case GL_FLOAT: vertexHLSL += " float "; break;
1265 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1266 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1267 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1268 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1269 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1270 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1271 default: UNREACHABLE();
1272 }
1273
1274 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1275
1276 semanticIndex += VariableRowCount(attribute->type);
1277 }
1278
1279 vertexHLSL += "};\n"
1280 "\n"
1281 "struct VS_OUTPUT\n"
1282 "{\n"
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001283 " float4 gl_Position : " + positionSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001284
1285 for (int r = 0; r < registers; r++)
1286 {
1287 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1288
1289 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1290 }
1291
1292 if (fragmentShader->mUsesFragCoord)
1293 {
1294 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1295 }
1296
1297 if (vertexShader->mUsesPointSize && sm3)
1298 {
1299 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1300 }
1301
1302 vertexHLSL += "};\n"
1303 "\n"
1304 "VS_OUTPUT main(VS_INPUT input)\n"
1305 "{\n";
1306
1307 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1308 {
1309 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1310
1311 if (VariableRowCount(attribute->type) > 1) // Matrix
1312 {
1313 vertexHLSL += "transpose";
1314 }
1315
1316 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1317 }
1318
1319 vertexHLSL += "\n"
1320 " gl_main();\n"
1321 "\n"
1322 " VS_OUTPUT output;\n"
1323 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001324 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001325 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1326 " output.gl_Position.w = gl_Position.w;\n";
1327
1328 if (vertexShader->mUsesPointSize && sm3)
1329 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001330 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001331 }
1332
1333 if (fragmentShader->mUsesFragCoord)
1334 {
1335 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1336 }
1337
1338 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1339 {
1340 if (varying->reg >= 0)
1341 {
1342 for (int i = 0; i < varying->size; i++)
1343 {
1344 int rows = VariableRowCount(varying->type);
1345
1346 for (int j = 0; j < rows; j++)
1347 {
1348 int r = varying->reg + i * rows + j;
1349 vertexHLSL += " output.v" + str(r);
1350
1351 bool sharedRegister = false; // Register used by multiple varyings
1352
1353 for (int x = 0; x < 4; x++)
1354 {
1355 if (packing[r][x] && packing[r][x] != packing[r][0])
1356 {
1357 sharedRegister = true;
1358 break;
1359 }
1360 }
1361
1362 if(sharedRegister)
1363 {
1364 vertexHLSL += ".";
1365
1366 for (int x = 0; x < 4; x++)
1367 {
1368 if (packing[r][x] == &*varying)
1369 {
1370 switch(x)
1371 {
1372 case 0: vertexHLSL += "x"; break;
1373 case 1: vertexHLSL += "y"; break;
1374 case 2: vertexHLSL += "z"; break;
1375 case 3: vertexHLSL += "w"; break;
1376 }
1377 }
1378 }
1379 }
1380
1381 vertexHLSL += " = " + varying->name;
1382
1383 if (varying->array)
1384 {
1385 vertexHLSL += "[" + str(i) + "]";
1386 }
1387
1388 if (rows > 1)
1389 {
1390 vertexHLSL += "[" + str(j) + "]";
1391 }
1392
1393 vertexHLSL += ";\n";
1394 }
1395 }
1396 }
1397 }
1398
1399 vertexHLSL += "\n"
1400 " return output;\n"
1401 "}\n";
1402
1403 pixelHLSL += "struct PS_INPUT\n"
1404 "{\n";
1405
1406 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1407 {
1408 if (varying->reg >= 0)
1409 {
1410 for (int i = 0; i < varying->size; i++)
1411 {
1412 int rows = VariableRowCount(varying->type);
1413 for (int j = 0; j < rows; j++)
1414 {
1415 std::string n = str(varying->reg + i * rows + j);
1416 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1417 }
1418 }
1419 }
1420 else UNREACHABLE();
1421 }
1422
1423 if (fragmentShader->mUsesFragCoord)
1424 {
1425 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1426 if (sm3) {
1427 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1428 }
1429 }
1430
1431 if (fragmentShader->mUsesPointCoord && sm3)
1432 {
1433 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1434 }
1435
1436 if (fragmentShader->mUsesFrontFacing)
1437 {
1438 pixelHLSL += " float vFace : VFACE;\n";
1439 }
1440
1441 pixelHLSL += "};\n"
1442 "\n"
1443 "struct PS_OUTPUT\n"
1444 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001445 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001446 "};\n"
1447 "\n"
1448 "PS_OUTPUT main(PS_INPUT input)\n"
1449 "{\n";
1450
1451 if (fragmentShader->mUsesFragCoord)
1452 {
1453 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1454
1455 if (sm3)
1456 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001457 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001458 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001459 }
1460 else
1461 {
1462 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1463 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001464 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001465 }
1466
daniel@transgaming.com12985182012-12-20 20:56:31 +00001467 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001468 " gl_FragCoord.w = rhw;\n";
1469 }
1470
1471 if (fragmentShader->mUsesPointCoord && sm3)
1472 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001473 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1474 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001475 }
1476
1477 if (fragmentShader->mUsesFrontFacing)
1478 {
daniel@transgaming.com12985182012-12-20 20:56:31 +00001479 pixelHLSL += " gl_FrontFacing = (input.vFace * dx_DepthFront.z >= 0.0);\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001480 }
1481
1482 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1483 {
1484 if (varying->reg >= 0)
1485 {
1486 for (int i = 0; i < varying->size; i++)
1487 {
1488 int rows = VariableRowCount(varying->type);
1489 for (int j = 0; j < rows; j++)
1490 {
1491 std::string n = str(varying->reg + i * rows + j);
1492 pixelHLSL += " " + varying->name;
1493
1494 if (varying->array)
1495 {
1496 pixelHLSL += "[" + str(i) + "]";
1497 }
1498
1499 if (rows > 1)
1500 {
1501 pixelHLSL += "[" + str(j) + "]";
1502 }
1503
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001504 switch (VariableColumnCount(varying->type))
1505 {
1506 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1507 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1508 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1509 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1510 default: UNREACHABLE();
1511 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001512 }
1513 }
1514 }
1515 else UNREACHABLE();
1516 }
1517
1518 pixelHLSL += "\n"
1519 " gl_main();\n"
1520 "\n"
1521 " PS_OUTPUT output;\n"
1522 " output.gl_Color[0] = gl_Color[0];\n"
1523 "\n"
1524 " return output;\n"
1525 "}\n";
1526
1527 return true;
1528}
1529
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001530bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1531{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001532 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001533
1534 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001535 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001536 if (format != GL_PROGRAM_BINARY_ANGLE)
1537 {
1538 infoLog.append("Invalid program binary format.");
1539 return false;
1540 }
1541
1542 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001543 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001544 if (version != BUILD_REVISION)
1545 {
1546 infoLog.append("Invalid program binary version.");
1547 return false;
1548 }
1549
1550 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1551 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001552 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001553 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001554 stream.read(&name);
1555 mLinkedAttribute[i].name = name;
1556 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001557 }
1558
1559 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1560 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001561 stream.read(&mSamplersPS[i].active);
1562 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001563
1564 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001565 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001566 mSamplersPS[i].textureType = (TextureType) textureType;
1567 }
1568
1569 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1570 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001571 stream.read(&mSamplersVS[i].active);
1572 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001573
1574 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001575 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001576 mSamplersVS[i].textureType = (TextureType) textureType;
1577 }
1578
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001579 stream.read(&mUsedVertexSamplerRange);
1580 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001581
1582 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001583 stream.read(&size);
1584 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001585 {
1586 infoLog.append("Invalid program binary.");
1587 return false;
1588 }
1589
1590 mUniforms.resize(size);
1591 for (unsigned int i = 0; i < size; ++i)
1592 {
1593 GLenum type;
1594 std::string _name;
1595 unsigned int arraySize;
1596
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001597 stream.read(&type);
1598 stream.read(&_name);
1599 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001600
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001601 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001602
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001603 stream.read(&mUniforms[i]->ps.float4Index);
1604 stream.read(&mUniforms[i]->ps.samplerIndex);
1605 stream.read(&mUniforms[i]->ps.boolIndex);
1606 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001607
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001608 stream.read(&mUniforms[i]->vs.float4Index);
1609 stream.read(&mUniforms[i]->vs.samplerIndex);
1610 stream.read(&mUniforms[i]->vs.boolIndex);
1611 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001612 }
1613
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001614 stream.read(&size);
1615 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001616 {
1617 infoLog.append("Invalid program binary.");
1618 return false;
1619 }
1620
1621 mUniformIndex.resize(size);
1622 for (unsigned int i = 0; i < size; ++i)
1623 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001624 stream.read(&mUniformIndex[i].name);
1625 stream.read(&mUniformIndex[i].element);
1626 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001627 }
1628
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001629 stream.read(&mDxDepthRangeLocation);
daniel@transgaming.com12985182012-12-20 20:56:31 +00001630 stream.read(&mDxDepthFrontLocation);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001631 stream.read(&mDxCoordLocation);
1632 stream.read(&mDxHalfPixelSizeLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001633
1634 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001635 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001636
1637 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001638 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001639
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001640 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001641
daniel@transgaming.com36038542012-11-28 20:59:26 +00001642 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001643 ptr += sizeof(GUID);
1644
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001645 GUID identifier = mRenderer->getAdapterIdentifier();
1646 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001647 {
1648 infoLog.append("Invalid program binary.");
1649 return false;
1650 }
1651
1652 const char *pixelShaderFunction = ptr;
1653 ptr += pixelShaderSize;
1654
1655 const char *vertexShaderFunction = ptr;
1656 ptr += vertexShaderSize;
1657
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001658 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1659 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1660 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001661 {
1662 infoLog.append("Could not create pixel shader.");
1663 return false;
1664 }
1665
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001666 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1667 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1668 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001669 {
1670 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001671 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001672 mPixelExecutable = NULL;
1673 return false;
1674 }
1675
1676 return true;
1677}
1678
1679bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1680{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001681 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001682
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001683 stream.write(GL_PROGRAM_BINARY_ANGLE);
1684 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001685
1686 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1687 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001688 stream.write(mLinkedAttribute[i].type);
1689 stream.write(mLinkedAttribute[i].name);
1690 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001691 }
1692
1693 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1694 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001695 stream.write(mSamplersPS[i].active);
1696 stream.write(mSamplersPS[i].logicalTextureUnit);
1697 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001698 }
1699
1700 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1701 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001702 stream.write(mSamplersVS[i].active);
1703 stream.write(mSamplersVS[i].logicalTextureUnit);
1704 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001705 }
1706
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001707 stream.write(mUsedVertexSamplerRange);
1708 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001709
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001710 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001711 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1712 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001713 stream.write(mUniforms[i]->type);
1714 stream.write(mUniforms[i]->_name);
1715 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001716
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001717 stream.write(mUniforms[i]->ps.float4Index);
1718 stream.write(mUniforms[i]->ps.samplerIndex);
1719 stream.write(mUniforms[i]->ps.boolIndex);
1720 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001721
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001722 stream.write(mUniforms[i]->vs.float4Index);
1723 stream.write(mUniforms[i]->vs.samplerIndex);
1724 stream.write(mUniforms[i]->vs.boolIndex);
1725 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001726 }
1727
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001728 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001729 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1730 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001731 stream.write(mUniformIndex[i].name);
1732 stream.write(mUniformIndex[i].element);
1733 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001734 }
1735
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001736 stream.write(mDxDepthRangeLocation);
daniel@transgaming.com12985182012-12-20 20:56:31 +00001737 stream.write(mDxDepthFrontLocation);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001738 stream.write(mDxCoordLocation);
1739 stream.write(mDxHalfPixelSizeLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001740
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001741 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001742 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001743
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001744 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001745 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001746
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001747 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001748
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001749 GLsizei streamLength = stream.length();
1750 const void *streamData = stream.data();
1751
1752 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001753 if (totalLength > bufSize)
1754 {
1755 if (length)
1756 {
1757 *length = 0;
1758 }
1759
1760 return false;
1761 }
1762
1763 if (binary)
1764 {
1765 char *ptr = (char*) binary;
1766
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001767 memcpy(ptr, streamData, streamLength);
1768 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001769
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001770 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001771 ptr += sizeof(GUID);
1772
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001773 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001774 ptr += pixelShaderSize;
1775
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001776 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001777 ptr += vertexShaderSize;
1778
1779 ASSERT(ptr - totalLength == binary);
1780 }
1781
1782 if (length)
1783 {
1784 *length = totalLength;
1785 }
1786
1787 return true;
1788}
1789
1790GLint ProgramBinary::getLength()
1791{
1792 GLint length;
1793 if (save(NULL, INT_MAX, &length))
1794 {
1795 return length;
1796 }
1797 else
1798 {
1799 return 0;
1800 }
1801}
1802
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001803bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001804{
1805 if (!fragmentShader || !fragmentShader->isCompiled())
1806 {
1807 return false;
1808 }
1809
1810 if (!vertexShader || !vertexShader->isCompiled())
1811 {
1812 return false;
1813 }
1814
1815 std::string pixelHLSL = fragmentShader->getHLSL();
1816 std::string vertexHLSL = vertexShader->getHLSL();
1817
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001818 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001819 {
1820 return false;
1821 }
1822
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001823 bool success = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001824 rx::D3DConstantTable *constantTableVS = NULL;
1825 rx::D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001826 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1827 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001828
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001829 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001830 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001831 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001832 constantTableVS = mVertexExecutable->getConstantTable();
1833 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001834 }
1835 else
1836 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001837 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001838 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001839
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001840 delete mVertexExecutable;
1841 mVertexExecutable = NULL;
1842 delete mPixelExecutable;
1843 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001844 }
1845
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001846 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1847 {
1848 success = false;
1849 }
1850
1851 if (constantTableVS && constantTablePS)
1852 {
1853 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1854 {
1855 success = false;
1856 }
1857 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001858
1859 // these uniforms are searched as already-decorated because gl_ and dx_
1860 // are reserved prefixes, and do not receive additional decoration
1861 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
daniel@transgaming.com12985182012-12-20 20:56:31 +00001862 mDxDepthFrontLocation = getUniformLocation("dx_DepthFront");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001863 mDxCoordLocation = getUniformLocation("dx_Coord");
1864 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001865
1866 Context *context = getContext();
1867 context->markDxUniformsDirty();
1868
1869 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001870}
1871
1872// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001873bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001874{
1875 unsigned int usedLocations = 0;
1876
1877 // Link attributes that have a binding location
1878 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1879 {
1880 int location = attributeBindings.getAttributeBinding(attribute->name);
1881
1882 if (location != -1) // Set by glBindAttribLocation
1883 {
1884 if (!mLinkedAttribute[location].name.empty())
1885 {
1886 // Multiple active attributes bound to the same location; not an error
1887 }
1888
1889 mLinkedAttribute[location] = *attribute;
1890
1891 int rows = VariableRowCount(attribute->type);
1892
1893 if (rows + location > MAX_VERTEX_ATTRIBS)
1894 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001895 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 +00001896
1897 return false;
1898 }
1899
1900 for (int i = 0; i < rows; i++)
1901 {
1902 usedLocations |= 1 << (location + i);
1903 }
1904 }
1905 }
1906
1907 // Link attributes that don't have a binding location
1908 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1909 {
1910 int location = attributeBindings.getAttributeBinding(attribute->name);
1911
1912 if (location == -1) // Not set by glBindAttribLocation
1913 {
1914 int rows = VariableRowCount(attribute->type);
1915 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1916
1917 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1918 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001919 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001920
1921 return false; // Fail to link
1922 }
1923
1924 mLinkedAttribute[availableIndex] = *attribute;
1925 }
1926 }
1927
1928 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1929 {
1930 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1931 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1932
1933 for (int r = 0; r < rows; r++)
1934 {
1935 mSemanticIndex[attributeIndex++] = index++;
1936 }
1937 }
1938
1939 return true;
1940}
1941
daniel@transgaming.com31240482012-11-28 21:06:41 +00001942bool ProgramBinary::linkUniforms(InfoLog &infoLog, rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001943{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001944 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001945 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001946 const rx::D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001947
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001948 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001949 {
1950 return false;
1951 }
1952 }
1953
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001954 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1955 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001956 const rx::D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001957
1958 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1959 {
1960 return false;
1961 }
1962 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001963 return true;
1964}
1965
1966// Adds the description of a constant found in the binary shader to the list of uniforms
1967// Returns true if succesful (uniform not already defined)
daniel@transgaming.com31240482012-11-28 21:06:41 +00001968bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const rx::D3DConstant *constant, const std::string &name,
1969 rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001970{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001971 if (constant->registerSet == rx::D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001972 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001973 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001974 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001975 const rx::D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1976 const rx::D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001977
1978 if (psConstant)
1979 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001980 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001981
1982 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1983 {
1984 mSamplersPS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001985 mSamplersPS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001986 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1987 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1988 }
1989 else
1990 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001991 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001992 return false;
1993 }
1994 }
1995
1996 if (vsConstant)
1997 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001998 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001999
2000 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2001 {
2002 mSamplersVS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002003 mSamplersVS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002004 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2005 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2006 }
2007 else
2008 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002009 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002010 return false;
2011 }
2012 }
2013 }
2014 }
2015
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002016 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002017 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002018 case rx::D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002019 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002020 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002021 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002022 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002023 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002024 const rx::D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002025
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002026 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002027
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002028 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002029 {
2030 return false;
2031 }
2032 }
2033 }
2034
2035 return true;
2036 }
daniel@transgaming.com31240482012-11-28 21:06:41 +00002037 case rx::D3DConstant::CLASS_SCALAR:
2038 case rx::D3DConstant::CLASS_VECTOR:
2039 case rx::D3DConstant::CLASS_MATRIX_COLUMNS:
2040 case rx::D3DConstant::CLASS_OBJECT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002041 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002042 default:
2043 UNREACHABLE();
2044 return false;
2045 }
2046}
2047
daniel@transgaming.com31240482012-11-28 21:06:41 +00002048bool ProgramBinary::defineUniform(GLenum shader, const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002049{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002050 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002051
2052 if(!uniform)
2053 {
2054 return false;
2055 }
2056
2057 // Check if already defined
2058 GLint location = getUniformLocation(uniform->name);
2059 GLenum type = uniform->type;
2060
2061 if (location >= 0)
2062 {
2063 delete uniform;
2064 uniform = mUniforms[mUniformIndex[location].index];
2065 }
2066
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002067 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2068 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002069
2070 if (location >= 0)
2071 {
2072 return uniform->type == type;
2073 }
2074
2075 mUniforms.push_back(uniform);
2076 unsigned int uniformIndex = mUniforms.size() - 1;
2077
2078 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2079 {
2080 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2081 }
2082
2083 return true;
2084}
2085
daniel@transgaming.com31240482012-11-28 21:06:41 +00002086Uniform *ProgramBinary::createUniform(const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002087{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002088 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002089 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002090 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002091 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002092 case rx::D3DConstant::PT_SAMPLER2D:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002093 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002094 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002095 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002096 default: UNREACHABLE();
2097 }
2098 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002099 case rx::D3DConstant::PT_SAMPLERCUBE:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002100 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002101 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002102 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002103 default: UNREACHABLE();
2104 }
2105 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002106 case rx::D3DConstant::PT_BOOL:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002107 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002108 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002109 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2110 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2111 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2112 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002113 default: UNREACHABLE();
2114 }
2115 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002116 case rx::D3DConstant::PT_INT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002117 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002118 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002119 case 1: return new Uniform(GL_INT, _name, constant->elements);
2120 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2121 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2122 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002123 default: UNREACHABLE();
2124 }
2125 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002126 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002127 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002128 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002129 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2130 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2131 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2132 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002133 default: UNREACHABLE();
2134 }
2135 break;
2136 default:
2137 UNREACHABLE();
2138 }
2139 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002140 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002141 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002142 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002143 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002144 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002145 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002146 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002147 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2148 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2149 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002150 default: UNREACHABLE();
2151 }
2152 break;
2153 default: UNREACHABLE();
2154 }
2155 }
2156 else UNREACHABLE();
2157
2158 return 0;
2159}
2160
2161// This method needs to match OutputHLSL::decorate
2162std::string ProgramBinary::decorateAttribute(const std::string &name)
2163{
2164 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2165 {
2166 return "_" + name;
2167 }
2168
2169 return name;
2170}
2171
2172std::string ProgramBinary::undecorateUniform(const std::string &_name)
2173{
2174 std::string name = _name;
2175
2176 // Remove any structure field decoration
2177 size_t pos = 0;
2178 while ((pos = name.find("._", pos)) != std::string::npos)
2179 {
2180 name.replace(pos, 2, ".");
2181 }
2182
2183 // Remove the leading decoration
2184 if (name[0] == '_')
2185 {
2186 return name.substr(1);
2187 }
2188 else if (name.compare(0, 3, "ar_") == 0)
2189 {
2190 return name.substr(3);
2191 }
2192
2193 return name;
2194}
2195
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002196// D3D9_REPLACE begin
2197void ProgramBinary::applyUniformnbv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002198{
2199 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2200 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2201
2202 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2203 {
2204 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2205 for (int i = 0; i < count; i++)
2206 {
2207 for (int j = 0; j < 4; j++)
2208 {
2209 if (j < width)
2210 {
2211 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2212 }
2213 else
2214 {
2215 vector[i * 4 + j] = 0.0f;
2216 }
2217 }
2218 }
2219 }
2220
2221 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2222 {
2223 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2224 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2225 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2226 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2227 for (int i = 0; i < copyCount; i++)
2228 {
2229 boolVector[i] = v[i] != GL_FALSE;
2230 }
2231 }
2232
2233 if (targetUniform->ps.float4Index >= 0)
2234 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002235 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002236 }
2237
2238 if (targetUniform->ps.boolIndex >= 0)
2239 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002240 device->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002241 }
2242
2243 if (targetUniform->vs.float4Index >= 0)
2244 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002245 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002246 }
2247
2248 if (targetUniform->vs.boolIndex >= 0)
2249 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002250 device->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002251 }
2252}
2253
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002254bool ProgramBinary::applyUniformnfv(IDirect3DDevice9 *device, Uniform *targetUniform, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002255{
2256 if (targetUniform->ps.registerCount)
2257 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002258 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002259 }
2260
2261 if (targetUniform->vs.registerCount)
2262 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002263 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002264 }
2265
2266 return true;
2267}
2268
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002269bool ProgramBinary::applyUniform1iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002270{
2271 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002272 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002273
2274 for (int i = 0; i < count; i++)
2275 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002276 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002277 }
2278
2279 if (targetUniform->ps.registerCount)
2280 {
2281 if (targetUniform->ps.samplerIndex >= 0)
2282 {
2283 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2284
2285 for (int i = 0; i < count; i++)
2286 {
2287 unsigned int samplerIndex = firstIndex + i;
2288
2289 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2290 {
2291 ASSERT(mSamplersPS[samplerIndex].active);
2292 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2293 }
2294 }
2295 }
2296 else
2297 {
2298 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002299 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002300 }
2301 }
2302
2303 if (targetUniform->vs.registerCount)
2304 {
2305 if (targetUniform->vs.samplerIndex >= 0)
2306 {
2307 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2308
2309 for (int i = 0; i < count; i++)
2310 {
2311 unsigned int samplerIndex = firstIndex + i;
2312
2313 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2314 {
2315 ASSERT(mSamplersVS[samplerIndex].active);
2316 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2317 }
2318 }
2319 }
2320 else
2321 {
2322 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002323 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002324 }
2325 }
2326
2327 return true;
2328}
2329
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002330bool ProgramBinary::applyUniform2iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002331{
2332 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002333 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002334
2335 for (int i = 0; i < count; i++)
2336 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002337 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002338
2339 v += 2;
2340 }
2341
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002342 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002343
2344 return true;
2345}
2346
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002347bool ProgramBinary::applyUniform3iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002348{
2349 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002350 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002351
2352 for (int i = 0; i < count; i++)
2353 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002354 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002355
2356 v += 3;
2357 }
2358
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002359 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002360
2361 return true;
2362}
2363
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002364bool ProgramBinary::applyUniform4iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002365{
2366 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002367 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002368
2369 for (int i = 0; i < count; i++)
2370 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002371 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002372
2373 v += 4;
2374 }
2375
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002376 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002377
2378 return true;
2379}
2380
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002381void ProgramBinary::applyUniformniv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002382{
2383 if (targetUniform->ps.registerCount)
2384 {
2385 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002386 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002387 }
2388
2389 if (targetUniform->vs.registerCount)
2390 {
2391 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002392 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002393 }
2394}
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002395// D3D9_REPLACE end
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002396
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002397bool ProgramBinary::isValidated() const
2398{
2399 return mValidated;
2400}
2401
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002402void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2403{
2404 // Skip over inactive attributes
2405 unsigned int activeAttribute = 0;
2406 unsigned int attribute;
2407 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2408 {
2409 if (mLinkedAttribute[attribute].name.empty())
2410 {
2411 continue;
2412 }
2413
2414 if (activeAttribute == index)
2415 {
2416 break;
2417 }
2418
2419 activeAttribute++;
2420 }
2421
2422 if (bufsize > 0)
2423 {
2424 const char *string = mLinkedAttribute[attribute].name.c_str();
2425
2426 strncpy(name, string, bufsize);
2427 name[bufsize - 1] = '\0';
2428
2429 if (length)
2430 {
2431 *length = strlen(name);
2432 }
2433 }
2434
2435 *size = 1; // Always a single 'type' instance
2436
2437 *type = mLinkedAttribute[attribute].type;
2438}
2439
2440GLint ProgramBinary::getActiveAttributeCount()
2441{
2442 int count = 0;
2443
2444 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2445 {
2446 if (!mLinkedAttribute[attributeIndex].name.empty())
2447 {
2448 count++;
2449 }
2450 }
2451
2452 return count;
2453}
2454
2455GLint ProgramBinary::getActiveAttributeMaxLength()
2456{
2457 int maxLength = 0;
2458
2459 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2460 {
2461 if (!mLinkedAttribute[attributeIndex].name.empty())
2462 {
2463 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2464 }
2465 }
2466
2467 return maxLength;
2468}
2469
2470void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2471{
2472 // Skip over internal uniforms
2473 unsigned int activeUniform = 0;
2474 unsigned int uniform;
2475 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2476 {
2477 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2478 {
2479 continue;
2480 }
2481
2482 if (activeUniform == index)
2483 {
2484 break;
2485 }
2486
2487 activeUniform++;
2488 }
2489
2490 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2491
2492 if (bufsize > 0)
2493 {
2494 std::string string = mUniforms[uniform]->name;
2495
2496 if (mUniforms[uniform]->isArray())
2497 {
2498 string += "[0]";
2499 }
2500
2501 strncpy(name, string.c_str(), bufsize);
2502 name[bufsize - 1] = '\0';
2503
2504 if (length)
2505 {
2506 *length = strlen(name);
2507 }
2508 }
2509
2510 *size = mUniforms[uniform]->arraySize;
2511
2512 *type = mUniforms[uniform]->type;
2513}
2514
2515GLint ProgramBinary::getActiveUniformCount()
2516{
2517 int count = 0;
2518
2519 unsigned int numUniforms = mUniforms.size();
2520 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2521 {
2522 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2523 {
2524 count++;
2525 }
2526 }
2527
2528 return count;
2529}
2530
2531GLint ProgramBinary::getActiveUniformMaxLength()
2532{
2533 int maxLength = 0;
2534
2535 unsigned int numUniforms = mUniforms.size();
2536 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2537 {
2538 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2539 {
2540 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2541 if (mUniforms[uniformIndex]->isArray())
2542 {
2543 length += 3; // Counting in "[0]".
2544 }
2545 maxLength = std::max(length, maxLength);
2546 }
2547 }
2548
2549 return maxLength;
2550}
2551
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002552void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002553{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002554 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002555 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002556 {
2557 mValidated = false;
2558 }
2559 else
2560 {
2561 mValidated = true;
2562 }
2563}
2564
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002565bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002566{
2567 // if any two active samplers in a program are of different types, but refer to the same
2568 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2569 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2570
2571 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2572 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2573
2574 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2575 {
2576 textureUnitType[i] = TEXTURE_UNKNOWN;
2577 }
2578
2579 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2580 {
2581 if (mSamplersPS[i].active)
2582 {
2583 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2584
2585 if (unit >= maxCombinedTextureImageUnits)
2586 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002587 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002588 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002589 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002590 }
2591
2592 return false;
2593 }
2594
2595 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2596 {
2597 if (mSamplersPS[i].textureType != textureUnitType[unit])
2598 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002599 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002600 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002601 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002602 }
2603
2604 return false;
2605 }
2606 }
2607 else
2608 {
2609 textureUnitType[unit] = mSamplersPS[i].textureType;
2610 }
2611 }
2612 }
2613
2614 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2615 {
2616 if (mSamplersVS[i].active)
2617 {
2618 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2619
2620 if (unit >= maxCombinedTextureImageUnits)
2621 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002622 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002623 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002624 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002625 }
2626
2627 return false;
2628 }
2629
2630 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2631 {
2632 if (mSamplersVS[i].textureType != textureUnitType[unit])
2633 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002634 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002635 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002636 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002637 }
2638
2639 return false;
2640 }
2641 }
2642 else
2643 {
2644 textureUnitType[unit] = mSamplersVS[i].textureType;
2645 }
2646 }
2647 }
2648
2649 return true;
2650}
2651
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002652void ProgramBinary::applyDxDepthRange(float near, float far, float diff)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002653{
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002654 GLfloat nearFarDiff[3] = {near, far, diff};
2655 setUniform3fv(mDxDepthRangeLocation, 1, nearFarDiff);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002656}
2657
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002658void ProgramBinary::applyDxDepthFront(float range, float start, float frontCCW)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002659{
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002660 GLfloat dz[3] = {range, start, frontCCW};
2661 setUniform3fv(mDxDepthFrontLocation, 1, dz);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002662}
2663
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002664void ProgramBinary::applyDxCoord(float halfWidth, float halfHeight, float x0, float y0)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002665{
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002666 GLfloat whxy[4] = {halfWidth,halfHeight, x0, y0};
2667 setUniform4fv(mDxCoordLocation, 1, whxy);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002668}
2669
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002670void ProgramBinary::applyDxHalfPixelSize(float width, float height)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002671{
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002672 GLfloat xy[2] = {width, height};
2673 setUniform2fv(mDxHalfPixelSizeLocation, 1, xy);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002674}
2675
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002676ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2677{
2678}
2679
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002680}