blob: 186c6cf7f5641de7effb2b50b350d65c43898bb7 [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
2// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000010#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000011#include "libGLESv2/ProgramBinary.h"
12
13#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000014#include "common/version.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/Shader.h"
18#include "libGLESv2/utilities.h"
19
20#include <string>
21
daniel@transgaming.com88853c52012-12-20 20:56:40 +000022#undef near
23#undef far
24
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000025namespace gl
26{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000027std::string str(int i)
28{
29 char buffer[20];
30 snprintf(buffer, sizeof(buffer), "%d", i);
31 return buffer;
32}
33
34Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
35 : type(type), _name(_name), name(ProgramBinary::undecorateUniform(_name)), arraySize(arraySize)
36{
37 int bytes = UniformInternalSize(type) * arraySize;
38 data = new unsigned char[bytes];
39 memset(data, 0, bytes);
40 dirty = true;
41}
42
43Uniform::~Uniform()
44{
45 delete[] data;
46}
47
48bool Uniform::isArray()
49{
daniel@transgaming.com22ba0f72012-09-27 17:46:04 +000050 size_t dot = _name.find_last_of('.');
51 if (dot == std::string::npos) dot = -1;
52
53 return _name.compare(dot + 1, dot + 4, "ar_") == 0;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000054}
55
56UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
57 : name(ProgramBinary::undecorateUniform(_name)), element(element), index(index)
58{
59}
60
daniel@transgaming.come87ca002012-07-24 18:30:43 +000061unsigned int ProgramBinary::mCurrentSerial = 1;
62
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000063ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000064{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000065 mPixelExecutable = NULL;
66 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000067
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000068 mValidated = false;
69
70 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
71 {
72 mSemanticIndex[index] = -1;
73 }
74
75 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
76 {
77 mSamplersPS[index].active = false;
78 }
79
80 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
81 {
82 mSamplersVS[index].active = false;
83 }
84
85 mUsedVertexSamplerRange = 0;
86 mUsedPixelSamplerRange = 0;
87
daniel@transgaming.com593ebc42012-12-20 20:56:46 +000088 mDxDepthRangeRegisterVS = -1;
89 mDxDepthRangeRegisterPS = -1;
90 mDxDepthFrontRegister = -1;
91 mDxCoordRegister = -1;
92 mDxHalfPixelSizeRegister = -1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000093}
94
95ProgramBinary::~ProgramBinary()
96{
daniel@transgaming.com95892412012-11-28 20:59:09 +000097 delete mPixelExecutable;
98 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000099
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000100 while (!mUniforms.empty())
101 {
102 delete mUniforms.back();
103 mUniforms.pop_back();
104 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000105}
106
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000107unsigned int ProgramBinary::getSerial() const
108{
109 return mSerial;
110}
111
112unsigned int ProgramBinary::issueSerial()
113{
114 return mCurrentSerial++;
115}
116
daniel@transgaming.com95892412012-11-28 20:59:09 +0000117rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000118{
119 return mPixelExecutable;
120}
121
daniel@transgaming.com95892412012-11-28 20:59:09 +0000122rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000123{
124 return mVertexExecutable;
125}
126
127GLuint ProgramBinary::getAttributeLocation(const char *name)
128{
129 if (name)
130 {
131 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
132 {
133 if (mLinkedAttribute[index].name == std::string(name))
134 {
135 return index;
136 }
137 }
138 }
139
140 return -1;
141}
142
143int ProgramBinary::getSemanticIndex(int attributeIndex)
144{
145 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
146
147 return mSemanticIndex[attributeIndex];
148}
149
150// Returns one more than the highest sampler index used.
151GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
152{
153 switch (type)
154 {
155 case SAMPLER_PIXEL:
156 return mUsedPixelSamplerRange;
157 case SAMPLER_VERTEX:
158 return mUsedVertexSamplerRange;
159 default:
160 UNREACHABLE();
161 return 0;
162 }
163}
164
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000165bool ProgramBinary::usesPointSize() const
166{
167 return mUsesPointSize;
168}
169
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000170// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
171// index (0-15 for the pixel shader and 0-3 for the vertex shader).
172GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
173{
174 GLint logicalTextureUnit = -1;
175
176 switch (type)
177 {
178 case SAMPLER_PIXEL:
179 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
180
181 if (mSamplersPS[samplerIndex].active)
182 {
183 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
184 }
185 break;
186 case SAMPLER_VERTEX:
187 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
188
189 if (mSamplersVS[samplerIndex].active)
190 {
191 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
192 }
193 break;
194 default: UNREACHABLE();
195 }
196
197 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
198 {
199 return logicalTextureUnit;
200 }
201
202 return -1;
203}
204
205// Returns the texture type for a given Direct3D 9 sampler type and
206// index (0-15 for the pixel shader and 0-3 for the vertex shader).
207TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
208{
209 switch (type)
210 {
211 case SAMPLER_PIXEL:
212 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
213 ASSERT(mSamplersPS[samplerIndex].active);
214 return mSamplersPS[samplerIndex].textureType;
215 case SAMPLER_VERTEX:
216 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
217 ASSERT(mSamplersVS[samplerIndex].active);
218 return mSamplersVS[samplerIndex].textureType;
219 default: UNREACHABLE();
220 }
221
222 return TEXTURE_2D;
223}
224
225GLint ProgramBinary::getUniformLocation(std::string name)
226{
227 unsigned int subscript = 0;
228
229 // Strip any trailing array operator and retrieve the subscript
230 size_t open = name.find_last_of('[');
231 size_t close = name.find_last_of(']');
232 if (open != std::string::npos && close == name.length() - 1)
233 {
234 subscript = atoi(name.substr(open + 1).c_str());
235 name.erase(open);
236 }
237
238 unsigned int numUniforms = mUniformIndex.size();
239 for (unsigned int location = 0; location < numUniforms; location++)
240 {
241 if (mUniformIndex[location].name == name &&
242 mUniformIndex[location].element == subscript)
243 {
244 return location;
245 }
246 }
247
248 return -1;
249}
250
251bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
252{
253 if (location < 0 || location >= (int)mUniformIndex.size())
254 {
255 return false;
256 }
257
258 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
259 targetUniform->dirty = true;
260
261 if (targetUniform->type == GL_FLOAT)
262 {
263 int arraySize = targetUniform->arraySize;
264
265 if (arraySize == 1 && count > 1)
266 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
267
268 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
269
270 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
271
272 for (int i = 0; i < count; i++)
273 {
274 target[0] = v[0];
275 target[1] = 0;
276 target[2] = 0;
277 target[3] = 0;
278 target += 4;
279 v += 1;
280 }
281 }
282 else if (targetUniform->type == GL_BOOL)
283 {
284 int arraySize = targetUniform->arraySize;
285
286 if (arraySize == 1 && count > 1)
287 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
288
289 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
290 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
291
292 for (int i = 0; i < count; ++i)
293 {
294 if (v[i] == 0.0f)
295 {
296 boolParams[i] = GL_FALSE;
297 }
298 else
299 {
300 boolParams[i] = GL_TRUE;
301 }
302 }
303 }
304 else
305 {
306 return false;
307 }
308
309 return true;
310}
311
312bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
313{
314 if (location < 0 || location >= (int)mUniformIndex.size())
315 {
316 return false;
317 }
318
319 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
320 targetUniform->dirty = true;
321
322 if (targetUniform->type == GL_FLOAT_VEC2)
323 {
324 int arraySize = targetUniform->arraySize;
325
326 if (arraySize == 1 && count > 1)
327 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
328
329 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
330
331 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
332
333 for (int i = 0; i < count; i++)
334 {
335 target[0] = v[0];
336 target[1] = v[1];
337 target[2] = 0;
338 target[3] = 0;
339 target += 4;
340 v += 2;
341 }
342 }
343 else if (targetUniform->type == GL_BOOL_VEC2)
344 {
345 int arraySize = targetUniform->arraySize;
346
347 if (arraySize == 1 && count > 1)
348 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
349
350 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
351
352 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
353
354 for (int i = 0; i < count * 2; ++i)
355 {
356 if (v[i] == 0.0f)
357 {
358 boolParams[i] = GL_FALSE;
359 }
360 else
361 {
362 boolParams[i] = GL_TRUE;
363 }
364 }
365 }
366 else
367 {
368 return false;
369 }
370
371 return true;
372}
373
374bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
375{
376 if (location < 0 || location >= (int)mUniformIndex.size())
377 {
378 return false;
379 }
380
381 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
382 targetUniform->dirty = true;
383
384 if (targetUniform->type == GL_FLOAT_VEC3)
385 {
386 int arraySize = targetUniform->arraySize;
387
388 if (arraySize == 1 && count > 1)
389 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
390
391 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
392
393 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
394
395 for (int i = 0; i < count; i++)
396 {
397 target[0] = v[0];
398 target[1] = v[1];
399 target[2] = v[2];
400 target[3] = 0;
401 target += 4;
402 v += 3;
403 }
404 }
405 else if (targetUniform->type == GL_BOOL_VEC3)
406 {
407 int arraySize = targetUniform->arraySize;
408
409 if (arraySize == 1 && count > 1)
410 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
411
412 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
413 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
414
415 for (int i = 0; i < count * 3; ++i)
416 {
417 if (v[i] == 0.0f)
418 {
419 boolParams[i] = GL_FALSE;
420 }
421 else
422 {
423 boolParams[i] = GL_TRUE;
424 }
425 }
426 }
427 else
428 {
429 return false;
430 }
431
432 return true;
433}
434
435bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
436{
437 if (location < 0 || location >= (int)mUniformIndex.size())
438 {
439 return false;
440 }
441
442 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
443 targetUniform->dirty = true;
444
445 if (targetUniform->type == GL_FLOAT_VEC4)
446 {
447 int arraySize = targetUniform->arraySize;
448
449 if (arraySize == 1 && count > 1)
450 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
451
452 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
453
454 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
455 v, 4 * sizeof(GLfloat) * count);
456 }
457 else if (targetUniform->type == GL_BOOL_VEC4)
458 {
459 int arraySize = targetUniform->arraySize;
460
461 if (arraySize == 1 && count > 1)
462 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
463
464 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
465 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
466
467 for (int i = 0; i < count * 4; ++i)
468 {
469 if (v[i] == 0.0f)
470 {
471 boolParams[i] = GL_FALSE;
472 }
473 else
474 {
475 boolParams[i] = GL_TRUE;
476 }
477 }
478 }
479 else
480 {
481 return false;
482 }
483
484 return true;
485}
486
487template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
488void transposeMatrix(T *target, const GLfloat *value)
489{
490 int copyWidth = std::min(targetWidth, srcWidth);
491 int copyHeight = std::min(targetHeight, srcHeight);
492
493 for (int x = 0; x < copyWidth; x++)
494 {
495 for (int y = 0; y < copyHeight; y++)
496 {
497 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
498 }
499 }
500 // clear unfilled right side
501 for (int y = 0; y < copyHeight; y++)
502 {
503 for (int x = srcWidth; x < targetWidth; x++)
504 {
505 target[y * targetWidth + x] = (T)0;
506 }
507 }
508 // clear unfilled bottom.
509 for (int y = srcHeight; y < targetHeight; y++)
510 {
511 for (int x = 0; x < targetWidth; x++)
512 {
513 target[y * targetWidth + x] = (T)0;
514 }
515 }
516}
517
518bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
519{
520 if (location < 0 || location >= (int)mUniformIndex.size())
521 {
522 return false;
523 }
524
525 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
526 targetUniform->dirty = true;
527
528 if (targetUniform->type != GL_FLOAT_MAT2)
529 {
530 return false;
531 }
532
533 int arraySize = targetUniform->arraySize;
534
535 if (arraySize == 1 && count > 1)
536 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
537
538 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
539
540 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
541 for (int i = 0; i < count; i++)
542 {
543 transposeMatrix<GLfloat,4,2,2,2>(target, value);
544 target += 8;
545 value += 4;
546 }
547
548 return true;
549}
550
551bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
552{
553 if (location < 0 || location >= (int)mUniformIndex.size())
554 {
555 return false;
556 }
557
558 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
559 targetUniform->dirty = true;
560
561 if (targetUniform->type != GL_FLOAT_MAT3)
562 {
563 return false;
564 }
565
566 int arraySize = targetUniform->arraySize;
567
568 if (arraySize == 1 && count > 1)
569 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
570
571 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
572
573 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
574 for (int i = 0; i < count; i++)
575 {
576 transposeMatrix<GLfloat,4,3,3,3>(target, value);
577 target += 12;
578 value += 9;
579 }
580
581 return true;
582}
583
584
585bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
586{
587 if (location < 0 || location >= (int)mUniformIndex.size())
588 {
589 return false;
590 }
591
592 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
593 targetUniform->dirty = true;
594
595 if (targetUniform->type != GL_FLOAT_MAT4)
596 {
597 return false;
598 }
599
600 int arraySize = targetUniform->arraySize;
601
602 if (arraySize == 1 && count > 1)
603 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
604
605 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
606
607 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
608 for (int i = 0; i < count; i++)
609 {
610 transposeMatrix<GLfloat,4,4,4,4>(target, value);
611 target += 16;
612 value += 16;
613 }
614
615 return true;
616}
617
618bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
619{
620 if (location < 0 || location >= (int)mUniformIndex.size())
621 {
622 return false;
623 }
624
625 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
626 targetUniform->dirty = true;
627
628 if (targetUniform->type == GL_INT ||
629 targetUniform->type == GL_SAMPLER_2D ||
630 targetUniform->type == GL_SAMPLER_CUBE)
631 {
632 int arraySize = targetUniform->arraySize;
633
634 if (arraySize == 1 && count > 1)
635 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
636
637 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
638
639 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
640 v, sizeof(GLint) * count);
641 }
642 else if (targetUniform->type == GL_BOOL)
643 {
644 int arraySize = targetUniform->arraySize;
645
646 if (arraySize == 1 && count > 1)
647 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
648
649 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
650 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
651
652 for (int i = 0; i < count; ++i)
653 {
654 if (v[i] == 0)
655 {
656 boolParams[i] = GL_FALSE;
657 }
658 else
659 {
660 boolParams[i] = GL_TRUE;
661 }
662 }
663 }
664 else
665 {
666 return false;
667 }
668
669 return true;
670}
671
672bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
673{
674 if (location < 0 || location >= (int)mUniformIndex.size())
675 {
676 return false;
677 }
678
679 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
680 targetUniform->dirty = true;
681
682 if (targetUniform->type == GL_INT_VEC2)
683 {
684 int arraySize = targetUniform->arraySize;
685
686 if (arraySize == 1 && count > 1)
687 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
688
689 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
690
691 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
692 v, 2 * sizeof(GLint) * count);
693 }
694 else if (targetUniform->type == GL_BOOL_VEC2)
695 {
696 int arraySize = targetUniform->arraySize;
697
698 if (arraySize == 1 && count > 1)
699 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
700
701 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
702 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
703
704 for (int i = 0; i < count * 2; ++i)
705 {
706 if (v[i] == 0)
707 {
708 boolParams[i] = GL_FALSE;
709 }
710 else
711 {
712 boolParams[i] = GL_TRUE;
713 }
714 }
715 }
716 else
717 {
718 return false;
719 }
720
721 return true;
722}
723
724bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
725{
726 if (location < 0 || location >= (int)mUniformIndex.size())
727 {
728 return false;
729 }
730
731 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
732 targetUniform->dirty = true;
733
734 if (targetUniform->type == GL_INT_VEC3)
735 {
736 int arraySize = targetUniform->arraySize;
737
738 if (arraySize == 1 && count > 1)
739 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
740
741 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
742
743 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
744 v, 3 * sizeof(GLint) * count);
745 }
746 else if (targetUniform->type == GL_BOOL_VEC3)
747 {
748 int arraySize = targetUniform->arraySize;
749
750 if (arraySize == 1 && count > 1)
751 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
752
753 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
754 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
755
756 for (int i = 0; i < count * 3; ++i)
757 {
758 if (v[i] == 0)
759 {
760 boolParams[i] = GL_FALSE;
761 }
762 else
763 {
764 boolParams[i] = GL_TRUE;
765 }
766 }
767 }
768 else
769 {
770 return false;
771 }
772
773 return true;
774}
775
776bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
777{
778 if (location < 0 || location >= (int)mUniformIndex.size())
779 {
780 return false;
781 }
782
783 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
784 targetUniform->dirty = true;
785
786 if (targetUniform->type == GL_INT_VEC4)
787 {
788 int arraySize = targetUniform->arraySize;
789
790 if (arraySize == 1 && count > 1)
791 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
792
793 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
794
795 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
796 v, 4 * sizeof(GLint) * count);
797 }
798 else if (targetUniform->type == GL_BOOL_VEC4)
799 {
800 int arraySize = targetUniform->arraySize;
801
802 if (arraySize == 1 && count > 1)
803 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
804
805 count = std::min(arraySize - (int)mUniformIndex[location].element, count);
806 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
807
808 for (int i = 0; i < count * 4; ++i)
809 {
810 if (v[i] == 0)
811 {
812 boolParams[i] = GL_FALSE;
813 }
814 else
815 {
816 boolParams[i] = GL_TRUE;
817 }
818 }
819 }
820 else
821 {
822 return false;
823 }
824
825 return true;
826}
827
828bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
829{
830 if (location < 0 || location >= (int)mUniformIndex.size())
831 {
832 return false;
833 }
834
835 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
836
837 // sized queries -- ensure the provided buffer is large enough
838 if (bufSize)
839 {
840 int requiredBytes = UniformExternalSize(targetUniform->type);
841 if (*bufSize < requiredBytes)
842 {
843 return false;
844 }
845 }
846
847 switch (targetUniform->type)
848 {
849 case GL_FLOAT_MAT2:
850 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
851 break;
852 case GL_FLOAT_MAT3:
853 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
854 break;
855 case GL_FLOAT_MAT4:
856 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
857 break;
858 default:
859 {
860 unsigned int count = UniformExternalComponentCount(targetUniform->type);
861 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
862
863 switch (UniformComponentType(targetUniform->type))
864 {
865 case GL_BOOL:
866 {
867 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
868
869 for (unsigned int i = 0; i < count; ++i)
870 {
871 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
872 }
873 }
874 break;
875 case GL_FLOAT:
876 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
877 count * sizeof(GLfloat));
878 break;
879 case GL_INT:
880 {
881 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
882
883 for (unsigned int i = 0; i < count; ++i)
884 {
885 params[i] = (float)intParams[i];
886 }
887 }
888 break;
889 default: UNREACHABLE();
890 }
891 }
892 }
893
894 return true;
895}
896
897bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
898{
899 if (location < 0 || location >= (int)mUniformIndex.size())
900 {
901 return false;
902 }
903
904 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
905
906 // sized queries -- ensure the provided buffer is large enough
907 if (bufSize)
908 {
909 int requiredBytes = UniformExternalSize(targetUniform->type);
910 if (*bufSize < requiredBytes)
911 {
912 return false;
913 }
914 }
915
916 switch (targetUniform->type)
917 {
918 case GL_FLOAT_MAT2:
919 {
920 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
921 }
922 break;
923 case GL_FLOAT_MAT3:
924 {
925 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
926 }
927 break;
928 case GL_FLOAT_MAT4:
929 {
930 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
931 }
932 break;
933 default:
934 {
935 unsigned int count = UniformExternalComponentCount(targetUniform->type);
936 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
937
938 switch (UniformComponentType(targetUniform->type))
939 {
940 case GL_BOOL:
941 {
942 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
943
944 for (unsigned int i = 0; i < count; ++i)
945 {
946 params[i] = (GLint)boolParams[i];
947 }
948 }
949 break;
950 case GL_FLOAT:
951 {
952 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
953
954 for (unsigned int i = 0; i < count; ++i)
955 {
956 params[i] = (GLint)floatParams[i];
957 }
958 }
959 break;
960 case GL_INT:
961 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
962 count * sizeof(GLint));
963 break;
964 default: UNREACHABLE();
965 }
966 }
967 }
968
969 return true;
970}
971
972void ProgramBinary::dirtyAllUniforms()
973{
974 unsigned int numUniforms = mUniforms.size();
975 for (unsigned int index = 0; index < numUniforms; index++)
976 {
977 mUniforms[index]->dirty = true;
978 }
979}
980
981// Applies all the uniforms set for this program object to the Direct3D 9 device
982void ProgramBinary::applyUniforms()
983{
daniel@transgaming.com77fbf972012-11-28 21:02:55 +0000984 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) // D3D9_REPLACE
985 {
986 return; // UNIMPLEMENTED
987 }
988
989 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
990
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000991 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) {
992 Uniform *targetUniform = *ub;
993
994 if (targetUniform->dirty)
995 {
996 int arraySize = targetUniform->arraySize;
997 GLfloat *f = (GLfloat*)targetUniform->data;
998 GLint *i = (GLint*)targetUniform->data;
999 GLboolean *b = (GLboolean*)targetUniform->data;
1000
1001 switch (targetUniform->type)
1002 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001003 case GL_BOOL: applyUniformnbv(device, targetUniform, arraySize, 1, b); break;
1004 case GL_BOOL_VEC2: applyUniformnbv(device, targetUniform, arraySize, 2, b); break;
1005 case GL_BOOL_VEC3: applyUniformnbv(device, targetUniform, arraySize, 3, b); break;
1006 case GL_BOOL_VEC4: applyUniformnbv(device, targetUniform, arraySize, 4, b); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001007 case GL_FLOAT:
1008 case GL_FLOAT_VEC2:
1009 case GL_FLOAT_VEC3:
1010 case GL_FLOAT_VEC4:
1011 case GL_FLOAT_MAT2:
1012 case GL_FLOAT_MAT3:
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001013 case GL_FLOAT_MAT4: applyUniformnfv(device, targetUniform, f); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001014 case GL_SAMPLER_2D:
1015 case GL_SAMPLER_CUBE:
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00001016 case GL_INT: applyUniform1iv(device, targetUniform, arraySize, i); break;
1017 case GL_INT_VEC2: applyUniform2iv(device, targetUniform, arraySize, i); break;
1018 case GL_INT_VEC3: applyUniform3iv(device, targetUniform, arraySize, i); break;
1019 case GL_INT_VEC4: applyUniform4iv(device, targetUniform, arraySize, i); break;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001020 default:
1021 UNREACHABLE();
1022 }
1023
1024 targetUniform->dirty = false;
1025 }
1026 }
1027}
1028
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001029// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1030// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001031int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001032{
1033 Context *context = getContext();
1034 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1035
1036 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1037 {
1038 int n = VariableRowCount(varying->type) * varying->size;
1039 int m = VariableColumnCount(varying->type);
1040 bool success = false;
1041
1042 if (m == 2 || m == 3 || m == 4)
1043 {
1044 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1045 {
1046 bool available = true;
1047
1048 for (int y = 0; y < n && available; y++)
1049 {
1050 for (int x = 0; x < m && available; x++)
1051 {
1052 if (packing[r + y][x])
1053 {
1054 available = false;
1055 }
1056 }
1057 }
1058
1059 if (available)
1060 {
1061 varying->reg = r;
1062 varying->col = 0;
1063
1064 for (int y = 0; y < n; y++)
1065 {
1066 for (int x = 0; x < m; x++)
1067 {
1068 packing[r + y][x] = &*varying;
1069 }
1070 }
1071
1072 success = true;
1073 }
1074 }
1075
1076 if (!success && m == 2)
1077 {
1078 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1079 {
1080 bool available = true;
1081
1082 for (int y = 0; y < n && available; y++)
1083 {
1084 for (int x = 2; x < 4 && available; x++)
1085 {
1086 if (packing[r + y][x])
1087 {
1088 available = false;
1089 }
1090 }
1091 }
1092
1093 if (available)
1094 {
1095 varying->reg = r;
1096 varying->col = 2;
1097
1098 for (int y = 0; y < n; y++)
1099 {
1100 for (int x = 2; x < 4; x++)
1101 {
1102 packing[r + y][x] = &*varying;
1103 }
1104 }
1105
1106 success = true;
1107 }
1108 }
1109 }
1110 }
1111 else if (m == 1)
1112 {
1113 int space[4] = {0};
1114
1115 for (int y = 0; y < maxVaryingVectors; y++)
1116 {
1117 for (int x = 0; x < 4; x++)
1118 {
1119 space[x] += packing[y][x] ? 0 : 1;
1120 }
1121 }
1122
1123 int column = 0;
1124
1125 for (int x = 0; x < 4; x++)
1126 {
1127 if (space[x] >= n && space[x] < space[column])
1128 {
1129 column = x;
1130 }
1131 }
1132
1133 if (space[column] >= n)
1134 {
1135 for (int r = 0; r < maxVaryingVectors; r++)
1136 {
1137 if (!packing[r][column])
1138 {
1139 varying->reg = r;
1140
1141 for (int y = r; y < r + n; y++)
1142 {
1143 packing[y][column] = &*varying;
1144 }
1145
1146 break;
1147 }
1148 }
1149
1150 varying->col = column;
1151
1152 success = true;
1153 }
1154 }
1155 else UNREACHABLE();
1156
1157 if (!success)
1158 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001159 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001160
1161 return -1;
1162 }
1163 }
1164
1165 // Return the number of used registers
1166 int registers = 0;
1167
1168 for (int r = 0; r < maxVaryingVectors; r++)
1169 {
1170 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1171 {
1172 registers++;
1173 }
1174 }
1175
1176 return registers;
1177}
1178
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001179bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001180{
1181 if (pixelHLSL.empty() || vertexHLSL.empty())
1182 {
1183 return false;
1184 }
1185
1186 // Reset the varying register assignments
1187 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1188 {
1189 fragVar->reg = -1;
1190 fragVar->col = -1;
1191 }
1192
1193 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1194 {
1195 vtxVar->reg = -1;
1196 vtxVar->col = -1;
1197 }
1198
1199 // Map the varyings to the register file
1200 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001201 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001202
1203 if (registers < 0)
1204 {
1205 return false;
1206 }
1207
1208 // Write the HLSL input/output declarations
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001209 const bool sm3 = (mRenderer->getMajorShaderModel() >= 3);
1210 const bool sm4 = (mRenderer->getMajorShaderModel() >= 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001211 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001212 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1213
1214 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1215 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001216 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001217
1218 return false;
1219 }
1220
1221 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1222 {
1223 bool matched = false;
1224
1225 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1226 {
1227 if (output->name == input->name)
1228 {
1229 if (output->type != input->type || output->size != input->size)
1230 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001231 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 +00001232
1233 return false;
1234 }
1235
1236 output->reg = input->reg;
1237 output->col = input->col;
1238
1239 matched = true;
1240 break;
1241 }
1242 }
1243
1244 if (!matched)
1245 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001246 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001247
1248 return false;
1249 }
1250 }
1251
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001252 mUsesPointSize = vertexShader->mUsesPointSize;
1253 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001254 std::string targetSemantic = sm4 ? "SV_Target" : "COLOR";
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001255 std::string positionSemantic = sm4 ? "SV_POSITION" : "POSITION";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001256
1257 vertexHLSL += "struct VS_INPUT\n"
1258 "{\n";
1259
1260 int semanticIndex = 0;
1261 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1262 {
1263 switch (attribute->type)
1264 {
1265 case GL_FLOAT: vertexHLSL += " float "; break;
1266 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1267 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1268 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1269 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1270 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1271 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1272 default: UNREACHABLE();
1273 }
1274
1275 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1276
1277 semanticIndex += VariableRowCount(attribute->type);
1278 }
1279
1280 vertexHLSL += "};\n"
1281 "\n"
1282 "struct VS_OUTPUT\n"
1283 "{\n"
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001284 " float4 gl_Position : " + positionSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001285
1286 for (int r = 0; r < registers; r++)
1287 {
1288 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1289
1290 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1291 }
1292
1293 if (fragmentShader->mUsesFragCoord)
1294 {
1295 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1296 }
1297
1298 if (vertexShader->mUsesPointSize && sm3)
1299 {
1300 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1301 }
1302
1303 vertexHLSL += "};\n"
1304 "\n"
1305 "VS_OUTPUT main(VS_INPUT input)\n"
1306 "{\n";
1307
1308 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1309 {
1310 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1311
1312 if (VariableRowCount(attribute->type) > 1) // Matrix
1313 {
1314 vertexHLSL += "transpose";
1315 }
1316
1317 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1318 }
1319
1320 vertexHLSL += "\n"
1321 " gl_main();\n"
1322 "\n"
1323 " VS_OUTPUT output;\n"
1324 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001325 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001326 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1327 " output.gl_Position.w = gl_Position.w;\n";
1328
1329 if (vertexShader->mUsesPointSize && sm3)
1330 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001331 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001332 }
1333
1334 if (fragmentShader->mUsesFragCoord)
1335 {
1336 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1337 }
1338
1339 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1340 {
1341 if (varying->reg >= 0)
1342 {
1343 for (int i = 0; i < varying->size; i++)
1344 {
1345 int rows = VariableRowCount(varying->type);
1346
1347 for (int j = 0; j < rows; j++)
1348 {
1349 int r = varying->reg + i * rows + j;
1350 vertexHLSL += " output.v" + str(r);
1351
1352 bool sharedRegister = false; // Register used by multiple varyings
1353
1354 for (int x = 0; x < 4; x++)
1355 {
1356 if (packing[r][x] && packing[r][x] != packing[r][0])
1357 {
1358 sharedRegister = true;
1359 break;
1360 }
1361 }
1362
1363 if(sharedRegister)
1364 {
1365 vertexHLSL += ".";
1366
1367 for (int x = 0; x < 4; x++)
1368 {
1369 if (packing[r][x] == &*varying)
1370 {
1371 switch(x)
1372 {
1373 case 0: vertexHLSL += "x"; break;
1374 case 1: vertexHLSL += "y"; break;
1375 case 2: vertexHLSL += "z"; break;
1376 case 3: vertexHLSL += "w"; break;
1377 }
1378 }
1379 }
1380 }
1381
1382 vertexHLSL += " = " + varying->name;
1383
1384 if (varying->array)
1385 {
1386 vertexHLSL += "[" + str(i) + "]";
1387 }
1388
1389 if (rows > 1)
1390 {
1391 vertexHLSL += "[" + str(j) + "]";
1392 }
1393
1394 vertexHLSL += ";\n";
1395 }
1396 }
1397 }
1398 }
1399
1400 vertexHLSL += "\n"
1401 " return output;\n"
1402 "}\n";
1403
1404 pixelHLSL += "struct PS_INPUT\n"
1405 "{\n";
1406
1407 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1408 {
1409 if (varying->reg >= 0)
1410 {
1411 for (int i = 0; i < varying->size; i++)
1412 {
1413 int rows = VariableRowCount(varying->type);
1414 for (int j = 0; j < rows; j++)
1415 {
1416 std::string n = str(varying->reg + i * rows + j);
1417 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1418 }
1419 }
1420 }
1421 else UNREACHABLE();
1422 }
1423
1424 if (fragmentShader->mUsesFragCoord)
1425 {
1426 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1427 if (sm3) {
1428 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1429 }
1430 }
1431
1432 if (fragmentShader->mUsesPointCoord && sm3)
1433 {
1434 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1435 }
1436
1437 if (fragmentShader->mUsesFrontFacing)
1438 {
1439 pixelHLSL += " float vFace : VFACE;\n";
1440 }
1441
1442 pixelHLSL += "};\n"
1443 "\n"
1444 "struct PS_OUTPUT\n"
1445 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001446 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001447 "};\n"
1448 "\n"
1449 "PS_OUTPUT main(PS_INPUT input)\n"
1450 "{\n";
1451
1452 if (fragmentShader->mUsesFragCoord)
1453 {
1454 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1455
1456 if (sm3)
1457 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001458 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001459 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001460 }
1461 else
1462 {
1463 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1464 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001465 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001466 }
1467
daniel@transgaming.com12985182012-12-20 20:56:31 +00001468 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001469 " gl_FragCoord.w = rhw;\n";
1470 }
1471
1472 if (fragmentShader->mUsesPointCoord && sm3)
1473 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001474 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1475 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001476 }
1477
1478 if (fragmentShader->mUsesFrontFacing)
1479 {
daniel@transgaming.com12985182012-12-20 20:56:31 +00001480 pixelHLSL += " gl_FrontFacing = (input.vFace * dx_DepthFront.z >= 0.0);\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001481 }
1482
1483 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1484 {
1485 if (varying->reg >= 0)
1486 {
1487 for (int i = 0; i < varying->size; i++)
1488 {
1489 int rows = VariableRowCount(varying->type);
1490 for (int j = 0; j < rows; j++)
1491 {
1492 std::string n = str(varying->reg + i * rows + j);
1493 pixelHLSL += " " + varying->name;
1494
1495 if (varying->array)
1496 {
1497 pixelHLSL += "[" + str(i) + "]";
1498 }
1499
1500 if (rows > 1)
1501 {
1502 pixelHLSL += "[" + str(j) + "]";
1503 }
1504
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001505 switch (VariableColumnCount(varying->type))
1506 {
1507 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1508 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1509 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1510 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1511 default: UNREACHABLE();
1512 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001513 }
1514 }
1515 }
1516 else UNREACHABLE();
1517 }
1518
1519 pixelHLSL += "\n"
1520 " gl_main();\n"
1521 "\n"
1522 " PS_OUTPUT output;\n"
1523 " output.gl_Color[0] = gl_Color[0];\n"
1524 "\n"
1525 " return output;\n"
1526 "}\n";
1527
1528 return true;
1529}
1530
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001531bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1532{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001533 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001534
1535 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001536 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001537 if (format != GL_PROGRAM_BINARY_ANGLE)
1538 {
1539 infoLog.append("Invalid program binary format.");
1540 return false;
1541 }
1542
1543 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001544 stream.read(&version);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001545 if (version != BUILD_REVISION)
1546 {
1547 infoLog.append("Invalid program binary version.");
1548 return false;
1549 }
1550
1551 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1552 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001553 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001554 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001555 stream.read(&name);
1556 mLinkedAttribute[i].name = name;
1557 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001558 }
1559
1560 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1561 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001562 stream.read(&mSamplersPS[i].active);
1563 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001564
1565 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001566 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001567 mSamplersPS[i].textureType = (TextureType) textureType;
1568 }
1569
1570 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1571 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001572 stream.read(&mSamplersVS[i].active);
1573 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001574
1575 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001576 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001577 mSamplersVS[i].textureType = (TextureType) textureType;
1578 }
1579
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001580 stream.read(&mUsedVertexSamplerRange);
1581 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001582
1583 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001584 stream.read(&size);
1585 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001586 {
1587 infoLog.append("Invalid program binary.");
1588 return false;
1589 }
1590
1591 mUniforms.resize(size);
1592 for (unsigned int i = 0; i < size; ++i)
1593 {
1594 GLenum type;
1595 std::string _name;
1596 unsigned int arraySize;
1597
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001598 stream.read(&type);
1599 stream.read(&_name);
1600 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001601
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001602 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001603
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001604 stream.read(&mUniforms[i]->ps.float4Index);
1605 stream.read(&mUniforms[i]->ps.samplerIndex);
1606 stream.read(&mUniforms[i]->ps.boolIndex);
1607 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001608
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001609 stream.read(&mUniforms[i]->vs.float4Index);
1610 stream.read(&mUniforms[i]->vs.samplerIndex);
1611 stream.read(&mUniforms[i]->vs.boolIndex);
1612 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001613 }
1614
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001615 stream.read(&size);
1616 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001617 {
1618 infoLog.append("Invalid program binary.");
1619 return false;
1620 }
1621
1622 mUniformIndex.resize(size);
1623 for (unsigned int i = 0; i < size; ++i)
1624 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001625 stream.read(&mUniformIndex[i].name);
1626 stream.read(&mUniformIndex[i].element);
1627 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001628 }
1629
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001630 stream.read(&mDxDepthRangeRegisterVS);
1631 stream.read(&mDxDepthRangeRegisterPS);
1632 stream.read(&mDxDepthFrontRegister);
1633 stream.read(&mDxCoordRegister);
1634 stream.read(&mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001635
1636 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001637 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001638
1639 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001640 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001641
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001642 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001643
daniel@transgaming.com36038542012-11-28 20:59:26 +00001644 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001645 ptr += sizeof(GUID);
1646
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001647 GUID identifier = mRenderer->getAdapterIdentifier();
1648 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001649 {
1650 infoLog.append("Invalid program binary.");
1651 return false;
1652 }
1653
1654 const char *pixelShaderFunction = ptr;
1655 ptr += pixelShaderSize;
1656
1657 const char *vertexShaderFunction = ptr;
1658 ptr += vertexShaderSize;
1659
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001660 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1661 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1662 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001663 {
1664 infoLog.append("Could not create pixel shader.");
1665 return false;
1666 }
1667
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001668 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1669 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1670 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001671 {
1672 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001673 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001674 mPixelExecutable = NULL;
1675 return false;
1676 }
1677
1678 return true;
1679}
1680
1681bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1682{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001683 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001684
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001685 stream.write(GL_PROGRAM_BINARY_ANGLE);
1686 stream.write(BUILD_REVISION);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001687
1688 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1689 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001690 stream.write(mLinkedAttribute[i].type);
1691 stream.write(mLinkedAttribute[i].name);
1692 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001693 }
1694
1695 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1696 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001697 stream.write(mSamplersPS[i].active);
1698 stream.write(mSamplersPS[i].logicalTextureUnit);
1699 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001700 }
1701
1702 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1703 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001704 stream.write(mSamplersVS[i].active);
1705 stream.write(mSamplersVS[i].logicalTextureUnit);
1706 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001707 }
1708
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001709 stream.write(mUsedVertexSamplerRange);
1710 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001711
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001712 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001713 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1714 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001715 stream.write(mUniforms[i]->type);
1716 stream.write(mUniforms[i]->_name);
1717 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001718
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001719 stream.write(mUniforms[i]->ps.float4Index);
1720 stream.write(mUniforms[i]->ps.samplerIndex);
1721 stream.write(mUniforms[i]->ps.boolIndex);
1722 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001723
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001724 stream.write(mUniforms[i]->vs.float4Index);
1725 stream.write(mUniforms[i]->vs.samplerIndex);
1726 stream.write(mUniforms[i]->vs.boolIndex);
1727 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001728 }
1729
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001730 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001731 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1732 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001733 stream.write(mUniformIndex[i].name);
1734 stream.write(mUniformIndex[i].element);
1735 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001736 }
1737
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001738 stream.write(mDxDepthRangeRegisterVS);
1739 stream.write(mDxDepthRangeRegisterPS);
1740 stream.write(mDxDepthFrontRegister);
1741 stream.write(mDxCoordRegister);
1742 stream.write(mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001743
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001744 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001745 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001746
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001747 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001748 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001749
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001750 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001751
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001752 GLsizei streamLength = stream.length();
1753 const void *streamData = stream.data();
1754
1755 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001756 if (totalLength > bufSize)
1757 {
1758 if (length)
1759 {
1760 *length = 0;
1761 }
1762
1763 return false;
1764 }
1765
1766 if (binary)
1767 {
1768 char *ptr = (char*) binary;
1769
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001770 memcpy(ptr, streamData, streamLength);
1771 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001772
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001773 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001774 ptr += sizeof(GUID);
1775
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001776 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001777 ptr += pixelShaderSize;
1778
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001779 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001780 ptr += vertexShaderSize;
1781
1782 ASSERT(ptr - totalLength == binary);
1783 }
1784
1785 if (length)
1786 {
1787 *length = totalLength;
1788 }
1789
1790 return true;
1791}
1792
1793GLint ProgramBinary::getLength()
1794{
1795 GLint length;
1796 if (save(NULL, INT_MAX, &length))
1797 {
1798 return length;
1799 }
1800 else
1801 {
1802 return 0;
1803 }
1804}
1805
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001806bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001807{
1808 if (!fragmentShader || !fragmentShader->isCompiled())
1809 {
1810 return false;
1811 }
1812
1813 if (!vertexShader || !vertexShader->isCompiled())
1814 {
1815 return false;
1816 }
1817
1818 std::string pixelHLSL = fragmentShader->getHLSL();
1819 std::string vertexHLSL = vertexShader->getHLSL();
1820
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001821 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001822 {
1823 return false;
1824 }
1825
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001826 bool success = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001827 rx::D3DConstantTable *constantTableVS = NULL;
1828 rx::D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001829 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1830 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001831
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001832 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001833 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001834 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001835 constantTableVS = mVertexExecutable->getConstantTable();
1836 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001837 }
1838 else
1839 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001840 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001841 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001842
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001843 delete mVertexExecutable;
1844 mVertexExecutable = NULL;
1845 delete mPixelExecutable;
1846 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001847 }
1848
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001849 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1850 {
1851 success = false;
1852 }
1853
1854 if (constantTableVS && constantTablePS)
1855 {
1856 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1857 {
1858 success = false;
1859 }
1860 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001861
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001862 Context *context = getContext();
1863 context->markDxUniformsDirty();
1864
1865 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001866}
1867
1868// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001869bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001870{
1871 unsigned int usedLocations = 0;
1872
1873 // Link attributes that have a binding location
1874 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1875 {
1876 int location = attributeBindings.getAttributeBinding(attribute->name);
1877
1878 if (location != -1) // Set by glBindAttribLocation
1879 {
1880 if (!mLinkedAttribute[location].name.empty())
1881 {
1882 // Multiple active attributes bound to the same location; not an error
1883 }
1884
1885 mLinkedAttribute[location] = *attribute;
1886
1887 int rows = VariableRowCount(attribute->type);
1888
1889 if (rows + location > MAX_VERTEX_ATTRIBS)
1890 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001891 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 +00001892
1893 return false;
1894 }
1895
1896 for (int i = 0; i < rows; i++)
1897 {
1898 usedLocations |= 1 << (location + i);
1899 }
1900 }
1901 }
1902
1903 // Link attributes that don't have a binding location
1904 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1905 {
1906 int location = attributeBindings.getAttributeBinding(attribute->name);
1907
1908 if (location == -1) // Not set by glBindAttribLocation
1909 {
1910 int rows = VariableRowCount(attribute->type);
1911 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1912
1913 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1914 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001915 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001916
1917 return false; // Fail to link
1918 }
1919
1920 mLinkedAttribute[availableIndex] = *attribute;
1921 }
1922 }
1923
1924 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1925 {
1926 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1927 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1928
1929 for (int r = 0; r < rows; r++)
1930 {
1931 mSemanticIndex[attributeIndex++] = index++;
1932 }
1933 }
1934
1935 return true;
1936}
1937
daniel@transgaming.com31240482012-11-28 21:06:41 +00001938bool ProgramBinary::linkUniforms(InfoLog &infoLog, rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001939{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001940 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001941 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001942 const rx::D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001943
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001944 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001945 {
1946 return false;
1947 }
1948 }
1949
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001950 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1951 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001952 const rx::D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001953
1954 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1955 {
1956 return false;
1957 }
1958 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001959 return true;
1960}
1961
1962// Adds the description of a constant found in the binary shader to the list of uniforms
1963// Returns true if succesful (uniform not already defined)
daniel@transgaming.com31240482012-11-28 21:06:41 +00001964bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const rx::D3DConstant *constant, const std::string &name,
1965 rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001966{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001967 if (constant->registerSet == rx::D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001968 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001969 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001970 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001971 const rx::D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1972 const rx::D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001973
1974 if (psConstant)
1975 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001976 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001977
1978 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1979 {
1980 mSamplersPS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001981 mSamplersPS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001982 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1983 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1984 }
1985 else
1986 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001987 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001988 return false;
1989 }
1990 }
1991
1992 if (vsConstant)
1993 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001994 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001995
1996 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
1997 {
1998 mSamplersVS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001999 mSamplersVS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002000 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2001 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2002 }
2003 else
2004 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002005 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002006 return false;
2007 }
2008 }
2009 }
2010 }
2011
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002012 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002013 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002014 case rx::D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002015 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002016 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002017 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002018 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002019 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002020 const rx::D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002021
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002022 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002023
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002024 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002025 {
2026 return false;
2027 }
2028 }
2029 }
2030
2031 return true;
2032 }
daniel@transgaming.com31240482012-11-28 21:06:41 +00002033 case rx::D3DConstant::CLASS_SCALAR:
2034 case rx::D3DConstant::CLASS_VECTOR:
2035 case rx::D3DConstant::CLASS_MATRIX_COLUMNS:
2036 case rx::D3DConstant::CLASS_OBJECT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002037 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002038 default:
2039 UNREACHABLE();
2040 return false;
2041 }
2042}
2043
daniel@transgaming.com31240482012-11-28 21:06:41 +00002044bool ProgramBinary::defineUniform(GLenum shader, const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002045{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002046 if (_name == "dx_DepthRange")
2047 {
2048 if (shader == GL_VERTEX_SHADER) mDxDepthRangeRegisterVS = constant->registerIndex;
2049 if (shader == GL_FRAGMENT_SHADER) mDxDepthRangeRegisterPS = constant->registerIndex;
2050 return true;
2051 }
2052
2053 if (_name == "dx_DepthFront")
2054 {
2055 mDxDepthFrontRegister = constant->registerIndex;
2056 return true;
2057 }
2058
2059 if (_name == "dx_Coord")
2060 {
2061 mDxCoordRegister = constant->registerIndex;
2062 return true;
2063 }
2064
2065 if (_name == "dx_HalfPixelSize")
2066 {
2067 mDxHalfPixelSizeRegister = constant->registerIndex;
2068 return true;
2069 }
2070
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002071 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002072
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002073 if (!uniform)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002074 {
2075 return false;
2076 }
2077
2078 // Check if already defined
2079 GLint location = getUniformLocation(uniform->name);
2080 GLenum type = uniform->type;
2081
2082 if (location >= 0)
2083 {
2084 delete uniform;
2085 uniform = mUniforms[mUniformIndex[location].index];
2086 }
2087
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002088 if (shader == GL_FRAGMENT_SHADER) uniform->ps.set(constant);
2089 if (shader == GL_VERTEX_SHADER) uniform->vs.set(constant);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002090
2091 if (location >= 0)
2092 {
2093 return uniform->type == type;
2094 }
2095
2096 mUniforms.push_back(uniform);
2097 unsigned int uniformIndex = mUniforms.size() - 1;
2098
2099 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2100 {
2101 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2102 }
2103
2104 return true;
2105}
2106
daniel@transgaming.com31240482012-11-28 21:06:41 +00002107Uniform *ProgramBinary::createUniform(const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002108{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002109 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002110 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002111 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002112 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002113 case rx::D3DConstant::PT_SAMPLER2D:
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_2D, _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_SAMPLERCUBE:
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_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002124 default: UNREACHABLE();
2125 }
2126 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002127 case rx::D3DConstant::PT_BOOL:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002128 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002129 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002130 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2131 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2132 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2133 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002134 default: UNREACHABLE();
2135 }
2136 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002137 case rx::D3DConstant::PT_INT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002138 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002139 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002140 case 1: return new Uniform(GL_INT, _name, constant->elements);
2141 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2142 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2143 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002144 default: UNREACHABLE();
2145 }
2146 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002147 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002148 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002149 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002150 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2151 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2152 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2153 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002154 default: UNREACHABLE();
2155 }
2156 break;
2157 default:
2158 UNREACHABLE();
2159 }
2160 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002161 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002162 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002163 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002164 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002165 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002166 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002167 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002168 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2169 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2170 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002171 default: UNREACHABLE();
2172 }
2173 break;
2174 default: UNREACHABLE();
2175 }
2176 }
2177 else UNREACHABLE();
2178
2179 return 0;
2180}
2181
2182// This method needs to match OutputHLSL::decorate
2183std::string ProgramBinary::decorateAttribute(const std::string &name)
2184{
2185 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2186 {
2187 return "_" + name;
2188 }
2189
2190 return name;
2191}
2192
2193std::string ProgramBinary::undecorateUniform(const std::string &_name)
2194{
2195 std::string name = _name;
2196
2197 // Remove any structure field decoration
2198 size_t pos = 0;
2199 while ((pos = name.find("._", pos)) != std::string::npos)
2200 {
2201 name.replace(pos, 2, ".");
2202 }
2203
2204 // Remove the leading decoration
2205 if (name[0] == '_')
2206 {
2207 return name.substr(1);
2208 }
2209 else if (name.compare(0, 3, "ar_") == 0)
2210 {
2211 return name.substr(3);
2212 }
2213
2214 return name;
2215}
2216
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002217// D3D9_REPLACE begin
2218void ProgramBinary::applyUniformnbv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002219{
2220 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
2221 BOOL boolVector[D3D9_MAX_BOOL_CONSTANTS];
2222
2223 if (targetUniform->ps.float4Index >= 0 || targetUniform->vs.float4Index >= 0)
2224 {
2225 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
2226 for (int i = 0; i < count; i++)
2227 {
2228 for (int j = 0; j < 4; j++)
2229 {
2230 if (j < width)
2231 {
2232 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
2233 }
2234 else
2235 {
2236 vector[i * 4 + j] = 0.0f;
2237 }
2238 }
2239 }
2240 }
2241
2242 if (targetUniform->ps.boolIndex >= 0 || targetUniform->vs.boolIndex >= 0)
2243 {
2244 int psCount = targetUniform->ps.boolIndex >= 0 ? targetUniform->ps.registerCount : 0;
2245 int vsCount = targetUniform->vs.boolIndex >= 0 ? targetUniform->vs.registerCount : 0;
2246 int copyCount = std::min(count * width, std::max(psCount, vsCount));
2247 ASSERT(copyCount <= D3D9_MAX_BOOL_CONSTANTS);
2248 for (int i = 0; i < copyCount; i++)
2249 {
2250 boolVector[i] = v[i] != GL_FALSE;
2251 }
2252 }
2253
2254 if (targetUniform->ps.float4Index >= 0)
2255 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002256 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002257 }
2258
2259 if (targetUniform->ps.boolIndex >= 0)
2260 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002261 device->SetPixelShaderConstantB(targetUniform->ps.boolIndex, boolVector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002262 }
2263
2264 if (targetUniform->vs.float4Index >= 0)
2265 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002266 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002267 }
2268
2269 if (targetUniform->vs.boolIndex >= 0)
2270 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002271 device->SetVertexShaderConstantB(targetUniform->vs.boolIndex, boolVector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002272 }
2273}
2274
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002275bool ProgramBinary::applyUniformnfv(IDirect3DDevice9 *device, Uniform *targetUniform, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002276{
2277 if (targetUniform->ps.registerCount)
2278 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002279 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, v, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002280 }
2281
2282 if (targetUniform->vs.registerCount)
2283 {
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002284 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, v, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002285 }
2286
2287 return true;
2288}
2289
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002290bool ProgramBinary::applyUniform1iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002291{
2292 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002293 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002294
2295 for (int i = 0; i < count; i++)
2296 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002297 vector[i] = Vector4((float)v[i], 0, 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002298 }
2299
2300 if (targetUniform->ps.registerCount)
2301 {
2302 if (targetUniform->ps.samplerIndex >= 0)
2303 {
2304 unsigned int firstIndex = targetUniform->ps.samplerIndex;
2305
2306 for (int i = 0; i < count; i++)
2307 {
2308 unsigned int samplerIndex = firstIndex + i;
2309
2310 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2311 {
2312 ASSERT(mSamplersPS[samplerIndex].active);
2313 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
2314 }
2315 }
2316 }
2317 else
2318 {
2319 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002320 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float*)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002321 }
2322 }
2323
2324 if (targetUniform->vs.registerCount)
2325 {
2326 if (targetUniform->vs.samplerIndex >= 0)
2327 {
2328 unsigned int firstIndex = targetUniform->vs.samplerIndex;
2329
2330 for (int i = 0; i < count; i++)
2331 {
2332 unsigned int samplerIndex = firstIndex + i;
2333
2334 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
2335 {
2336 ASSERT(mSamplersVS[samplerIndex].active);
2337 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
2338 }
2339 }
2340 }
2341 else
2342 {
2343 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002344 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002345 }
2346 }
2347
2348 return true;
2349}
2350
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002351bool ProgramBinary::applyUniform2iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002352{
2353 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002354 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002355
2356 for (int i = 0; i < count; i++)
2357 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002358 vector[i] = Vector4((float)v[0], (float)v[1], 0, 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002359
2360 v += 2;
2361 }
2362
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002363 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002364
2365 return true;
2366}
2367
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002368bool ProgramBinary::applyUniform3iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002369{
2370 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002371 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002372
2373 for (int i = 0; i < count; i++)
2374 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002375 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], 0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002376
2377 v += 3;
2378 }
2379
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002380 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002381
2382 return true;
2383}
2384
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002385bool ProgramBinary::applyUniform4iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002386{
2387 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002388 Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002389
2390 for (int i = 0; i < count; i++)
2391 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002392 vector[i] = Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002393
2394 v += 4;
2395 }
2396
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002397 applyUniformniv(device, targetUniform, count, vector);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002398
2399 return true;
2400}
2401
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002402void ProgramBinary::applyUniformniv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const Vector4 *vector)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002403{
2404 if (targetUniform->ps.registerCount)
2405 {
2406 ASSERT(targetUniform->ps.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002407 device->SetPixelShaderConstantF(targetUniform->ps.float4Index, (const float *)vector, targetUniform->ps.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002408 }
2409
2410 if (targetUniform->vs.registerCount)
2411 {
2412 ASSERT(targetUniform->vs.float4Index >= 0);
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002413 device->SetVertexShaderConstantF(targetUniform->vs.float4Index, (const float *)vector, targetUniform->vs.registerCount);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002414 }
2415}
daniel@transgaming.com77fbf972012-11-28 21:02:55 +00002416// D3D9_REPLACE end
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002417
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002418bool ProgramBinary::isValidated() const
2419{
2420 return mValidated;
2421}
2422
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002423void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2424{
2425 // Skip over inactive attributes
2426 unsigned int activeAttribute = 0;
2427 unsigned int attribute;
2428 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2429 {
2430 if (mLinkedAttribute[attribute].name.empty())
2431 {
2432 continue;
2433 }
2434
2435 if (activeAttribute == index)
2436 {
2437 break;
2438 }
2439
2440 activeAttribute++;
2441 }
2442
2443 if (bufsize > 0)
2444 {
2445 const char *string = mLinkedAttribute[attribute].name.c_str();
2446
2447 strncpy(name, string, bufsize);
2448 name[bufsize - 1] = '\0';
2449
2450 if (length)
2451 {
2452 *length = strlen(name);
2453 }
2454 }
2455
2456 *size = 1; // Always a single 'type' instance
2457
2458 *type = mLinkedAttribute[attribute].type;
2459}
2460
2461GLint ProgramBinary::getActiveAttributeCount()
2462{
2463 int count = 0;
2464
2465 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2466 {
2467 if (!mLinkedAttribute[attributeIndex].name.empty())
2468 {
2469 count++;
2470 }
2471 }
2472
2473 return count;
2474}
2475
2476GLint ProgramBinary::getActiveAttributeMaxLength()
2477{
2478 int maxLength = 0;
2479
2480 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2481 {
2482 if (!mLinkedAttribute[attributeIndex].name.empty())
2483 {
2484 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2485 }
2486 }
2487
2488 return maxLength;
2489}
2490
2491void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2492{
2493 // Skip over internal uniforms
2494 unsigned int activeUniform = 0;
2495 unsigned int uniform;
2496 for (uniform = 0; uniform < mUniforms.size(); uniform++)
2497 {
2498 if (mUniforms[uniform]->name.compare(0, 3, "dx_") == 0)
2499 {
2500 continue;
2501 }
2502
2503 if (activeUniform == index)
2504 {
2505 break;
2506 }
2507
2508 activeUniform++;
2509 }
2510
2511 ASSERT(uniform < mUniforms.size()); // index must be smaller than getActiveUniformCount()
2512
2513 if (bufsize > 0)
2514 {
2515 std::string string = mUniforms[uniform]->name;
2516
2517 if (mUniforms[uniform]->isArray())
2518 {
2519 string += "[0]";
2520 }
2521
2522 strncpy(name, string.c_str(), bufsize);
2523 name[bufsize - 1] = '\0';
2524
2525 if (length)
2526 {
2527 *length = strlen(name);
2528 }
2529 }
2530
2531 *size = mUniforms[uniform]->arraySize;
2532
2533 *type = mUniforms[uniform]->type;
2534}
2535
2536GLint ProgramBinary::getActiveUniformCount()
2537{
2538 int count = 0;
2539
2540 unsigned int numUniforms = mUniforms.size();
2541 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2542 {
2543 if (mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2544 {
2545 count++;
2546 }
2547 }
2548
2549 return count;
2550}
2551
2552GLint ProgramBinary::getActiveUniformMaxLength()
2553{
2554 int maxLength = 0;
2555
2556 unsigned int numUniforms = mUniforms.size();
2557 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2558 {
2559 if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.compare(0, 3, "dx_") != 0)
2560 {
2561 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2562 if (mUniforms[uniformIndex]->isArray())
2563 {
2564 length += 3; // Counting in "[0]".
2565 }
2566 maxLength = std::max(length, maxLength);
2567 }
2568 }
2569
2570 return maxLength;
2571}
2572
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002573void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002574{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002575 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002576 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002577 {
2578 mValidated = false;
2579 }
2580 else
2581 {
2582 mValidated = true;
2583 }
2584}
2585
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002586bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002587{
2588 // if any two active samplers in a program are of different types, but refer to the same
2589 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2590 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2591
2592 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2593 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2594
2595 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2596 {
2597 textureUnitType[i] = TEXTURE_UNKNOWN;
2598 }
2599
2600 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2601 {
2602 if (mSamplersPS[i].active)
2603 {
2604 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2605
2606 if (unit >= maxCombinedTextureImageUnits)
2607 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002608 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002609 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002610 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002611 }
2612
2613 return false;
2614 }
2615
2616 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2617 {
2618 if (mSamplersPS[i].textureType != textureUnitType[unit])
2619 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002620 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002621 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002622 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002623 }
2624
2625 return false;
2626 }
2627 }
2628 else
2629 {
2630 textureUnitType[unit] = mSamplersPS[i].textureType;
2631 }
2632 }
2633 }
2634
2635 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2636 {
2637 if (mSamplersVS[i].active)
2638 {
2639 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2640
2641 if (unit >= maxCombinedTextureImageUnits)
2642 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002643 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002644 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002645 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002646 }
2647
2648 return false;
2649 }
2650
2651 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2652 {
2653 if (mSamplersVS[i].textureType != textureUnitType[unit])
2654 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002655 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002656 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002657 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002658 }
2659
2660 return false;
2661 }
2662 }
2663 else
2664 {
2665 textureUnitType[unit] = mSamplersVS[i].textureType;
2666 }
2667 }
2668 }
2669
2670 return true;
2671}
2672
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002673void ProgramBinary::applyDxDepthRange(float near, float far, float diff)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002674{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002675 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2676 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2677
2678 if (mDxDepthRangeRegisterVS >= 0)
2679 {
2680 float nearFarDiff[4] = {near, far, diff};
2681 device->SetVertexShaderConstantF(mDxDepthRangeRegisterVS, nearFarDiff, 1);
2682 }
2683
2684 if (mDxDepthRangeRegisterPS >= 0)
2685 {
2686 float nearFarDiff[4] = {near, far, diff};
2687 device->SetPixelShaderConstantF(mDxDepthRangeRegisterPS, nearFarDiff, 1);
2688 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002689}
2690
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002691void ProgramBinary::applyDxDepthFront(float range, float start, float frontCCW)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002692{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002693 if (mDxDepthFrontRegister >= 0)
2694 {
2695 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2696 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2697
2698 GLfloat dz[4] = {range, start, frontCCW};
2699 device->SetPixelShaderConstantF(mDxDepthFrontRegister, dz, 1);
2700 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002701}
2702
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002703void ProgramBinary::applyDxCoord(float halfWidth, float halfHeight, float x0, float y0)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002704{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002705 if (mDxCoordRegister >= 0)
2706 {
2707 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2708 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2709
2710 GLfloat whxy[4] = {halfWidth,halfHeight, x0, y0};
2711 device->SetPixelShaderConstantF(mDxCoordRegister, whxy, 1);
2712 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002713}
2714
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002715void ProgramBinary::applyDxHalfPixelSize(float width, float height)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002716{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002717 if (mDxHalfPixelSizeRegister >= 0)
2718 {
2719 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2720 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2721
2722 GLfloat xy[4] = {width, height};
2723 device->SetVertexShaderConstantF(mDxHalfPixelSizeRegister, xy, 1);
2724 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002725}
2726
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002727ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2728{
2729}
2730
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002731}