blob: 061997b6e472d5b6a922f4d1103743436e290466 [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
22#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
23#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
24#endif
25
26namespace gl
27{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000028std::string str(int i)
29{
30 char buffer[20];
31 snprintf(buffer, sizeof(buffer), "%d", i);
32 return buffer;
33}
34
35Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
36 : type(type), _name(_name), name(ProgramBinary::undecorateUniform(_name)), arraySize(arraySize)
37{
38 int bytes = UniformInternalSize(type) * arraySize;
39 data = new unsigned char[bytes];
40 memset(data, 0, bytes);
41 dirty = true;
42}
43
44Uniform::~Uniform()
45{
46 delete[] data;
47}
48
49bool Uniform::isArray()
50{
daniel@transgaming.com22ba0f72012-09-27 17:46:04 +000051 size_t dot = _name.find_last_of('.');
52 if (dot == std::string::npos) dot = -1;
53
54 return _name.compare(dot + 1, dot + 4, "ar_") == 0;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000055}
56
57UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
58 : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index)
59{
60}
61
daniel@transgaming.come87ca002012-07-24 18:30:43 +000062unsigned int ProgramBinary::mCurrentSerial = 1;
63
daniel@transgaming.com70062c92012-11-28 19:32:30 +000064ProgramBinary::ProgramBinary(rx::Renderer *renderer) : RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000065{
daniel@transgaming.com70062c92012-11-28 19:32:30 +000066 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL); // D3D9_REPLACE
67 mRenderer = static_cast<rx::Renderer9*>(renderer);
daniel@transgaming.come4733d72012-10-31 18:07:01 +000068 mDevice = mRenderer->getDevice(); // D3D9_REPLACE
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000069
70 mPixelExecutable = NULL;
71 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000072
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000073 mValidated = false;
74
75 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
76 {
77 mSemanticIndex[index] = -1;
78 }
79
80 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
81 {
82 mSamplersPS[index].active = false;
83 }
84
85 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
86 {
87 mSamplersVS[index].active = false;
88 }
89
90 mUsedVertexSamplerRange = 0;
91 mUsedPixelSamplerRange = 0;
92
93 mDxDepthRangeLocation = -1;
94 mDxDepthLocation = -1;
95 mDxCoordLocation = -1;
96 mDxHalfPixelSizeLocation = -1;
97 mDxFrontCCWLocation = -1;
98 mDxPointsOrLinesLocation = -1;
99}
100
101ProgramBinary::~ProgramBinary()
102{
103 if (mPixelExecutable)
104 {
105 mPixelExecutable->Release();
106 }
107
108 if (mVertexExecutable)
109 {
110 mVertexExecutable->Release();
111 }
112
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000113 while (!mUniforms.empty())
114 {
115 delete mUniforms.back();
116 mUniforms.pop_back();
117 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000118}
119
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000120unsigned int ProgramBinary::getSerial() const
121{
122 return mSerial;
123}
124
125unsigned int ProgramBinary::issueSerial()
126{
127 return mCurrentSerial++;
128}
129
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000130IDirect3DPixelShader9 *ProgramBinary::getPixelShader()
131{
132 return mPixelExecutable;
133}
134
135IDirect3DVertexShader9 *ProgramBinary::getVertexShader()
136{
137 return mVertexExecutable;
138}
139
140GLuint ProgramBinary::getAttributeLocation(const char *name)
141{
142 if (name)
143 {
144 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
145 {
146 if (mLinkedAttribute[index].name == std::string(name))
147 {
148 return index;
149 }
150 }
151 }
152
153 return -1;
154}
155
156int ProgramBinary::getSemanticIndex(int attributeIndex)
157{
158 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
159
160 return mSemanticIndex[attributeIndex];
161}
162
163// Returns one more than the highest sampler index used.
164GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
165{
166 switch (type)
167 {
168 case SAMPLER_PIXEL:
169 return mUsedPixelSamplerRange;
170 case SAMPLER_VERTEX:
171 return mUsedVertexSamplerRange;
172 default:
173 UNREACHABLE();
174 return 0;
175 }
176}
177
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000178bool ProgramBinary::usesPointSize() const
179{
180 return mUsesPointSize;
181}
182
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000183// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
184// index (0-15 for the pixel shader and 0-3 for the vertex shader).
185GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
186{
187 GLint logicalTextureUnit = -1;
188
189 switch (type)
190 {
191 case SAMPLER_PIXEL:
192 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
193
194 if (mSamplersPS[samplerIndex].active)
195 {
196 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
197 }
198 break;
199 case SAMPLER_VERTEX:
200 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
201
202 if (mSamplersVS[samplerIndex].active)
203 {
204 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
205 }
206 break;
207 default: UNREACHABLE();
208 }
209
210 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
211 {
212 return logicalTextureUnit;
213 }
214
215 return -1;
216}
217
218// Returns the texture type for a given Direct3D 9 sampler type and
219// index (0-15 for the pixel shader and 0-3 for the vertex shader).
220TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
221{
222 switch (type)
223 {
224 case SAMPLER_PIXEL:
225 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
226 ASSERT(mSamplersPS[samplerIndex].active);
227 return mSamplersPS[samplerIndex].textureType;
228 case SAMPLER_VERTEX:
229 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
230 ASSERT(mSamplersVS[samplerIndex].active);
231 return mSamplersVS[samplerIndex].textureType;
232 default: UNREACHABLE();
233 }
234
235 return TEXTURE_2D;
236}
237
238GLint ProgramBinary::getUniformLocation(std::string name)
239{
240 unsigned int subscript = 0;
241
242 // Strip any trailing array operator and retrieve the subscript
243 size_t open = name.find_last_of('[');
244 size_t close = name.find_last_of(']');
245 if (open != std::string::npos && close == name.length() - 1)
246 {
247 subscript = atoi(name.substr(open + 1).c_str());
248 name.erase(open);
249 }
250
251 unsigned int numUniforms = mUniformIndex.size();
252 for (unsigned int location = 0; location < numUniforms; location++)
253 {
254 if (mUniformIndex[location].name == name &&
255 mUniformIndex[location].element == subscript)
256 {
257 return location;
258 }
259 }
260
261 return -1;
262}
263
264bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
265{
266 if (location < 0 || location >= (int)mUniformIndex.size())
267 {
268 return false;
269 }
270
271 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
272 targetUniform->dirty = true;
273
274 if (targetUniform->type == GL_FLOAT)
275 {
276 int arraySize = targetUniform->arraySize;
277
278 if (arraySize == 1 && count > 1)
279 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
280
281 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
282
283 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
284
285 for (int i = 0; i < count; i++)
286 {
287 target[0] = v[0];
288 target[1] = 0;
289 target[2] = 0;
290 target[3] = 0;
291 target += 4;
292 v += 1;
293 }
294 }
295 else if (targetUniform->type == GL_BOOL)
296 {
297 int arraySize = targetUniform->arraySize;
298
299 if (arraySize == 1 && count > 1)
300 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
301
302 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
303 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
304
305 for (int i = 0; i < count; ++i)
306 {
307 if (v[i] == 0.0f)
308 {
309 boolParams[i] = GL_FALSE;
310 }
311 else
312 {
313 boolParams[i] = GL_TRUE;
314 }
315 }
316 }
317 else
318 {
319 return false;
320 }
321
322 return true;
323}
324
325bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
326{
327 if (location < 0 || location >= (int)mUniformIndex.size())
328 {
329 return false;
330 }
331
332 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
333 targetUniform->dirty = true;
334
335 if (targetUniform->type == GL_FLOAT_VEC2)
336 {
337 int arraySize = targetUniform->arraySize;
338
339 if (arraySize == 1 && count > 1)
340 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
341
342 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
343
344 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
345
346 for (int i = 0; i < count; i++)
347 {
348 target[0] = v[0];
349 target[1] = v[1];
350 target[2] = 0;
351 target[3] = 0;
352 target += 4;
353 v += 2;
354 }
355 }
356 else if (targetUniform->type == GL_BOOL_VEC2)
357 {
358 int arraySize = targetUniform->arraySize;
359
360 if (arraySize == 1 && count > 1)
361 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
362
363 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
364
365 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
366
367 for (int i = 0; i < count * 2; ++i)
368 {
369 if (v[i] == 0.0f)
370 {
371 boolParams[i] = GL_FALSE;
372 }
373 else
374 {
375 boolParams[i] = GL_TRUE;
376 }
377 }
378 }
379 else
380 {
381 return false;
382 }
383
384 return true;
385}
386
387bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
388{
389 if (location < 0 || location >= (int)mUniformIndex.size())
390 {
391 return false;
392 }
393
394 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
395 targetUniform->dirty = true;
396
397 if (targetUniform->type == GL_FLOAT_VEC3)
398 {
399 int arraySize = targetUniform->arraySize;
400
401 if (arraySize == 1 && count > 1)
402 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
403
404 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
405
406 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
407
408 for (int i = 0; i < count; i++)
409 {
410 target[0] = v[0];
411 target[1] = v[1];
412 target[2] = v[2];
413 target[3] = 0;
414 target += 4;
415 v += 3;
416 }
417 }
418 else if (targetUniform->type == GL_BOOL_VEC3)
419 {
420 int arraySize = targetUniform->arraySize;
421
422 if (arraySize == 1 && count > 1)
423 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
424
425 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
426 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
427
428 for (int i = 0; i < count * 3; ++i)
429 {
430 if (v[i] == 0.0f)
431 {
432 boolParams[i] = GL_FALSE;
433 }
434 else
435 {
436 boolParams[i] = GL_TRUE;
437 }
438 }
439 }
440 else
441 {
442 return false;
443 }
444
445 return true;
446}
447
448bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
449{
450 if (location < 0 || location >= (int)mUniformIndex.size())
451 {
452 return false;
453 }
454
455 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
456 targetUniform->dirty = true;
457
458 if (targetUniform->type == GL_FLOAT_VEC4)
459 {
460 int arraySize = targetUniform->arraySize;
461
462 if (arraySize == 1 && count > 1)
463 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
464
465 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
466
467 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
468 v, 4 * sizeof(GLfloat) * count);
469 }
470 else if (targetUniform->type == GL_BOOL_VEC4)
471 {
472 int arraySize = targetUniform->arraySize;
473
474 if (arraySize == 1 && count > 1)
475 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
476
477 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
478 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
479
480 for (int i = 0; i < count * 4; ++i)
481 {
482 if (v[i] == 0.0f)
483 {
484 boolParams[i] = GL_FALSE;
485 }
486 else
487 {
488 boolParams[i] = GL_TRUE;
489 }
490 }
491 }
492 else
493 {
494 return false;
495 }
496
497 return true;
498}
499
500template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
501void transposeMatrix(T *target, const GLfloat *value)
502{
503 int copyWidth = std::min(targetWidth, srcWidth);
504 int copyHeight = std::min(targetHeight, srcHeight);
505
506 for (int x = 0; x < copyWidth; x++)
507 {
508 for (int y = 0; y < copyHeight; y++)
509 {
510 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
511 }
512 }
513 // clear unfilled right side
514 for (int y = 0; y < copyHeight; y++)
515 {
516 for (int x = srcWidth; x < targetWidth; x++)
517 {
518 target[y * targetWidth + x] = (T)0;
519 }
520 }
521 // clear unfilled bottom.
522 for (int y = srcHeight; y < targetHeight; y++)
523 {
524 for (int x = 0; x < targetWidth; x++)
525 {
526 target[y * targetWidth + x] = (T)0;
527 }
528 }
529}
530
531bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
532{
533 if (location < 0 || location >= (int)mUniformIndex.size())
534 {
535 return false;
536 }
537
538 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
539 targetUniform->dirty = true;
540
541 if (targetUniform->type != GL_FLOAT_MAT2)
542 {
543 return false;
544 }
545
546 int arraySize = targetUniform->arraySize;
547
548 if (arraySize == 1 && count > 1)
549 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
550
551 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
552
553 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
554 for (int i = 0; i < count; i++)
555 {
556 transposeMatrix<GLfloat,4,2,2,2>(target, value);
557 target += 8;
558 value += 4;
559 }
560
561 return true;
562}
563
564bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
565{
566 if (location < 0 || location >= (int)mUniformIndex.size())
567 {
568 return false;
569 }
570
571 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
572 targetUniform->dirty = true;
573
574 if (targetUniform->type != GL_FLOAT_MAT3)
575 {
576 return false;
577 }
578
579 int arraySize = targetUniform->arraySize;
580
581 if (arraySize == 1 && count > 1)
582 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
583
584 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
585
586 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
587 for (int i = 0; i < count; i++)
588 {
589 transposeMatrix<GLfloat,4,3,3,3>(target, value);
590 target += 12;
591 value += 9;
592 }
593
594 return true;
595}
596
597
598bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
599{
600 if (location < 0 || location >= (int)mUniformIndex.size())
601 {
602 return false;
603 }
604
605 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
606 targetUniform->dirty = true;
607
608 if (targetUniform->type != GL_FLOAT_MAT4)
609 {
610 return false;
611 }
612
613 int arraySize = targetUniform->arraySize;
614
615 if (arraySize == 1 && count > 1)
616 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
617
618 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
619
620 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
621 for (int i = 0; i < count; i++)
622 {
623 transposeMatrix<GLfloat,4,4,4,4>(target, value);
624 target += 16;
625 value += 16;
626 }
627
628 return true;
629}
630
631bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
632{
633 if (location < 0 || location >= (int)mUniformIndex.size())
634 {
635 return false;
636 }
637
638 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
639 targetUniform->dirty = true;
640
641 if (targetUniform->type == GL_INT ||
642 targetUniform->type == GL_SAMPLER_2D ||
643 targetUniform->type == GL_SAMPLER_CUBE)
644 {
645 int arraySize = targetUniform->arraySize;
646
647 if (arraySize == 1 && count > 1)
648 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
649
650 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
651
652 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
653 v, sizeof(GLint) * count);
654 }
655 else if (targetUniform->type == GL_BOOL)
656 {
657 int arraySize = targetUniform->arraySize;
658
659 if (arraySize == 1 && count > 1)
660 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
661
662 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
663 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
664
665 for (int i = 0; i < count; ++i)
666 {
667 if (v[i] == 0)
668 {
669 boolParams[i] = GL_FALSE;
670 }
671 else
672 {
673 boolParams[i] = GL_TRUE;
674 }
675 }
676 }
677 else
678 {
679 return false;
680 }
681
682 return true;
683}
684
685bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
686{
687 if (location < 0 || location >= (int)mUniformIndex.size())
688 {
689 return false;
690 }
691
692 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
693 targetUniform->dirty = true;
694
695 if (targetUniform->type == GL_INT_VEC2)
696 {
697 int arraySize = targetUniform->arraySize;
698
699 if (arraySize == 1 && count > 1)
700 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
701
702 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
703
704 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
705 v, 2 * sizeof(GLint) * count);
706 }
707 else if (targetUniform->type == GL_BOOL_VEC2)
708 {
709 int arraySize = targetUniform->arraySize;
710
711 if (arraySize == 1 && count > 1)
712 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
713
714 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
715 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
716
717 for (int i = 0; i < count * 2; ++i)
718 {
719 if (v[i] == 0)
720 {
721 boolParams[i] = GL_FALSE;
722 }
723 else
724 {
725 boolParams[i] = GL_TRUE;
726 }
727 }
728 }
729 else
730 {
731 return false;
732 }
733
734 return true;
735}
736
737bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
738{
739 if (location < 0 || location >= (int)mUniformIndex.size())
740 {
741 return false;
742 }
743
744 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
745 targetUniform->dirty = true;
746
747 if (targetUniform->type == GL_INT_VEC3)
748 {
749 int arraySize = targetUniform->arraySize;
750
751 if (arraySize == 1 && count > 1)
752 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
753
754 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
755
756 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
757 v, 3 * sizeof(GLint) * count);
758 }
759 else if (targetUniform->type == GL_BOOL_VEC3)
760 {
761 int arraySize = targetUniform->arraySize;
762
763 if (arraySize == 1 && count > 1)
764 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
765
766 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
767 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
768
769 for (int i = 0; i < count * 3; ++i)
770 {
771 if (v[i] == 0)
772 {
773 boolParams[i] = GL_FALSE;
774 }
775 else
776 {
777 boolParams[i] = GL_TRUE;
778 }
779 }
780 }
781 else
782 {
783 return false;
784 }
785
786 return true;
787}
788
789bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
790{
791 if (location < 0 || location >= (int)mUniformIndex.size())
792 {
793 return false;
794 }
795
796 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
797 targetUniform->dirty = true;
798
799 if (targetUniform->type == GL_INT_VEC4)
800 {
801 int arraySize = targetUniform->arraySize;
802
803 if (arraySize == 1 && count > 1)
804 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
805
806 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
807
808 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
809 v, 4 * sizeof(GLint) * count);
810 }
811 else if (targetUniform->type == GL_BOOL_VEC4)
812 {
813 int arraySize = targetUniform->arraySize;
814
815 if (arraySize == 1 && count > 1)
816 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
817
818 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
819 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
820
821 for (int i = 0; i < count * 4; ++i)
822 {
823 if (v[i] == 0)
824 {
825 boolParams[i] = GL_FALSE;
826 }
827 else
828 {
829 boolParams[i] = GL_TRUE;
830 }
831 }
832 }
833 else
834 {
835 return false;
836 }
837
838 return true;
839}
840
841bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
842{
843 if (location < 0 || location >= (int)mUniformIndex.size())
844 {
845 return false;
846 }
847
848 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
849
850 // sized queries -- ensure the provided buffer is large enough
851 if (bufSize)
852 {
853 int requiredBytes = UniformExternalSize(targetUniform->type);
854 if (*bufSize < requiredBytes)
855 {
856 return false;
857 }
858 }
859
860 switch (targetUniform->type)
861 {
862 case GL_FLOAT_MAT2:
863 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
864 break;
865 case GL_FLOAT_MAT3:
866 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
867 break;
868 case GL_FLOAT_MAT4:
869 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
870 break;
871 default:
872 {
873 unsigned int count = UniformExternalComponentCount(targetUniform->type);
874 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
875
876 switch (UniformComponentType(targetUniform->type))
877 {
878 case GL_BOOL:
879 {
880 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
881
882 for (unsigned int i = 0; i < count; ++i)
883 {
884 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
885 }
886 }
887 break;
888 case GL_FLOAT:
889 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
890 count * sizeof(GLfloat));
891 break;
892 case GL_INT:
893 {
894 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
895
896 for (unsigned int i = 0; i < count; ++i)
897 {
898 params[i] = (float)intParams[i];
899 }
900 }
901 break;
902 default: UNREACHABLE();
903 }
904 }
905 }
906
907 return true;
908}
909
910bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
911{
912 if (location < 0 || location >= (int)mUniformIndex.size())
913 {
914 return false;
915 }
916
917 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
918
919 // sized queries -- ensure the provided buffer is large enough
920 if (bufSize)
921 {
922 int requiredBytes = UniformExternalSize(targetUniform->type);
923 if (*bufSize < requiredBytes)
924 {
925 return false;
926 }
927 }
928
929 switch (targetUniform->type)
930 {
931 case GL_FLOAT_MAT2:
932 {
933 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
934 }
935 break;
936 case GL_FLOAT_MAT3:
937 {
938 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
939 }
940 break;
941 case GL_FLOAT_MAT4:
942 {
943 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
944 }
945 break;
946 default:
947 {
948 unsigned int count = UniformExternalComponentCount(targetUniform->type);
949 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
950
951 switch (UniformComponentType(targetUniform->type))
952 {
953 case GL_BOOL:
954 {
955 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
956
957 for (unsigned int i = 0; i < count; ++i)
958 {
959 params[i] = (GLint)boolParams[i];
960 }
961 }
962 break;
963 case GL_FLOAT:
964 {
965 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
966
967 for (unsigned int i = 0; i < count; ++i)
968 {
969 params[i] = (GLint)floatParams[i];
970 }
971 }
972 break;
973 case GL_INT:
974 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
975 count * sizeof(GLint));
976 break;
977 default: UNREACHABLE();
978 }
979 }
980 }
981
982 return true;
983}
984
985void ProgramBinary::dirtyAllUniforms()
986{
987 unsigned int numUniforms = mUniforms.size();
988 for (unsigned int index = 0; index < numUniforms; index++)
989 {
990 mUniforms[index]->dirty = true;
991 }
992}
993
994// Applies all the uniforms set for this program object to the Direct3D 9 device
995void ProgramBinary::applyUniforms()
996{
997 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) {
998 Uniform *targetUniform = *ub;
999
1000 if (targetUniform->dirty)
1001 {
1002 int arraySize = targetUniform->arraySize;
1003 GLfloat *f = (GLfloat*)targetUniform->data;
1004 GLint *i = (GLint*)targetUniform->data;
1005 GLboolean *b = (GLboolean*)targetUniform->data;
1006
1007 switch (targetUniform->type)
1008 {
1009 case GL_BOOL: applyUniformnbv(targetUniform, arraySize, 1, b); break;
1010 case GL_BOOL_VEC2: applyUniformnbv(targetUniform, arraySize, 2, b); break;
1011 case GL_BOOL_VEC3: applyUniformnbv(targetUniform, arraySize, 3, b); break;
1012 case GL_BOOL_VEC4: applyUniformnbv(targetUniform, arraySize, 4, b); break;
1013 case GL_FLOAT:
1014 case GL_FLOAT_VEC2:
1015 case GL_FLOAT_VEC3:
1016 case GL_FLOAT_VEC4:
1017 case GL_FLOAT_MAT2:
1018 case GL_FLOAT_MAT3:
1019 case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break;
1020 case GL_SAMPLER_2D:
1021 case GL_SAMPLER_CUBE:
1022 case GL_INT: applyUniform1iv(targetUniform, arraySize, i); break;
1023 case GL_INT_VEC2: applyUniform2iv(targetUniform, arraySize, i); break;
1024 case GL_INT_VEC3: applyUniform3iv(targetUniform, arraySize, i); break;
1025 case GL_INT_VEC4: applyUniform4iv(targetUniform, arraySize, i); break;
1026 default:
1027 UNREACHABLE();
1028 }
1029
1030 targetUniform->dirty = false;
1031 }
1032 }
1033}
1034
1035// Compiles the HLSL code of the attached shaders into executable binaries
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001036ID3D10Blob *ProgramBinary::compileToBinary(InfoLog &infoLog, const char *hlsl, const char *profile, D3DConstantTable **constantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001037{
1038 if (!hlsl)
1039 {
1040 return NULL;
1041 }
1042
daniel@transgaming.come3e826d2012-11-28 19:42:35 +00001043 HRESULT result = S_OK;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001044 UINT flags = 0;
1045 std::string sourceText;
1046 if (perfActive())
1047 {
1048 flags |= D3DCOMPILE_DEBUG;
1049#ifdef NDEBUG
1050 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
1051#else
1052 flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
1053#endif
1054
1055 std::string sourcePath = getTempPath();
1056 sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl);
1057 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
1058 }
1059 else
1060 {
1061 flags |= ANGLE_COMPILE_OPTIMIZATION_LEVEL;
1062 sourceText = hlsl;
1063 }
1064
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001065 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
1066 // Try the default flags first and if compilation fails, try some alternatives.
1067 const static UINT extraFlags[] =
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001068 {
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001069 0,
1070 D3DCOMPILE_AVOID_FLOW_CONTROL,
1071 D3DCOMPILE_PREFER_FLOW_CONTROL
1072 };
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001073
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001074 const static char * const extraFlagNames[] =
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001075 {
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001076 "default",
1077 "avoid flow control",
1078 "prefer flow control"
1079 };
1080
1081 for (int i = 0; i < sizeof(extraFlags) / sizeof(UINT); ++i)
1082 {
1083 ID3D10Blob *errorMessage = NULL;
1084 ID3D10Blob *binary = NULL;
daniel@transgaming.com7d738a22012-11-28 19:43:08 +00001085 result = mRenderer->compileShaderSource(hlsl, g_fakepath, profile, flags | extraFlags[i], &binary, &errorMessage);
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001086
1087 if (errorMessage)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001088 {
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001089 const char *message = (const char*)errorMessage->GetBufferPointer();
1090
1091 infoLog.appendSanitized(message);
1092 TRACE("\n%s", hlsl);
1093 TRACE("\n%s", message);
1094
1095 errorMessage->Release();
1096 errorMessage = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001097 }
1098
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001099 if (SUCCEEDED(result))
1100 {
1101 D3DConstantTable *table = new D3DConstantTable(binary->GetBufferPointer(), binary->GetBufferSize());
1102 if (table->error())
1103 {
1104 delete table;
1105 binary->Release();
1106 return NULL;
1107 }
1108
1109 *constantTable = table;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001110
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001111 return binary;
1112 }
1113 else
1114 {
1115 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1116 {
1117 return error(GL_OUT_OF_MEMORY, (ID3D10Blob*) NULL);
1118 }
1119
1120 infoLog.append("Warning: D3D shader compilation failed with ");
1121 infoLog.append(extraFlagNames[i]);
1122 infoLog.append(" flags.");
1123 if (i + 1 < sizeof(extraFlagNames) / sizeof(char*))
1124 {
1125 infoLog.append(" Retrying with ");
1126 infoLog.append(extraFlagNames[i + 1]);
1127 infoLog.append(".\n");
1128 }
1129 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001130 }
1131
apatrick@chromium.org637ca472012-10-04 18:12:56 +00001132 return NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001133}
1134
1135// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1136// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001137int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001138{
1139 Context *context = getContext();
1140 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1141
1142 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1143 {
1144 int n = VariableRowCount(varying->type) * varying->size;
1145 int m = VariableColumnCount(varying->type);
1146 bool success = false;
1147
1148 if (m == 2 || m == 3 || m == 4)
1149 {
1150 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1151 {
1152 bool available = true;
1153
1154 for (int y = 0; y < n && available; y++)
1155 {
1156 for (int x = 0; x < m && available; x++)
1157 {
1158 if (packing[r + y][x])
1159 {
1160 available = false;
1161 }
1162 }
1163 }
1164
1165 if (available)
1166 {
1167 varying->reg = r;
1168 varying->col = 0;
1169
1170 for (int y = 0; y < n; y++)
1171 {
1172 for (int x = 0; x < m; x++)
1173 {
1174 packing[r + y][x] = &*varying;
1175 }
1176 }
1177
1178 success = true;
1179 }
1180 }
1181
1182 if (!success && m == 2)
1183 {
1184 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1185 {
1186 bool available = true;
1187
1188 for (int y = 0; y < n && available; y++)
1189 {
1190 for (int x = 2; x < 4 && available; x++)
1191 {
1192 if (packing[r + y][x])
1193 {
1194 available = false;
1195 }
1196 }
1197 }
1198
1199 if (available)
1200 {
1201 varying->reg = r;
1202 varying->col = 2;
1203
1204 for (int y = 0; y < n; y++)
1205 {
1206 for (int x = 2; x < 4; x++)
1207 {
1208 packing[r + y][x] = &*varying;
1209 }
1210 }
1211
1212 success = true;
1213 }
1214 }
1215 }
1216 }
1217 else if (m == 1)
1218 {
1219 int space[4] = {0};
1220
1221 for (int y = 0; y < maxVaryingVectors; y++)
1222 {
1223 for (int x = 0; x < 4; x++)
1224 {
1225 space[x] += packing[y][x] ? 0 : 1;
1226 }
1227 }
1228
1229 int column = 0;
1230
1231 for (int x = 0; x < 4; x++)
1232 {
1233 if (space[x] >= n && space[x] < space[column])
1234 {
1235 column = x;
1236 }
1237 }
1238
1239 if (space[column] >= n)
1240 {
1241 for (int r = 0; r < maxVaryingVectors; r++)
1242 {
1243 if (!packing[r][column])
1244 {
1245 varying->reg = r;
1246
1247 for (int y = r; y < r + n; y++)
1248 {
1249 packing[y][column] = &*varying;
1250 }
1251
1252 break;
1253 }
1254 }
1255
1256 varying->col = column;
1257
1258 success = true;
1259 }
1260 }
1261 else UNREACHABLE();
1262
1263 if (!success)
1264 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001265 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001266
1267 return -1;
1268 }
1269 }
1270
1271 // Return the number of used registers
1272 int registers = 0;
1273
1274 for (int r = 0; r < maxVaryingVectors; r++)
1275 {
1276 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1277 {
1278 registers++;
1279 }
1280 }
1281
1282 return registers;
1283}
1284
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001285bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001286{
1287 if (pixelHLSL.empty() || vertexHLSL.empty())
1288 {
1289 return false;
1290 }
1291
1292 // Reset the varying register assignments
1293 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1294 {
1295 fragVar->reg = -1;
1296 fragVar->col = -1;
1297 }
1298
1299 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1300 {
1301 vtxVar->reg = -1;
1302 vtxVar->col = -1;
1303 }
1304
1305 // Map the varyings to the register file
1306 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001307 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001308
1309 if (registers < 0)
1310 {
1311 return false;
1312 }
1313
1314 // Write the HLSL input/output declarations
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001315 const bool sm3 = mRenderer->getMajorShaderModel() >= 3;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001316 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001317 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1318
1319 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1320 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001321 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001322
1323 return false;
1324 }
1325
1326 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1327 {
1328 bool matched = false;
1329
1330 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1331 {
1332 if (output->name == input->name)
1333 {
1334 if (output->type != input->type || output->size != input->size)
1335 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001336 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 +00001337
1338 return false;
1339 }
1340
1341 output->reg = input->reg;
1342 output->col = input->col;
1343
1344 matched = true;
1345 break;
1346 }
1347 }
1348
1349 if (!matched)
1350 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001351 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001352
1353 return false;
1354 }
1355 }
1356
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001357 mUsesPointSize = vertexShader->mUsesPointSize;
1358 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001359
1360 vertexHLSL += "struct VS_INPUT\n"
1361 "{\n";
1362
1363 int semanticIndex = 0;
1364 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1365 {
1366 switch (attribute->type)
1367 {
1368 case GL_FLOAT: vertexHLSL += " float "; break;
1369 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1370 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1371 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1372 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1373 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1374 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1375 default: UNREACHABLE();
1376 }
1377
1378 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1379
1380 semanticIndex += VariableRowCount(attribute->type);
1381 }
1382
1383 vertexHLSL += "};\n"
1384 "\n"
1385 "struct VS_OUTPUT\n"
1386 "{\n"
1387 " float4 gl_Position : POSITION;\n";
1388
1389 for (int r = 0; r < registers; r++)
1390 {
1391 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1392
1393 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1394 }
1395
1396 if (fragmentShader->mUsesFragCoord)
1397 {
1398 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1399 }
1400
1401 if (vertexShader->mUsesPointSize && sm3)
1402 {
1403 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1404 }
1405
1406 vertexHLSL += "};\n"
1407 "\n"
1408 "VS_OUTPUT main(VS_INPUT input)\n"
1409 "{\n";
1410
1411 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1412 {
1413 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1414
1415 if (VariableRowCount(attribute->type) > 1) // Matrix
1416 {
1417 vertexHLSL += "transpose";
1418 }
1419
1420 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1421 }
1422
1423 vertexHLSL += "\n"
1424 " gl_main();\n"
1425 "\n"
1426 " VS_OUTPUT output;\n"
1427 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001428 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001429 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1430 " output.gl_Position.w = gl_Position.w;\n";
1431
1432 if (vertexShader->mUsesPointSize && sm3)
1433 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001434 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001435 }
1436
1437 if (fragmentShader->mUsesFragCoord)
1438 {
1439 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1440 }
1441
1442 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1443 {
1444 if (varying->reg >= 0)
1445 {
1446 for (int i = 0; i < varying->size; i++)
1447 {
1448 int rows = VariableRowCount(varying->type);
1449
1450 for (int j = 0; j < rows; j++)
1451 {
1452 int r = varying->reg + i * rows + j;
1453 vertexHLSL += " output.v" + str(r);
1454
1455 bool sharedRegister = false; // Register used by multiple varyings
1456
1457 for (int x = 0; x < 4; x++)
1458 {
1459 if (packing[r][x] && packing[r][x] != packing[r][0])
1460 {
1461 sharedRegister = true;
1462 break;
1463 }
1464 }
1465
1466 if(sharedRegister)
1467 {
1468 vertexHLSL += ".";
1469
1470 for (int x = 0; x < 4; x++)
1471 {
1472 if (packing[r][x] == &*varying)
1473 {
1474 switch(x)
1475 {
1476 case 0: vertexHLSL += "x"; break;
1477 case 1: vertexHLSL += "y"; break;
1478 case 2: vertexHLSL += "z"; break;
1479 case 3: vertexHLSL += "w"; break;
1480 }
1481 }
1482 }
1483 }
1484
1485 vertexHLSL += " = " + varying->name;
1486
1487 if (varying->array)
1488 {
1489 vertexHLSL += "[" + str(i) + "]";
1490 }
1491
1492 if (rows > 1)
1493 {
1494 vertexHLSL += "[" + str(j) + "]";
1495 }
1496
1497 vertexHLSL += ";\n";
1498 }
1499 }
1500 }
1501 }
1502
1503 vertexHLSL += "\n"
1504 " return output;\n"
1505 "}\n";
1506
1507 pixelHLSL += "struct PS_INPUT\n"
1508 "{\n";
1509
1510 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1511 {
1512 if (varying->reg >= 0)
1513 {
1514 for (int i = 0; i < varying->size; i++)
1515 {
1516 int rows = VariableRowCount(varying->type);
1517 for (int j = 0; j < rows; j++)
1518 {
1519 std::string n = str(varying->reg + i * rows + j);
1520 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1521 }
1522 }
1523 }
1524 else UNREACHABLE();
1525 }
1526
1527 if (fragmentShader->mUsesFragCoord)
1528 {
1529 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1530 if (sm3) {
1531 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1532 }
1533 }
1534
1535 if (fragmentShader->mUsesPointCoord && sm3)
1536 {
1537 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1538 }
1539
1540 if (fragmentShader->mUsesFrontFacing)
1541 {
1542 pixelHLSL += " float vFace : VFACE;\n";
1543 }
1544
1545 pixelHLSL += "};\n"
1546 "\n"
1547 "struct PS_OUTPUT\n"
1548 "{\n"
1549 " float4 gl_Color[1] : COLOR;\n"
1550 "};\n"
1551 "\n"
1552 "PS_OUTPUT main(PS_INPUT input)\n"
1553 "{\n";
1554
1555 if (fragmentShader->mUsesFragCoord)
1556 {
1557 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1558
1559 if (sm3)
1560 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001561 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001562 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001563 }
1564 else
1565 {
1566 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1567 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001568 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001569 }
1570
1571 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n"
1572 " gl_FragCoord.w = rhw;\n";
1573 }
1574
1575 if (fragmentShader->mUsesPointCoord && sm3)
1576 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001577 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1578 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001579 }
1580
1581 if (fragmentShader->mUsesFrontFacing)
1582 {
1583 pixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n";
1584 }
1585
1586 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1587 {
1588 if (varying->reg >= 0)
1589 {
1590 for (int i = 0; i < varying->size; i++)
1591 {
1592 int rows = VariableRowCount(varying->type);
1593 for (int j = 0; j < rows; j++)
1594 {
1595 std::string n = str(varying->reg + i * rows + j);
1596 pixelHLSL += " " + varying->name;
1597
1598 if (varying->array)
1599 {
1600 pixelHLSL += "[" + str(i) + "]";
1601 }
1602
1603 if (rows > 1)
1604 {
1605 pixelHLSL += "[" + str(j) + "]";
1606 }
1607
1608 pixelHLSL += " = input.v" + n + ";\n";
1609 }
1610 }
1611 }
1612 else UNREACHABLE();
1613 }
1614
1615 pixelHLSL += "\n"
1616 " gl_main();\n"
1617 "\n"
1618 " PS_OUTPUT output;\n"
1619 " output.gl_Color[0] = gl_Color[0];\n"
1620 "\n"
1621 " return output;\n"
1622 "}\n";
1623
1624 return true;
1625}
1626
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001627bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1628{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001629 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001630
1631 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001632 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001633 if (format != GL_PROGRAM_BINARY_ANGLE)
1634 {
1635 infoLog.append("Invalid program binary format.");
1636 return false;
1637 }
1638
1639 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001640 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001641 if (version != BUILD_REVISION)
1642 {
1643 infoLog.append("Invalid program binary version.");
1644 return false;
1645 }
1646
1647 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1648 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001649 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001650 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001651 stream.read(&name);
1652 mLinkedAttribute[i].name = name;
1653 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001654 }
1655
1656 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1657 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001658 stream.read(&mSamplersPS[i].active);
1659 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001660
1661 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001662 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001663 mSamplersPS[i].textureType = (TextureType) textureType;
1664 }
1665
1666 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1667 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001668 stream.read(&mSamplersVS[i].active);
1669 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001670
1671 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001672 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001673 mSamplersVS[i].textureType = (TextureType) textureType;
1674 }
1675
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001676 stream.read(&mUsedVertexSamplerRange);
1677 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001678
1679 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001680 stream.read(&size);
1681 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001682 {
1683 infoLog.append("Invalid program binary.");
1684 return false;
1685 }
1686
1687 mUniforms.resize(size);
1688 for (unsigned int i = 0; i < size; ++i)
1689 {
1690 GLenum type;
1691 std::string _name;
1692 unsigned int arraySize;
1693
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001694 stream.read(&type);
1695 stream.read(&_name);
1696 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001697
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001698 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001699
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001700 stream.read(&mUniforms[i]->ps.float4Index);
1701 stream.read(&mUniforms[i]->ps.samplerIndex);
1702 stream.read(&mUniforms[i]->ps.boolIndex);
1703 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001704
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001705 stream.read(&mUniforms[i]->vs.float4Index);
1706 stream.read(&mUniforms[i]->vs.samplerIndex);
1707 stream.read(&mUniforms[i]->vs.boolIndex);
1708 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001709 }
1710
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001711 stream.read(&size);
1712 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001713 {
1714 infoLog.append("Invalid program binary.");
1715 return false;
1716 }
1717
1718 mUniformIndex.resize(size);
1719 for (unsigned int i = 0; i < size; ++i)
1720 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001721 stream.read(&mUniformIndex[i].name);
1722 stream.read(&mUniformIndex[i].element);
1723 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001724 }
1725
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001726 stream.read(&mDxDepthRangeLocation);
1727 stream.read(&mDxDepthLocation);
1728 stream.read(&mDxCoordLocation);
1729 stream.read(&mDxHalfPixelSizeLocation);
1730 stream.read(&mDxFrontCCWLocation);
1731 stream.read(&mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001732
1733 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001734 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001735
1736 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001737 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001738
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001739 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001740
1741 const D3DCAPS9 *binaryIdentifier = (const D3DCAPS9*) ptr;
1742 ptr += sizeof(GUID);
1743
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001744 GUID identifier = mRenderer->getAdapterIdentifier();
1745 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001746 {
1747 infoLog.append("Invalid program binary.");
1748 return false;
1749 }
1750
1751 const char *pixelShaderFunction = ptr;
1752 ptr += pixelShaderSize;
1753
1754 const char *vertexShaderFunction = ptr;
1755 ptr += vertexShaderSize;
1756
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001757 mPixelExecutable = mRenderer->createPixelShader(reinterpret_cast<const DWORD*>(pixelShaderFunction), pixelShaderSize);
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001758 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001759 {
1760 infoLog.append("Could not create pixel shader.");
1761 return false;
1762 }
1763
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001764 mVertexExecutable = mRenderer->createVertexShader(reinterpret_cast<const DWORD*>(vertexShaderFunction), vertexShaderSize);
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001765 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001766 {
1767 infoLog.append("Could not create vertex shader.");
1768 mPixelExecutable->Release();
1769 mPixelExecutable = NULL;
1770 return false;
1771 }
1772
1773 return true;
1774}
1775
1776bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1777{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001778 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001779
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001780 stream.write(GL_PROGRAM_BINARY_ANGLE);
1781 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001782
1783 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1784 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001785 stream.write(mLinkedAttribute[i].type);
1786 stream.write(mLinkedAttribute[i].name);
1787 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001788 }
1789
1790 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1791 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001792 stream.write(mSamplersPS[i].active);
1793 stream.write(mSamplersPS[i].logicalTextureUnit);
1794 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001795 }
1796
1797 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1798 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001799 stream.write(mSamplersVS[i].active);
1800 stream.write(mSamplersVS[i].logicalTextureUnit);
1801 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001802 }
1803
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001804 stream.write(mUsedVertexSamplerRange);
1805 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001806
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001807 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001808 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1809 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001810 stream.write(mUniforms[i]->type);
1811 stream.write(mUniforms[i]->_name);
1812 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001813
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001814 stream.write(mUniforms[i]->ps.float4Index);
1815 stream.write(mUniforms[i]->ps.samplerIndex);
1816 stream.write(mUniforms[i]->ps.boolIndex);
1817 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001818
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001819 stream.write(mUniforms[i]->vs.float4Index);
1820 stream.write(mUniforms[i]->vs.samplerIndex);
1821 stream.write(mUniforms[i]->vs.boolIndex);
1822 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001823 }
1824
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001825 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001826 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1827 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001828 stream.write(mUniformIndex[i].name);
1829 stream.write(mUniformIndex[i].element);
1830 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001831 }
1832
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001833 stream.write(mDxDepthRangeLocation);
1834 stream.write(mDxDepthLocation);
1835 stream.write(mDxCoordLocation);
1836 stream.write(mDxHalfPixelSizeLocation);
1837 stream.write(mDxFrontCCWLocation);
1838 stream.write(mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001839
1840 UINT pixelShaderSize;
1841 HRESULT result = mPixelExecutable->GetFunction(NULL, &pixelShaderSize);
1842 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001843 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001844
1845 UINT vertexShaderSize;
1846 result = mVertexExecutable->GetFunction(NULL, &vertexShaderSize);
1847 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001848 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001849
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001850 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001851
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001852 GLsizei streamLength = stream.length();
1853 const void *streamData = stream.data();
1854
1855 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001856 if (totalLength > bufSize)
1857 {
1858 if (length)
1859 {
1860 *length = 0;
1861 }
1862
1863 return false;
1864 }
1865
1866 if (binary)
1867 {
1868 char *ptr = (char*) binary;
1869
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001870 memcpy(ptr, streamData, streamLength);
1871 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001872
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001873 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001874 ptr += sizeof(GUID);
1875
1876 result = mPixelExecutable->GetFunction(ptr, &pixelShaderSize);
1877 ASSERT(SUCCEEDED(result));
1878 ptr += pixelShaderSize;
1879
1880 result = mVertexExecutable->GetFunction(ptr, &vertexShaderSize);
1881 ASSERT(SUCCEEDED(result));
1882 ptr += vertexShaderSize;
1883
1884 ASSERT(ptr - totalLength == binary);
1885 }
1886
1887 if (length)
1888 {
1889 *length = totalLength;
1890 }
1891
1892 return true;
1893}
1894
1895GLint ProgramBinary::getLength()
1896{
1897 GLint length;
1898 if (save(NULL, INT_MAX, &length))
1899 {
1900 return length;
1901 }
1902 else
1903 {
1904 return 0;
1905 }
1906}
1907
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001908bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001909{
1910 if (!fragmentShader || !fragmentShader->isCompiled())
1911 {
1912 return false;
1913 }
1914
1915 if (!vertexShader || !vertexShader->isCompiled())
1916 {
1917 return false;
1918 }
1919
1920 std::string pixelHLSL = fragmentShader->getHLSL();
1921 std::string vertexHLSL = vertexShader->getHLSL();
1922
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001923 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001924 {
1925 return false;
1926 }
1927
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001928 const char *vertexProfile = mRenderer->getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
1929 const char *pixelProfile = mRenderer->getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001930
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001931 bool success = true;
1932 D3DConstantTable *constantTableVS = NULL;
1933 D3DConstantTable *constantTablePS = NULL;
1934 ID3D10Blob *vertexBinary = compileToBinary(infoLog, vertexHLSL.c_str(), vertexProfile, &constantTableVS);
1935 ID3D10Blob *pixelBinary = compileToBinary(infoLog, pixelHLSL.c_str(), pixelProfile, &constantTablePS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001936
1937 if (vertexBinary && pixelBinary)
1938 {
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001939 mVertexExecutable = mRenderer->createVertexShader((DWORD*)vertexBinary->GetBufferPointer(), vertexBinary->GetBufferSize());
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001940 mPixelExecutable = mRenderer->createPixelShader((DWORD*)pixelBinary->GetBufferPointer(), pixelBinary->GetBufferSize());
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001941
1942 if (!mPixelExecutable || !mVertexExecutable)
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001943 {
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001944 if (mVertexExecutable) mVertexExecutable->Release();
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001945 mVertexExecutable = NULL;
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001946 if (mPixelExecutable) mPixelExecutable->Release();
1947 mPixelExecutable = NULL;
1948 infoLog.append("Failed to create D3D shaders.");
1949 success = false;
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001950 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001951 }
1952 else
1953 {
1954 success = false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001955 }
1956
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001957 if (vertexBinary) vertexBinary->Release();
1958 if (pixelBinary) pixelBinary->Release();
1959 vertexBinary = NULL;
1960 pixelBinary = NULL;
1961
1962 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1963 {
1964 success = false;
1965 }
1966
1967 if (constantTableVS && constantTablePS)
1968 {
1969 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1970 {
1971 success = false;
1972 }
1973 }
1974 delete constantTableVS;
1975 delete constantTablePS;
1976
1977 // these uniforms are searched as already-decorated because gl_ and dx_
1978 // are reserved prefixes, and do not receive additional decoration
1979 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1980 mDxDepthLocation = getUniformLocation("dx_Depth");
1981 mDxCoordLocation = getUniformLocation("dx_Coord");
1982 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1983 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1984 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1985
1986 Context *context = getContext();
1987 context->markDxUniformsDirty();
1988
1989 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001990}
1991
1992// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001993bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001994{
1995 unsigned int usedLocations = 0;
1996
1997 // Link attributes that have a binding location
1998 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1999 {
2000 int location = attributeBindings.getAttributeBinding(attribute->name);
2001
2002 if (location != -1) // Set by glBindAttribLocation
2003 {
2004 if (!mLinkedAttribute[location].name.empty())
2005 {
2006 // Multiple active attributes bound to the same location; not an error
2007 }
2008
2009 mLinkedAttribute[location] = *attribute;
2010
2011 int rows = VariableRowCount(attribute->type);
2012
2013 if (rows + location > MAX_VERTEX_ATTRIBS)
2014 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002015 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 +00002016
2017 return false;
2018 }
2019
2020 for (int i = 0; i < rows; i++)
2021 {
2022 usedLocations |= 1 << (location + i);
2023 }
2024 }
2025 }
2026
2027 // Link attributes that don't have a binding location
2028 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
2029 {
2030 int location = attributeBindings.getAttributeBinding(attribute->name);
2031
2032 if (location == -1) // Not set by glBindAttribLocation
2033 {
2034 int rows = VariableRowCount(attribute->type);
2035 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
2036
2037 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
2038 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002039 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002040
2041 return false; // Fail to link
2042 }
2043
2044 mLinkedAttribute[availableIndex] = *attribute;
2045 }
2046 }
2047
2048 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
2049 {
2050 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
2051 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
2052
2053 for (int r = 0; r < rows; r++)
2054 {
2055 mSemanticIndex[attributeIndex++] = index++;
2056 }
2057 }
2058
2059 return true;
2060}
2061
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002062bool ProgramBinary::linkUniforms(InfoLog &infoLog, D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002063{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002064 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002065 {
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002066 const D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002067
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002068 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002069 {
2070 return false;
2071 }
2072 }
2073
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002074 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
2075 {
2076 const D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
2077
2078 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
2079 {
2080 return false;
2081 }
2082 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002083 return true;
2084}
2085
2086// Adds the description of a constant found in the binary shader to the list of uniforms
2087// Returns true if succesful (uniform not already defined)
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002088bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DConstant *constant, const std::string &name,
2089 D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002090{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002091 if (constant->registerSet == D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002092 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002093 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002094 {
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002095 const D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
2096 const D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002097
2098 if (psConstant)
2099 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002100 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002101
2102 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2103 {
2104 mSamplersPS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002105 mSamplersPS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002106 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2107 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2108 }
2109 else
2110 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002111 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002112 return false;
2113 }
2114 }
2115
2116 if (vsConstant)
2117 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002118 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002119
2120 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2121 {
2122 mSamplersVS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002123 mSamplersVS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002124 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2125 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2126 }
2127 else
2128 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002129 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002130 return false;
2131 }
2132 }
2133 }
2134 }
2135
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002136 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002137 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002138 case D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002139 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002140 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002141 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002142 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002143 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002144 const D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002145
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002146 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002147
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002148 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002149 {
2150 return false;
2151 }
2152 }
2153 }
2154
2155 return true;
2156 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002157 case D3DConstant::CLASS_SCALAR:
2158 case D3DConstant::CLASS_VECTOR:
2159 case D3DConstant::CLASS_MATRIX_COLUMNS:
2160 case D3DConstant::CLASS_OBJECT:
2161 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002162 default:
2163 UNREACHABLE();
2164 return false;
2165 }
2166}
2167
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002168bool ProgramBinary::defineUniform(GLenum shader, const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002169{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002170 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002171
2172 if(!uniform)
2173 {
2174 return false;
2175 }
2176
2177 // Check if already defined
2178 GLint location = getUniformLocation(uniform->name);
2179 GLenum type = uniform->type;
2180
2181 if (location >= 0)
2182 {
2183 delete uniform;
2184 uniform = mUniforms[mUniformIndex[location].index];
2185 }
2186
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002187 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2188 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002189
2190 if (location >= 0)
2191 {
2192 return uniform->type == type;
2193 }
2194
2195 mUniforms.push_back(uniform);
2196 unsigned int uniformIndex = mUniforms.size() - 1;
2197
2198 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2199 {
2200 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2201 }
2202
2203 return true;
2204}
2205
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002206Uniform *ProgramBinary::createUniform(const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002207{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002208 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002209 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002210 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002211 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002212 case D3DConstant::PT_SAMPLER2D:
2213 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002214 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002215 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002216 default: UNREACHABLE();
2217 }
2218 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002219 case D3DConstant::PT_SAMPLERCUBE:
2220 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002221 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002222 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002223 default: UNREACHABLE();
2224 }
2225 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002226 case D3DConstant::PT_BOOL:
2227 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002228 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002229 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2230 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2231 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2232 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002233 default: UNREACHABLE();
2234 }
2235 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002236 case D3DConstant::PT_INT:
2237 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002238 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002239 case 1: return new Uniform(GL_INT, _name, constant->elements);
2240 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2241 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2242 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002243 default: UNREACHABLE();
2244 }
2245 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002246 case D3DConstant::PT_FLOAT:
2247 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002248 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002249 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2250 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2251 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2252 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002253 default: UNREACHABLE();
2254 }
2255 break;
2256 default:
2257 UNREACHABLE();
2258 }
2259 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002260 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002261 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002262 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002263 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002264 case D3DConstant::PT_FLOAT:
2265 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002266 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002267 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2268 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2269 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002270 default: UNREACHABLE();
2271 }
2272 break;
2273 default: UNREACHABLE();
2274 }
2275 }
2276 else UNREACHABLE();
2277
2278 return 0;
2279}
2280
2281// This method needs to match OutputHLSL::decorate
2282std::string ProgramBinary::decorateAttribute(const std::string &name)
2283{
2284 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2285 {
2286 return "_" + name;
2287 }
2288
2289 return name;
2290}
2291
2292std::string ProgramBinary::undecorateUniform(const std::string &_name)
2293{
2294 std::string name = _name;
2295
2296 // Remove any structure field decoration
2297 size_t pos = 0;
2298 while ((pos = name.find("._", pos)) != std::string::npos)
2299 {
2300 name.replace(pos, 2, ".");
2301 }
2302
2303 // Remove the leading decoration
2304 if (name[0] == '_')
2305 {
2306 return name.substr(1);
2307 }
2308 else if (name.compare(0, 3, "ar_") == 0)
2309 {
2310 return name.substr(3);
2311 }
2312
2313 return name;
2314}
2315
2316void ProgramBinary::applyUniformnbv(Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
2317{
2318 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2319 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2320
2321 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2322 {
2323 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2324 for (int i = 0; i < count; i++)
2325 {
2326 for (int j = 0; j < 4; j++)
2327 {
2328 if (j < width)
2329 {
2330 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2331 }
2332 else
2333 {
2334 vector[i * 4 + j] = 0.0f;
2335 }
2336 }
2337 }
2338 }
2339
2340 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2341 {
2342 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2343 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2344 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2345 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2346 for (int i = 0; i < copyCount; i++)
2347 {
2348 boolVector[i] = v[i] != GL_FALSE;
2349 }
2350 }
2351
2352 if (targetUniform->ps.float4Index >= 0)
2353 {
2354 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
2355 }
2356
2357 if (targetUniform->ps.boolIndex >= 0)
2358 {
2359 mDevice->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
2360 }
2361
2362 if (targetUniform->vs.float4Index >= 0)
2363 {
2364 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
2365 }
2366
2367 if (targetUniform->vs.boolIndex >= 0)
2368 {
2369 mDevice->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
2370 }
2371}
2372
2373bool ProgramBinary::applyUniformnfv(Uniform *targetUniform, const GLfloat *v)
2374{
2375 if (targetUniform->ps.registerCount)
2376 {
2377 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
2378 }
2379
2380 if (targetUniform->vs.registerCount)
2381 {
2382 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
2383 }
2384
2385 return true;
2386}
2387
2388bool ProgramBinary::applyUniform1iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2389{
2390 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002391 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002392
2393 for (int i = 0; i < count; i++)
2394 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002395 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002396 }
2397
2398 if (targetUniform->ps.registerCount)
2399 {
2400 if (targetUniform->ps.samplerIndex >= 0)
2401 {
2402 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2403
2404 for (int i = 0; i < count; i++)
2405 {
2406 unsigned int samplerIndex = firstIndex + i;
2407
2408 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2409 {
2410 ASSERT(mSamplersPS[samplerIndex].active);
2411 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2412 }
2413 }
2414 }
2415 else
2416 {
2417 ASSERT(targetUniform->ps.float4Index >= 0);
2418 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
2419 }
2420 }
2421
2422 if (targetUniform->vs.registerCount)
2423 {
2424 if (targetUniform->vs.samplerIndex >= 0)
2425 {
2426 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2427
2428 for (int i = 0; i < count; i++)
2429 {
2430 unsigned int samplerIndex = firstIndex + i;
2431
2432 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2433 {
2434 ASSERT(mSamplersVS[samplerIndex].active);
2435 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2436 }
2437 }
2438 }
2439 else
2440 {
2441 ASSERT(targetUniform->vs.float4Index >= 0);
2442 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2443 }
2444 }
2445
2446 return true;
2447}
2448
2449bool ProgramBinary::applyUniform2iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2450{
2451 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002452 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002453
2454 for (int i = 0; i < count; i++)
2455 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002456 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002457
2458 v += 2;
2459 }
2460
2461 applyUniformniv(targetUniform, count, vector);
2462
2463 return true;
2464}
2465
2466bool ProgramBinary::applyUniform3iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2467{
2468 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002469 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002470
2471 for (int i = 0; i < count; i++)
2472 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002473 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002474
2475 v += 3;
2476 }
2477
2478 applyUniformniv(targetUniform, count, vector);
2479
2480 return true;
2481}
2482
2483bool ProgramBinary::applyUniform4iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2484{
2485 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002486 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002487
2488 for (int i = 0; i < count; i++)
2489 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002490 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002491
2492 v += 4;
2493 }
2494
2495 applyUniformniv(targetUniform, count, vector);
2496
2497 return true;
2498}
2499
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002500void ProgramBinary::applyUniformniv(Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002501{
2502 if (targetUniform->ps.registerCount)
2503 {
2504 ASSERT(targetUniform->ps.float4Index >= 0);
2505 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
2506 }
2507
2508 if (targetUniform->vs.registerCount)
2509 {
2510 ASSERT(targetUniform->vs.float4Index >= 0);
2511 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2512 }
2513}
2514
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002515bool ProgramBinary::isValidated() const
2516{
2517 return mValidated;
2518}
2519
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002520void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2521{
2522 // Skip over inactive attributes
2523 unsigned int activeAttribute = 0;
2524 unsigned int attribute;
2525 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2526 {
2527 if (mLinkedAttribute[attribute].name.empty())
2528 {
2529 continue;
2530 }
2531
2532 if (activeAttribute == index)
2533 {
2534 break;
2535 }
2536
2537 activeAttribute++;
2538 }
2539
2540 if (bufsize > 0)
2541 {
2542 const char *string = mLinkedAttribute[attribute].name.c_str();
2543
2544 strncpy(name, string, bufsize);
2545 name[bufsize - 1] = '\0';
2546
2547 if (length)
2548 {
2549 *length = strlen(name);
2550 }
2551 }
2552
2553 *size = 1; // Always a single 'type' instance
2554
2555 *type = mLinkedAttribute[attribute].type;
2556}
2557
2558GLint ProgramBinary::getActiveAttributeCount()
2559{
2560 int count = 0;
2561
2562 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2563 {
2564 if (!mLinkedAttribute[attributeIndex].name.empty())
2565 {
2566 count++;
2567 }
2568 }
2569
2570 return count;
2571}
2572
2573GLint ProgramBinary::getActiveAttributeMaxLength()
2574{
2575 int maxLength = 0;
2576
2577 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2578 {
2579 if (!mLinkedAttribute[attributeIndex].name.empty())
2580 {
2581 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2582 }
2583 }
2584
2585 return maxLength;
2586}
2587
2588void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2589{
2590 // Skip over internal uniforms
2591 unsigned int activeUniform = 0;
2592 unsigned int uniform;
2593 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2594 {
2595 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2596 {
2597 continue;
2598 }
2599
2600 if (activeUniform == index)
2601 {
2602 break;
2603 }
2604
2605 activeUniform++;
2606 }
2607
2608 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2609
2610 if (bufsize > 0)
2611 {
2612 std::string string = mUniforms[uniform]->name;
2613
2614 if (mUniforms[uniform]->isArray())
2615 {
2616 string += "[0]";
2617 }
2618
2619 strncpy(name, string.c_str(), bufsize);
2620 name[bufsize - 1] = '\0';
2621
2622 if (length)
2623 {
2624 *length = strlen(name);
2625 }
2626 }
2627
2628 *size = mUniforms[uniform]->arraySize;
2629
2630 *type = mUniforms[uniform]->type;
2631}
2632
2633GLint ProgramBinary::getActiveUniformCount()
2634{
2635 int count = 0;
2636
2637 unsigned int numUniforms = mUniforms.size();
2638 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2639 {
2640 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2641 {
2642 count++;
2643 }
2644 }
2645
2646 return count;
2647}
2648
2649GLint ProgramBinary::getActiveUniformMaxLength()
2650{
2651 int maxLength = 0;
2652
2653 unsigned int numUniforms = mUniforms.size();
2654 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2655 {
2656 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2657 {
2658 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2659 if (mUniforms[uniformIndex]->isArray())
2660 {
2661 length += 3; // Counting in "[0]".
2662 }
2663 maxLength = std::max(length, maxLength);
2664 }
2665 }
2666
2667 return maxLength;
2668}
2669
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002670void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002671{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002672 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002673 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002674 {
2675 mValidated = false;
2676 }
2677 else
2678 {
2679 mValidated = true;
2680 }
2681}
2682
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002683bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002684{
2685 // if any two active samplers in a program are of different types, but refer to the same
2686 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2687 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2688
2689 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2690 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2691
2692 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2693 {
2694 textureUnitType[i] = TEXTURE_UNKNOWN;
2695 }
2696
2697 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2698 {
2699 if (mSamplersPS[i].active)
2700 {
2701 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2702
2703 if (unit >= maxCombinedTextureImageUnits)
2704 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002705 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002706 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002707 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002708 }
2709
2710 return false;
2711 }
2712
2713 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2714 {
2715 if (mSamplersPS[i].textureType != textureUnitType[unit])
2716 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002717 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002718 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002719 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002720 }
2721
2722 return false;
2723 }
2724 }
2725 else
2726 {
2727 textureUnitType[unit] = mSamplersPS[i].textureType;
2728 }
2729 }
2730 }
2731
2732 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2733 {
2734 if (mSamplersVS[i].active)
2735 {
2736 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2737
2738 if (unit >= maxCombinedTextureImageUnits)
2739 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002740 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002741 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002742 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002743 }
2744
2745 return false;
2746 }
2747
2748 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2749 {
2750 if (mSamplersVS[i].textureType != textureUnitType[unit])
2751 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002752 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002753 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002754 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002755 }
2756
2757 return false;
2758 }
2759 }
2760 else
2761 {
2762 textureUnitType[unit] = mSamplersVS[i].textureType;
2763 }
2764 }
2765 }
2766
2767 return true;
2768}
2769
2770GLint ProgramBinary::getDxDepthRangeLocation() const
2771{
2772 return mDxDepthRangeLocation;
2773}
2774
2775GLint ProgramBinary::getDxDepthLocation() const
2776{
2777 return mDxDepthLocation;
2778}
2779
2780GLint ProgramBinary::getDxCoordLocation() const
2781{
2782 return mDxCoordLocation;
2783}
2784
2785GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2786{
2787 return mDxHalfPixelSizeLocation;
2788}
2789
2790GLint ProgramBinary::getDxFrontCCWLocation() const
2791{
2792 return mDxFrontCCWLocation;
2793}
2794
2795GLint ProgramBinary::getDxPointsOrLinesLocation() const
2796{
2797 return mDxPointsOrLinesLocation;
2798}
2799
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002800ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2801{
2802}
2803
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002804}