blob: 1a3695b6558dc6d01971a7cdaf526cb826a91302 [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
2// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000010#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000011#include "libGLESv2/ProgramBinary.h"
12
13#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000014#include "common/version.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/Shader.h"
18#include "libGLESv2/utilities.h"
19
20#include <string>
21
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000022namespace gl
23{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000024std::string str(int i)
25{
26 char buffer[20];
27 snprintf(buffer, sizeof(buffer), "%d", i);
28 return buffer;
29}
30
31Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
32 : type(type), _name(_name), name(ProgramBinary::undecorateUniform(_name)), arraySize(arraySize)
33{
34 int bytes = UniformInternalSize(type) * arraySize;
35 data = new unsigned char[bytes];
36 memset(data, 0, bytes);
37 dirty = true;
38}
39
40Uniform::~Uniform()
41{
42 delete[] data;
43}
44
45bool Uniform::isArray()
46{
daniel@transgaming.com22ba0f72012-09-27 17:46:04 +000047 size_t dot = _name.find_last_of('.');
48 if (dot == std::string::npos) dot = -1;
49
50 return _name.compare(dot + 1, dot + 4, "ar_") == 0;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000051}
52
53UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
54 : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index)
55{
56}
57
daniel@transgaming.come87ca002012-07-24 18:30:43 +000058unsigned int ProgramBinary::mCurrentSerial = 1;
59
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000060ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000061{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000062 mPixelExecutable = NULL;
63 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000064
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000065 mValidated = false;
66
67 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
68 {
69 mSemanticIndex[index] = -1;
70 }
71
72 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
73 {
74 mSamplersPS[index].active = false;
75 }
76
77 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
78 {
79 mSamplersVS[index].active = false;
80 }
81
82 mUsedVertexSamplerRange = 0;
83 mUsedPixelSamplerRange = 0;
84
85 mDxDepthRangeLocation = -1;
daniel@transgaming.com12985182012-12-20 20:56:31 +000086 mDxDepthFrontLocation = -1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000087 mDxCoordLocation = -1;
88 mDxHalfPixelSizeLocation = -1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000089}
90
91ProgramBinary::~ProgramBinary()
92{
daniel@transgaming.com95892412012-11-28 20:59:09 +000093 delete mPixelExecutable;
94 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000095
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000096 while (!mUniforms.empty())
97 {
98 delete mUniforms.back();
99 mUniforms.pop_back();
100 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000101}
102
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000103unsigned int ProgramBinary::getSerial() const
104{
105 return mSerial;
106}
107
108unsigned int ProgramBinary::issueSerial()
109{
110 return mCurrentSerial++;
111}
112
daniel@transgaming.com95892412012-11-28 20:59:09 +0000113rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000114{
115 return mPixelExecutable;
116}
117
daniel@transgaming.com95892412012-11-28 20:59:09 +0000118rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000119{
120 return mVertexExecutable;
121}
122
123GLuint ProgramBinary::getAttributeLocation(const char *name)
124{
125 if (name)
126 {
127 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
128 {
129 if (mLinkedAttribute[index].name == std::string(name))
130 {
131 return index;
132 }
133 }
134 }
135
136 return -1;
137}
138
139int ProgramBinary::getSemanticIndex(int attributeIndex)
140{
141 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
142
143 return mSemanticIndex[attributeIndex];
144}
145
146// Returns one more than the highest sampler index used.
147GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
148{
149 switch (type)
150 {
151 case SAMPLER_PIXEL:
152 return mUsedPixelSamplerRange;
153 case SAMPLER_VERTEX:
154 return mUsedVertexSamplerRange;
155 default:
156 UNREACHABLE();
157 return 0;
158 }
159}
160
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000161bool ProgramBinary::usesPointSize() const
162{
163 return mUsesPointSize;
164}
165
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000166// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
167// index (0-15 for the pixel shader and 0-3 for the vertex shader).
168GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
169{
170 GLint logicalTextureUnit = -1;
171
172 switch (type)
173 {
174 case SAMPLER_PIXEL:
175 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
176
177 if (mSamplersPS[samplerIndex].active)
178 {
179 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
180 }
181 break;
182 case SAMPLER_VERTEX:
183 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
184
185 if (mSamplersVS[samplerIndex].active)
186 {
187 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
188 }
189 break;
190 default: UNREACHABLE();
191 }
192
193 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
194 {
195 return logicalTextureUnit;
196 }
197
198 return -1;
199}
200
201// Returns the texture type for a given Direct3D 9 sampler type and
202// index (0-15 for the pixel shader and 0-3 for the vertex shader).
203TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
204{
205 switch (type)
206 {
207 case SAMPLER_PIXEL:
208 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
209 ASSERT(mSamplersPS[samplerIndex].active);
210 return mSamplersPS[samplerIndex].textureType;
211 case SAMPLER_VERTEX:
212 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
213 ASSERT(mSamplersVS[samplerIndex].active);
214 return mSamplersVS[samplerIndex].textureType;
215 default: UNREACHABLE();
216 }
217
218 return TEXTURE_2D;
219}
220
221GLint ProgramBinary::getUniformLocation(std::string name)
222{
223 unsigned int subscript = 0;
224
225 // Strip any trailing array operator and retrieve the subscript
226 size_t open = name.find_last_of('[');
227 size_t close = name.find_last_of(']');
228 if (open != std::string::npos && close == name.length() - 1)
229 {
230 subscript = atoi(name.substr(open + 1).c_str());
231 name.erase(open);
232 }
233
234 unsigned int numUniforms = mUniformIndex.size();
235 for (unsigned int location = 0; location < numUniforms; location++)
236 {
237 if (mUniformIndex[location].name == name &&
238 mUniformIndex[location].element == subscript)
239 {
240 return location;
241 }
242 }
243
244 return -1;
245}
246
247bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
248{
249 if (location < 0 || location >= (int)mUniformIndex.size())
250 {
251 return false;
252 }
253
254 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
255 targetUniform->dirty = true;
256
257 if (targetUniform->type == GL_FLOAT)
258 {
259 int arraySize = targetUniform->arraySize;
260
261 if (arraySize == 1 && count > 1)
262 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
263
264 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
265
266 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
267
268 for (int i = 0; i < count; i++)
269 {
270 target[0] = v[0];
271 target[1] = 0;
272 target[2] = 0;
273 target[3] = 0;
274 target += 4;
275 v += 1;
276 }
277 }
278 else if (targetUniform->type == GL_BOOL)
279 {
280 int arraySize = targetUniform->arraySize;
281
282 if (arraySize == 1 && count > 1)
283 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
284
285 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
286 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
287
288 for (int i = 0; i < count; ++i)
289 {
290 if (v[i] == 0.0f)
291 {
292 boolParams[i] = GL_FALSE;
293 }
294 else
295 {
296 boolParams[i] = GL_TRUE;
297 }
298 }
299 }
300 else
301 {
302 return false;
303 }
304
305 return true;
306}
307
308bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
309{
310 if (location < 0 || location >= (int)mUniformIndex.size())
311 {
312 return false;
313 }
314
315 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
316 targetUniform->dirty = true;
317
318 if (targetUniform->type == GL_FLOAT_VEC2)
319 {
320 int arraySize = targetUniform->arraySize;
321
322 if (arraySize == 1 && count > 1)
323 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
324
325 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
326
327 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
328
329 for (int i = 0; i < count; i++)
330 {
331 target[0] = v[0];
332 target[1] = v[1];
333 target[2] = 0;
334 target[3] = 0;
335 target += 4;
336 v += 2;
337 }
338 }
339 else if (targetUniform->type == GL_BOOL_VEC2)
340 {
341 int arraySize = targetUniform->arraySize;
342
343 if (arraySize == 1 && count > 1)
344 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
345
346 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
347
348 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
349
350 for (int i = 0; i < count * 2; ++i)
351 {
352 if (v[i] == 0.0f)
353 {
354 boolParams[i] = GL_FALSE;
355 }
356 else
357 {
358 boolParams[i] = GL_TRUE;
359 }
360 }
361 }
362 else
363 {
364 return false;
365 }
366
367 return true;
368}
369
370bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
371{
372 if (location < 0 || location >= (int)mUniformIndex.size())
373 {
374 return false;
375 }
376
377 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
378 targetUniform->dirty = true;
379
380 if (targetUniform->type == GL_FLOAT_VEC3)
381 {
382 int arraySize = targetUniform->arraySize;
383
384 if (arraySize == 1 && count > 1)
385 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
386
387 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
388
389 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
390
391 for (int i = 0; i < count; i++)
392 {
393 target[0] = v[0];
394 target[1] = v[1];
395 target[2] = v[2];
396 target[3] = 0;
397 target += 4;
398 v += 3;
399 }
400 }
401 else if (targetUniform->type == GL_BOOL_VEC3)
402 {
403 int arraySize = targetUniform->arraySize;
404
405 if (arraySize == 1 && count > 1)
406 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
407
408 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
409 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
410
411 for (int i = 0; i < count * 3; ++i)
412 {
413 if (v[i] == 0.0f)
414 {
415 boolParams[i] = GL_FALSE;
416 }
417 else
418 {
419 boolParams[i] = GL_TRUE;
420 }
421 }
422 }
423 else
424 {
425 return false;
426 }
427
428 return true;
429}
430
431bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
432{
433 if (location < 0 || location >= (int)mUniformIndex.size())
434 {
435 return false;
436 }
437
438 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
439 targetUniform->dirty = true;
440
441 if (targetUniform->type == GL_FLOAT_VEC4)
442 {
443 int arraySize = targetUniform->arraySize;
444
445 if (arraySize == 1 && count > 1)
446 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
447
448 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
449
450 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
451 v, 4 * sizeof(GLfloat) * count);
452 }
453 else if (targetUniform->type == GL_BOOL_VEC4)
454 {
455 int arraySize = targetUniform->arraySize;
456
457 if (arraySize == 1 && count > 1)
458 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
459
460 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
461 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
462
463 for (int i = 0; i < count * 4; ++i)
464 {
465 if (v[i] == 0.0f)
466 {
467 boolParams[i] = GL_FALSE;
468 }
469 else
470 {
471 boolParams[i] = GL_TRUE;
472 }
473 }
474 }
475 else
476 {
477 return false;
478 }
479
480 return true;
481}
482
483template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
484void transposeMatrix(T *target, const GLfloat *value)
485{
486 int copyWidth = std::min(targetWidth, srcWidth);
487 int copyHeight = std::min(targetHeight, srcHeight);
488
489 for (int x = 0; x < copyWidth; x++)
490 {
491 for (int y = 0; y < copyHeight; y++)
492 {
493 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
494 }
495 }
496 // clear unfilled right side
497 for (int y = 0; y < copyHeight; y++)
498 {
499 for (int x = srcWidth; x < targetWidth; x++)
500 {
501 target[y * targetWidth + x] = (T)0;
502 }
503 }
504 // clear unfilled bottom.
505 for (int y = srcHeight; y < targetHeight; y++)
506 {
507 for (int x = 0; x < targetWidth; x++)
508 {
509 target[y * targetWidth + x] = (T)0;
510 }
511 }
512}
513
514bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
515{
516 if (location < 0 || location >= (int)mUniformIndex.size())
517 {
518 return false;
519 }
520
521 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
522 targetUniform->dirty = true;
523
524 if (targetUniform->type != GL_FLOAT_MAT2)
525 {
526 return false;
527 }
528
529 int arraySize = targetUniform->arraySize;
530
531 if (arraySize == 1 && count > 1)
532 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
533
534 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
535
536 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
537 for (int i = 0; i < count; i++)
538 {
539 transposeMatrix<GLfloat,4,2,2,2>(target, value);
540 target += 8;
541 value += 4;
542 }
543
544 return true;
545}
546
547bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
548{
549 if (location < 0 || location >= (int)mUniformIndex.size())
550 {
551 return false;
552 }
553
554 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
555 targetUniform->dirty = true;
556
557 if (targetUniform->type != GL_FLOAT_MAT3)
558 {
559 return false;
560 }
561
562 int arraySize = targetUniform->arraySize;
563
564 if (arraySize == 1 && count > 1)
565 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
566
567 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
568
569 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
570 for (int i = 0; i < count; i++)
571 {
572 transposeMatrix<GLfloat,4,3,3,3>(target, value);
573 target += 12;
574 value += 9;
575 }
576
577 return true;
578}
579
580
581bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
582{
583 if (location < 0 || location >= (int)mUniformIndex.size())
584 {
585 return false;
586 }
587
588 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
589 targetUniform->dirty = true;
590
591 if (targetUniform->type != GL_FLOAT_MAT4)
592 {
593 return false;
594 }
595
596 int arraySize = targetUniform->arraySize;
597
598 if (arraySize == 1 && count > 1)
599 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
600
601 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
602
603 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
604 for (int i = 0; i < count; i++)
605 {
606 transposeMatrix<GLfloat,4,4,4,4>(target, value);
607 target += 16;
608 value += 16;
609 }
610
611 return true;
612}
613
614bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
615{
616 if (location < 0 || location >= (int)mUniformIndex.size())
617 {
618 return false;
619 }
620
621 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
622 targetUniform->dirty = true;
623
624 if (targetUniform->type == GL_INT ||
625 targetUniform->type == GL_SAMPLER_2D ||
626 targetUniform->type == GL_SAMPLER_CUBE)
627 {
628 int arraySize = targetUniform->arraySize;
629
630 if (arraySize == 1 && count > 1)
631 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
632
633 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
634
635 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
636 v, sizeof(GLint) * count);
637 }
638 else if (targetUniform->type == GL_BOOL)
639 {
640 int arraySize = targetUniform->arraySize;
641
642 if (arraySize == 1 && count > 1)
643 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
644
645 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
646 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
647
648 for (int i = 0; i < count; ++i)
649 {
650 if (v[i] == 0)
651 {
652 boolParams[i] = GL_FALSE;
653 }
654 else
655 {
656 boolParams[i] = GL_TRUE;
657 }
658 }
659 }
660 else
661 {
662 return false;
663 }
664
665 return true;
666}
667
668bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
669{
670 if (location < 0 || location >= (int)mUniformIndex.size())
671 {
672 return false;
673 }
674
675 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
676 targetUniform->dirty = true;
677
678 if (targetUniform->type == GL_INT_VEC2)
679 {
680 int arraySize = targetUniform->arraySize;
681
682 if (arraySize == 1 && count > 1)
683 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
684
685 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
686
687 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
688 v, 2 * sizeof(GLint) * count);
689 }
690 else if (targetUniform->type == GL_BOOL_VEC2)
691 {
692 int arraySize = targetUniform->arraySize;
693
694 if (arraySize == 1 && count > 1)
695 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
696
697 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
698 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
699
700 for (int i = 0; i < count * 2; ++i)
701 {
702 if (v[i] == 0)
703 {
704 boolParams[i] = GL_FALSE;
705 }
706 else
707 {
708 boolParams[i] = GL_TRUE;
709 }
710 }
711 }
712 else
713 {
714 return false;
715 }
716
717 return true;
718}
719
720bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
721{
722 if (location < 0 || location >= (int)mUniformIndex.size())
723 {
724 return false;
725 }
726
727 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
728 targetUniform->dirty = true;
729
730 if (targetUniform->type == GL_INT_VEC3)
731 {
732 int arraySize = targetUniform->arraySize;
733
734 if (arraySize == 1 && count > 1)
735 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
736
737 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
738
739 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
740 v, 3 * sizeof(GLint) * count);
741 }
742 else if (targetUniform->type == GL_BOOL_VEC3)
743 {
744 int arraySize = targetUniform->arraySize;
745
746 if (arraySize == 1 && count > 1)
747 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
748
749 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
750 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
751
752 for (int i = 0; i < count * 3; ++i)
753 {
754 if (v[i] == 0)
755 {
756 boolParams[i] = GL_FALSE;
757 }
758 else
759 {
760 boolParams[i] = GL_TRUE;
761 }
762 }
763 }
764 else
765 {
766 return false;
767 }
768
769 return true;
770}
771
772bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
773{
774 if (location < 0 || location >= (int)mUniformIndex.size())
775 {
776 return false;
777 }
778
779 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
780 targetUniform->dirty = true;
781
782 if (targetUniform->type == GL_INT_VEC4)
783 {
784 int arraySize = targetUniform->arraySize;
785
786 if (arraySize == 1 && count > 1)
787 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
788
789 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
790
791 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
792 v, 4 * sizeof(GLint) * count);
793 }
794 else if (targetUniform->type == GL_BOOL_VEC4)
795 {
796 int arraySize = targetUniform->arraySize;
797
798 if (arraySize == 1 && count > 1)
799 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
800
801 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
802 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
803
804 for (int i = 0; i < count * 4; ++i)
805 {
806 if (v[i] == 0)
807 {
808 boolParams[i] = GL_FALSE;
809 }
810 else
811 {
812 boolParams[i] = GL_TRUE;
813 }
814 }
815 }
816 else
817 {
818 return false;
819 }
820
821 return true;
822}
823
824bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
825{
826 if (location < 0 || location >= (int)mUniformIndex.size())
827 {
828 return false;
829 }
830
831 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
832
833 // sized queries -- ensure the provided buffer is large enough
834 if (bufSize)
835 {
836 int requiredBytes = UniformExternalSize(targetUniform->type);
837 if (*bufSize < requiredBytes)
838 {
839 return false;
840 }
841 }
842
843 switch (targetUniform->type)
844 {
845 case GL_FLOAT_MAT2:
846 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
847 break;
848 case GL_FLOAT_MAT3:
849 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
850 break;
851 case GL_FLOAT_MAT4:
852 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
853 break;
854 default:
855 {
856 unsigned int count = UniformExternalComponentCount(targetUniform->type);
857 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
858
859 switch (UniformComponentType(targetUniform->type))
860 {
861 case GL_BOOL:
862 {
863 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
864
865 for (unsigned int i = 0; i < count; ++i)
866 {
867 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
868 }
869 }
870 break;
871 case GL_FLOAT:
872 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
873 count * sizeof(GLfloat));
874 break;
875 case GL_INT:
876 {
877 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
878
879 for (unsigned int i = 0; i < count; ++i)
880 {
881 params[i] = (float)intParams[i];
882 }
883 }
884 break;
885 default: UNREACHABLE();
886 }
887 }
888 }
889
890 return true;
891}
892
893bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
894{
895 if (location < 0 || location >= (int)mUniformIndex.size())
896 {
897 return false;
898 }
899
900 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
901
902 // sized queries -- ensure the provided buffer is large enough
903 if (bufSize)
904 {
905 int requiredBytes = UniformExternalSize(targetUniform->type);
906 if (*bufSize < requiredBytes)
907 {
908 return false;
909 }
910 }
911
912 switch (targetUniform->type)
913 {
914 case GL_FLOAT_MAT2:
915 {
916 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
917 }
918 break;
919 case GL_FLOAT_MAT3:
920 {
921 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
922 }
923 break;
924 case GL_FLOAT_MAT4:
925 {
926 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
927 }
928 break;
929 default:
930 {
931 unsigned int count = UniformExternalComponentCount(targetUniform->type);
932 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
933
934 switch (UniformComponentType(targetUniform->type))
935 {
936 case GL_BOOL:
937 {
938 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
939
940 for (unsigned int i = 0; i < count; ++i)
941 {
942 params[i] = (GLint)boolParams[i];
943 }
944 }
945 break;
946 case GL_FLOAT:
947 {
948 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
949
950 for (unsigned int i = 0; i < count; ++i)
951 {
952 params[i] = (GLint)floatParams[i];
953 }
954 }
955 break;
956 case GL_INT:
957 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
958 count * sizeof(GLint));
959 break;
960 default: UNREACHABLE();
961 }
962 }
963 }
964
965 return true;
966}
967
968void ProgramBinary::dirtyAllUniforms()
969{
970 unsigned int numUniforms = mUniforms.size();
971 for (unsigned int index = 0; index < numUniforms; index++)
972 {
973 mUniforms[index]->dirty = true;
974 }
975}
976
977// Applies all the uniforms set for this program object to the Direct3D 9 device
978void ProgramBinary::applyUniforms()
979{
daniel@transgaming.com77fbf972012-11-28 21:02:55 +0000980 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) // D3D9_REPLACE
981 {
982 return; // UNIMPLEMENTED
983 }
984
985 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
986
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000987 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) {
988 Uniform *targetUniform = *ub;
989
990 if (targetUniform->dirty)
991 {
992 int arraySize = targetUniform->arraySize;
993 GLfloat *f = (GLfloat*)targetUniform->data;
994 GLint *i = (GLint*)targetUniform->data;
995 GLboolean *b = (GLboolean*)targetUniform->data;
996
997 switch (targetUniform->type)
998 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +0000999 case GL_BOOL: applyUniformnbv(device, targetUniform, arraySize, 1, b); break;
1000 case GL_BOOL_VEC2: applyUniformnbv(device, targetUniform, arraySize, 2, b); break;
1001 case GL_BOOL_VEC3: applyUniformnbv(device, targetUniform, arraySize, 3, b); break;
1002 case GL_BOOL_VEC4: applyUniformnbv(device, targetUniform, arraySize, 4, b); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001003 case GL_FLOAT:
1004 case GL_FLOAT_VEC2:
1005 case GL_FLOAT_VEC3:
1006 case GL_FLOAT_VEC4:
1007 case GL_FLOAT_MAT2:
1008 case GL_FLOAT_MAT3:
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001009 case GL_FLOAT_MAT4: applyUniformnfv(device, targetUniform, f); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001010 case GL_SAMPLER_2D:
1011 case GL_SAMPLER_CUBE:
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001012 case GL_INT: applyUniform1iv(device, targetUniform, arraySize, i); break;
1013 case GL_INT_VEC2: applyUniform2iv(device, targetUniform, arraySize, i); break;
1014 case GL_INT_VEC3: applyUniform3iv(device, targetUniform, arraySize, i); break;
1015 case GL_INT_VEC4: applyUniform4iv(device, targetUniform, arraySize, i); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001016 default:
1017 UNREACHABLE();
1018 }
1019
1020 targetUniform->dirty = false;
1021 }
1022 }
1023}
1024
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001025// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1026// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001027int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001028{
1029 Context *context = getContext();
1030 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1031
1032 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1033 {
1034 int n = VariableRowCount(varying->type) * varying->size;
1035 int m = VariableColumnCount(varying->type);
1036 bool success = false;
1037
1038 if (m == 2 || m == 3 || m == 4)
1039 {
1040 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1041 {
1042 bool available = true;
1043
1044 for (int y = 0; y < n && available; y++)
1045 {
1046 for (int x = 0; x < m && available; x++)
1047 {
1048 if (packing[r + y][x])
1049 {
1050 available = false;
1051 }
1052 }
1053 }
1054
1055 if (available)
1056 {
1057 varying->reg = r;
1058 varying->col = 0;
1059
1060 for (int y = 0; y < n; y++)
1061 {
1062 for (int x = 0; x < m; x++)
1063 {
1064 packing[r + y][x] = &*varying;
1065 }
1066 }
1067
1068 success = true;
1069 }
1070 }
1071
1072 if (!success && m == 2)
1073 {
1074 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1075 {
1076 bool available = true;
1077
1078 for (int y = 0; y < n && available; y++)
1079 {
1080 for (int x = 2; x < 4 && available; x++)
1081 {
1082 if (packing[r + y][x])
1083 {
1084 available = false;
1085 }
1086 }
1087 }
1088
1089 if (available)
1090 {
1091 varying->reg = r;
1092 varying->col = 2;
1093
1094 for (int y = 0; y < n; y++)
1095 {
1096 for (int x = 2; x < 4; x++)
1097 {
1098 packing[r + y][x] = &*varying;
1099 }
1100 }
1101
1102 success = true;
1103 }
1104 }
1105 }
1106 }
1107 else if (m == 1)
1108 {
1109 int space[4] = {0};
1110
1111 for (int y = 0; y < maxVaryingVectors; y++)
1112 {
1113 for (int x = 0; x < 4; x++)
1114 {
1115 space[x] += packing[y][x] ? 0 : 1;
1116 }
1117 }
1118
1119 int column = 0;
1120
1121 for (int x = 0; x < 4; x++)
1122 {
1123 if (space[x] >= n && space[x] < space[column])
1124 {
1125 column = x;
1126 }
1127 }
1128
1129 if (space[column] >= n)
1130 {
1131 for (int r = 0; r < maxVaryingVectors; r++)
1132 {
1133 if (!packing[r][column])
1134 {
1135 varying->reg = r;
1136
1137 for (int y = r; y < r + n; y++)
1138 {
1139 packing[y][column] = &*varying;
1140 }
1141
1142 break;
1143 }
1144 }
1145
1146 varying->col = column;
1147
1148 success = true;
1149 }
1150 }
1151 else UNREACHABLE();
1152
1153 if (!success)
1154 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001155 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001156
1157 return -1;
1158 }
1159 }
1160
1161 // Return the number of used registers
1162 int registers = 0;
1163
1164 for (int r = 0; r < maxVaryingVectors; r++)
1165 {
1166 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1167 {
1168 registers++;
1169 }
1170 }
1171
1172 return registers;
1173}
1174
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001175bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001176{
1177 if (pixelHLSL.empty() || vertexHLSL.empty())
1178 {
1179 return false;
1180 }
1181
1182 // Reset the varying register assignments
1183 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1184 {
1185 fragVar->reg = -1;
1186 fragVar->col = -1;
1187 }
1188
1189 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1190 {
1191 vtxVar->reg = -1;
1192 vtxVar->col = -1;
1193 }
1194
1195 // Map the varyings to the register file
1196 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001197 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001198
1199 if (registers < 0)
1200 {
1201 return false;
1202 }
1203
1204 // Write the HLSL input/output declarations
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001205 const bool sm3 = (mRenderer->getMajorShaderModel() >= 3);
1206 const bool sm4 = (mRenderer->getMajorShaderModel() >= 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001207 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001208 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1209
1210 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1211 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001212 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001213
1214 return false;
1215 }
1216
1217 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1218 {
1219 bool matched = false;
1220
1221 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1222 {
1223 if (output->name == input->name)
1224 {
1225 if (output->type != input->type || output->size != input->size)
1226 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001227 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 +00001228
1229 return false;
1230 }
1231
1232 output->reg = input->reg;
1233 output->col = input->col;
1234
1235 matched = true;
1236 break;
1237 }
1238 }
1239
1240 if (!matched)
1241 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001242 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001243
1244 return false;
1245 }
1246 }
1247
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001248 mUsesPointSize = vertexShader->mUsesPointSize;
1249 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001250 std::string targetSemantic = sm4 ? "SV_Target" : "COLOR";
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001251 std::string positionSemantic = sm4 ? "SV_POSITION" : "POSITION";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001252
1253 vertexHLSL += "struct VS_INPUT\n"
1254 "{\n";
1255
1256 int semanticIndex = 0;
1257 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1258 {
1259 switch (attribute->type)
1260 {
1261 case GL_FLOAT: vertexHLSL += " float "; break;
1262 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1263 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1264 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1265 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1266 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1267 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1268 default: UNREACHABLE();
1269 }
1270
1271 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1272
1273 semanticIndex += VariableRowCount(attribute->type);
1274 }
1275
1276 vertexHLSL += "};\n"
1277 "\n"
1278 "struct VS_OUTPUT\n"
1279 "{\n"
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001280 " float4 gl_Position : " + positionSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001281
1282 for (int r = 0; r < registers; r++)
1283 {
1284 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1285
1286 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1287 }
1288
1289 if (fragmentShader->mUsesFragCoord)
1290 {
1291 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1292 }
1293
1294 if (vertexShader->mUsesPointSize && sm3)
1295 {
1296 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1297 }
1298
1299 vertexHLSL += "};\n"
1300 "\n"
1301 "VS_OUTPUT main(VS_INPUT input)\n"
1302 "{\n";
1303
1304 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1305 {
1306 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1307
1308 if (VariableRowCount(attribute->type) > 1) // Matrix
1309 {
1310 vertexHLSL += "transpose";
1311 }
1312
1313 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1314 }
1315
1316 vertexHLSL += "\n"
1317 " gl_main();\n"
1318 "\n"
1319 " VS_OUTPUT output;\n"
1320 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001321 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001322 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1323 " output.gl_Position.w = gl_Position.w;\n";
1324
1325 if (vertexShader->mUsesPointSize && sm3)
1326 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001327 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001328 }
1329
1330 if (fragmentShader->mUsesFragCoord)
1331 {
1332 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1333 }
1334
1335 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1336 {
1337 if (varying->reg >= 0)
1338 {
1339 for (int i = 0; i < varying->size; i++)
1340 {
1341 int rows = VariableRowCount(varying->type);
1342
1343 for (int j = 0; j < rows; j++)
1344 {
1345 int r = varying->reg + i * rows + j;
1346 vertexHLSL += " output.v" + str(r);
1347
1348 bool sharedRegister = false; // Register used by multiple varyings
1349
1350 for (int x = 0; x < 4; x++)
1351 {
1352 if (packing[r][x] && packing[r][x] != packing[r][0])
1353 {
1354 sharedRegister = true;
1355 break;
1356 }
1357 }
1358
1359 if(sharedRegister)
1360 {
1361 vertexHLSL += ".";
1362
1363 for (int x = 0; x < 4; x++)
1364 {
1365 if (packing[r][x] == &*varying)
1366 {
1367 switch(x)
1368 {
1369 case 0: vertexHLSL += "x"; break;
1370 case 1: vertexHLSL += "y"; break;
1371 case 2: vertexHLSL += "z"; break;
1372 case 3: vertexHLSL += "w"; break;
1373 }
1374 }
1375 }
1376 }
1377
1378 vertexHLSL += " = " + varying->name;
1379
1380 if (varying->array)
1381 {
1382 vertexHLSL += "[" + str(i) + "]";
1383 }
1384
1385 if (rows > 1)
1386 {
1387 vertexHLSL += "[" + str(j) + "]";
1388 }
1389
1390 vertexHLSL += ";\n";
1391 }
1392 }
1393 }
1394 }
1395
1396 vertexHLSL += "\n"
1397 " return output;\n"
1398 "}\n";
1399
1400 pixelHLSL += "struct PS_INPUT\n"
1401 "{\n";
1402
1403 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1404 {
1405 if (varying->reg >= 0)
1406 {
1407 for (int i = 0; i < varying->size; i++)
1408 {
1409 int rows = VariableRowCount(varying->type);
1410 for (int j = 0; j < rows; j++)
1411 {
1412 std::string n = str(varying->reg + i * rows + j);
1413 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1414 }
1415 }
1416 }
1417 else UNREACHABLE();
1418 }
1419
1420 if (fragmentShader->mUsesFragCoord)
1421 {
1422 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1423 if (sm3) {
1424 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1425 }
1426 }
1427
1428 if (fragmentShader->mUsesPointCoord && sm3)
1429 {
1430 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1431 }
1432
1433 if (fragmentShader->mUsesFrontFacing)
1434 {
1435 pixelHLSL += " float vFace : VFACE;\n";
1436 }
1437
1438 pixelHLSL += "};\n"
1439 "\n"
1440 "struct PS_OUTPUT\n"
1441 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001442 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001443 "};\n"
1444 "\n"
1445 "PS_OUTPUT main(PS_INPUT input)\n"
1446 "{\n";
1447
1448 if (fragmentShader->mUsesFragCoord)
1449 {
1450 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1451
1452 if (sm3)
1453 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001454 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001455 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001456 }
1457 else
1458 {
1459 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1460 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001461 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001462 }
1463
daniel@transgaming.com12985182012-12-20 20:56:31 +00001464 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001465 " gl_FragCoord.w = rhw;\n";
1466 }
1467
1468 if (fragmentShader->mUsesPointCoord && sm3)
1469 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001470 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1471 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001472 }
1473
1474 if (fragmentShader->mUsesFrontFacing)
1475 {
daniel@transgaming.com12985182012-12-20 20:56:31 +00001476 pixelHLSL += " gl_FrontFacing = (input.vFace * dx_DepthFront.z >= 0.0);\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001477 }
1478
1479 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1480 {
1481 if (varying->reg >= 0)
1482 {
1483 for (int i = 0; i < varying->size; i++)
1484 {
1485 int rows = VariableRowCount(varying->type);
1486 for (int j = 0; j < rows; j++)
1487 {
1488 std::string n = str(varying->reg + i * rows + j);
1489 pixelHLSL += " " + varying->name;
1490
1491 if (varying->array)
1492 {
1493 pixelHLSL += "[" + str(i) + "]";
1494 }
1495
1496 if (rows > 1)
1497 {
1498 pixelHLSL += "[" + str(j) + "]";
1499 }
1500
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001501 switch (VariableColumnCount(varying->type))
1502 {
1503 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1504 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1505 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1506 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1507 default: UNREACHABLE();
1508 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001509 }
1510 }
1511 }
1512 else UNREACHABLE();
1513 }
1514
1515 pixelHLSL += "\n"
1516 " gl_main();\n"
1517 "\n"
1518 " PS_OUTPUT output;\n"
1519 " output.gl_Color[0] = gl_Color[0];\n"
1520 "\n"
1521 " return output;\n"
1522 "}\n";
1523
1524 return true;
1525}
1526
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001527bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1528{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001529 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001530
1531 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001532 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001533 if (format != GL_PROGRAM_BINARY_ANGLE)
1534 {
1535 infoLog.append("Invalid program binary format.");
1536 return false;
1537 }
1538
1539 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001540 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001541 if (version != BUILD_REVISION)
1542 {
1543 infoLog.append("Invalid program binary version.");
1544 return false;
1545 }
1546
1547 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1548 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001549 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001550 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001551 stream.read(&name);
1552 mLinkedAttribute[i].name = name;
1553 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001554 }
1555
1556 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1557 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001558 stream.read(&mSamplersPS[i].active);
1559 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001560
1561 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001562 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001563 mSamplersPS[i].textureType = (TextureType) textureType;
1564 }
1565
1566 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1567 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001568 stream.read(&mSamplersVS[i].active);
1569 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001570
1571 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001572 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001573 mSamplersVS[i].textureType = (TextureType) textureType;
1574 }
1575
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001576 stream.read(&mUsedVertexSamplerRange);
1577 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001578
1579 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001580 stream.read(&size);
1581 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001582 {
1583 infoLog.append("Invalid program binary.");
1584 return false;
1585 }
1586
1587 mUniforms.resize(size);
1588 for (unsigned int i = 0; i < size; ++i)
1589 {
1590 GLenum type;
1591 std::string _name;
1592 unsigned int arraySize;
1593
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001594 stream.read(&type);
1595 stream.read(&_name);
1596 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001597
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001598 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001599
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001600 stream.read(&mUniforms[i]->ps.float4Index);
1601 stream.read(&mUniforms[i]->ps.samplerIndex);
1602 stream.read(&mUniforms[i]->ps.boolIndex);
1603 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001604
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001605 stream.read(&mUniforms[i]->vs.float4Index);
1606 stream.read(&mUniforms[i]->vs.samplerIndex);
1607 stream.read(&mUniforms[i]->vs.boolIndex);
1608 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001609 }
1610
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001611 stream.read(&size);
1612 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001613 {
1614 infoLog.append("Invalid program binary.");
1615 return false;
1616 }
1617
1618 mUniformIndex.resize(size);
1619 for (unsigned int i = 0; i < size; ++i)
1620 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001621 stream.read(&mUniformIndex[i].name);
1622 stream.read(&mUniformIndex[i].element);
1623 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001624 }
1625
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001626 stream.read(&mDxDepthRangeLocation);
daniel@transgaming.com12985182012-12-20 20:56:31 +00001627 stream.read(&mDxDepthFrontLocation);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001628 stream.read(&mDxCoordLocation);
1629 stream.read(&mDxHalfPixelSizeLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001630
1631 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001632 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001633
1634 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001635 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001636
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001637 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001638
daniel@transgaming.com36038542012-11-28 20:59:26 +00001639 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001640 ptr += sizeof(GUID);
1641
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001642 GUID identifier = mRenderer->getAdapterIdentifier();
1643 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001644 {
1645 infoLog.append("Invalid program binary.");
1646 return false;
1647 }
1648
1649 const char *pixelShaderFunction = ptr;
1650 ptr += pixelShaderSize;
1651
1652 const char *vertexShaderFunction = ptr;
1653 ptr += vertexShaderSize;
1654
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001655 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1656 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1657 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001658 {
1659 infoLog.append("Could not create pixel shader.");
1660 return false;
1661 }
1662
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001663 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1664 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1665 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001666 {
1667 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001668 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001669 mPixelExecutable = NULL;
1670 return false;
1671 }
1672
1673 return true;
1674}
1675
1676bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1677{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001678 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001679
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001680 stream.write(GL_PROGRAM_BINARY_ANGLE);
1681 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001682
1683 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1684 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001685 stream.write(mLinkedAttribute[i].type);
1686 stream.write(mLinkedAttribute[i].name);
1687 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001688 }
1689
1690 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1691 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001692 stream.write(mSamplersPS[i].active);
1693 stream.write(mSamplersPS[i].logicalTextureUnit);
1694 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001695 }
1696
1697 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1698 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001699 stream.write(mSamplersVS[i].active);
1700 stream.write(mSamplersVS[i].logicalTextureUnit);
1701 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001702 }
1703
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001704 stream.write(mUsedVertexSamplerRange);
1705 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001706
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001707 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001708 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1709 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001710 stream.write(mUniforms[i]->type);
1711 stream.write(mUniforms[i]->_name);
1712 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001713
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001714 stream.write(mUniforms[i]->ps.float4Index);
1715 stream.write(mUniforms[i]->ps.samplerIndex);
1716 stream.write(mUniforms[i]->ps.boolIndex);
1717 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001718
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001719 stream.write(mUniforms[i]->vs.float4Index);
1720 stream.write(mUniforms[i]->vs.samplerIndex);
1721 stream.write(mUniforms[i]->vs.boolIndex);
1722 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001723 }
1724
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001725 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001726 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1727 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001728 stream.write(mUniformIndex[i].name);
1729 stream.write(mUniformIndex[i].element);
1730 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001731 }
1732
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001733 stream.write(mDxDepthRangeLocation);
daniel@transgaming.com12985182012-12-20 20:56:31 +00001734 stream.write(mDxDepthFrontLocation);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001735 stream.write(mDxCoordLocation);
1736 stream.write(mDxHalfPixelSizeLocation);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001737
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001738 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001739 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001740
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001741 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001742 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001743
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001744 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001745
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001746 GLsizei streamLength = stream.length();
1747 const void *streamData = stream.data();
1748
1749 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001750 if (totalLength > bufSize)
1751 {
1752 if (length)
1753 {
1754 *length = 0;
1755 }
1756
1757 return false;
1758 }
1759
1760 if (binary)
1761 {
1762 char *ptr = (char*) binary;
1763
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001764 memcpy(ptr, streamData, streamLength);
1765 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001766
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001767 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001768 ptr += sizeof(GUID);
1769
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001770 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001771 ptr += pixelShaderSize;
1772
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001773 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001774 ptr += vertexShaderSize;
1775
1776 ASSERT(ptr - totalLength == binary);
1777 }
1778
1779 if (length)
1780 {
1781 *length = totalLength;
1782 }
1783
1784 return true;
1785}
1786
1787GLint ProgramBinary::getLength()
1788{
1789 GLint length;
1790 if (save(NULL, INT_MAX, &length))
1791 {
1792 return length;
1793 }
1794 else
1795 {
1796 return 0;
1797 }
1798}
1799
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001800bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001801{
1802 if (!fragmentShader || !fragmentShader->isCompiled())
1803 {
1804 return false;
1805 }
1806
1807 if (!vertexShader || !vertexShader->isCompiled())
1808 {
1809 return false;
1810 }
1811
1812 std::string pixelHLSL = fragmentShader->getHLSL();
1813 std::string vertexHLSL = vertexShader->getHLSL();
1814
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001815 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001816 {
1817 return false;
1818 }
1819
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001820 bool success = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001821 rx::D3DConstantTable *constantTableVS = NULL;
1822 rx::D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001823 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1824 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001825
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001826 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001827 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001828 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001829 constantTableVS = mVertexExecutable->getConstantTable();
1830 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001831 }
1832 else
1833 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001834 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001835 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001836
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001837 delete mVertexExecutable;
1838 mVertexExecutable = NULL;
1839 delete mPixelExecutable;
1840 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001841 }
1842
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001843 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1844 {
1845 success = false;
1846 }
1847
1848 if (constantTableVS && constantTablePS)
1849 {
1850 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1851 {
1852 success = false;
1853 }
1854 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001855
1856 // these uniforms are searched as already-decorated because gl_ and dx_
1857 // are reserved prefixes, and do not receive additional decoration
1858 mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
daniel@transgaming.com12985182012-12-20 20:56:31 +00001859 mDxDepthFrontLocation = getUniformLocation("dx_DepthFront");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001860 mDxCoordLocation = getUniformLocation("dx_Coord");
1861 mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001862
1863 Context *context = getContext();
1864 context->markDxUniformsDirty();
1865
1866 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001867}
1868
1869// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001870bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001871{
1872 unsigned int usedLocations = 0;
1873
1874 // Link attributes that have a binding location
1875 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1876 {
1877 int location = attributeBindings.getAttributeBinding(attribute->name);
1878
1879 if (location != -1) // Set by glBindAttribLocation
1880 {
1881 if (!mLinkedAttribute[location].name.empty())
1882 {
1883 // Multiple active attributes bound to the same location; not an error
1884 }
1885
1886 mLinkedAttribute[location] = *attribute;
1887
1888 int rows = VariableRowCount(attribute->type);
1889
1890 if (rows + location > MAX_VERTEX_ATTRIBS)
1891 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001892 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 +00001893
1894 return false;
1895 }
1896
1897 for (int i = 0; i < rows; i++)
1898 {
1899 usedLocations |= 1 << (location + i);
1900 }
1901 }
1902 }
1903
1904 // Link attributes that don't have a binding location
1905 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1906 {
1907 int location = attributeBindings.getAttributeBinding(attribute->name);
1908
1909 if (location == -1) // Not set by glBindAttribLocation
1910 {
1911 int rows = VariableRowCount(attribute->type);
1912 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1913
1914 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1915 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001916 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001917
1918 return false; // Fail to link
1919 }
1920
1921 mLinkedAttribute[availableIndex] = *attribute;
1922 }
1923 }
1924
1925 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1926 {
1927 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1928 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1929
1930 for (int r = 0; r < rows; r++)
1931 {
1932 mSemanticIndex[attributeIndex++] = index++;
1933 }
1934 }
1935
1936 return true;
1937}
1938
daniel@transgaming.com31240482012-11-28 21:06:41 +00001939bool ProgramBinary::linkUniforms(InfoLog &infoLog, rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001940{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001941 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001942 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001943 const rx::D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001944
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001945 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001946 {
1947 return false;
1948 }
1949 }
1950
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001951 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1952 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001953 const rx::D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001954
1955 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1956 {
1957 return false;
1958 }
1959 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001960 return true;
1961}
1962
1963// Adds the description of a constant found in the binary shader to the list of uniforms
1964// Returns true if succesful (uniform not already defined)
daniel@transgaming.com31240482012-11-28 21:06:41 +00001965bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const rx::D3DConstant *constant, const std::string &name,
1966 rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001967{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001968 if (constant->registerSet == rx::D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001969 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001970 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001971 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001972 const rx::D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1973 const rx::D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001974
1975 if (psConstant)
1976 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001977 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001978
1979 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1980 {
1981 mSamplersPS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001982 mSamplersPS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001983 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1984 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1985 }
1986 else
1987 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001988 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001989 return false;
1990 }
1991 }
1992
1993 if (vsConstant)
1994 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001995 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001996
1997 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
1998 {
1999 mSamplersVS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002000 mSamplersVS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002001 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2002 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2003 }
2004 else
2005 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002006 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002007 return false;
2008 }
2009 }
2010 }
2011 }
2012
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002013 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002014 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002015 case rx::D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002016 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002017 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002018 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002019 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002020 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002021 const rx::D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002023 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002024
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002025 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002026 {
2027 return false;
2028 }
2029 }
2030 }
2031
2032 return true;
2033 }
daniel@transgaming.com31240482012-11-28 21:06:41 +00002034 case rx::D3DConstant::CLASS_SCALAR:
2035 case rx::D3DConstant::CLASS_VECTOR:
2036 case rx::D3DConstant::CLASS_MATRIX_COLUMNS:
2037 case rx::D3DConstant::CLASS_OBJECT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002038 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002039 default:
2040 UNREACHABLE();
2041 return false;
2042 }
2043}
2044
daniel@transgaming.com31240482012-11-28 21:06:41 +00002045bool ProgramBinary::defineUniform(GLenum shader, const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002046{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002047 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002048
2049 if(!uniform)
2050 {
2051 return false;
2052 }
2053
2054 // Check if already defined
2055 GLint location = getUniformLocation(uniform->name);
2056 GLenum type = uniform->type;
2057
2058 if (location >= 0)
2059 {
2060 delete uniform;
2061 uniform = mUniforms[mUniformIndex[location].index];
2062 }
2063
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002064 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2065 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002066
2067 if (location >= 0)
2068 {
2069 return uniform->type == type;
2070 }
2071
2072 mUniforms.push_back(uniform);
2073 unsigned int uniformIndex = mUniforms.size() - 1;
2074
2075 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2076 {
2077 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2078 }
2079
2080 return true;
2081}
2082
daniel@transgaming.com31240482012-11-28 21:06:41 +00002083Uniform *ProgramBinary::createUniform(const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002084{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002085 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002086 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002087 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002088 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002089 case rx::D3DConstant::PT_SAMPLER2D:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002090 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002091 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002092 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002093 default: UNREACHABLE();
2094 }
2095 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002096 case rx::D3DConstant::PT_SAMPLERCUBE:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002097 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002098 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002099 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002100 default: UNREACHABLE();
2101 }
2102 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002103 case rx::D3DConstant::PT_BOOL:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002104 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002105 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002106 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2107 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2108 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2109 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002110 default: UNREACHABLE();
2111 }
2112 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002113 case rx::D3DConstant::PT_INT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002114 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002115 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002116 case 1: return new Uniform(GL_INT, _name, constant->elements);
2117 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2118 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2119 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002120 default: UNREACHABLE();
2121 }
2122 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002123 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002124 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002125 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002126 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2127 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2128 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2129 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002130 default: UNREACHABLE();
2131 }
2132 break;
2133 default:
2134 UNREACHABLE();
2135 }
2136 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002137 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002138 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002139 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002140 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002141 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002142 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002143 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002144 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2145 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2146 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002147 default: UNREACHABLE();
2148 }
2149 break;
2150 default: UNREACHABLE();
2151 }
2152 }
2153 else UNREACHABLE();
2154
2155 return 0;
2156}
2157
2158// This method needs to match OutputHLSL::decorate
2159std::string ProgramBinary::decorateAttribute(const std::string &name)
2160{
2161 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2162 {
2163 return "_" + name;
2164 }
2165
2166 return name;
2167}
2168
2169std::string ProgramBinary::undecorateUniform(const std::string &_name)
2170{
2171 std::string name = _name;
2172
2173 // Remove any structure field decoration
2174 size_t pos = 0;
2175 while ((pos = name.find("._", pos)) != std::string::npos)
2176 {
2177 name.replace(pos, 2, ".");
2178 }
2179
2180 // Remove the leading decoration
2181 if (name[0] == '_')
2182 {
2183 return name.substr(1);
2184 }
2185 else if (name.compare(0, 3, "ar_") == 0)
2186 {
2187 return name.substr(3);
2188 }
2189
2190 return name;
2191}
2192
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002193// D3D9_REPLACE begin
2194void ProgramBinary::applyUniformnbv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002195{
2196 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2197 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2198
2199 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2200 {
2201 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2202 for (int i = 0; i < count; i++)
2203 {
2204 for (int j = 0; j < 4; j++)
2205 {
2206 if (j < width)
2207 {
2208 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2209 }
2210 else
2211 {
2212 vector[i * 4 + j] = 0.0f;
2213 }
2214 }
2215 }
2216 }
2217
2218 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2219 {
2220 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2221 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2222 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2223 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2224 for (int i = 0; i < copyCount; i++)
2225 {
2226 boolVector[i] = v[i] != GL_FALSE;
2227 }
2228 }
2229
2230 if (targetUniform->ps.float4Index >= 0)
2231 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002232 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002233 }
2234
2235 if (targetUniform->ps.boolIndex >= 0)
2236 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002237 device->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002238 }
2239
2240 if (targetUniform->vs.float4Index >= 0)
2241 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002242 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002243 }
2244
2245 if (targetUniform->vs.boolIndex >= 0)
2246 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002247 device->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002248 }
2249}
2250
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002251bool ProgramBinary::applyUniformnfv(IDirect3DDevice9 *device, Uniform *targetUniform, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002252{
2253 if (targetUniform->ps.registerCount)
2254 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002255 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002256 }
2257
2258 if (targetUniform->vs.registerCount)
2259 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002260 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002261 }
2262
2263 return true;
2264}
2265
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002266bool ProgramBinary::applyUniform1iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002267{
2268 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002269 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002270
2271 for (int i = 0; i < count; i++)
2272 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002273 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002274 }
2275
2276 if (targetUniform->ps.registerCount)
2277 {
2278 if (targetUniform->ps.samplerIndex >= 0)
2279 {
2280 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2281
2282 for (int i = 0; i < count; i++)
2283 {
2284 unsigned int samplerIndex = firstIndex + i;
2285
2286 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2287 {
2288 ASSERT(mSamplersPS[samplerIndex].active);
2289 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2290 }
2291 }
2292 }
2293 else
2294 {
2295 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002296 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002297 }
2298 }
2299
2300 if (targetUniform->vs.registerCount)
2301 {
2302 if (targetUniform->vs.samplerIndex >= 0)
2303 {
2304 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2305
2306 for (int i = 0; i < count; i++)
2307 {
2308 unsigned int samplerIndex = firstIndex + i;
2309
2310 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2311 {
2312 ASSERT(mSamplersVS[samplerIndex].active);
2313 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2314 }
2315 }
2316 }
2317 else
2318 {
2319 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002320 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002321 }
2322 }
2323
2324 return true;
2325}
2326
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002327bool ProgramBinary::applyUniform2iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002328{
2329 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002330 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002331
2332 for (int i = 0; i < count; i++)
2333 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002334 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002335
2336 v += 2;
2337 }
2338
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002339 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002340
2341 return true;
2342}
2343
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002344bool ProgramBinary::applyUniform3iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002345{
2346 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002347 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002348
2349 for (int i = 0; i < count; i++)
2350 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002351 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002352
2353 v += 3;
2354 }
2355
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002356 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002357
2358 return true;
2359}
2360
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002361bool ProgramBinary::applyUniform4iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002362{
2363 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002364 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002365
2366 for (int i = 0; i < count; i++)
2367 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002368 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002369
2370 v += 4;
2371 }
2372
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002373 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002374
2375 return true;
2376}
2377
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002378void ProgramBinary::applyUniformniv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002379{
2380 if (targetUniform->ps.registerCount)
2381 {
2382 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002383 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002384 }
2385
2386 if (targetUniform->vs.registerCount)
2387 {
2388 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002389 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002390 }
2391}
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002392// D3D9_REPLACE end
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002393
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002394bool ProgramBinary::isValidated() const
2395{
2396 return mValidated;
2397}
2398
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002399void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2400{
2401 // Skip over inactive attributes
2402 unsigned int activeAttribute = 0;
2403 unsigned int attribute;
2404 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2405 {
2406 if (mLinkedAttribute[attribute].name.empty())
2407 {
2408 continue;
2409 }
2410
2411 if (activeAttribute == index)
2412 {
2413 break;
2414 }
2415
2416 activeAttribute++;
2417 }
2418
2419 if (bufsize > 0)
2420 {
2421 const char *string = mLinkedAttribute[attribute].name.c_str();
2422
2423 strncpy(name, string, bufsize);
2424 name[bufsize - 1] = '\0';
2425
2426 if (length)
2427 {
2428 *length = strlen(name);
2429 }
2430 }
2431
2432 *size = 1; // Always a single 'type' instance
2433
2434 *type = mLinkedAttribute[attribute].type;
2435}
2436
2437GLint ProgramBinary::getActiveAttributeCount()
2438{
2439 int count = 0;
2440
2441 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2442 {
2443 if (!mLinkedAttribute[attributeIndex].name.empty())
2444 {
2445 count++;
2446 }
2447 }
2448
2449 return count;
2450}
2451
2452GLint ProgramBinary::getActiveAttributeMaxLength()
2453{
2454 int maxLength = 0;
2455
2456 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2457 {
2458 if (!mLinkedAttribute[attributeIndex].name.empty())
2459 {
2460 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2461 }
2462 }
2463
2464 return maxLength;
2465}
2466
2467void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2468{
2469 // Skip over internal uniforms
2470 unsigned int activeUniform = 0;
2471 unsigned int uniform;
2472 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2473 {
2474 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2475 {
2476 continue;
2477 }
2478
2479 if (activeUniform == index)
2480 {
2481 break;
2482 }
2483
2484 activeUniform++;
2485 }
2486
2487 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2488
2489 if (bufsize > 0)
2490 {
2491 std::string string = mUniforms[uniform]->name;
2492
2493 if (mUniforms[uniform]->isArray())
2494 {
2495 string += "[0]";
2496 }
2497
2498 strncpy(name, string.c_str(), bufsize);
2499 name[bufsize - 1] = '\0';
2500
2501 if (length)
2502 {
2503 *length = strlen(name);
2504 }
2505 }
2506
2507 *size = mUniforms[uniform]->arraySize;
2508
2509 *type = mUniforms[uniform]->type;
2510}
2511
2512GLint ProgramBinary::getActiveUniformCount()
2513{
2514 int count = 0;
2515
2516 unsigned int numUniforms = mUniforms.size();
2517 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2518 {
2519 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2520 {
2521 count++;
2522 }
2523 }
2524
2525 return count;
2526}
2527
2528GLint ProgramBinary::getActiveUniformMaxLength()
2529{
2530 int maxLength = 0;
2531
2532 unsigned int numUniforms = mUniforms.size();
2533 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2534 {
2535 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2536 {
2537 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2538 if (mUniforms[uniformIndex]->isArray())
2539 {
2540 length += 3; // Counting in "[0]".
2541 }
2542 maxLength = std::max(length, maxLength);
2543 }
2544 }
2545
2546 return maxLength;
2547}
2548
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002549void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002550{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002551 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002552 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002553 {
2554 mValidated = false;
2555 }
2556 else
2557 {
2558 mValidated = true;
2559 }
2560}
2561
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002562bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002563{
2564 // if any two active samplers in a program are of different types, but refer to the same
2565 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2566 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2567
2568 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2569 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2570
2571 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2572 {
2573 textureUnitType[i] = TEXTURE_UNKNOWN;
2574 }
2575
2576 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2577 {
2578 if (mSamplersPS[i].active)
2579 {
2580 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2581
2582 if (unit >= maxCombinedTextureImageUnits)
2583 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002584 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002585 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002586 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002587 }
2588
2589 return false;
2590 }
2591
2592 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2593 {
2594 if (mSamplersPS[i].textureType != textureUnitType[unit])
2595 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002596 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002597 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002598 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002599 }
2600
2601 return false;
2602 }
2603 }
2604 else
2605 {
2606 textureUnitType[unit] = mSamplersPS[i].textureType;
2607 }
2608 }
2609 }
2610
2611 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2612 {
2613 if (mSamplersVS[i].active)
2614 {
2615 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2616
2617 if (unit >= maxCombinedTextureImageUnits)
2618 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002619 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002620 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002621 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002622 }
2623
2624 return false;
2625 }
2626
2627 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2628 {
2629 if (mSamplersVS[i].textureType != textureUnitType[unit])
2630 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002631 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002632 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002633 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002634 }
2635
2636 return false;
2637 }
2638 }
2639 else
2640 {
2641 textureUnitType[unit] = mSamplersVS[i].textureType;
2642 }
2643 }
2644 }
2645
2646 return true;
2647}
2648
2649GLint ProgramBinary::getDxDepthRangeLocation() const
2650{
2651 return mDxDepthRangeLocation;
2652}
2653
daniel@transgaming.com12985182012-12-20 20:56:31 +00002654GLint ProgramBinary::getDxDepthFrontLocation() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002655{
daniel@transgaming.com12985182012-12-20 20:56:31 +00002656 return mDxDepthFrontLocation;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002657}
2658
2659GLint ProgramBinary::getDxCoordLocation() const
2660{
2661 return mDxCoordLocation;
2662}
2663
2664GLint ProgramBinary::getDxHalfPixelSizeLocation() const
2665{
2666 return mDxHalfPixelSizeLocation;
2667}
2668
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002669ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2670{
2671}
2672
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002673}