blob: 8b8ddb281af94f3bb11e28b636ed0f7729889181 [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"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000018
19#include <string>
20
daniel@transgaming.com88853c52012-12-20 20:56:40 +000021#undef near
22#undef far
23
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000024namespace gl
25{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000026std::string str(int i)
27{
28 char buffer[20];
29 snprintf(buffer, sizeof(buffer), "%d", i);
30 return buffer;
31}
32
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000033UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +000034 : name(Uniform::undecorate(_name)), element(element), index(index)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000035{
36}
37
daniel@transgaming.come87ca002012-07-24 18:30:43 +000038unsigned int ProgramBinary::mCurrentSerial = 1;
39
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000040ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000041{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000042 mPixelExecutable = NULL;
43 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000044
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000045 mValidated = false;
46
47 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
48 {
49 mSemanticIndex[index] = -1;
50 }
51
52 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
53 {
54 mSamplersPS[index].active = false;
55 }
56
57 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
58 {
59 mSamplersVS[index].active = false;
60 }
61
62 mUsedVertexSamplerRange = 0;
63 mUsedPixelSamplerRange = 0;
64
daniel@transgaming.com593ebc42012-12-20 20:56:46 +000065 mDxDepthRangeRegisterVS = -1;
66 mDxDepthRangeRegisterPS = -1;
67 mDxDepthFrontRegister = -1;
68 mDxCoordRegister = -1;
69 mDxHalfPixelSizeRegister = -1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000070}
71
72ProgramBinary::~ProgramBinary()
73{
daniel@transgaming.com95892412012-11-28 20:59:09 +000074 delete mPixelExecutable;
75 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000076
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000077 while (!mUniforms.empty())
78 {
79 delete mUniforms.back();
80 mUniforms.pop_back();
81 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000082}
83
daniel@transgaming.come87ca002012-07-24 18:30:43 +000084unsigned int ProgramBinary::getSerial() const
85{
86 return mSerial;
87}
88
89unsigned int ProgramBinary::issueSerial()
90{
91 return mCurrentSerial++;
92}
93
daniel@transgaming.com95892412012-11-28 20:59:09 +000094rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000095{
96 return mPixelExecutable;
97}
98
daniel@transgaming.com95892412012-11-28 20:59:09 +000099rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000100{
101 return mVertexExecutable;
102}
103
104GLuint ProgramBinary::getAttributeLocation(const char *name)
105{
106 if (name)
107 {
108 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
109 {
110 if (mLinkedAttribute[index].name == std::string(name))
111 {
112 return index;
113 }
114 }
115 }
116
117 return -1;
118}
119
120int ProgramBinary::getSemanticIndex(int attributeIndex)
121{
122 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
123
124 return mSemanticIndex[attributeIndex];
125}
126
127// Returns one more than the highest sampler index used.
128GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
129{
130 switch (type)
131 {
132 case SAMPLER_PIXEL:
133 return mUsedPixelSamplerRange;
134 case SAMPLER_VERTEX:
135 return mUsedVertexSamplerRange;
136 default:
137 UNREACHABLE();
138 return 0;
139 }
140}
141
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000142bool ProgramBinary::usesPointSize() const
143{
144 return mUsesPointSize;
145}
146
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000147// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
148// index (0-15 for the pixel shader and 0-3 for the vertex shader).
149GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
150{
151 GLint logicalTextureUnit = -1;
152
153 switch (type)
154 {
155 case SAMPLER_PIXEL:
156 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
157
158 if (mSamplersPS[samplerIndex].active)
159 {
160 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
161 }
162 break;
163 case SAMPLER_VERTEX:
164 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
165
166 if (mSamplersVS[samplerIndex].active)
167 {
168 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
169 }
170 break;
171 default: UNREACHABLE();
172 }
173
174 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
175 {
176 return logicalTextureUnit;
177 }
178
179 return -1;
180}
181
182// Returns the texture type for a given Direct3D 9 sampler type and
183// index (0-15 for the pixel shader and 0-3 for the vertex shader).
184TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
185{
186 switch (type)
187 {
188 case SAMPLER_PIXEL:
189 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
190 ASSERT(mSamplersPS[samplerIndex].active);
191 return mSamplersPS[samplerIndex].textureType;
192 case SAMPLER_VERTEX:
193 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
194 ASSERT(mSamplersVS[samplerIndex].active);
195 return mSamplersVS[samplerIndex].textureType;
196 default: UNREACHABLE();
197 }
198
199 return TEXTURE_2D;
200}
201
202GLint ProgramBinary::getUniformLocation(std::string name)
203{
204 unsigned int subscript = 0;
205
206 // Strip any trailing array operator and retrieve the subscript
207 size_t open = name.find_last_of('[');
208 size_t close = name.find_last_of(']');
209 if (open != std::string::npos && close == name.length() - 1)
210 {
211 subscript = atoi(name.substr(open + 1).c_str());
212 name.erase(open);
213 }
214
215 unsigned int numUniforms = mUniformIndex.size();
216 for (unsigned int location = 0; location < numUniforms; location++)
217 {
218 if (mUniformIndex[location].name == name &&
219 mUniformIndex[location].element == subscript)
220 {
221 return location;
222 }
223 }
224
225 return -1;
226}
227
228bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
229{
230 if (location < 0 || location >= (int)mUniformIndex.size())
231 {
232 return false;
233 }
234
235 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
236 targetUniform->dirty = true;
237
238 if (targetUniform->type == GL_FLOAT)
239 {
240 int arraySize = targetUniform->arraySize;
241
242 if (arraySize == 1 && count > 1)
243 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
244
245 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
246
247 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
248
249 for (int i = 0; i < count; i++)
250 {
251 target[0] = v[0];
252 target[1] = 0;
253 target[2] = 0;
254 target[3] = 0;
255 target += 4;
256 v += 1;
257 }
258 }
259 else if (targetUniform->type == GL_BOOL)
260 {
261 int arraySize = targetUniform->arraySize;
262
263 if (arraySize == 1 && count > 1)
264 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
265
266 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
267 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
268
269 for (int i = 0; i < count; ++i)
270 {
271 if (v[i] == 0.0f)
272 {
273 boolParams[i] = GL_FALSE;
274 }
275 else
276 {
277 boolParams[i] = GL_TRUE;
278 }
279 }
280 }
281 else
282 {
283 return false;
284 }
285
286 return true;
287}
288
289bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
290{
291 if (location < 0 || location >= (int)mUniformIndex.size())
292 {
293 return false;
294 }
295
296 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
297 targetUniform->dirty = true;
298
299 if (targetUniform->type == GL_FLOAT_VEC2)
300 {
301 int arraySize = targetUniform->arraySize;
302
303 if (arraySize == 1 && count > 1)
304 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
305
306 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
307
308 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
309
310 for (int i = 0; i < count; i++)
311 {
312 target[0] = v[0];
313 target[1] = v[1];
314 target[2] = 0;
315 target[3] = 0;
316 target += 4;
317 v += 2;
318 }
319 }
320 else if (targetUniform->type == GL_BOOL_VEC2)
321 {
322 int arraySize = targetUniform->arraySize;
323
324 if (arraySize == 1 && count > 1)
325 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
326
327 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
328
329 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
330
331 for (int i = 0; i < count * 2; ++i)
332 {
333 if (v[i] == 0.0f)
334 {
335 boolParams[i] = GL_FALSE;
336 }
337 else
338 {
339 boolParams[i] = GL_TRUE;
340 }
341 }
342 }
343 else
344 {
345 return false;
346 }
347
348 return true;
349}
350
351bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
352{
353 if (location < 0 || location >= (int)mUniformIndex.size())
354 {
355 return false;
356 }
357
358 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
359 targetUniform->dirty = true;
360
361 if (targetUniform->type == GL_FLOAT_VEC3)
362 {
363 int arraySize = targetUniform->arraySize;
364
365 if (arraySize == 1 && count > 1)
366 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
367
368 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
369
370 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
371
372 for (int i = 0; i < count; i++)
373 {
374 target[0] = v[0];
375 target[1] = v[1];
376 target[2] = v[2];
377 target[3] = 0;
378 target += 4;
379 v += 3;
380 }
381 }
382 else if (targetUniform->type == GL_BOOL_VEC3)
383 {
384 int arraySize = targetUniform->arraySize;
385
386 if (arraySize == 1 && count > 1)
387 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
388
389 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
390 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
391
392 for (int i = 0; i < count * 3; ++i)
393 {
394 if (v[i] == 0.0f)
395 {
396 boolParams[i] = GL_FALSE;
397 }
398 else
399 {
400 boolParams[i] = GL_TRUE;
401 }
402 }
403 }
404 else
405 {
406 return false;
407 }
408
409 return true;
410}
411
412bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
413{
414 if (location < 0 || location >= (int)mUniformIndex.size())
415 {
416 return false;
417 }
418
419 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
420 targetUniform->dirty = true;
421
422 if (targetUniform->type == GL_FLOAT_VEC4)
423 {
424 int arraySize = targetUniform->arraySize;
425
426 if (arraySize == 1 && count > 1)
427 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
428
429 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
430
431 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
432 v, 4 * sizeof(GLfloat) * count);
433 }
434 else if (targetUniform->type == GL_BOOL_VEC4)
435 {
436 int arraySize = targetUniform->arraySize;
437
438 if (arraySize == 1 && count > 1)
439 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
440
441 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
442 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
443
444 for (int i = 0; i < count * 4; ++i)
445 {
446 if (v[i] == 0.0f)
447 {
448 boolParams[i] = GL_FALSE;
449 }
450 else
451 {
452 boolParams[i] = GL_TRUE;
453 }
454 }
455 }
456 else
457 {
458 return false;
459 }
460
461 return true;
462}
463
464template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
465void transposeMatrix(T *target, const GLfloat *value)
466{
467 int copyWidth = std::min(targetWidth, srcWidth);
468 int copyHeight = std::min(targetHeight, srcHeight);
469
470 for (int x = 0; x < copyWidth; x++)
471 {
472 for (int y = 0; y < copyHeight; y++)
473 {
474 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
475 }
476 }
477 // clear unfilled right side
478 for (int y = 0; y < copyHeight; y++)
479 {
480 for (int x = srcWidth; x < targetWidth; x++)
481 {
482 target[y * targetWidth + x] = (T)0;
483 }
484 }
485 // clear unfilled bottom.
486 for (int y = srcHeight; y < targetHeight; y++)
487 {
488 for (int x = 0; x < targetWidth; x++)
489 {
490 target[y * targetWidth + x] = (T)0;
491 }
492 }
493}
494
495bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
496{
497 if (location < 0 || location >= (int)mUniformIndex.size())
498 {
499 return false;
500 }
501
502 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
503 targetUniform->dirty = true;
504
505 if (targetUniform->type != GL_FLOAT_MAT2)
506 {
507 return false;
508 }
509
510 int arraySize = targetUniform->arraySize;
511
512 if (arraySize == 1 && count > 1)
513 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
514
515 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
516
517 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
518 for (int i = 0; i < count; i++)
519 {
520 transposeMatrix<GLfloat,4,2,2,2>(target, value);
521 target += 8;
522 value += 4;
523 }
524
525 return true;
526}
527
528bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
529{
530 if (location < 0 || location >= (int)mUniformIndex.size())
531 {
532 return false;
533 }
534
535 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
536 targetUniform->dirty = true;
537
538 if (targetUniform->type != GL_FLOAT_MAT3)
539 {
540 return false;
541 }
542
543 int arraySize = targetUniform->arraySize;
544
545 if (arraySize == 1 && count > 1)
546 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
547
548 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
549
550 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
551 for (int i = 0; i < count; i++)
552 {
553 transposeMatrix<GLfloat,4,3,3,3>(target, value);
554 target += 12;
555 value += 9;
556 }
557
558 return true;
559}
560
561
562bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
563{
564 if (location < 0 || location >= (int)mUniformIndex.size())
565 {
566 return false;
567 }
568
569 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
570 targetUniform->dirty = true;
571
572 if (targetUniform->type != GL_FLOAT_MAT4)
573 {
574 return false;
575 }
576
577 int arraySize = targetUniform->arraySize;
578
579 if (arraySize == 1 && count > 1)
580 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
581
582 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
583
584 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
585 for (int i = 0; i < count; i++)
586 {
587 transposeMatrix<GLfloat,4,4,4,4>(target, value);
588 target += 16;
589 value += 16;
590 }
591
592 return true;
593}
594
595bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
596{
597 if (location < 0 || location >= (int)mUniformIndex.size())
598 {
599 return false;
600 }
601
602 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
603 targetUniform->dirty = true;
604
605 if (targetUniform->type == GL_INT ||
606 targetUniform->type == GL_SAMPLER_2D ||
607 targetUniform->type == GL_SAMPLER_CUBE)
608 {
609 int arraySize = targetUniform->arraySize;
610
611 if (arraySize == 1 && count > 1)
612 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
613
614 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
615
616 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
617 v, sizeof(GLint) * count);
618 }
619 else if (targetUniform->type == GL_BOOL)
620 {
621 int arraySize = targetUniform->arraySize;
622
623 if (arraySize == 1 && count > 1)
624 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
625
626 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
627 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
628
629 for (int i = 0; i < count; ++i)
630 {
631 if (v[i] == 0)
632 {
633 boolParams[i] = GL_FALSE;
634 }
635 else
636 {
637 boolParams[i] = GL_TRUE;
638 }
639 }
640 }
641 else
642 {
643 return false;
644 }
645
646 return true;
647}
648
649bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
650{
651 if (location < 0 || location >= (int)mUniformIndex.size())
652 {
653 return false;
654 }
655
656 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
657 targetUniform->dirty = true;
658
659 if (targetUniform->type == GL_INT_VEC2)
660 {
661 int arraySize = targetUniform->arraySize;
662
663 if (arraySize == 1 && count > 1)
664 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
665
666 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
667
668 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
669 v, 2 * sizeof(GLint) * count);
670 }
671 else if (targetUniform->type == GL_BOOL_VEC2)
672 {
673 int arraySize = targetUniform->arraySize;
674
675 if (arraySize == 1 && count > 1)
676 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
677
678 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
679 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
680
681 for (int i = 0; i < count * 2; ++i)
682 {
683 if (v[i] == 0)
684 {
685 boolParams[i] = GL_FALSE;
686 }
687 else
688 {
689 boolParams[i] = GL_TRUE;
690 }
691 }
692 }
693 else
694 {
695 return false;
696 }
697
698 return true;
699}
700
701bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
702{
703 if (location < 0 || location >= (int)mUniformIndex.size())
704 {
705 return false;
706 }
707
708 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
709 targetUniform->dirty = true;
710
711 if (targetUniform->type == GL_INT_VEC3)
712 {
713 int arraySize = targetUniform->arraySize;
714
715 if (arraySize == 1 && count > 1)
716 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
717
718 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
719
720 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
721 v, 3 * sizeof(GLint) * count);
722 }
723 else if (targetUniform->type == GL_BOOL_VEC3)
724 {
725 int arraySize = targetUniform->arraySize;
726
727 if (arraySize == 1 && count > 1)
728 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
729
730 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
731 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
732
733 for (int i = 0; i < count * 3; ++i)
734 {
735 if (v[i] == 0)
736 {
737 boolParams[i] = GL_FALSE;
738 }
739 else
740 {
741 boolParams[i] = GL_TRUE;
742 }
743 }
744 }
745 else
746 {
747 return false;
748 }
749
750 return true;
751}
752
753bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
754{
755 if (location < 0 || location >= (int)mUniformIndex.size())
756 {
757 return false;
758 }
759
760 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
761 targetUniform->dirty = true;
762
763 if (targetUniform->type == GL_INT_VEC4)
764 {
765 int arraySize = targetUniform->arraySize;
766
767 if (arraySize == 1 && count > 1)
768 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
769
770 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
771
772 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
773 v, 4 * sizeof(GLint) * count);
774 }
775 else if (targetUniform->type == GL_BOOL_VEC4)
776 {
777 int arraySize = targetUniform->arraySize;
778
779 if (arraySize == 1 && count > 1)
780 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
781
782 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
783 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
784
785 for (int i = 0; i < count * 4; ++i)
786 {
787 if (v[i] == 0)
788 {
789 boolParams[i] = GL_FALSE;
790 }
791 else
792 {
793 boolParams[i] = GL_TRUE;
794 }
795 }
796 }
797 else
798 {
799 return false;
800 }
801
802 return true;
803}
804
805bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
806{
807 if (location < 0 || location >= (int)mUniformIndex.size())
808 {
809 return false;
810 }
811
812 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
813
814 // sized queries -- ensure the provided buffer is large enough
815 if (bufSize)
816 {
817 int requiredBytes = UniformExternalSize(targetUniform->type);
818 if (*bufSize < requiredBytes)
819 {
820 return false;
821 }
822 }
823
824 switch (targetUniform->type)
825 {
826 case GL_FLOAT_MAT2:
827 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
828 break;
829 case GL_FLOAT_MAT3:
830 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
831 break;
832 case GL_FLOAT_MAT4:
833 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
834 break;
835 default:
836 {
837 unsigned int count = UniformExternalComponentCount(targetUniform->type);
838 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
839
840 switch (UniformComponentType(targetUniform->type))
841 {
842 case GL_BOOL:
843 {
844 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
845
846 for (unsigned int i = 0; i < count; ++i)
847 {
848 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
849 }
850 }
851 break;
852 case GL_FLOAT:
853 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
854 count * sizeof(GLfloat));
855 break;
856 case GL_INT:
857 {
858 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
859
860 for (unsigned int i = 0; i < count; ++i)
861 {
862 params[i] = (float)intParams[i];
863 }
864 }
865 break;
866 default: UNREACHABLE();
867 }
868 }
869 }
870
871 return true;
872}
873
874bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
875{
876 if (location < 0 || location >= (int)mUniformIndex.size())
877 {
878 return false;
879 }
880
881 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
882
883 // sized queries -- ensure the provided buffer is large enough
884 if (bufSize)
885 {
886 int requiredBytes = UniformExternalSize(targetUniform->type);
887 if (*bufSize < requiredBytes)
888 {
889 return false;
890 }
891 }
892
893 switch (targetUniform->type)
894 {
895 case GL_FLOAT_MAT2:
896 {
897 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
898 }
899 break;
900 case GL_FLOAT_MAT3:
901 {
902 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
903 }
904 break;
905 case GL_FLOAT_MAT4:
906 {
907 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
908 }
909 break;
910 default:
911 {
912 unsigned int count = UniformExternalComponentCount(targetUniform->type);
913 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
914
915 switch (UniformComponentType(targetUniform->type))
916 {
917 case GL_BOOL:
918 {
919 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
920
921 for (unsigned int i = 0; i < count; ++i)
922 {
923 params[i] = (GLint)boolParams[i];
924 }
925 }
926 break;
927 case GL_FLOAT:
928 {
929 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
930
931 for (unsigned int i = 0; i < count; ++i)
932 {
933 params[i] = (GLint)floatParams[i];
934 }
935 }
936 break;
937 case GL_INT:
938 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
939 count * sizeof(GLint));
940 break;
941 default: UNREACHABLE();
942 }
943 }
944 }
945
946 return true;
947}
948
949void ProgramBinary::dirtyAllUniforms()
950{
951 unsigned int numUniforms = mUniforms.size();
952 for (unsigned int index = 0; index < numUniforms; index++)
953 {
954 mUniforms[index]->dirty = true;
955 }
956}
957
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000958// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000959void ProgramBinary::applyUniforms()
960{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000961 // Retrieve sampler uniform values
962 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
963 {
964 Uniform *targetUniform = *ub;
965
966 if (targetUniform->dirty)
967 {
968 int count = targetUniform->arraySize;
969 GLint *v = (GLint*)targetUniform->data;
970
971 switch (targetUniform->type)
972 {
973 case GL_SAMPLER_2D:
974 case GL_SAMPLER_CUBE:
975 {
976 if (targetUniform->ps.registerCount)
977 {
978 if (targetUniform->ps.samplerIndex >= 0)
979 {
980 unsigned int firstIndex = targetUniform->ps.samplerIndex;
981
982 for (int i = 0; i < count; i++)
983 {
984 unsigned int samplerIndex = firstIndex + i;
985
986 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
987 {
988 ASSERT(mSamplersPS[samplerIndex].active);
989 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
990 }
991 }
992 }
993 }
994
995 if (targetUniform->vs.registerCount)
996 {
997 if (targetUniform->vs.samplerIndex >= 0)
998 {
999 unsigned int firstIndex = targetUniform->vs.samplerIndex;
1000
1001 for (int i = 0; i < count; i++)
1002 {
1003 unsigned int samplerIndex = firstIndex + i;
1004
1005 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
1006 {
1007 ASSERT(mSamplersVS[samplerIndex].active);
1008 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
1009 }
1010 }
1011 }
1012 }
1013 }
1014 break;
1015 }
1016 }
1017 }
1018
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001019 mRenderer->applyUniforms(&mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001020}
1021
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001022// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1023// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001024int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001025{
1026 Context *context = getContext();
1027 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1028
1029 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1030 {
1031 int n = VariableRowCount(varying->type) * varying->size;
1032 int m = VariableColumnCount(varying->type);
1033 bool success = false;
1034
1035 if (m == 2 || m == 3 || m == 4)
1036 {
1037 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1038 {
1039 bool available = true;
1040
1041 for (int y = 0; y < n && available; y++)
1042 {
1043 for (int x = 0; x < m && available; x++)
1044 {
1045 if (packing[r + y][x])
1046 {
1047 available = false;
1048 }
1049 }
1050 }
1051
1052 if (available)
1053 {
1054 varying->reg = r;
1055 varying->col = 0;
1056
1057 for (int y = 0; y < n; y++)
1058 {
1059 for (int x = 0; x < m; x++)
1060 {
1061 packing[r + y][x] = &*varying;
1062 }
1063 }
1064
1065 success = true;
1066 }
1067 }
1068
1069 if (!success && m == 2)
1070 {
1071 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1072 {
1073 bool available = true;
1074
1075 for (int y = 0; y < n && available; y++)
1076 {
1077 for (int x = 2; x < 4 && available; x++)
1078 {
1079 if (packing[r + y][x])
1080 {
1081 available = false;
1082 }
1083 }
1084 }
1085
1086 if (available)
1087 {
1088 varying->reg = r;
1089 varying->col = 2;
1090
1091 for (int y = 0; y < n; y++)
1092 {
1093 for (int x = 2; x < 4; x++)
1094 {
1095 packing[r + y][x] = &*varying;
1096 }
1097 }
1098
1099 success = true;
1100 }
1101 }
1102 }
1103 }
1104 else if (m == 1)
1105 {
1106 int space[4] = {0};
1107
1108 for (int y = 0; y < maxVaryingVectors; y++)
1109 {
1110 for (int x = 0; x < 4; x++)
1111 {
1112 space[x] += packing[y][x] ? 0 : 1;
1113 }
1114 }
1115
1116 int column = 0;
1117
1118 for (int x = 0; x < 4; x++)
1119 {
1120 if (space[x] >= n && space[x] < space[column])
1121 {
1122 column = x;
1123 }
1124 }
1125
1126 if (space[column] >= n)
1127 {
1128 for (int r = 0; r < maxVaryingVectors; r++)
1129 {
1130 if (!packing[r][column])
1131 {
1132 varying->reg = r;
1133
1134 for (int y = r; y < r + n; y++)
1135 {
1136 packing[y][column] = &*varying;
1137 }
1138
1139 break;
1140 }
1141 }
1142
1143 varying->col = column;
1144
1145 success = true;
1146 }
1147 }
1148 else UNREACHABLE();
1149
1150 if (!success)
1151 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001152 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001153
1154 return -1;
1155 }
1156 }
1157
1158 // Return the number of used registers
1159 int registers = 0;
1160
1161 for (int r = 0; r < maxVaryingVectors; r++)
1162 {
1163 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1164 {
1165 registers++;
1166 }
1167 }
1168
1169 return registers;
1170}
1171
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001172bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001173{
1174 if (pixelHLSL.empty() || vertexHLSL.empty())
1175 {
1176 return false;
1177 }
1178
1179 // Reset the varying register assignments
1180 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1181 {
1182 fragVar->reg = -1;
1183 fragVar->col = -1;
1184 }
1185
1186 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1187 {
1188 vtxVar->reg = -1;
1189 vtxVar->col = -1;
1190 }
1191
1192 // Map the varyings to the register file
1193 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001194 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001195
1196 if (registers < 0)
1197 {
1198 return false;
1199 }
1200
1201 // Write the HLSL input/output declarations
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001202 const bool sm3 = (mRenderer->getMajorShaderModel() >= 3);
1203 const bool sm4 = (mRenderer->getMajorShaderModel() >= 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001204 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001205 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1206
1207 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1208 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001209 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001210
1211 return false;
1212 }
1213
1214 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1215 {
1216 bool matched = false;
1217
1218 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1219 {
1220 if (output->name == input->name)
1221 {
1222 if (output->type != input->type || output->size != input->size)
1223 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001224 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 +00001225
1226 return false;
1227 }
1228
1229 output->reg = input->reg;
1230 output->col = input->col;
1231
1232 matched = true;
1233 break;
1234 }
1235 }
1236
1237 if (!matched)
1238 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001239 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001240
1241 return false;
1242 }
1243 }
1244
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001245 mUsesPointSize = vertexShader->mUsesPointSize;
1246 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001247 std::string targetSemantic = sm4 ? "SV_Target" : "COLOR";
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001248 std::string positionSemantic = sm4 ? "SV_POSITION" : "POSITION";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001249
1250 vertexHLSL += "struct VS_INPUT\n"
1251 "{\n";
1252
1253 int semanticIndex = 0;
1254 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1255 {
1256 switch (attribute->type)
1257 {
1258 case GL_FLOAT: vertexHLSL += " float "; break;
1259 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1260 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1261 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1262 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1263 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1264 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1265 default: UNREACHABLE();
1266 }
1267
1268 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1269
1270 semanticIndex += VariableRowCount(attribute->type);
1271 }
1272
1273 vertexHLSL += "};\n"
1274 "\n"
1275 "struct VS_OUTPUT\n"
1276 "{\n"
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001277 " float4 gl_Position : " + positionSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001278
1279 for (int r = 0; r < registers; r++)
1280 {
1281 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1282
1283 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1284 }
1285
1286 if (fragmentShader->mUsesFragCoord)
1287 {
1288 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1289 }
1290
1291 if (vertexShader->mUsesPointSize && sm3)
1292 {
1293 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1294 }
1295
1296 vertexHLSL += "};\n"
1297 "\n"
1298 "VS_OUTPUT main(VS_INPUT input)\n"
1299 "{\n";
1300
1301 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1302 {
1303 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1304
1305 if (VariableRowCount(attribute->type) > 1) // Matrix
1306 {
1307 vertexHLSL += "transpose";
1308 }
1309
1310 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1311 }
1312
1313 vertexHLSL += "\n"
1314 " gl_main();\n"
1315 "\n"
1316 " VS_OUTPUT output;\n"
1317 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001318 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001319 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1320 " output.gl_Position.w = gl_Position.w;\n";
1321
1322 if (vertexShader->mUsesPointSize && sm3)
1323 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001324 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001325 }
1326
1327 if (fragmentShader->mUsesFragCoord)
1328 {
1329 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1330 }
1331
1332 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1333 {
1334 if (varying->reg >= 0)
1335 {
1336 for (int i = 0; i < varying->size; i++)
1337 {
1338 int rows = VariableRowCount(varying->type);
1339
1340 for (int j = 0; j < rows; j++)
1341 {
1342 int r = varying->reg + i * rows + j;
1343 vertexHLSL += " output.v" + str(r);
1344
1345 bool sharedRegister = false; // Register used by multiple varyings
1346
1347 for (int x = 0; x < 4; x++)
1348 {
1349 if (packing[r][x] && packing[r][x] != packing[r][0])
1350 {
1351 sharedRegister = true;
1352 break;
1353 }
1354 }
1355
1356 if(sharedRegister)
1357 {
1358 vertexHLSL += ".";
1359
1360 for (int x = 0; x < 4; x++)
1361 {
1362 if (packing[r][x] == &*varying)
1363 {
1364 switch(x)
1365 {
1366 case 0: vertexHLSL += "x"; break;
1367 case 1: vertexHLSL += "y"; break;
1368 case 2: vertexHLSL += "z"; break;
1369 case 3: vertexHLSL += "w"; break;
1370 }
1371 }
1372 }
1373 }
1374
1375 vertexHLSL += " = " + varying->name;
1376
1377 if (varying->array)
1378 {
1379 vertexHLSL += "[" + str(i) + "]";
1380 }
1381
1382 if (rows > 1)
1383 {
1384 vertexHLSL += "[" + str(j) + "]";
1385 }
1386
1387 vertexHLSL += ";\n";
1388 }
1389 }
1390 }
1391 }
1392
1393 vertexHLSL += "\n"
1394 " return output;\n"
1395 "}\n";
1396
1397 pixelHLSL += "struct PS_INPUT\n"
1398 "{\n";
1399
1400 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1401 {
1402 if (varying->reg >= 0)
1403 {
1404 for (int i = 0; i < varying->size; i++)
1405 {
1406 int rows = VariableRowCount(varying->type);
1407 for (int j = 0; j < rows; j++)
1408 {
1409 std::string n = str(varying->reg + i * rows + j);
1410 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1411 }
1412 }
1413 }
1414 else UNREACHABLE();
1415 }
1416
1417 if (fragmentShader->mUsesFragCoord)
1418 {
1419 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1420 if (sm3) {
1421 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1422 }
1423 }
1424
1425 if (fragmentShader->mUsesPointCoord && sm3)
1426 {
1427 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1428 }
1429
1430 if (fragmentShader->mUsesFrontFacing)
1431 {
1432 pixelHLSL += " float vFace : VFACE;\n";
1433 }
1434
1435 pixelHLSL += "};\n"
1436 "\n"
1437 "struct PS_OUTPUT\n"
1438 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001439 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001440 "};\n"
1441 "\n"
1442 "PS_OUTPUT main(PS_INPUT input)\n"
1443 "{\n";
1444
1445 if (fragmentShader->mUsesFragCoord)
1446 {
1447 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1448
1449 if (sm3)
1450 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001451 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001452 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001453 }
1454 else
1455 {
1456 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1457 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001458 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001459 }
1460
daniel@transgaming.com12985182012-12-20 20:56:31 +00001461 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001462 " gl_FragCoord.w = rhw;\n";
1463 }
1464
1465 if (fragmentShader->mUsesPointCoord && sm3)
1466 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001467 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1468 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001469 }
1470
1471 if (fragmentShader->mUsesFrontFacing)
1472 {
daniel@transgaming.com12985182012-12-20 20:56:31 +00001473 pixelHLSL += " gl_FrontFacing = (input.vFace * dx_DepthFront.z >= 0.0);\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001474 }
1475
1476 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1477 {
1478 if (varying->reg >= 0)
1479 {
1480 for (int i = 0; i < varying->size; i++)
1481 {
1482 int rows = VariableRowCount(varying->type);
1483 for (int j = 0; j < rows; j++)
1484 {
1485 std::string n = str(varying->reg + i * rows + j);
1486 pixelHLSL += " " + varying->name;
1487
1488 if (varying->array)
1489 {
1490 pixelHLSL += "[" + str(i) + "]";
1491 }
1492
1493 if (rows > 1)
1494 {
1495 pixelHLSL += "[" + str(j) + "]";
1496 }
1497
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001498 switch (VariableColumnCount(varying->type))
1499 {
1500 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1501 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1502 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1503 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1504 default: UNREACHABLE();
1505 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001506 }
1507 }
1508 }
1509 else UNREACHABLE();
1510 }
1511
1512 pixelHLSL += "\n"
1513 " gl_main();\n"
1514 "\n"
1515 " PS_OUTPUT output;\n"
1516 " output.gl_Color[0] = gl_Color[0];\n"
1517 "\n"
1518 " return output;\n"
1519 "}\n";
1520
1521 return true;
1522}
1523
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001524bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1525{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001526 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001527
1528 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001529 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001530 if (format != GL_PROGRAM_BINARY_ANGLE)
1531 {
1532 infoLog.append("Invalid program binary format.");
1533 return false;
1534 }
1535
1536 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001537 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001538 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001539 {
1540 infoLog.append("Invalid program binary version.");
1541 return false;
1542 }
1543
1544 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1545 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001546 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001547 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001548 stream.read(&name);
1549 mLinkedAttribute[i].name = name;
1550 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001551 }
1552
1553 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1554 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001555 stream.read(&mSamplersPS[i].active);
1556 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001557
1558 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001559 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001560 mSamplersPS[i].textureType = (TextureType) textureType;
1561 }
1562
1563 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1564 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001565 stream.read(&mSamplersVS[i].active);
1566 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001567
1568 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001569 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001570 mSamplersVS[i].textureType = (TextureType) textureType;
1571 }
1572
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001573 stream.read(&mUsedVertexSamplerRange);
1574 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001575
1576 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001577 stream.read(&size);
1578 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001579 {
1580 infoLog.append("Invalid program binary.");
1581 return false;
1582 }
1583
1584 mUniforms.resize(size);
1585 for (unsigned int i = 0; i < size; ++i)
1586 {
1587 GLenum type;
1588 std::string _name;
1589 unsigned int arraySize;
1590
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001591 stream.read(&type);
1592 stream.read(&_name);
1593 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001594
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001595 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001596
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001597 stream.read(&mUniforms[i]->ps.float4Index);
1598 stream.read(&mUniforms[i]->ps.samplerIndex);
1599 stream.read(&mUniforms[i]->ps.boolIndex);
1600 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001601
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001602 stream.read(&mUniforms[i]->vs.float4Index);
1603 stream.read(&mUniforms[i]->vs.samplerIndex);
1604 stream.read(&mUniforms[i]->vs.boolIndex);
1605 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001606 }
1607
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001608 stream.read(&size);
1609 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001610 {
1611 infoLog.append("Invalid program binary.");
1612 return false;
1613 }
1614
1615 mUniformIndex.resize(size);
1616 for (unsigned int i = 0; i < size; ++i)
1617 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001618 stream.read(&mUniformIndex[i].name);
1619 stream.read(&mUniformIndex[i].element);
1620 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001621 }
1622
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001623 stream.read(&mDxDepthRangeRegisterVS);
1624 stream.read(&mDxDepthRangeRegisterPS);
1625 stream.read(&mDxDepthFrontRegister);
1626 stream.read(&mDxCoordRegister);
1627 stream.read(&mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001628
1629 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001630 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001631
1632 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001633 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001634
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001635 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001636
daniel@transgaming.com36038542012-11-28 20:59:26 +00001637 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001638 ptr += sizeof(GUID);
1639
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001640 GUID identifier = mRenderer->getAdapterIdentifier();
1641 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001642 {
1643 infoLog.append("Invalid program binary.");
1644 return false;
1645 }
1646
1647 const char *pixelShaderFunction = ptr;
1648 ptr += pixelShaderSize;
1649
1650 const char *vertexShaderFunction = ptr;
1651 ptr += vertexShaderSize;
1652
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001653 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1654 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1655 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001656 {
1657 infoLog.append("Could not create pixel shader.");
1658 return false;
1659 }
1660
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001661 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1662 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1663 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001664 {
1665 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001666 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001667 mPixelExecutable = NULL;
1668 return false;
1669 }
1670
1671 return true;
1672}
1673
1674bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1675{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001676 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001677
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001678 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001679 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001680
1681 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1682 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001683 stream.write(mLinkedAttribute[i].type);
1684 stream.write(mLinkedAttribute[i].name);
1685 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001686 }
1687
1688 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1689 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001690 stream.write(mSamplersPS[i].active);
1691 stream.write(mSamplersPS[i].logicalTextureUnit);
1692 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001693 }
1694
1695 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1696 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001697 stream.write(mSamplersVS[i].active);
1698 stream.write(mSamplersVS[i].logicalTextureUnit);
1699 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001700 }
1701
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001702 stream.write(mUsedVertexSamplerRange);
1703 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001704
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001705 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001706 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1707 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001708 stream.write(mUniforms[i]->type);
1709 stream.write(mUniforms[i]->_name);
1710 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001711
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001712 stream.write(mUniforms[i]->ps.float4Index);
1713 stream.write(mUniforms[i]->ps.samplerIndex);
1714 stream.write(mUniforms[i]->ps.boolIndex);
1715 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001716
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001717 stream.write(mUniforms[i]->vs.float4Index);
1718 stream.write(mUniforms[i]->vs.samplerIndex);
1719 stream.write(mUniforms[i]->vs.boolIndex);
1720 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001721 }
1722
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001723 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001724 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1725 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001726 stream.write(mUniformIndex[i].name);
1727 stream.write(mUniformIndex[i].element);
1728 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001729 }
1730
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001731 stream.write(mDxDepthRangeRegisterVS);
1732 stream.write(mDxDepthRangeRegisterPS);
1733 stream.write(mDxDepthFrontRegister);
1734 stream.write(mDxCoordRegister);
1735 stream.write(mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001736
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001737 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001738 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001739
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001740 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001741 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001742
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001743 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001744
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001745 GLsizei streamLength = stream.length();
1746 const void *streamData = stream.data();
1747
1748 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001749 if (totalLength > bufSize)
1750 {
1751 if (length)
1752 {
1753 *length = 0;
1754 }
1755
1756 return false;
1757 }
1758
1759 if (binary)
1760 {
1761 char *ptr = (char*) binary;
1762
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001763 memcpy(ptr, streamData, streamLength);
1764 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001765
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001766 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001767 ptr += sizeof(GUID);
1768
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001769 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001770 ptr += pixelShaderSize;
1771
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001772 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001773 ptr += vertexShaderSize;
1774
1775 ASSERT(ptr - totalLength == binary);
1776 }
1777
1778 if (length)
1779 {
1780 *length = totalLength;
1781 }
1782
1783 return true;
1784}
1785
1786GLint ProgramBinary::getLength()
1787{
1788 GLint length;
1789 if (save(NULL, INT_MAX, &length))
1790 {
1791 return length;
1792 }
1793 else
1794 {
1795 return 0;
1796 }
1797}
1798
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001799bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001800{
1801 if (!fragmentShader || !fragmentShader->isCompiled())
1802 {
1803 return false;
1804 }
1805
1806 if (!vertexShader || !vertexShader->isCompiled())
1807 {
1808 return false;
1809 }
1810
1811 std::string pixelHLSL = fragmentShader->getHLSL();
1812 std::string vertexHLSL = vertexShader->getHLSL();
1813
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001814 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001815 {
1816 return false;
1817 }
1818
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001819 bool success = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001820 rx::D3DConstantTable *constantTableVS = NULL;
1821 rx::D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001822 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1823 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001824
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001825 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001826 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001827 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001828 constantTableVS = mVertexExecutable->getConstantTable();
1829 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001830 }
1831 else
1832 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001833 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001834 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001835
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001836 delete mVertexExecutable;
1837 mVertexExecutable = NULL;
1838 delete mPixelExecutable;
1839 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001840 }
1841
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001842 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1843 {
1844 success = false;
1845 }
1846
1847 if (constantTableVS && constantTablePS)
1848 {
1849 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1850 {
1851 success = false;
1852 }
1853 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001854
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001855 Context *context = getContext();
1856 context->markDxUniformsDirty();
1857
1858 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001859}
1860
1861// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001862bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001863{
1864 unsigned int usedLocations = 0;
1865
1866 // Link attributes that have a binding location
1867 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1868 {
1869 int location = attributeBindings.getAttributeBinding(attribute->name);
1870
1871 if (location != -1) // Set by glBindAttribLocation
1872 {
1873 if (!mLinkedAttribute[location].name.empty())
1874 {
1875 // Multiple active attributes bound to the same location; not an error
1876 }
1877
1878 mLinkedAttribute[location] = *attribute;
1879
1880 int rows = VariableRowCount(attribute->type);
1881
1882 if (rows + location > MAX_VERTEX_ATTRIBS)
1883 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001884 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 +00001885
1886 return false;
1887 }
1888
1889 for (int i = 0; i < rows; i++)
1890 {
1891 usedLocations |= 1 << (location + i);
1892 }
1893 }
1894 }
1895
1896 // Link attributes that don't have a binding location
1897 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1898 {
1899 int location = attributeBindings.getAttributeBinding(attribute->name);
1900
1901 if (location == -1) // Not set by glBindAttribLocation
1902 {
1903 int rows = VariableRowCount(attribute->type);
1904 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1905
1906 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1907 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001908 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001909
1910 return false; // Fail to link
1911 }
1912
1913 mLinkedAttribute[availableIndex] = *attribute;
1914 }
1915 }
1916
1917 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1918 {
1919 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1920 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1921
1922 for (int r = 0; r < rows; r++)
1923 {
1924 mSemanticIndex[attributeIndex++] = index++;
1925 }
1926 }
1927
1928 return true;
1929}
1930
daniel@transgaming.com31240482012-11-28 21:06:41 +00001931bool ProgramBinary::linkUniforms(InfoLog &infoLog, rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001932{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001933 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001934 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001935 const rx::D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001936
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001937 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001938 {
1939 return false;
1940 }
1941 }
1942
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001943 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1944 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001945 const rx::D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001946
1947 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1948 {
1949 return false;
1950 }
1951 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001952 return true;
1953}
1954
1955// Adds the description of a constant found in the binary shader to the list of uniforms
1956// Returns true if succesful (uniform not already defined)
daniel@transgaming.com31240482012-11-28 21:06:41 +00001957bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const rx::D3DConstant *constant, const std::string &name,
1958 rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001959{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001960 if (constant->registerSet == rx::D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001961 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001962 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001963 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001964 const rx::D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1965 const rx::D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001966
1967 if (psConstant)
1968 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001969 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001970
1971 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1972 {
1973 mSamplersPS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001974 mSamplersPS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001975 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1976 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1977 }
1978 else
1979 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001980 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001981 return false;
1982 }
1983 }
1984
1985 if (vsConstant)
1986 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001987 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001988
1989 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
1990 {
1991 mSamplersVS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001992 mSamplersVS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001993 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
1994 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
1995 }
1996 else
1997 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001998 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001999 return false;
2000 }
2001 }
2002 }
2003 }
2004
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002005 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002006 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002007 case rx::D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002008 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002009 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002010 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002011 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002012 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002013 const rx::D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002014
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002015 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002016
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002017 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002018 {
2019 return false;
2020 }
2021 }
2022 }
2023
2024 return true;
2025 }
daniel@transgaming.com31240482012-11-28 21:06:41 +00002026 case rx::D3DConstant::CLASS_SCALAR:
2027 case rx::D3DConstant::CLASS_VECTOR:
2028 case rx::D3DConstant::CLASS_MATRIX_COLUMNS:
2029 case rx::D3DConstant::CLASS_OBJECT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002030 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002031 default:
2032 UNREACHABLE();
2033 return false;
2034 }
2035}
2036
daniel@transgaming.com31240482012-11-28 21:06:41 +00002037bool ProgramBinary::defineUniform(GLenum shader, const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002038{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002039 if (_name == "dx_DepthRange")
2040 {
2041 if (shader == GL_VERTEX_SHADER) mDxDepthRangeRegisterVS = constant->registerIndex;
2042 if (shader == GL_FRAGMENT_SHADER) mDxDepthRangeRegisterPS = constant->registerIndex;
2043 return true;
2044 }
2045
2046 if (_name == "dx_DepthFront")
2047 {
2048 mDxDepthFrontRegister = constant->registerIndex;
2049 return true;
2050 }
2051
2052 if (_name == "dx_Coord")
2053 {
2054 mDxCoordRegister = constant->registerIndex;
2055 return true;
2056 }
2057
2058 if (_name == "dx_HalfPixelSize")
2059 {
2060 mDxHalfPixelSizeRegister = constant->registerIndex;
2061 return true;
2062 }
2063
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002064 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002065
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002066 if (!uniform)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002067 {
2068 return false;
2069 }
2070
2071 // Check if already defined
2072 GLint location = getUniformLocation(uniform->name);
2073 GLenum type = uniform->type;
2074
2075 if (location >= 0)
2076 {
2077 delete uniform;
2078 uniform = mUniforms[mUniformIndex[location].index];
2079 }
2080
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002081 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2082 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002083
2084 if (location >= 0)
2085 {
2086 return uniform->type == type;
2087 }
2088
2089 mUniforms.push_back(uniform);
2090 unsigned int uniformIndex = mUniforms.size() - 1;
2091
2092 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2093 {
2094 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2095 }
2096
2097 return true;
2098}
2099
daniel@transgaming.com31240482012-11-28 21:06:41 +00002100Uniform *ProgramBinary::createUniform(const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002101{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002102 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002103 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002104 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002105 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002106 case rx::D3DConstant::PT_SAMPLER2D:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002107 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002108 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002109 case 1: return new Uniform(GL_SAMPLER_2D, _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_SAMPLERCUBE:
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_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002117 default: UNREACHABLE();
2118 }
2119 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002120 case rx::D3DConstant::PT_BOOL:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002121 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002122 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002123 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2124 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2125 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2126 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002127 default: UNREACHABLE();
2128 }
2129 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002130 case rx::D3DConstant::PT_INT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002131 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002132 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002133 case 1: return new Uniform(GL_INT, _name, constant->elements);
2134 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2135 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2136 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002137 default: UNREACHABLE();
2138 }
2139 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002140 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002141 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002142 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002143 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2144 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2145 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2146 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002147 default: UNREACHABLE();
2148 }
2149 break;
2150 default:
2151 UNREACHABLE();
2152 }
2153 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002154 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002155 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002156 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002157 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002158 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002159 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002160 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002161 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2162 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2163 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002164 default: UNREACHABLE();
2165 }
2166 break;
2167 default: UNREACHABLE();
2168 }
2169 }
2170 else UNREACHABLE();
2171
2172 return 0;
2173}
2174
2175// This method needs to match OutputHLSL::decorate
2176std::string ProgramBinary::decorateAttribute(const std::string &name)
2177{
2178 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2179 {
2180 return "_" + name;
2181 }
2182
2183 return name;
2184}
2185
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002186bool ProgramBinary::isValidated() const
2187{
2188 return mValidated;
2189}
2190
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002191void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2192{
2193 // Skip over inactive attributes
2194 unsigned int activeAttribute = 0;
2195 unsigned int attribute;
2196 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2197 {
2198 if (mLinkedAttribute[attribute].name.empty())
2199 {
2200 continue;
2201 }
2202
2203 if (activeAttribute == index)
2204 {
2205 break;
2206 }
2207
2208 activeAttribute++;
2209 }
2210
2211 if (bufsize > 0)
2212 {
2213 const char *string = mLinkedAttribute[attribute].name.c_str();
2214
2215 strncpy(name, string, bufsize);
2216 name[bufsize - 1] = '\0';
2217
2218 if (length)
2219 {
2220 *length = strlen(name);
2221 }
2222 }
2223
2224 *size = 1; // Always a single 'type' instance
2225
2226 *type = mLinkedAttribute[attribute].type;
2227}
2228
2229GLint ProgramBinary::getActiveAttributeCount()
2230{
2231 int count = 0;
2232
2233 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2234 {
2235 if (!mLinkedAttribute[attributeIndex].name.empty())
2236 {
2237 count++;
2238 }
2239 }
2240
2241 return count;
2242}
2243
2244GLint ProgramBinary::getActiveAttributeMaxLength()
2245{
2246 int maxLength = 0;
2247
2248 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2249 {
2250 if (!mLinkedAttribute[attributeIndex].name.empty())
2251 {
2252 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2253 }
2254 }
2255
2256 return maxLength;
2257}
2258
2259void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2260{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002261 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002262
2263 if (bufsize > 0)
2264 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002265 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002266
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002267 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002268 {
2269 string += "[0]";
2270 }
2271
2272 strncpy(name, string.c_str(), bufsize);
2273 name[bufsize - 1] = '\0';
2274
2275 if (length)
2276 {
2277 *length = strlen(name);
2278 }
2279 }
2280
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002281 *size = mUniforms[index]->arraySize;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002282
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002283 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002284}
2285
2286GLint ProgramBinary::getActiveUniformCount()
2287{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002288 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002289}
2290
2291GLint ProgramBinary::getActiveUniformMaxLength()
2292{
2293 int maxLength = 0;
2294
2295 unsigned int numUniforms = mUniforms.size();
2296 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2297 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002298 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002299 {
2300 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2301 if (mUniforms[uniformIndex]->isArray())
2302 {
2303 length += 3; // Counting in "[0]".
2304 }
2305 maxLength = std::max(length, maxLength);
2306 }
2307 }
2308
2309 return maxLength;
2310}
2311
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002312void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002313{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002314 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002315 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002316 {
2317 mValidated = false;
2318 }
2319 else
2320 {
2321 mValidated = true;
2322 }
2323}
2324
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002325bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002326{
2327 // if any two active samplers in a program are of different types, but refer to the same
2328 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2329 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2330
2331 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2332 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2333
2334 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2335 {
2336 textureUnitType[i] = TEXTURE_UNKNOWN;
2337 }
2338
2339 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2340 {
2341 if (mSamplersPS[i].active)
2342 {
2343 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2344
2345 if (unit >= maxCombinedTextureImageUnits)
2346 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002347 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002348 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002349 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002350 }
2351
2352 return false;
2353 }
2354
2355 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2356 {
2357 if (mSamplersPS[i].textureType != textureUnitType[unit])
2358 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002359 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002360 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002361 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002362 }
2363
2364 return false;
2365 }
2366 }
2367 else
2368 {
2369 textureUnitType[unit] = mSamplersPS[i].textureType;
2370 }
2371 }
2372 }
2373
2374 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2375 {
2376 if (mSamplersVS[i].active)
2377 {
2378 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2379
2380 if (unit >= maxCombinedTextureImageUnits)
2381 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002382 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002383 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002384 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002385 }
2386
2387 return false;
2388 }
2389
2390 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2391 {
2392 if (mSamplersVS[i].textureType != textureUnitType[unit])
2393 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002394 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002395 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002396 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002397 }
2398
2399 return false;
2400 }
2401 }
2402 else
2403 {
2404 textureUnitType[unit] = mSamplersVS[i].textureType;
2405 }
2406 }
2407 }
2408
2409 return true;
2410}
2411
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002412void ProgramBinary::applyDxDepthRange(float near, float far, float diff)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002413{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002414 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2415 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2416
2417 if (mDxDepthRangeRegisterVS >= 0)
2418 {
2419 float nearFarDiff[4] = {near, far, diff};
2420 device->SetVertexShaderConstantF(mDxDepthRangeRegisterVS, nearFarDiff, 1);
2421 }
2422
2423 if (mDxDepthRangeRegisterPS >= 0)
2424 {
2425 float nearFarDiff[4] = {near, far, diff};
2426 device->SetPixelShaderConstantF(mDxDepthRangeRegisterPS, nearFarDiff, 1);
2427 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002428}
2429
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002430void ProgramBinary::applyDxDepthFront(float range, float start, float frontCCW)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002431{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002432 if (mDxDepthFrontRegister >= 0)
2433 {
2434 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2435 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2436
2437 GLfloat dz[4] = {range, start, frontCCW};
2438 device->SetPixelShaderConstantF(mDxDepthFrontRegister, dz, 1);
2439 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002440}
2441
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002442void ProgramBinary::applyDxCoord(float halfWidth, float halfHeight, float x0, float y0)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002443{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002444 if (mDxCoordRegister >= 0)
2445 {
2446 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2447 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2448
2449 GLfloat whxy[4] = {halfWidth,halfHeight, x0, y0};
2450 device->SetPixelShaderConstantF(mDxCoordRegister, whxy, 1);
2451 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002452}
2453
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002454void ProgramBinary::applyDxHalfPixelSize(float width, float height)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002455{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002456 if (mDxHalfPixelSizeRegister >= 0)
2457 {
2458 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2459 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2460
2461 GLfloat xy[4] = {width, height};
2462 device->SetVertexShaderConstantF(mDxHalfPixelSizeRegister, xy, 1);
2463 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002464}
2465
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002466ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2467{
2468}
2469
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002470}