blob: 62ba329b16d2e7ce185533ab20d94cda2e0a7fe3 [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
2// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000010#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000011#include "libGLESv2/ProgramBinary.h"
12
13#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000014#include "common/version.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/Shader.h"
18#include "libGLESv2/utilities.h"
19
20#include <string>
21
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000022namespace gl
23{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000024std::string str(int i)
25{
26 char buffer[20];
27 snprintf(buffer, sizeof(buffer), "%d", i);
28 return buffer;
29}
30
31Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
32 : type(type), _name(_name), name(ProgramBinary::undecorateUniform(_name)), arraySize(arraySize)
33{
34 int bytes = UniformInternalSize(type) * arraySize;
35 data = new unsigned char[bytes];
36 memset(data, 0, bytes);
37 dirty = true;
38}
39
40Uniform::~Uniform()
41{
42 delete[] data;
43}
44
45bool Uniform::isArray()
46{
daniel@transgaming.com22ba0f72012-09-27 17:46:04 +000047 size_t dot = _name.find_last_of('.');
48 if (dot == std::string::npos) dot = -1;
49
50 return _name.compare(dot + 1, dot + 4, "ar_") == 0;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000051}
52
53UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
54 : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index)
55{
56}
57
daniel@transgaming.come87ca002012-07-24 18:30:43 +000058unsigned int ProgramBinary::mCurrentSerial = 1;
59
daniel@transgaming.com70062c92012-11-28 19:32:30 +000060ProgramBinary::ProgramBinary(rx::Renderer *renderer) : RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000061{
daniel@transgaming.com70062c92012-11-28 19:32:30 +000062 ASSERT(dynamic_cast<rx::Renderer9*>(renderer) != NULL); // D3D9_REPLACE
63 mRenderer = static_cast<rx::Renderer9*>(renderer);
daniel@transgaming.come4733d72012-10-31 18:07:01 +000064 mDevice = mRenderer->getDevice(); // D3D9_REPLACE
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000065
66 mPixelExecutable = NULL;
67 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000068
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000069 mValidated = false;
70
71 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
72 {
73 mSemanticIndex[index] = -1;
74 }
75
76 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
77 {
78 mSamplersPS[index].active = false;
79 }
80
81 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
82 {
83 mSamplersVS[index].active = false;
84 }
85
86 mUsedVertexSamplerRange = 0;
87 mUsedPixelSamplerRange = 0;
88
89 mDxDepthRangeLocation = -1;
90 mDxDepthLocation = -1;
91 mDxCoordLocation = -1;
92 mDxHalfPixelSizeLocation = -1;
93 mDxFrontCCWLocation = -1;
94 mDxPointsOrLinesLocation = -1;
95}
96
97ProgramBinary::~ProgramBinary()
98{
99 if (mPixelExecutable)
100 {
101 mPixelExecutable->Release();
102 }
103
104 if (mVertexExecutable)
105 {
106 mVertexExecutable->Release();
107 }
108
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000109 while (!mUniforms.empty())
110 {
111 delete mUniforms.back();
112 mUniforms.pop_back();
113 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000114}
115
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000116unsigned int ProgramBinary::getSerial() const
117{
118 return mSerial;
119}
120
121unsigned int ProgramBinary::issueSerial()
122{
123 return mCurrentSerial++;
124}
125
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000126IDirect3DPixelShader9 *ProgramBinary::getPixelShader()
127{
128 return mPixelExecutable;
129}
130
131IDirect3DVertexShader9 *ProgramBinary::getVertexShader()
132{
133 return mVertexExecutable;
134}
135
136GLuint ProgramBinary::getAttributeLocation(const char *name)
137{
138 if (name)
139 {
140 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
141 {
142 if (mLinkedAttribute[index].name == std::string(name))
143 {
144 return index;
145 }
146 }
147 }
148
149 return -1;
150}
151
152int ProgramBinary::getSemanticIndex(int attributeIndex)
153{
154 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
155
156 return mSemanticIndex[attributeIndex];
157}
158
159// Returns one more than the highest sampler index used.
160GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
161{
162 switch (type)
163 {
164 case SAMPLER_PIXEL:
165 return mUsedPixelSamplerRange;
166 case SAMPLER_VERTEX:
167 return mUsedVertexSamplerRange;
168 default:
169 UNREACHABLE();
170 return 0;
171 }
172}
173
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000174bool ProgramBinary::usesPointSize() const
175{
176 return mUsesPointSize;
177}
178
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000179// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
180// index (0-15 for the pixel shader and 0-3 for the vertex shader).
181GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
182{
183 GLint logicalTextureUnit = -1;
184
185 switch (type)
186 {
187 case SAMPLER_PIXEL:
188 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
189
190 if (mSamplersPS[samplerIndex].active)
191 {
192 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
193 }
194 break;
195 case SAMPLER_VERTEX:
196 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
197
198 if (mSamplersVS[samplerIndex].active)
199 {
200 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
201 }
202 break;
203 default: UNREACHABLE();
204 }
205
206 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
207 {
208 return logicalTextureUnit;
209 }
210
211 return -1;
212}
213
214// Returns the texture type for a given Direct3D 9 sampler type and
215// index (0-15 for the pixel shader and 0-3 for the vertex shader).
216TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
217{
218 switch (type)
219 {
220 case SAMPLER_PIXEL:
221 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
222 ASSERT(mSamplersPS[samplerIndex].active);
223 return mSamplersPS[samplerIndex].textureType;
224 case SAMPLER_VERTEX:
225 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
226 ASSERT(mSamplersVS[samplerIndex].active);
227 return mSamplersVS[samplerIndex].textureType;
228 default: UNREACHABLE();
229 }
230
231 return TEXTURE_2D;
232}
233
234GLint ProgramBinary::getUniformLocation(std::string name)
235{
236 unsigned int subscript = 0;
237
238 // Strip any trailing array operator and retrieve the subscript
239 size_t open = name.find_last_of('[');
240 size_t close = name.find_last_of(']');
241 if (open != std::string::npos && close == name.length() - 1)
242 {
243 subscript = atoi(name.substr(open + 1).c_str());
244 name.erase(open);
245 }
246
247 unsigned int numUniforms = mUniformIndex.size();
248 for (unsigned int location = 0; location < numUniforms; location++)
249 {
250 if (mUniformIndex[location].name == name &&
251 mUniformIndex[location].element == subscript)
252 {
253 return location;
254 }
255 }
256
257 return -1;
258}
259
260bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
261{
262 if (location < 0 || location >= (int)mUniformIndex.size())
263 {
264 return false;
265 }
266
267 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
268 targetUniform->dirty = true;
269
270 if (targetUniform->type == GL_FLOAT)
271 {
272 int arraySize = targetUniform->arraySize;
273
274 if (arraySize == 1 && count > 1)
275 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
276
277 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
278
279 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
280
281 for (int i = 0; i < count; i++)
282 {
283 target[0] = v[0];
284 target[1] = 0;
285 target[2] = 0;
286 target[3] = 0;
287 target += 4;
288 v += 1;
289 }
290 }
291 else if (targetUniform->type == GL_BOOL)
292 {
293 int arraySize = targetUniform->arraySize;
294
295 if (arraySize == 1 && count > 1)
296 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
297
298 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
299 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
300
301 for (int i = 0; i < count; ++i)
302 {
303 if (v[i] == 0.0f)
304 {
305 boolParams[i] = GL_FALSE;
306 }
307 else
308 {
309 boolParams[i] = GL_TRUE;
310 }
311 }
312 }
313 else
314 {
315 return false;
316 }
317
318 return true;
319}
320
321bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
322{
323 if (location < 0 || location >= (int)mUniformIndex.size())
324 {
325 return false;
326 }
327
328 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
329 targetUniform->dirty = true;
330
331 if (targetUniform->type == GL_FLOAT_VEC2)
332 {
333 int arraySize = targetUniform->arraySize;
334
335 if (arraySize == 1 && count > 1)
336 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
337
338 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
339
340 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
341
342 for (int i = 0; i < count; i++)
343 {
344 target[0] = v[0];
345 target[1] = v[1];
346 target[2] = 0;
347 target[3] = 0;
348 target += 4;
349 v += 2;
350 }
351 }
352 else if (targetUniform->type == GL_BOOL_VEC2)
353 {
354 int arraySize = targetUniform->arraySize;
355
356 if (arraySize == 1 && count > 1)
357 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
358
359 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
360
361 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
362
363 for (int i = 0; i < count * 2; ++i)
364 {
365 if (v[i] == 0.0f)
366 {
367 boolParams[i] = GL_FALSE;
368 }
369 else
370 {
371 boolParams[i] = GL_TRUE;
372 }
373 }
374 }
375 else
376 {
377 return false;
378 }
379
380 return true;
381}
382
383bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
384{
385 if (location < 0 || location >= (int)mUniformIndex.size())
386 {
387 return false;
388 }
389
390 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
391 targetUniform->dirty = true;
392
393 if (targetUniform->type == GL_FLOAT_VEC3)
394 {
395 int arraySize = targetUniform->arraySize;
396
397 if (arraySize == 1 && count > 1)
398 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
399
400 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
401
402 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
403
404 for (int i = 0; i < count; i++)
405 {
406 target[0] = v[0];
407 target[1] = v[1];
408 target[2] = v[2];
409 target[3] = 0;
410 target += 4;
411 v += 3;
412 }
413 }
414 else if (targetUniform->type == GL_BOOL_VEC3)
415 {
416 int arraySize = targetUniform->arraySize;
417
418 if (arraySize == 1 && count > 1)
419 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
420
421 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
422 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
423
424 for (int i = 0; i < count * 3; ++i)
425 {
426 if (v[i] == 0.0f)
427 {
428 boolParams[i] = GL_FALSE;
429 }
430 else
431 {
432 boolParams[i] = GL_TRUE;
433 }
434 }
435 }
436 else
437 {
438 return false;
439 }
440
441 return true;
442}
443
444bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
445{
446 if (location < 0 || location >= (int)mUniformIndex.size())
447 {
448 return false;
449 }
450
451 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
452 targetUniform->dirty = true;
453
454 if (targetUniform->type == GL_FLOAT_VEC4)
455 {
456 int arraySize = targetUniform->arraySize;
457
458 if (arraySize == 1 && count > 1)
459 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
460
461 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
462
463 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
464 v, 4 * sizeof(GLfloat) * count);
465 }
466 else if (targetUniform->type == GL_BOOL_VEC4)
467 {
468 int arraySize = targetUniform->arraySize;
469
470 if (arraySize == 1 && count > 1)
471 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
472
473 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
474 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
475
476 for (int i = 0; i < count * 4; ++i)
477 {
478 if (v[i] == 0.0f)
479 {
480 boolParams[i] = GL_FALSE;
481 }
482 else
483 {
484 boolParams[i] = GL_TRUE;
485 }
486 }
487 }
488 else
489 {
490 return false;
491 }
492
493 return true;
494}
495
496template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
497void transposeMatrix(T *target, const GLfloat *value)
498{
499 int copyWidth = std::min(targetWidth, srcWidth);
500 int copyHeight = std::min(targetHeight, srcHeight);
501
502 for (int x = 0; x < copyWidth; x++)
503 {
504 for (int y = 0; y < copyHeight; y++)
505 {
506 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
507 }
508 }
509 // clear unfilled right side
510 for (int y = 0; y < copyHeight; y++)
511 {
512 for (int x = srcWidth; x < targetWidth; x++)
513 {
514 target[y * targetWidth + x] = (T)0;
515 }
516 }
517 // clear unfilled bottom.
518 for (int y = srcHeight; y < targetHeight; y++)
519 {
520 for (int x = 0; x < targetWidth; x++)
521 {
522 target[y * targetWidth + x] = (T)0;
523 }
524 }
525}
526
527bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
528{
529 if (location < 0 || location >= (int)mUniformIndex.size())
530 {
531 return false;
532 }
533
534 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
535 targetUniform->dirty = true;
536
537 if (targetUniform->type != GL_FLOAT_MAT2)
538 {
539 return false;
540 }
541
542 int arraySize = targetUniform->arraySize;
543
544 if (arraySize == 1 && count > 1)
545 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
546
547 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
548
549 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
550 for (int i = 0; i < count; i++)
551 {
552 transposeMatrix<GLfloat,4,2,2,2>(target, value);
553 target += 8;
554 value += 4;
555 }
556
557 return true;
558}
559
560bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
561{
562 if (location < 0 || location >= (int)mUniformIndex.size())
563 {
564 return false;
565 }
566
567 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
568 targetUniform->dirty = true;
569
570 if (targetUniform->type != GL_FLOAT_MAT3)
571 {
572 return false;
573 }
574
575 int arraySize = targetUniform->arraySize;
576
577 if (arraySize == 1 && count > 1)
578 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
579
580 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
581
582 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
583 for (int i = 0; i < count; i++)
584 {
585 transposeMatrix<GLfloat,4,3,3,3>(target, value);
586 target += 12;
587 value += 9;
588 }
589
590 return true;
591}
592
593
594bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
595{
596 if (location < 0 || location >= (int)mUniformIndex.size())
597 {
598 return false;
599 }
600
601 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
602 targetUniform->dirty = true;
603
604 if (targetUniform->type != GL_FLOAT_MAT4)
605 {
606 return false;
607 }
608
609 int arraySize = targetUniform->arraySize;
610
611 if (arraySize == 1 && count > 1)
612 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
613
614 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
615
616 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
617 for (int i = 0; i < count; i++)
618 {
619 transposeMatrix<GLfloat,4,4,4,4>(target, value);
620 target += 16;
621 value += 16;
622 }
623
624 return true;
625}
626
627bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
628{
629 if (location < 0 || location >= (int)mUniformIndex.size())
630 {
631 return false;
632 }
633
634 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
635 targetUniform->dirty = true;
636
637 if (targetUniform->type == GL_INT ||
638 targetUniform->type == GL_SAMPLER_2D ||
639 targetUniform->type == GL_SAMPLER_CUBE)
640 {
641 int arraySize = targetUniform->arraySize;
642
643 if (arraySize == 1 && count > 1)
644 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
645
646 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
647
648 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
649 v, sizeof(GLint) * count);
650 }
651 else if (targetUniform->type == GL_BOOL)
652 {
653 int arraySize = targetUniform->arraySize;
654
655 if (arraySize == 1 && count > 1)
656 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
657
658 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
659 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
660
661 for (int i = 0; i < count; ++i)
662 {
663 if (v[i] == 0)
664 {
665 boolParams[i] = GL_FALSE;
666 }
667 else
668 {
669 boolParams[i] = GL_TRUE;
670 }
671 }
672 }
673 else
674 {
675 return false;
676 }
677
678 return true;
679}
680
681bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
682{
683 if (location < 0 || location >= (int)mUniformIndex.size())
684 {
685 return false;
686 }
687
688 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
689 targetUniform->dirty = true;
690
691 if (targetUniform->type == GL_INT_VEC2)
692 {
693 int arraySize = targetUniform->arraySize;
694
695 if (arraySize == 1 && count > 1)
696 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
697
698 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
699
700 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
701 v, 2 * sizeof(GLint) * count);
702 }
703 else if (targetUniform->type == GL_BOOL_VEC2)
704 {
705 int arraySize = targetUniform->arraySize;
706
707 if (arraySize == 1 && count > 1)
708 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
709
710 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
711 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
712
713 for (int i = 0; i < count * 2; ++i)
714 {
715 if (v[i] == 0)
716 {
717 boolParams[i] = GL_FALSE;
718 }
719 else
720 {
721 boolParams[i] = GL_TRUE;
722 }
723 }
724 }
725 else
726 {
727 return false;
728 }
729
730 return true;
731}
732
733bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
734{
735 if (location < 0 || location >= (int)mUniformIndex.size())
736 {
737 return false;
738 }
739
740 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
741 targetUniform->dirty = true;
742
743 if (targetUniform->type == GL_INT_VEC3)
744 {
745 int arraySize = targetUniform->arraySize;
746
747 if (arraySize == 1 && count > 1)
748 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
749
750 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
751
752 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
753 v, 3 * sizeof(GLint) * count);
754 }
755 else if (targetUniform->type == GL_BOOL_VEC3)
756 {
757 int arraySize = targetUniform->arraySize;
758
759 if (arraySize == 1 && count > 1)
760 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
761
762 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
763 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
764
765 for (int i = 0; i < count * 3; ++i)
766 {
767 if (v[i] == 0)
768 {
769 boolParams[i] = GL_FALSE;
770 }
771 else
772 {
773 boolParams[i] = GL_TRUE;
774 }
775 }
776 }
777 else
778 {
779 return false;
780 }
781
782 return true;
783}
784
785bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
786{
787 if (location < 0 || location >= (int)mUniformIndex.size())
788 {
789 return false;
790 }
791
792 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
793 targetUniform->dirty = true;
794
795 if (targetUniform->type == GL_INT_VEC4)
796 {
797 int arraySize = targetUniform->arraySize;
798
799 if (arraySize == 1 && count > 1)
800 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
801
802 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
803
804 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
805 v, 4 * sizeof(GLint) * count);
806 }
807 else if (targetUniform->type == GL_BOOL_VEC4)
808 {
809 int arraySize = targetUniform->arraySize;
810
811 if (arraySize == 1 && count > 1)
812 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
813
814 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
815 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
816
817 for (int i = 0; i < count * 4; ++i)
818 {
819 if (v[i] == 0)
820 {
821 boolParams[i] = GL_FALSE;
822 }
823 else
824 {
825 boolParams[i] = GL_TRUE;
826 }
827 }
828 }
829 else
830 {
831 return false;
832 }
833
834 return true;
835}
836
837bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
838{
839 if (location < 0 || location >= (int)mUniformIndex.size())
840 {
841 return false;
842 }
843
844 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
845
846 // sized queries -- ensure the provided buffer is large enough
847 if (bufSize)
848 {
849 int requiredBytes = UniformExternalSize(targetUniform->type);
850 if (*bufSize < requiredBytes)
851 {
852 return false;
853 }
854 }
855
856 switch (targetUniform->type)
857 {
858 case GL_FLOAT_MAT2:
859 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
860 break;
861 case GL_FLOAT_MAT3:
862 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
863 break;
864 case GL_FLOAT_MAT4:
865 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
866 break;
867 default:
868 {
869 unsigned int count = UniformExternalComponentCount(targetUniform->type);
870 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
871
872 switch (UniformComponentType(targetUniform->type))
873 {
874 case GL_BOOL:
875 {
876 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
877
878 for (unsigned int i = 0; i < count; ++i)
879 {
880 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
881 }
882 }
883 break;
884 case GL_FLOAT:
885 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
886 count * sizeof(GLfloat));
887 break;
888 case GL_INT:
889 {
890 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
891
892 for (unsigned int i = 0; i < count; ++i)
893 {
894 params[i] = (float)intParams[i];
895 }
896 }
897 break;
898 default: UNREACHABLE();
899 }
900 }
901 }
902
903 return true;
904}
905
906bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
907{
908 if (location < 0 || location >= (int)mUniformIndex.size())
909 {
910 return false;
911 }
912
913 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
914
915 // sized queries -- ensure the provided buffer is large enough
916 if (bufSize)
917 {
918 int requiredBytes = UniformExternalSize(targetUniform->type);
919 if (*bufSize < requiredBytes)
920 {
921 return false;
922 }
923 }
924
925 switch (targetUniform->type)
926 {
927 case GL_FLOAT_MAT2:
928 {
929 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
930 }
931 break;
932 case GL_FLOAT_MAT3:
933 {
934 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
935 }
936 break;
937 case GL_FLOAT_MAT4:
938 {
939 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
940 }
941 break;
942 default:
943 {
944 unsigned int count = UniformExternalComponentCount(targetUniform->type);
945 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
946
947 switch (UniformComponentType(targetUniform->type))
948 {
949 case GL_BOOL:
950 {
951 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
952
953 for (unsigned int i = 0; i < count; ++i)
954 {
955 params[i] = (GLint)boolParams[i];
956 }
957 }
958 break;
959 case GL_FLOAT:
960 {
961 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
962
963 for (unsigned int i = 0; i < count; ++i)
964 {
965 params[i] = (GLint)floatParams[i];
966 }
967 }
968 break;
969 case GL_INT:
970 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
971 count * sizeof(GLint));
972 break;
973 default: UNREACHABLE();
974 }
975 }
976 }
977
978 return true;
979}
980
981void ProgramBinary::dirtyAllUniforms()
982{
983 unsigned int numUniforms = mUniforms.size();
984 for (unsigned int index = 0; index < numUniforms; index++)
985 {
986 mUniforms[index]->dirty = true;
987 }
988}
989
990// Applies all the uniforms set for this program object to the Direct3D 9 device
991void ProgramBinary::applyUniforms()
992{
993 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) {
994 Uniform *targetUniform = *ub;
995
996 if (targetUniform->dirty)
997 {
998 int arraySize = targetUniform->arraySize;
999 GLfloat *f = (GLfloat*)targetUniform->data;
1000 GLint *i = (GLint*)targetUniform->data;
1001 GLboolean *b = (GLboolean*)targetUniform->data;
1002
1003 switch (targetUniform->type)
1004 {
1005 case GL_BOOL: applyUniformnbv(targetUniform, arraySize, 1, b); break;
1006 case GL_BOOL_VEC2: applyUniformnbv(targetUniform, arraySize, 2, b); break;
1007 case GL_BOOL_VEC3: applyUniformnbv(targetUniform, arraySize, 3, b); break;
1008 case GL_BOOL_VEC4: applyUniformnbv(targetUniform, arraySize, 4, b); break;
1009 case GL_FLOAT:
1010 case GL_FLOAT_VEC2:
1011 case GL_FLOAT_VEC3:
1012 case GL_FLOAT_VEC4:
1013 case GL_FLOAT_MAT2:
1014 case GL_FLOAT_MAT3:
1015 case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break;
1016 case GL_SAMPLER_2D:
1017 case GL_SAMPLER_CUBE:
1018 case GL_INT: applyUniform1iv(targetUniform, arraySize, i); break;
1019 case GL_INT_VEC2: applyUniform2iv(targetUniform, arraySize, i); break;
1020 case GL_INT_VEC3: applyUniform3iv(targetUniform, arraySize, i); break;
1021 case GL_INT_VEC4: applyUniform4iv(targetUniform, arraySize, i); break;
1022 default:
1023 UNREACHABLE();
1024 }
1025
1026 targetUniform->dirty = false;
1027 }
1028 }
1029}
1030
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001031// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1032// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001033int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001034{
1035 Context *context = getContext();
1036 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1037
1038 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1039 {
1040 int n = VariableRowCount(varying->type) * varying->size;
1041 int m = VariableColumnCount(varying->type);
1042 bool success = false;
1043
1044 if (m == 2 || m == 3 || m == 4)
1045 {
1046 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1047 {
1048 bool available = true;
1049
1050 for (int y = 0; y < n && available; y++)
1051 {
1052 for (int x = 0; x < m && available; x++)
1053 {
1054 if (packing[r + y][x])
1055 {
1056 available = false;
1057 }
1058 }
1059 }
1060
1061 if (available)
1062 {
1063 varying->reg = r;
1064 varying->col = 0;
1065
1066 for (int y = 0; y < n; y++)
1067 {
1068 for (int x = 0; x < m; x++)
1069 {
1070 packing[r + y][x] = &*varying;
1071 }
1072 }
1073
1074 success = true;
1075 }
1076 }
1077
1078 if (!success && m == 2)
1079 {
1080 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1081 {
1082 bool available = true;
1083
1084 for (int y = 0; y < n && available; y++)
1085 {
1086 for (int x = 2; x < 4 && available; x++)
1087 {
1088 if (packing[r + y][x])
1089 {
1090 available = false;
1091 }
1092 }
1093 }
1094
1095 if (available)
1096 {
1097 varying->reg = r;
1098 varying->col = 2;
1099
1100 for (int y = 0; y < n; y++)
1101 {
1102 for (int x = 2; x < 4; x++)
1103 {
1104 packing[r + y][x] = &*varying;
1105 }
1106 }
1107
1108 success = true;
1109 }
1110 }
1111 }
1112 }
1113 else if (m == 1)
1114 {
1115 int space[4] = {0};
1116
1117 for (int y = 0; y < maxVaryingVectors; y++)
1118 {
1119 for (int x = 0; x < 4; x++)
1120 {
1121 space[x] += packing[y][x] ? 0 : 1;
1122 }
1123 }
1124
1125 int column = 0;
1126
1127 for (int x = 0; x < 4; x++)
1128 {
1129 if (space[x] >= n && space[x] < space[column])
1130 {
1131 column = x;
1132 }
1133 }
1134
1135 if (space[column] >= n)
1136 {
1137 for (int r = 0; r < maxVaryingVectors; r++)
1138 {
1139 if (!packing[r][column])
1140 {
1141 varying->reg = r;
1142
1143 for (int y = r; y < r + n; y++)
1144 {
1145 packing[y][column] = &*varying;
1146 }
1147
1148 break;
1149 }
1150 }
1151
1152 varying->col = column;
1153
1154 success = true;
1155 }
1156 }
1157 else UNREACHABLE();
1158
1159 if (!success)
1160 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001161 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001162
1163 return -1;
1164 }
1165 }
1166
1167 // Return the number of used registers
1168 int registers = 0;
1169
1170 for (int r = 0; r < maxVaryingVectors; r++)
1171 {
1172 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1173 {
1174 registers++;
1175 }
1176 }
1177
1178 return registers;
1179}
1180
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001181bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001182{
1183 if (pixelHLSL.empty() || vertexHLSL.empty())
1184 {
1185 return false;
1186 }
1187
1188 // Reset the varying register assignments
1189 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1190 {
1191 fragVar->reg = -1;
1192 fragVar->col = -1;
1193 }
1194
1195 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1196 {
1197 vtxVar->reg = -1;
1198 vtxVar->col = -1;
1199 }
1200
1201 // Map the varyings to the register file
1202 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001203 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001204
1205 if (registers < 0)
1206 {
1207 return false;
1208 }
1209
1210 // Write the HLSL input/output declarations
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00001211 const bool sm3 = mRenderer->getMajorShaderModel() >= 3;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001212 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001213 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1214
1215 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1216 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001217 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001218
1219 return false;
1220 }
1221
1222 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1223 {
1224 bool matched = false;
1225
1226 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1227 {
1228 if (output->name == input->name)
1229 {
1230 if (output->type != input->type || output->size != input->size)
1231 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001232 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 +00001233
1234 return false;
1235 }
1236
1237 output->reg = input->reg;
1238 output->col = input->col;
1239
1240 matched = true;
1241 break;
1242 }
1243 }
1244
1245 if (!matched)
1246 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001247 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001248
1249 return false;
1250 }
1251 }
1252
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001253 mUsesPointSize = vertexShader->mUsesPointSize;
1254 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001255
1256 vertexHLSL += "struct VS_INPUT\n"
1257 "{\n";
1258
1259 int semanticIndex = 0;
1260 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1261 {
1262 switch (attribute->type)
1263 {
1264 case GL_FLOAT: vertexHLSL += " float "; break;
1265 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1266 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1267 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1268 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1269 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1270 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1271 default: UNREACHABLE();
1272 }
1273
1274 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1275
1276 semanticIndex += VariableRowCount(attribute->type);
1277 }
1278
1279 vertexHLSL += "};\n"
1280 "\n"
1281 "struct VS_OUTPUT\n"
1282 "{\n"
1283 " float4 gl_Position : POSITION;\n";
1284
1285 for (int r = 0; r < registers; r++)
1286 {
1287 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1288
1289 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1290 }
1291
1292 if (fragmentShader->mUsesFragCoord)
1293 {
1294 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1295 }
1296
1297 if (vertexShader->mUsesPointSize && sm3)
1298 {
1299 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1300 }
1301
1302 vertexHLSL += "};\n"
1303 "\n"
1304 "VS_OUTPUT main(VS_INPUT input)\n"
1305 "{\n";
1306
1307 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1308 {
1309 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1310
1311 if (VariableRowCount(attribute->type) > 1) // Matrix
1312 {
1313 vertexHLSL += "transpose";
1314 }
1315
1316 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1317 }
1318
1319 vertexHLSL += "\n"
1320 " gl_main();\n"
1321 "\n"
1322 " VS_OUTPUT output;\n"
1323 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001324 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001325 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1326 " output.gl_Position.w = gl_Position.w;\n";
1327
1328 if (vertexShader->mUsesPointSize && sm3)
1329 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001330 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001331 }
1332
1333 if (fragmentShader->mUsesFragCoord)
1334 {
1335 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1336 }
1337
1338 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1339 {
1340 if (varying->reg >= 0)
1341 {
1342 for (int i = 0; i < varying->size; i++)
1343 {
1344 int rows = VariableRowCount(varying->type);
1345
1346 for (int j = 0; j < rows; j++)
1347 {
1348 int r = varying->reg + i * rows + j;
1349 vertexHLSL += " output.v" + str(r);
1350
1351 bool sharedRegister = false; // Register used by multiple varyings
1352
1353 for (int x = 0; x < 4; x++)
1354 {
1355 if (packing[r][x] && packing[r][x] != packing[r][0])
1356 {
1357 sharedRegister = true;
1358 break;
1359 }
1360 }
1361
1362 if(sharedRegister)
1363 {
1364 vertexHLSL += ".";
1365
1366 for (int x = 0; x < 4; x++)
1367 {
1368 if (packing[r][x] == &*varying)
1369 {
1370 switch(x)
1371 {
1372 case 0: vertexHLSL += "x"; break;
1373 case 1: vertexHLSL += "y"; break;
1374 case 2: vertexHLSL += "z"; break;
1375 case 3: vertexHLSL += "w"; break;
1376 }
1377 }
1378 }
1379 }
1380
1381 vertexHLSL += " = " + varying->name;
1382
1383 if (varying->array)
1384 {
1385 vertexHLSL += "[" + str(i) + "]";
1386 }
1387
1388 if (rows > 1)
1389 {
1390 vertexHLSL += "[" + str(j) + "]";
1391 }
1392
1393 vertexHLSL += ";\n";
1394 }
1395 }
1396 }
1397 }
1398
1399 vertexHLSL += "\n"
1400 " return output;\n"
1401 "}\n";
1402
1403 pixelHLSL += "struct PS_INPUT\n"
1404 "{\n";
1405
1406 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1407 {
1408 if (varying->reg >= 0)
1409 {
1410 for (int i = 0; i < varying->size; i++)
1411 {
1412 int rows = VariableRowCount(varying->type);
1413 for (int j = 0; j < rows; j++)
1414 {
1415 std::string n = str(varying->reg + i * rows + j);
1416 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1417 }
1418 }
1419 }
1420 else UNREACHABLE();
1421 }
1422
1423 if (fragmentShader->mUsesFragCoord)
1424 {
1425 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1426 if (sm3) {
1427 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1428 }
1429 }
1430
1431 if (fragmentShader->mUsesPointCoord && sm3)
1432 {
1433 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1434 }
1435
1436 if (fragmentShader->mUsesFrontFacing)
1437 {
1438 pixelHLSL += " float vFace : VFACE;\n";
1439 }
1440
1441 pixelHLSL += "};\n"
1442 "\n"
1443 "struct PS_OUTPUT\n"
1444 "{\n"
1445 " float4 gl_Color[1] : COLOR;\n"
1446 "};\n"
1447 "\n"
1448 "PS_OUTPUT main(PS_INPUT input)\n"
1449 "{\n";
1450
1451 if (fragmentShader->mUsesFragCoord)
1452 {
1453 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1454
1455 if (sm3)
1456 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001457 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001458 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001459 }
1460 else
1461 {
1462 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1463 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001464 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001465 }
1466
1467 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_Depth.x + dx_Depth.y;\n"
1468 " gl_FragCoord.w = rhw;\n";
1469 }
1470
1471 if (fragmentShader->mUsesPointCoord && sm3)
1472 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001473 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1474 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001475 }
1476
1477 if (fragmentShader->mUsesFrontFacing)
1478 {
1479 pixelHLSL += " gl_FrontFacing = dx_PointsOrLines || (dx_FrontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0));\n";
1480 }
1481
1482 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1483 {
1484 if (varying->reg >= 0)
1485 {
1486 for (int i = 0; i < varying->size; i++)
1487 {
1488 int rows = VariableRowCount(varying->type);
1489 for (int j = 0; j < rows; j++)
1490 {
1491 std::string n = str(varying->reg + i * rows + j);
1492 pixelHLSL += " " + varying->name;
1493
1494 if (varying->array)
1495 {
1496 pixelHLSL += "[" + str(i) + "]";
1497 }
1498
1499 if (rows > 1)
1500 {
1501 pixelHLSL += "[" + str(j) + "]";
1502 }
1503
1504 pixelHLSL += " = input.v" + n + ";\n";
1505 }
1506 }
1507 }
1508 else UNREACHABLE();
1509 }
1510
1511 pixelHLSL += "\n"
1512 " gl_main();\n"
1513 "\n"
1514 " PS_OUTPUT output;\n"
1515 " output.gl_Color[0] = gl_Color[0];\n"
1516 "\n"
1517 " return output;\n"
1518 "}\n";
1519
1520 return true;
1521}
1522
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001523bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1524{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001525 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001526
1527 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001528 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001529 if (format != GL_PROGRAM_BINARY_ANGLE)
1530 {
1531 infoLog.append("Invalid program binary format.");
1532 return false;
1533 }
1534
1535 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001536 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001537 if (version != BUILD_REVISION)
1538 {
1539 infoLog.append("Invalid program binary version.");
1540 return false;
1541 }
1542
1543 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1544 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001545 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001546 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001547 stream.read(&name);
1548 mLinkedAttribute[i].name = name;
1549 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001550 }
1551
1552 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1553 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001554 stream.read(&mSamplersPS[i].active);
1555 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001556
1557 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001558 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001559 mSamplersPS[i].textureType = (TextureType) textureType;
1560 }
1561
1562 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1563 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001564 stream.read(&mSamplersVS[i].active);
1565 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001566
1567 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001568 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001569 mSamplersVS[i].textureType = (TextureType) textureType;
1570 }
1571
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001572 stream.read(&mUsedVertexSamplerRange);
1573 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001574
1575 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001576 stream.read(&size);
1577 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001578 {
1579 infoLog.append("Invalid program binary.");
1580 return false;
1581 }
1582
1583 mUniforms.resize(size);
1584 for (unsigned int i = 0; i < size; ++i)
1585 {
1586 GLenum type;
1587 std::string _name;
1588 unsigned int arraySize;
1589
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001590 stream.read(&type);
1591 stream.read(&_name);
1592 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001593
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001594 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001595
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001596 stream.read(&mUniforms[i]->ps.float4Index);
1597 stream.read(&mUniforms[i]->ps.samplerIndex);
1598 stream.read(&mUniforms[i]->ps.boolIndex);
1599 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001600
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001601 stream.read(&mUniforms[i]->vs.float4Index);
1602 stream.read(&mUniforms[i]->vs.samplerIndex);
1603 stream.read(&mUniforms[i]->vs.boolIndex);
1604 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001605 }
1606
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001607 stream.read(&size);
1608 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001609 {
1610 infoLog.append("Invalid program binary.");
1611 return false;
1612 }
1613
1614 mUniformIndex.resize(size);
1615 for (unsigned int i = 0; i < size; ++i)
1616 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001617 stream.read(&mUniformIndex[i].name);
1618 stream.read(&mUniformIndex[i].element);
1619 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001620 }
1621
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001622 stream.read(&mDxDepthRangeLocation);
1623 stream.read(&mDxDepthLocation);
1624 stream.read(&mDxCoordLocation);
1625 stream.read(&mDxHalfPixelSizeLocation);
1626 stream.read(&mDxFrontCCWLocation);
1627 stream.read(&mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001628
1629 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001630 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001631
1632 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001633 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001634
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001635 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001636
1637 const D3DCAPS9 *binaryIdentifier = (const D3DCAPS9*) ptr;
1638 ptr += sizeof(GUID);
1639
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001640 GUID identifier = mRenderer->getAdapterIdentifier();
1641 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001642 {
1643 infoLog.append("Invalid program binary.");
1644 return false;
1645 }
1646
1647 const char *pixelShaderFunction = ptr;
1648 ptr += pixelShaderSize;
1649
1650 const char *vertexShaderFunction = ptr;
1651 ptr += vertexShaderSize;
1652
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001653 mPixelExecutable = mRenderer->createPixelShader(reinterpret_cast<const DWORD*>(pixelShaderFunction), pixelShaderSize);
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001654 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001655 {
1656 infoLog.append("Could not create pixel shader.");
1657 return false;
1658 }
1659
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001660 mVertexExecutable = mRenderer->createVertexShader(reinterpret_cast<const DWORD*>(vertexShaderFunction), vertexShaderSize);
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001661 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001662 {
1663 infoLog.append("Could not create vertex shader.");
1664 mPixelExecutable->Release();
1665 mPixelExecutable = NULL;
1666 return false;
1667 }
1668
1669 return true;
1670}
1671
1672bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1673{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001674 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001675
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001676 stream.write(GL_PROGRAM_BINARY_ANGLE);
1677 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001678
1679 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1680 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001681 stream.write(mLinkedAttribute[i].type);
1682 stream.write(mLinkedAttribute[i].name);
1683 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001684 }
1685
1686 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1687 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001688 stream.write(mSamplersPS[i].active);
1689 stream.write(mSamplersPS[i].logicalTextureUnit);
1690 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001691 }
1692
1693 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1694 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001695 stream.write(mSamplersVS[i].active);
1696 stream.write(mSamplersVS[i].logicalTextureUnit);
1697 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001698 }
1699
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001700 stream.write(mUsedVertexSamplerRange);
1701 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001702
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001703 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001704 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1705 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001706 stream.write(mUniforms[i]->type);
1707 stream.write(mUniforms[i]->_name);
1708 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001709
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001710 stream.write(mUniforms[i]->ps.float4Index);
1711 stream.write(mUniforms[i]->ps.samplerIndex);
1712 stream.write(mUniforms[i]->ps.boolIndex);
1713 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001714
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001715 stream.write(mUniforms[i]->vs.float4Index);
1716 stream.write(mUniforms[i]->vs.samplerIndex);
1717 stream.write(mUniforms[i]->vs.boolIndex);
1718 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001719 }
1720
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001721 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001722 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1723 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001724 stream.write(mUniformIndex[i].name);
1725 stream.write(mUniformIndex[i].element);
1726 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001727 }
1728
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001729 stream.write(mDxDepthRangeLocation);
1730 stream.write(mDxDepthLocation);
1731 stream.write(mDxCoordLocation);
1732 stream.write(mDxHalfPixelSizeLocation);
1733 stream.write(mDxFrontCCWLocation);
1734 stream.write(mDxPointsOrLinesLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001735
1736 UINT pixelShaderSize;
1737 HRESULT result = mPixelExecutable->GetFunction(NULL, &pixelShaderSize);
1738 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001739 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001740
1741 UINT vertexShaderSize;
1742 result = mVertexExecutable->GetFunction(NULL, &vertexShaderSize);
1743 ASSERT(SUCCEEDED(result));
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001744 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001745
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001746 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001747
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001748 GLsizei streamLength = stream.length();
1749 const void *streamData = stream.data();
1750
1751 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001752 if (totalLength > bufSize)
1753 {
1754 if (length)
1755 {
1756 *length = 0;
1757 }
1758
1759 return false;
1760 }
1761
1762 if (binary)
1763 {
1764 char *ptr = (char*) binary;
1765
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001766 memcpy(ptr, streamData, streamLength);
1767 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001768
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001769 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001770 ptr += sizeof(GUID);
1771
1772 result = mPixelExecutable->GetFunction(ptr, &pixelShaderSize);
1773 ASSERT(SUCCEEDED(result));
1774 ptr += pixelShaderSize;
1775
1776 result = mVertexExecutable->GetFunction(ptr, &vertexShaderSize);
1777 ASSERT(SUCCEEDED(result));
1778 ptr += vertexShaderSize;
1779
1780 ASSERT(ptr - totalLength == binary);
1781 }
1782
1783 if (length)
1784 {
1785 *length = totalLength;
1786 }
1787
1788 return true;
1789}
1790
1791GLint ProgramBinary::getLength()
1792{
1793 GLint length;
1794 if (save(NULL, INT_MAX, &length))
1795 {
1796 return length;
1797 }
1798 else
1799 {
1800 return 0;
1801 }
1802}
1803
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001804bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001805{
1806 if (!fragmentShader || !fragmentShader->isCompiled())
1807 {
1808 return false;
1809 }
1810
1811 if (!vertexShader || !vertexShader->isCompiled())
1812 {
1813 return false;
1814 }
1815
1816 std::string pixelHLSL = fragmentShader->getHLSL();
1817 std::string vertexHLSL = vertexShader->getHLSL();
1818
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001819 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001820 {
1821 return false;
1822 }
1823
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001824 bool success = true;
1825 D3DConstantTable *constantTableVS = NULL;
1826 D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00001827 rx::ShaderExecutable *vertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1828 rx::ShaderExecutable *pixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001829
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00001830 if (vertexExecutable && pixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001831 {
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00001832 rx::ShaderExecutable9 *vshader9 = rx::ShaderExecutable9::makeShaderExecutable9(vertexExecutable);
1833 rx::ShaderExecutable9 *pshader9 = rx::ShaderExecutable9::makeShaderExecutable9(pixelExecutable);
1834 mVertexExecutable = vshader9->getVertexShader();
1835 mPixelExecutable = pshader9->getPixelShader();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001836
1837 if (!mPixelExecutable || !mVertexExecutable)
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001838 {
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001839 if (mVertexExecutable) mVertexExecutable->Release();
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001840 mVertexExecutable = NULL;
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001841 if (mPixelExecutable) mPixelExecutable->Release();
1842 mPixelExecutable = NULL;
1843 infoLog.append("Failed to create D3D shaders.");
1844 success = false;
apatrick@chromium.org3cfd7222012-07-13 22:36:58 +00001845 }
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00001846
1847 constantTableVS = vshader9->getConstantTable();
1848 constantTablePS = pshader9->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001849 }
1850 else
1851 {
1852 success = false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001853 }
1854
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001855 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1856 {
1857 success = false;
1858 }
1859
1860 if (constantTableVS && constantTablePS)
1861 {
1862 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1863 {
1864 success = false;
1865 }
1866 }
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00001867 delete vertexExecutable;
1868 delete pixelExecutable;
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001869
1870 // these uniforms are searched as already-decorated because gl_ and dx_
1871 // are reserved prefixes, and do not receive additional decoration
1872 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
1873 mDxDepthLocation = getUniformLocation("dx_Depth");
1874 mDxCoordLocation = getUniformLocation("dx_Coord");
1875 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
1876 mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
1877 mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
1878
1879 Context *context = getContext();
1880 context->markDxUniformsDirty();
1881
1882 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001883}
1884
1885// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001886bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001887{
1888 unsigned int usedLocations = 0;
1889
1890 // Link attributes that have a binding location
1891 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1892 {
1893 int location = attributeBindings.getAttributeBinding(attribute->name);
1894
1895 if (location != -1) // Set by glBindAttribLocation
1896 {
1897 if (!mLinkedAttribute[location].name.empty())
1898 {
1899 // Multiple active attributes bound to the same location; not an error
1900 }
1901
1902 mLinkedAttribute[location] = *attribute;
1903
1904 int rows = VariableRowCount(attribute->type);
1905
1906 if (rows + location > MAX_VERTEX_ATTRIBS)
1907 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001908 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 +00001909
1910 return false;
1911 }
1912
1913 for (int i = 0; i < rows; i++)
1914 {
1915 usedLocations |= 1 << (location + i);
1916 }
1917 }
1918 }
1919
1920 // Link attributes that don't have a binding location
1921 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1922 {
1923 int location = attributeBindings.getAttributeBinding(attribute->name);
1924
1925 if (location == -1) // Not set by glBindAttribLocation
1926 {
1927 int rows = VariableRowCount(attribute->type);
1928 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1929
1930 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1931 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001932 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001933
1934 return false; // Fail to link
1935 }
1936
1937 mLinkedAttribute[availableIndex] = *attribute;
1938 }
1939 }
1940
1941 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1942 {
1943 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1944 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1945
1946 for (int r = 0; r < rows; r++)
1947 {
1948 mSemanticIndex[attributeIndex++] = index++;
1949 }
1950 }
1951
1952 return true;
1953}
1954
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001955bool ProgramBinary::linkUniforms(InfoLog &infoLog, D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001956{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001957 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001958 {
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001959 const D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001960
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001961 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001962 {
1963 return false;
1964 }
1965 }
1966
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001967 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1968 {
1969 const D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
1970
1971 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1972 {
1973 return false;
1974 }
1975 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001976 return true;
1977}
1978
1979// Adds the description of a constant found in the binary shader to the list of uniforms
1980// Returns true if succesful (uniform not already defined)
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001981bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const D3DConstant *constant, const std::string &name,
1982 D3DConstantTable *vsConstantTable, D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001983{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001984 if (constant->registerSet == D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001985 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001986 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001987 {
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00001988 const D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1989 const D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001990
1991 if (psConstant)
1992 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001993 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001994
1995 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1996 {
1997 mSamplersPS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001998 mSamplersPS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001999 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2000 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2001 }
2002 else
2003 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002004 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002005 return false;
2006 }
2007 }
2008
2009 if (vsConstant)
2010 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002011 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002012
2013 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2014 {
2015 mSamplersVS[samplerIndex].active = true;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002016 mSamplersVS[samplerIndex].textureType = (constant->type == D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002017 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2018 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2019 }
2020 else
2021 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002022 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002023 return false;
2024 }
2025 }
2026 }
2027 }
2028
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002029 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002030 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002031 case D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002032 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002033 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002034 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002035 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002036 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002037 const D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002038
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002039 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002040
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002041 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002042 {
2043 return false;
2044 }
2045 }
2046 }
2047
2048 return true;
2049 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002050 case D3DConstant::CLASS_SCALAR:
2051 case D3DConstant::CLASS_VECTOR:
2052 case D3DConstant::CLASS_MATRIX_COLUMNS:
2053 case D3DConstant::CLASS_OBJECT:
2054 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002055 default:
2056 UNREACHABLE();
2057 return false;
2058 }
2059}
2060
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002061bool ProgramBinary::defineUniform(GLenum shader, const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002062{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002063 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002064
2065 if(!uniform)
2066 {
2067 return false;
2068 }
2069
2070 // Check if already defined
2071 GLint location = getUniformLocation(uniform->name);
2072 GLenum type = uniform->type;
2073
2074 if (location >= 0)
2075 {
2076 delete uniform;
2077 uniform = mUniforms[mUniformIndex[location].index];
2078 }
2079
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002080 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2081 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002082
2083 if (location >= 0)
2084 {
2085 return uniform->type == type;
2086 }
2087
2088 mUniforms.push_back(uniform);
2089 unsigned int uniformIndex = mUniforms.size() - 1;
2090
2091 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2092 {
2093 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2094 }
2095
2096 return true;
2097}
2098
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002099Uniform *ProgramBinary::createUniform(const D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002100{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002101 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002102 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002103 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002104 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002105 case D3DConstant::PT_SAMPLER2D:
2106 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002107 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002108 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002109 default: UNREACHABLE();
2110 }
2111 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002112 case D3DConstant::PT_SAMPLERCUBE:
2113 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002114 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002115 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002116 default: UNREACHABLE();
2117 }
2118 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002119 case D3DConstant::PT_BOOL:
2120 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002121 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002122 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2123 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2124 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2125 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002126 default: UNREACHABLE();
2127 }
2128 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002129 case D3DConstant::PT_INT:
2130 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002131 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002132 case 1: return new Uniform(GL_INT, _name, constant->elements);
2133 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2134 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2135 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002136 default: UNREACHABLE();
2137 }
2138 break;
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002139 case D3DConstant::PT_FLOAT:
2140 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002141 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002142 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2143 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2144 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2145 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002146 default: UNREACHABLE();
2147 }
2148 break;
2149 default:
2150 UNREACHABLE();
2151 }
2152 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002153 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002154 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002155 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002156 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002157 case D3DConstant::PT_FLOAT:
2158 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002159 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002160 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2161 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2162 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002163 default: UNREACHABLE();
2164 }
2165 break;
2166 default: UNREACHABLE();
2167 }
2168 }
2169 else UNREACHABLE();
2170
2171 return 0;
2172}
2173
2174// This method needs to match OutputHLSL::decorate
2175std::string ProgramBinary::decorateAttribute(const std::string &name)
2176{
2177 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2178 {
2179 return "_" + name;
2180 }
2181
2182 return name;
2183}
2184
2185std::string ProgramBinary::undecorateUniform(const std::string &_name)
2186{
2187 std::string name = _name;
2188
2189 // Remove any structure field decoration
2190 size_t pos = 0;
2191 while ((pos = name.find("._", pos)) != std::string::npos)
2192 {
2193 name.replace(pos, 2, ".");
2194 }
2195
2196 // Remove the leading decoration
2197 if (name[0] == '_')
2198 {
2199 return name.substr(1);
2200 }
2201 else if (name.compare(0, 3, "ar_") == 0)
2202 {
2203 return name.substr(3);
2204 }
2205
2206 return name;
2207}
2208
2209void ProgramBinary::applyUniformnbv(Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
2210{
2211 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2212 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2213
2214 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2215 {
2216 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2217 for (int i = 0; i < count; i++)
2218 {
2219 for (int j = 0; j < 4; j++)
2220 {
2221 if (j < width)
2222 {
2223 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2224 }
2225 else
2226 {
2227 vector[i * 4 + j] = 0.0f;
2228 }
2229 }
2230 }
2231 }
2232
2233 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2234 {
2235 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2236 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2237 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2238 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2239 for (int i = 0; i < copyCount; i++)
2240 {
2241 boolVector[i] = v[i] != GL_FALSE;
2242 }
2243 }
2244
2245 if (targetUniform->ps.float4Index >= 0)
2246 {
2247 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
2248 }
2249
2250 if (targetUniform->ps.boolIndex >= 0)
2251 {
2252 mDevice->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
2253 }
2254
2255 if (targetUniform->vs.float4Index >= 0)
2256 {
2257 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
2258 }
2259
2260 if (targetUniform->vs.boolIndex >= 0)
2261 {
2262 mDevice->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
2263 }
2264}
2265
2266bool ProgramBinary::applyUniformnfv(Uniform *targetUniform, const GLfloat *v)
2267{
2268 if (targetUniform->ps.registerCount)
2269 {
2270 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
2271 }
2272
2273 if (targetUniform->vs.registerCount)
2274 {
2275 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
2276 }
2277
2278 return true;
2279}
2280
2281bool ProgramBinary::applyUniform1iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2282{
2283 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002284 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002285
2286 for (int i = 0; i < count; i++)
2287 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002288 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002289 }
2290
2291 if (targetUniform->ps.registerCount)
2292 {
2293 if (targetUniform->ps.samplerIndex >= 0)
2294 {
2295 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2296
2297 for (int i = 0; i < count; i++)
2298 {
2299 unsigned int samplerIndex = firstIndex + i;
2300
2301 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2302 {
2303 ASSERT(mSamplersPS[samplerIndex].active);
2304 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2305 }
2306 }
2307 }
2308 else
2309 {
2310 ASSERT(targetUniform->ps.float4Index >= 0);
2311 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
2312 }
2313 }
2314
2315 if (targetUniform->vs.registerCount)
2316 {
2317 if (targetUniform->vs.samplerIndex >= 0)
2318 {
2319 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2320
2321 for (int i = 0; i < count; i++)
2322 {
2323 unsigned int samplerIndex = firstIndex + i;
2324
2325 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2326 {
2327 ASSERT(mSamplersVS[samplerIndex].active);
2328 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2329 }
2330 }
2331 }
2332 else
2333 {
2334 ASSERT(targetUniform->vs.float4Index >= 0);
2335 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2336 }
2337 }
2338
2339 return true;
2340}
2341
2342bool ProgramBinary::applyUniform2iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2343{
2344 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002345 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002346
2347 for (int i = 0; i < count; i++)
2348 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002349 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002350
2351 v += 2;
2352 }
2353
2354 applyUniformniv(targetUniform, count, vector);
2355
2356 return true;
2357}
2358
2359bool ProgramBinary::applyUniform3iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2360{
2361 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002362 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002363
2364 for (int i = 0; i < count; i++)
2365 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002366 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002367
2368 v += 3;
2369 }
2370
2371 applyUniformniv(targetUniform, count, vector);
2372
2373 return true;
2374}
2375
2376bool ProgramBinary::applyUniform4iv(Uniform *targetUniform, GLsizei count, const GLint *v)
2377{
2378 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002379 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002380
2381 for (int i = 0; i < count; i++)
2382 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002383 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002384
2385 v += 4;
2386 }
2387
2388 applyUniformniv(targetUniform, count, vector);
2389
2390 return true;
2391}
2392
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002393void ProgramBinary::applyUniformniv(Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002394{
2395 if (targetUniform->ps.registerCount)
2396 {
2397 ASSERT(targetUniform->ps.float4Index >= 0);
2398 mDevice->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
2399 }
2400
2401 if (targetUniform->vs.registerCount)
2402 {
2403 ASSERT(targetUniform->vs.float4Index >= 0);
2404 mDevice->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
2405 }
2406}
2407
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002408bool ProgramBinary::isValidated() const
2409{
2410 return mValidated;
2411}
2412
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002413void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2414{
2415 // Skip over inactive attributes
2416 unsigned int activeAttribute = 0;
2417 unsigned int attribute;
2418 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2419 {
2420 if (mLinkedAttribute[attribute].name.empty())
2421 {
2422 continue;
2423 }
2424
2425 if (activeAttribute == index)
2426 {
2427 break;
2428 }
2429
2430 activeAttribute++;
2431 }
2432
2433 if (bufsize > 0)
2434 {
2435 const char *string = mLinkedAttribute[attribute].name.c_str();
2436
2437 strncpy(name, string, bufsize);
2438 name[bufsize - 1] = '\0';
2439
2440 if (length)
2441 {
2442 *length = strlen(name);
2443 }
2444 }
2445
2446 *size = 1; // Always a single 'type' instance
2447
2448 *type = mLinkedAttribute[attribute].type;
2449}
2450
2451GLint ProgramBinary::getActiveAttributeCount()
2452{
2453 int count = 0;
2454
2455 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2456 {
2457 if (!mLinkedAttribute[attributeIndex].name.empty())
2458 {
2459 count++;
2460 }
2461 }
2462
2463 return count;
2464}
2465
2466GLint ProgramBinary::getActiveAttributeMaxLength()
2467{
2468 int maxLength = 0;
2469
2470 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2471 {
2472 if (!mLinkedAttribute[attributeIndex].name.empty())
2473 {
2474 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2475 }
2476 }
2477
2478 return maxLength;
2479}
2480
2481void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2482{
2483 // Skip over internal uniforms
2484 unsigned int activeUniform = 0;
2485 unsigned int uniform;
2486 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2487 {
2488 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2489 {
2490 continue;
2491 }
2492
2493 if (activeUniform == index)
2494 {
2495 break;
2496 }
2497
2498 activeUniform++;
2499 }
2500
2501 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2502
2503 if (bufsize > 0)
2504 {
2505 std::string string = mUniforms[uniform]->name;
2506
2507 if (mUniforms[uniform]->isArray())
2508 {
2509 string += "[0]";
2510 }
2511
2512 strncpy(name, string.c_str(), bufsize);
2513 name[bufsize - 1] = '\0';
2514
2515 if (length)
2516 {
2517 *length = strlen(name);
2518 }
2519 }
2520
2521 *size = mUniforms[uniform]->arraySize;
2522
2523 *type = mUniforms[uniform]->type;
2524}
2525
2526GLint ProgramBinary::getActiveUniformCount()
2527{
2528 int count = 0;
2529
2530 unsigned int numUniforms = mUniforms.size();
2531 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2532 {
2533 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2534 {
2535 count++;
2536 }
2537 }
2538
2539 return count;
2540}
2541
2542GLint ProgramBinary::getActiveUniformMaxLength()
2543{
2544 int maxLength = 0;
2545
2546 unsigned int numUniforms = mUniforms.size();
2547 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2548 {
2549 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2550 {
2551 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2552 if (mUniforms[uniformIndex]->isArray())
2553 {
2554 length += 3; // Counting in "[0]".
2555 }
2556 maxLength = std::max(length, maxLength);
2557 }
2558 }
2559
2560 return maxLength;
2561}
2562
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002563void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002564{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002565 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002566 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002567 {
2568 mValidated = false;
2569 }
2570 else
2571 {
2572 mValidated = true;
2573 }
2574}
2575
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002576bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002577{
2578 // if any two active samplers in a program are of different types, but refer to the same
2579 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2580 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2581
2582 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2583 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2584
2585 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2586 {
2587 textureUnitType[i] = TEXTURE_UNKNOWN;
2588 }
2589
2590 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2591 {
2592 if (mSamplersPS[i].active)
2593 {
2594 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2595
2596 if (unit >= maxCombinedTextureImageUnits)
2597 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002598 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002599 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002600 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002601 }
2602
2603 return false;
2604 }
2605
2606 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2607 {
2608 if (mSamplersPS[i].textureType != textureUnitType[unit])
2609 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002610 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002611 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002612 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002613 }
2614
2615 return false;
2616 }
2617 }
2618 else
2619 {
2620 textureUnitType[unit] = mSamplersPS[i].textureType;
2621 }
2622 }
2623 }
2624
2625 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2626 {
2627 if (mSamplersVS[i].active)
2628 {
2629 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2630
2631 if (unit >= maxCombinedTextureImageUnits)
2632 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002633 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002634 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002635 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002636 }
2637
2638 return false;
2639 }
2640
2641 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2642 {
2643 if (mSamplersVS[i].textureType != textureUnitType[unit])
2644 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002645 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002646 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002647 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002648 }
2649
2650 return false;
2651 }
2652 }
2653 else
2654 {
2655 textureUnitType[unit] = mSamplersVS[i].textureType;
2656 }
2657 }
2658 }
2659
2660 return true;
2661}
2662
2663GLint ProgramBinary::getDxDepthRangeLocation() const
2664{
2665 return mDxDepthRangeLocation;
2666}
2667
2668GLint ProgramBinary::getDxDepthLocation() const
2669{
2670 return mDxDepthLocation;
2671}
2672
2673GLint ProgramBinary::getDxCoordLocation() const
2674{
2675 return mDxCoordLocation;
2676}
2677
2678GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2679{
2680 return mDxHalfPixelSizeLocation;
2681}
2682
2683GLint ProgramBinary::getDxFrontCCWLocation() const
2684{
2685 return mDxFrontCCWLocation;
2686}
2687
2688GLint ProgramBinary::getDxPointsOrLinesLocation() const
2689{
2690 return mDxPointsOrLinesLocation;
2691}
2692
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002693ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2694{
2695}
2696
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002697}