blob: 6e5cd9eccb83fc6c20c555b80d62f73d0aa408c8 [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000010#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000011#include "libGLESv2/ProgramBinary.h"
12
13#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000014#include "common/version.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/Shader.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000018
19#include <string>
20
daniel@transgaming.com88853c52012-12-20 20:56:40 +000021#undef near
22#undef far
23
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000024namespace gl
25{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000026std::string str(int i)
27{
28 char buffer[20];
29 snprintf(buffer, sizeof(buffer), "%d", i);
30 return buffer;
31}
32
daniel@transgaming.comdb019952012-12-20 21:13:32 +000033UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index)
34 : name(name), element(element), index(index)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000035{
36}
37
daniel@transgaming.come87ca002012-07-24 18:30:43 +000038unsigned int ProgramBinary::mCurrentSerial = 1;
39
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000040ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000041{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000042 mPixelExecutable = NULL;
43 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000044
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000045 mValidated = false;
46
47 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
48 {
49 mSemanticIndex[index] = -1;
50 }
51
52 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
53 {
54 mSamplersPS[index].active = false;
55 }
56
57 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
58 {
59 mSamplersVS[index].active = false;
60 }
61
62 mUsedVertexSamplerRange = 0;
63 mUsedPixelSamplerRange = 0;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000064}
65
66ProgramBinary::~ProgramBinary()
67{
daniel@transgaming.com95892412012-11-28 20:59:09 +000068 delete mPixelExecutable;
69 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000070
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000071 while (!mUniforms.empty())
72 {
73 delete mUniforms.back();
74 mUniforms.pop_back();
75 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000076}
77
daniel@transgaming.come87ca002012-07-24 18:30:43 +000078unsigned int ProgramBinary::getSerial() const
79{
80 return mSerial;
81}
82
83unsigned int ProgramBinary::issueSerial()
84{
85 return mCurrentSerial++;
86}
87
daniel@transgaming.com95892412012-11-28 20:59:09 +000088rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000089{
90 return mPixelExecutable;
91}
92
daniel@transgaming.com95892412012-11-28 20:59:09 +000093rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000094{
95 return mVertexExecutable;
96}
97
98GLuint ProgramBinary::getAttributeLocation(const char *name)
99{
100 if (name)
101 {
102 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
103 {
104 if (mLinkedAttribute[index].name == std::string(name))
105 {
106 return index;
107 }
108 }
109 }
110
111 return -1;
112}
113
114int ProgramBinary::getSemanticIndex(int attributeIndex)
115{
116 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
117
118 return mSemanticIndex[attributeIndex];
119}
120
121// Returns one more than the highest sampler index used.
122GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
123{
124 switch (type)
125 {
126 case SAMPLER_PIXEL:
127 return mUsedPixelSamplerRange;
128 case SAMPLER_VERTEX:
129 return mUsedVertexSamplerRange;
130 default:
131 UNREACHABLE();
132 return 0;
133 }
134}
135
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000136bool ProgramBinary::usesPointSize() const
137{
138 return mUsesPointSize;
139}
140
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000141// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
142// index (0-15 for the pixel shader and 0-3 for the vertex shader).
143GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
144{
145 GLint logicalTextureUnit = -1;
146
147 switch (type)
148 {
149 case SAMPLER_PIXEL:
150 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
151
152 if (mSamplersPS[samplerIndex].active)
153 {
154 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
155 }
156 break;
157 case SAMPLER_VERTEX:
158 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
159
160 if (mSamplersVS[samplerIndex].active)
161 {
162 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
163 }
164 break;
165 default: UNREACHABLE();
166 }
167
168 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
169 {
170 return logicalTextureUnit;
171 }
172
173 return -1;
174}
175
176// Returns the texture type for a given Direct3D 9 sampler type and
177// index (0-15 for the pixel shader and 0-3 for the vertex shader).
178TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
179{
180 switch (type)
181 {
182 case SAMPLER_PIXEL:
183 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
184 ASSERT(mSamplersPS[samplerIndex].active);
185 return mSamplersPS[samplerIndex].textureType;
186 case SAMPLER_VERTEX:
187 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
188 ASSERT(mSamplersVS[samplerIndex].active);
189 return mSamplersVS[samplerIndex].textureType;
190 default: UNREACHABLE();
191 }
192
193 return TEXTURE_2D;
194}
195
196GLint ProgramBinary::getUniformLocation(std::string name)
197{
198 unsigned int subscript = 0;
199
200 // Strip any trailing array operator and retrieve the subscript
201 size_t open = name.find_last_of('[');
202 size_t close = name.find_last_of(']');
203 if (open != std::string::npos && close == name.length() - 1)
204 {
205 subscript = atoi(name.substr(open + 1).c_str());
206 name.erase(open);
207 }
208
209 unsigned int numUniforms = mUniformIndex.size();
210 for (unsigned int location = 0; location < numUniforms; location++)
211 {
212 if (mUniformIndex[location].name == name &&
213 mUniformIndex[location].element == subscript)
214 {
215 return location;
216 }
217 }
218
219 return -1;
220}
221
222bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
223{
224 if (location < 0 || location >= (int)mUniformIndex.size())
225 {
226 return false;
227 }
228
229 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
230 targetUniform->dirty = true;
231
232 if (targetUniform->type == GL_FLOAT)
233 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000234 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000235
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000236 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000237 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
238
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000239 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000240
241 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
242
243 for (int i = 0; i < count; i++)
244 {
245 target[0] = v[0];
246 target[1] = 0;
247 target[2] = 0;
248 target[3] = 0;
249 target += 4;
250 v += 1;
251 }
252 }
253 else if (targetUniform->type == GL_BOOL)
254 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000255 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000256
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000257 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000258 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
259
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000260 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000261 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
262
263 for (int i = 0; i < count; ++i)
264 {
265 if (v[i] == 0.0f)
266 {
267 boolParams[i] = GL_FALSE;
268 }
269 else
270 {
271 boolParams[i] = GL_TRUE;
272 }
273 }
274 }
275 else
276 {
277 return false;
278 }
279
280 return true;
281}
282
283bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
284{
285 if (location < 0 || location >= (int)mUniformIndex.size())
286 {
287 return false;
288 }
289
290 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
291 targetUniform->dirty = true;
292
293 if (targetUniform->type == GL_FLOAT_VEC2)
294 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000295 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000296
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000297 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000298 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
299
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000300 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000301
302 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
303
304 for (int i = 0; i < count; i++)
305 {
306 target[0] = v[0];
307 target[1] = v[1];
308 target[2] = 0;
309 target[3] = 0;
310 target += 4;
311 v += 2;
312 }
313 }
314 else if (targetUniform->type == GL_BOOL_VEC2)
315 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000316 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000317
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000318 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000319 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
320
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000321 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000322
323 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
324
325 for (int i = 0; i < count * 2; ++i)
326 {
327 if (v[i] == 0.0f)
328 {
329 boolParams[i] = GL_FALSE;
330 }
331 else
332 {
333 boolParams[i] = GL_TRUE;
334 }
335 }
336 }
337 else
338 {
339 return false;
340 }
341
342 return true;
343}
344
345bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
346{
347 if (location < 0 || location >= (int)mUniformIndex.size())
348 {
349 return false;
350 }
351
352 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
353 targetUniform->dirty = true;
354
355 if (targetUniform->type == GL_FLOAT_VEC3)
356 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000357 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000358
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000359 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000360 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
361
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000362 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000363
364 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
365
366 for (int i = 0; i < count; i++)
367 {
368 target[0] = v[0];
369 target[1] = v[1];
370 target[2] = v[2];
371 target[3] = 0;
372 target += 4;
373 v += 3;
374 }
375 }
376 else if (targetUniform->type == GL_BOOL_VEC3)
377 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000378 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000379
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000380 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000381 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
382
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000383 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000384 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
385
386 for (int i = 0; i < count * 3; ++i)
387 {
388 if (v[i] == 0.0f)
389 {
390 boolParams[i] = GL_FALSE;
391 }
392 else
393 {
394 boolParams[i] = GL_TRUE;
395 }
396 }
397 }
398 else
399 {
400 return false;
401 }
402
403 return true;
404}
405
406bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
407{
408 if (location < 0 || location >= (int)mUniformIndex.size())
409 {
410 return false;
411 }
412
413 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
414 targetUniform->dirty = true;
415
416 if (targetUniform->type == GL_FLOAT_VEC4)
417 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000418 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000419
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000420 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000421 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
422
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000423 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000424
425 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
426 v, 4 * sizeof(GLfloat) * count);
427 }
428 else if (targetUniform->type == GL_BOOL_VEC4)
429 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000430 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000431
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000432 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000433 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
434
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000435 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000436 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
437
438 for (int i = 0; i < count * 4; ++i)
439 {
440 if (v[i] == 0.0f)
441 {
442 boolParams[i] = GL_FALSE;
443 }
444 else
445 {
446 boolParams[i] = GL_TRUE;
447 }
448 }
449 }
450 else
451 {
452 return false;
453 }
454
455 return true;
456}
457
458template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
459void transposeMatrix(T *target, const GLfloat *value)
460{
461 int copyWidth = std::min(targetWidth, srcWidth);
462 int copyHeight = std::min(targetHeight, srcHeight);
463
464 for (int x = 0; x < copyWidth; x++)
465 {
466 for (int y = 0; y < copyHeight; y++)
467 {
468 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
469 }
470 }
471 // clear unfilled right side
472 for (int y = 0; y < copyHeight; y++)
473 {
474 for (int x = srcWidth; x < targetWidth; x++)
475 {
476 target[y * targetWidth + x] = (T)0;
477 }
478 }
479 // clear unfilled bottom.
480 for (int y = srcHeight; y < targetHeight; y++)
481 {
482 for (int x = 0; x < targetWidth; x++)
483 {
484 target[y * targetWidth + x] = (T)0;
485 }
486 }
487}
488
489bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
490{
491 if (location < 0 || location >= (int)mUniformIndex.size())
492 {
493 return false;
494 }
495
496 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
497 targetUniform->dirty = true;
498
499 if (targetUniform->type != GL_FLOAT_MAT2)
500 {
501 return false;
502 }
503
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000504 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000505
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000506 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000507 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
508
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000509 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000510
511 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
512 for (int i = 0; i < count; i++)
513 {
514 transposeMatrix<GLfloat,4,2,2,2>(target, value);
515 target += 8;
516 value += 4;
517 }
518
519 return true;
520}
521
522bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
523{
524 if (location < 0 || location >= (int)mUniformIndex.size())
525 {
526 return false;
527 }
528
529 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
530 targetUniform->dirty = true;
531
532 if (targetUniform->type != GL_FLOAT_MAT3)
533 {
534 return false;
535 }
536
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000537 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000538
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000539 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000540 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
541
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000542 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000543
544 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
545 for (int i = 0; i < count; i++)
546 {
547 transposeMatrix<GLfloat,4,3,3,3>(target, value);
548 target += 12;
549 value += 9;
550 }
551
552 return true;
553}
554
555
556bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
557{
558 if (location < 0 || location >= (int)mUniformIndex.size())
559 {
560 return false;
561 }
562
563 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
564 targetUniform->dirty = true;
565
566 if (targetUniform->type != GL_FLOAT_MAT4)
567 {
568 return false;
569 }
570
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000571 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000572
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000573 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000574 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
575
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000576 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000577
578 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
579 for (int i = 0; i < count; i++)
580 {
581 transposeMatrix<GLfloat,4,4,4,4>(target, value);
582 target += 16;
583 value += 16;
584 }
585
586 return true;
587}
588
589bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
590{
591 if (location < 0 || location >= (int)mUniformIndex.size())
592 {
593 return false;
594 }
595
596 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
597 targetUniform->dirty = true;
598
599 if (targetUniform->type == GL_INT ||
600 targetUniform->type == GL_SAMPLER_2D ||
601 targetUniform->type == GL_SAMPLER_CUBE)
602 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000603 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000604
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000605 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000606 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
607
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000608 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000609
610 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
611 v, sizeof(GLint) * count);
612 }
613 else if (targetUniform->type == GL_BOOL)
614 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000615 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000616
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000617 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000618 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
619
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000620 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000621 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
622
623 for (int i = 0; i < count; ++i)
624 {
625 if (v[i] == 0)
626 {
627 boolParams[i] = GL_FALSE;
628 }
629 else
630 {
631 boolParams[i] = GL_TRUE;
632 }
633 }
634 }
635 else
636 {
637 return false;
638 }
639
640 return true;
641}
642
643bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
644{
645 if (location < 0 || location >= (int)mUniformIndex.size())
646 {
647 return false;
648 }
649
650 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
651 targetUniform->dirty = true;
652
653 if (targetUniform->type == GL_INT_VEC2)
654 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000655 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000656
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000657 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000658 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
659
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000660 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000661
662 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
663 v, 2 * sizeof(GLint) * count);
664 }
665 else if (targetUniform->type == GL_BOOL_VEC2)
666 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000667 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000668
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000669 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000670 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
671
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000672 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000673 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
674
675 for (int i = 0; i < count * 2; ++i)
676 {
677 if (v[i] == 0)
678 {
679 boolParams[i] = GL_FALSE;
680 }
681 else
682 {
683 boolParams[i] = GL_TRUE;
684 }
685 }
686 }
687 else
688 {
689 return false;
690 }
691
692 return true;
693}
694
695bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
696{
697 if (location < 0 || location >= (int)mUniformIndex.size())
698 {
699 return false;
700 }
701
702 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
703 targetUniform->dirty = true;
704
705 if (targetUniform->type == GL_INT_VEC3)
706 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000707 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000708
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000709 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000710 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
711
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000712 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000713
714 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
715 v, 3 * sizeof(GLint) * count);
716 }
717 else if (targetUniform->type == GL_BOOL_VEC3)
718 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000719 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000720
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000721 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000722 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
723
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000724 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000725 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
726
727 for (int i = 0; i < count * 3; ++i)
728 {
729 if (v[i] == 0)
730 {
731 boolParams[i] = GL_FALSE;
732 }
733 else
734 {
735 boolParams[i] = GL_TRUE;
736 }
737 }
738 }
739 else
740 {
741 return false;
742 }
743
744 return true;
745}
746
747bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
748{
749 if (location < 0 || location >= (int)mUniformIndex.size())
750 {
751 return false;
752 }
753
754 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
755 targetUniform->dirty = true;
756
757 if (targetUniform->type == GL_INT_VEC4)
758 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000759 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000760
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000761 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000762 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
763
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000764 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000765
766 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
767 v, 4 * sizeof(GLint) * count);
768 }
769 else if (targetUniform->type == GL_BOOL_VEC4)
770 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000771 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000772
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000773 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000774 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
775
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000776 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000777 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
778
779 for (int i = 0; i < count * 4; ++i)
780 {
781 if (v[i] == 0)
782 {
783 boolParams[i] = GL_FALSE;
784 }
785 else
786 {
787 boolParams[i] = GL_TRUE;
788 }
789 }
790 }
791 else
792 {
793 return false;
794 }
795
796 return true;
797}
798
799bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
800{
801 if (location < 0 || location >= (int)mUniformIndex.size())
802 {
803 return false;
804 }
805
806 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
807
808 // sized queries -- ensure the provided buffer is large enough
809 if (bufSize)
810 {
811 int requiredBytes = UniformExternalSize(targetUniform->type);
812 if (*bufSize < requiredBytes)
813 {
814 return false;
815 }
816 }
817
818 switch (targetUniform->type)
819 {
820 case GL_FLOAT_MAT2:
821 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
822 break;
823 case GL_FLOAT_MAT3:
824 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
825 break;
826 case GL_FLOAT_MAT4:
827 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
828 break;
829 default:
830 {
831 unsigned int count = UniformExternalComponentCount(targetUniform->type);
832 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
833
834 switch (UniformComponentType(targetUniform->type))
835 {
836 case GL_BOOL:
837 {
838 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
839
840 for (unsigned int i = 0; i < count; ++i)
841 {
842 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
843 }
844 }
845 break;
846 case GL_FLOAT:
847 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
848 count * sizeof(GLfloat));
849 break;
850 case GL_INT:
851 {
852 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
853
854 for (unsigned int i = 0; i < count; ++i)
855 {
856 params[i] = (float)intParams[i];
857 }
858 }
859 break;
860 default: UNREACHABLE();
861 }
862 }
863 }
864
865 return true;
866}
867
868bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
869{
870 if (location < 0 || location >= (int)mUniformIndex.size())
871 {
872 return false;
873 }
874
875 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
876
877 // sized queries -- ensure the provided buffer is large enough
878 if (bufSize)
879 {
880 int requiredBytes = UniformExternalSize(targetUniform->type);
881 if (*bufSize < requiredBytes)
882 {
883 return false;
884 }
885 }
886
887 switch (targetUniform->type)
888 {
889 case GL_FLOAT_MAT2:
890 {
891 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
892 }
893 break;
894 case GL_FLOAT_MAT3:
895 {
896 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
897 }
898 break;
899 case GL_FLOAT_MAT4:
900 {
901 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
902 }
903 break;
904 default:
905 {
906 unsigned int count = UniformExternalComponentCount(targetUniform->type);
907 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
908
909 switch (UniformComponentType(targetUniform->type))
910 {
911 case GL_BOOL:
912 {
913 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
914
915 for (unsigned int i = 0; i < count; ++i)
916 {
917 params[i] = (GLint)boolParams[i];
918 }
919 }
920 break;
921 case GL_FLOAT:
922 {
923 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
924
925 for (unsigned int i = 0; i < count; ++i)
926 {
927 params[i] = (GLint)floatParams[i];
928 }
929 }
930 break;
931 case GL_INT:
932 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
933 count * sizeof(GLint));
934 break;
935 default: UNREACHABLE();
936 }
937 }
938 }
939
940 return true;
941}
942
943void ProgramBinary::dirtyAllUniforms()
944{
945 unsigned int numUniforms = mUniforms.size();
946 for (unsigned int index = 0; index < numUniforms; index++)
947 {
948 mUniforms[index]->dirty = true;
949 }
950}
951
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000952// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000953void ProgramBinary::applyUniforms()
954{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000955 // Retrieve sampler uniform values
956 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
957 {
958 Uniform *targetUniform = *ub;
959
960 if (targetUniform->dirty)
961 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000962 if (targetUniform->type == GL_SAMPLER_2D ||
963 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000964 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000965 int count = targetUniform->elementCount();
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000966 GLint *v = (GLint*)targetUniform->data;
967
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000968 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000969 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000970 unsigned int firstIndex = targetUniform->psRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000971
972 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000973 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000974 unsigned int samplerIndex = firstIndex + i;
975
976 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000977 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000978 ASSERT(mSamplersPS[samplerIndex].active);
979 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000980 }
981 }
982 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000983
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000984 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000985 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000986 unsigned int firstIndex = targetUniform->vsRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000987
988 for (int i = 0; i < count; i++)
989 {
990 unsigned int samplerIndex = firstIndex + i;
991
992 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
993 {
994 ASSERT(mSamplersVS[samplerIndex].active);
995 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
996 }
997 }
998 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000999 }
1000 }
1001 }
1002
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001003 mRenderer->applyUniforms(&mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001004}
1005
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001006// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1007// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001008int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001009{
1010 Context *context = getContext();
1011 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1012
1013 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1014 {
1015 int n = VariableRowCount(varying->type) * varying->size;
1016 int m = VariableColumnCount(varying->type);
1017 bool success = false;
1018
1019 if (m == 2 || m == 3 || m == 4)
1020 {
1021 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1022 {
1023 bool available = true;
1024
1025 for (int y = 0; y < n && available; y++)
1026 {
1027 for (int x = 0; x < m && available; x++)
1028 {
1029 if (packing[r + y][x])
1030 {
1031 available = false;
1032 }
1033 }
1034 }
1035
1036 if (available)
1037 {
1038 varying->reg = r;
1039 varying->col = 0;
1040
1041 for (int y = 0; y < n; y++)
1042 {
1043 for (int x = 0; x < m; x++)
1044 {
1045 packing[r + y][x] = &*varying;
1046 }
1047 }
1048
1049 success = true;
1050 }
1051 }
1052
1053 if (!success && m == 2)
1054 {
1055 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1056 {
1057 bool available = true;
1058
1059 for (int y = 0; y < n && available; y++)
1060 {
1061 for (int x = 2; x < 4 && available; x++)
1062 {
1063 if (packing[r + y][x])
1064 {
1065 available = false;
1066 }
1067 }
1068 }
1069
1070 if (available)
1071 {
1072 varying->reg = r;
1073 varying->col = 2;
1074
1075 for (int y = 0; y < n; y++)
1076 {
1077 for (int x = 2; x < 4; x++)
1078 {
1079 packing[r + y][x] = &*varying;
1080 }
1081 }
1082
1083 success = true;
1084 }
1085 }
1086 }
1087 }
1088 else if (m == 1)
1089 {
1090 int space[4] = {0};
1091
1092 for (int y = 0; y < maxVaryingVectors; y++)
1093 {
1094 for (int x = 0; x < 4; x++)
1095 {
1096 space[x] += packing[y][x] ? 0 : 1;
1097 }
1098 }
1099
1100 int column = 0;
1101
1102 for (int x = 0; x < 4; x++)
1103 {
1104 if (space[x] >= n && space[x] < space[column])
1105 {
1106 column = x;
1107 }
1108 }
1109
1110 if (space[column] >= n)
1111 {
1112 for (int r = 0; r < maxVaryingVectors; r++)
1113 {
1114 if (!packing[r][column])
1115 {
1116 varying->reg = r;
1117
1118 for (int y = r; y < r + n; y++)
1119 {
1120 packing[y][column] = &*varying;
1121 }
1122
1123 break;
1124 }
1125 }
1126
1127 varying->col = column;
1128
1129 success = true;
1130 }
1131 }
1132 else UNREACHABLE();
1133
1134 if (!success)
1135 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001136 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001137
1138 return -1;
1139 }
1140 }
1141
1142 // Return the number of used registers
1143 int registers = 0;
1144
1145 for (int r = 0; r < maxVaryingVectors; r++)
1146 {
1147 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1148 {
1149 registers++;
1150 }
1151 }
1152
1153 return registers;
1154}
1155
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001156bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001157{
1158 if (pixelHLSL.empty() || vertexHLSL.empty())
1159 {
1160 return false;
1161 }
1162
1163 // Reset the varying register assignments
1164 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1165 {
1166 fragVar->reg = -1;
1167 fragVar->col = -1;
1168 }
1169
1170 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1171 {
1172 vtxVar->reg = -1;
1173 vtxVar->col = -1;
1174 }
1175
1176 // Map the varyings to the register file
1177 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001178 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001179
1180 if (registers < 0)
1181 {
1182 return false;
1183 }
1184
1185 // Write the HLSL input/output declarations
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001186 const int shaderModel = mRenderer->getMajorShaderModel();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001187 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001188 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1189
1190 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1191 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001192 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001193
1194 return false;
1195 }
1196
1197 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1198 {
1199 bool matched = false;
1200
1201 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1202 {
1203 if (output->name == input->name)
1204 {
1205 if (output->type != input->type || output->size != input->size)
1206 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001207 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 +00001208
1209 return false;
1210 }
1211
1212 output->reg = input->reg;
1213 output->col = input->col;
1214
1215 matched = true;
1216 break;
1217 }
1218 }
1219
1220 if (!matched)
1221 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001222 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001223
1224 return false;
1225 }
1226 }
1227
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001228 mUsesPointSize = vertexShader->mUsesPointSize;
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001229 std::string varyingSemantic = (mUsesPointSize && shaderModel >= 3) ? "COLOR" : "TEXCOORD";
1230 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
1231 std::string positionSemantic = (shaderModel >= 4) ? "SV_POSITION" : "POSITION";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001232
1233 vertexHLSL += "struct VS_INPUT\n"
1234 "{\n";
1235
1236 int semanticIndex = 0;
1237 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1238 {
1239 switch (attribute->type)
1240 {
1241 case GL_FLOAT: vertexHLSL += " float "; break;
1242 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1243 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1244 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1245 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1246 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1247 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1248 default: UNREACHABLE();
1249 }
1250
1251 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1252
1253 semanticIndex += VariableRowCount(attribute->type);
1254 }
1255
1256 vertexHLSL += "};\n"
1257 "\n"
1258 "struct VS_OUTPUT\n"
daniel@transgaming.com9c4a6252013-01-11 04:07:18 +00001259 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001260
1261 for (int r = 0; r < registers; r++)
1262 {
1263 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1264
1265 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1266 }
1267
1268 if (fragmentShader->mUsesFragCoord)
1269 {
1270 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1271 }
1272
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001273 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001274 {
1275 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1276 }
1277
daniel@transgaming.com9c4a6252013-01-11 04:07:18 +00001278 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n"
1279 "};\n"
1280 "\n"
1281 "VS_OUTPUT main(VS_INPUT input)\n"
1282 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001283
1284 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1285 {
1286 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1287
1288 if (VariableRowCount(attribute->type) > 1) // Matrix
1289 {
1290 vertexHLSL += "transpose";
1291 }
1292
1293 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1294 }
1295
1296 vertexHLSL += "\n"
1297 " gl_main();\n"
1298 "\n"
1299 " VS_OUTPUT output;\n"
1300 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001301 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001302 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1303 " output.gl_Position.w = gl_Position.w;\n";
1304
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001305 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001306 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001307 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001308 }
1309
1310 if (fragmentShader->mUsesFragCoord)
1311 {
1312 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1313 }
1314
1315 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1316 {
1317 if (varying->reg >= 0)
1318 {
1319 for (int i = 0; i < varying->size; i++)
1320 {
1321 int rows = VariableRowCount(varying->type);
1322
1323 for (int j = 0; j < rows; j++)
1324 {
1325 int r = varying->reg + i * rows + j;
1326 vertexHLSL += " output.v" + str(r);
1327
1328 bool sharedRegister = false; // Register used by multiple varyings
1329
1330 for (int x = 0; x < 4; x++)
1331 {
1332 if (packing[r][x] && packing[r][x] != packing[r][0])
1333 {
1334 sharedRegister = true;
1335 break;
1336 }
1337 }
1338
1339 if(sharedRegister)
1340 {
1341 vertexHLSL += ".";
1342
1343 for (int x = 0; x < 4; x++)
1344 {
1345 if (packing[r][x] == &*varying)
1346 {
1347 switch(x)
1348 {
1349 case 0: vertexHLSL += "x"; break;
1350 case 1: vertexHLSL += "y"; break;
1351 case 2: vertexHLSL += "z"; break;
1352 case 3: vertexHLSL += "w"; break;
1353 }
1354 }
1355 }
1356 }
1357
1358 vertexHLSL += " = " + varying->name;
1359
1360 if (varying->array)
1361 {
1362 vertexHLSL += "[" + str(i) + "]";
1363 }
1364
1365 if (rows > 1)
1366 {
1367 vertexHLSL += "[" + str(j) + "]";
1368 }
1369
1370 vertexHLSL += ";\n";
1371 }
1372 }
1373 }
1374 }
1375
1376 vertexHLSL += "\n"
1377 " return output;\n"
1378 "}\n";
1379
1380 pixelHLSL += "struct PS_INPUT\n"
1381 "{\n";
1382
1383 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1384 {
1385 if (varying->reg >= 0)
1386 {
1387 for (int i = 0; i < varying->size; i++)
1388 {
1389 int rows = VariableRowCount(varying->type);
1390 for (int j = 0; j < rows; j++)
1391 {
1392 std::string n = str(varying->reg + i * rows + j);
daniel@transgaming.com00c0d152013-01-11 04:07:23 +00001393 pixelHLSL += " float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001394 }
1395 }
1396 }
1397 else UNREACHABLE();
1398 }
1399
1400 if (fragmentShader->mUsesFragCoord)
1401 {
1402 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001403
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001404 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001405 {
1406 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1407 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001408 else if (shaderModel >= 3)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001409 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001410 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1411 }
1412 }
1413
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001414 if (fragmentShader->mUsesPointCoord && shaderModel == 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001415 {
1416 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1417 }
1418
1419 if (fragmentShader->mUsesFrontFacing)
1420 {
1421 pixelHLSL += " float vFace : VFACE;\n";
1422 }
1423
1424 pixelHLSL += "};\n"
1425 "\n"
1426 "struct PS_OUTPUT\n"
1427 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001428 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001429 "};\n"
1430 "\n"
1431 "PS_OUTPUT main(PS_INPUT input)\n"
1432 "{\n";
1433
1434 if (fragmentShader->mUsesFragCoord)
1435 {
1436 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1437
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001438 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001439 {
1440 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1441 " gl_FragCoord.y = input.dx_VPos.y;\n";
1442 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001443 else if (shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001444 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001445 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001446 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001447 }
1448 else
1449 {
1450 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1451 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001452 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001453 }
1454
daniel@transgaming.com12985182012-12-20 20:56:31 +00001455 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001456 " gl_FragCoord.w = rhw;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001457 }
1458
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001459 if (fragmentShader->mUsesPointCoord && shaderModel == 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001460 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001461 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1462 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001463 }
1464
1465 if (fragmentShader->mUsesFrontFacing)
1466 {
daniel@transgaming.com12985182012-12-20 20:56:31 +00001467 pixelHLSL += " gl_FrontFacing = (input.vFace * dx_DepthFront.z >= 0.0);\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001468 }
1469
1470 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1471 {
1472 if (varying->reg >= 0)
1473 {
1474 for (int i = 0; i < varying->size; i++)
1475 {
1476 int rows = VariableRowCount(varying->type);
1477 for (int j = 0; j < rows; j++)
1478 {
1479 std::string n = str(varying->reg + i * rows + j);
1480 pixelHLSL += " " + varying->name;
1481
1482 if (varying->array)
1483 {
1484 pixelHLSL += "[" + str(i) + "]";
1485 }
1486
1487 if (rows > 1)
1488 {
1489 pixelHLSL += "[" + str(j) + "]";
1490 }
1491
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001492 switch (VariableColumnCount(varying->type))
1493 {
1494 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1495 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1496 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1497 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1498 default: UNREACHABLE();
1499 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001500 }
1501 }
1502 }
1503 else UNREACHABLE();
1504 }
1505
1506 pixelHLSL += "\n"
1507 " gl_main();\n"
1508 "\n"
1509 " PS_OUTPUT output;\n"
1510 " output.gl_Color[0] = gl_Color[0];\n"
1511 "\n"
1512 " return output;\n"
1513 "}\n";
1514
1515 return true;
1516}
1517
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001518bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1519{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001520 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001521
1522 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001523 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001524 if (format != GL_PROGRAM_BINARY_ANGLE)
1525 {
1526 infoLog.append("Invalid program binary format.");
1527 return false;
1528 }
1529
1530 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001531 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001532 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001533 {
1534 infoLog.append("Invalid program binary version.");
1535 return false;
1536 }
1537
1538 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1539 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001540 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001541 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001542 stream.read(&name);
1543 mLinkedAttribute[i].name = name;
1544 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001545 }
1546
1547 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1548 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001549 stream.read(&mSamplersPS[i].active);
1550 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001551
1552 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001553 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001554 mSamplersPS[i].textureType = (TextureType) textureType;
1555 }
1556
1557 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1558 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001559 stream.read(&mSamplersVS[i].active);
1560 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001561
1562 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001563 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001564 mSamplersVS[i].textureType = (TextureType) textureType;
1565 }
1566
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001567 stream.read(&mUsedVertexSamplerRange);
1568 stream.read(&mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001569 stream.read(&mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001570
1571 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001572 stream.read(&size);
1573 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001574 {
1575 infoLog.append("Invalid program binary.");
1576 return false;
1577 }
1578
1579 mUniforms.resize(size);
1580 for (unsigned int i = 0; i < size; ++i)
1581 {
1582 GLenum type;
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001583 std::string name;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001584 unsigned int arraySize;
1585
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001586 stream.read(&type);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001587 stream.read(&name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001588 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001589
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001590 mUniforms[i] = new Uniform(type, name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001591
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001592 stream.read(&mUniforms[i]->psRegisterIndex);
1593 stream.read(&mUniforms[i]->vsRegisterIndex);
1594 stream.read(&mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001595 }
1596
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001597 stream.read(&size);
1598 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001599 {
1600 infoLog.append("Invalid program binary.");
1601 return false;
1602 }
1603
1604 mUniformIndex.resize(size);
1605 for (unsigned int i = 0; i < size; ++i)
1606 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001607 stream.read(&mUniformIndex[i].name);
1608 stream.read(&mUniformIndex[i].element);
1609 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001610 }
1611
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001612 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001613 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001614
1615 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001616 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001617
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001618 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001619
daniel@transgaming.com36038542012-11-28 20:59:26 +00001620 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001621 ptr += sizeof(GUID);
1622
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001623 GUID identifier = mRenderer->getAdapterIdentifier();
1624 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001625 {
1626 infoLog.append("Invalid program binary.");
1627 return false;
1628 }
1629
1630 const char *pixelShaderFunction = ptr;
1631 ptr += pixelShaderSize;
1632
1633 const char *vertexShaderFunction = ptr;
1634 ptr += vertexShaderSize;
1635
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001636 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
daniel@transgaming.com2275f912012-12-20 21:13:22 +00001637 pixelShaderSize, GL_FRAGMENT_SHADER);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001638 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001639 {
1640 infoLog.append("Could not create pixel shader.");
1641 return false;
1642 }
1643
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001644 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
daniel@transgaming.com2275f912012-12-20 21:13:22 +00001645 vertexShaderSize, GL_VERTEX_SHADER);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001646 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001647 {
1648 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001649 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001650 mPixelExecutable = NULL;
1651 return false;
1652 }
1653
1654 return true;
1655}
1656
1657bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1658{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001659 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001660
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001661 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001662 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001663
1664 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1665 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001666 stream.write(mLinkedAttribute[i].type);
1667 stream.write(mLinkedAttribute[i].name);
1668 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001669 }
1670
1671 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1672 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001673 stream.write(mSamplersPS[i].active);
1674 stream.write(mSamplersPS[i].logicalTextureUnit);
1675 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001676 }
1677
1678 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1679 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001680 stream.write(mSamplersVS[i].active);
1681 stream.write(mSamplersVS[i].logicalTextureUnit);
1682 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001683 }
1684
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001685 stream.write(mUsedVertexSamplerRange);
1686 stream.write(mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001687 stream.write(mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001688
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001689 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001690 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1691 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001692 stream.write(mUniforms[i]->type);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001693 stream.write(mUniforms[i]->name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001694 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001695
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001696 stream.write(mUniforms[i]->psRegisterIndex);
1697 stream.write(mUniforms[i]->vsRegisterIndex);
1698 stream.write(mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001699 }
1700
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001701 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001702 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1703 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001704 stream.write(mUniformIndex[i].name);
1705 stream.write(mUniformIndex[i].element);
1706 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001707 }
1708
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001709 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001710 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001711
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001712 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001713 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001714
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001715 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001716
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001717 GLsizei streamLength = stream.length();
1718 const void *streamData = stream.data();
1719
1720 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001721 if (totalLength > bufSize)
1722 {
1723 if (length)
1724 {
1725 *length = 0;
1726 }
1727
1728 return false;
1729 }
1730
1731 if (binary)
1732 {
1733 char *ptr = (char*) binary;
1734
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001735 memcpy(ptr, streamData, streamLength);
1736 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001737
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001738 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001739 ptr += sizeof(GUID);
1740
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001741 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001742 ptr += pixelShaderSize;
1743
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001744 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001745 ptr += vertexShaderSize;
1746
1747 ASSERT(ptr - totalLength == binary);
1748 }
1749
1750 if (length)
1751 {
1752 *length = totalLength;
1753 }
1754
1755 return true;
1756}
1757
1758GLint ProgramBinary::getLength()
1759{
1760 GLint length;
1761 if (save(NULL, INT_MAX, &length))
1762 {
1763 return length;
1764 }
1765 else
1766 {
1767 return 0;
1768 }
1769}
1770
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001771bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001772{
1773 if (!fragmentShader || !fragmentShader->isCompiled())
1774 {
1775 return false;
1776 }
1777
1778 if (!vertexShader || !vertexShader->isCompiled())
1779 {
1780 return false;
1781 }
1782
1783 std::string pixelHLSL = fragmentShader->getHLSL();
1784 std::string vertexHLSL = vertexShader->getHLSL();
1785
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001786 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001787 {
1788 return false;
1789 }
1790
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001791 bool success = true;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001792 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1793 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001794
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001795 if (!mVertexExecutable || !mPixelExecutable)
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001796 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001797 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001798 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001799
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001800 delete mVertexExecutable;
1801 mVertexExecutable = NULL;
1802 delete mPixelExecutable;
1803 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001804 }
1805
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001806 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1807 {
1808 success = false;
1809 }
1810
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001811 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001812 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001813 success = false;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001814 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001815
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001816 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001817}
1818
1819// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001820bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001821{
1822 unsigned int usedLocations = 0;
1823
1824 // Link attributes that have a binding location
1825 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1826 {
1827 int location = attributeBindings.getAttributeBinding(attribute->name);
1828
1829 if (location != -1) // Set by glBindAttribLocation
1830 {
1831 if (!mLinkedAttribute[location].name.empty())
1832 {
1833 // Multiple active attributes bound to the same location; not an error
1834 }
1835
1836 mLinkedAttribute[location] = *attribute;
1837
1838 int rows = VariableRowCount(attribute->type);
1839
1840 if (rows + location > MAX_VERTEX_ATTRIBS)
1841 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001842 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 +00001843
1844 return false;
1845 }
1846
1847 for (int i = 0; i < rows; i++)
1848 {
1849 usedLocations |= 1 << (location + i);
1850 }
1851 }
1852 }
1853
1854 // Link attributes that don't have a binding location
1855 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1856 {
1857 int location = attributeBindings.getAttributeBinding(attribute->name);
1858
1859 if (location == -1) // Not set by glBindAttribLocation
1860 {
1861 int rows = VariableRowCount(attribute->type);
1862 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1863
1864 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1865 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001866 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001867
1868 return false; // Fail to link
1869 }
1870
1871 mLinkedAttribute[availableIndex] = *attribute;
1872 }
1873 }
1874
1875 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1876 {
1877 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1878 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1879
1880 for (int r = 0; r < rows; r++)
1881 {
1882 mSemanticIndex[attributeIndex++] = index++;
1883 }
1884 }
1885
1886 return true;
1887}
1888
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001889bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001890{
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001891 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001892 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001893 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001894 {
1895 return false;
1896 }
1897 }
1898
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001899 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++)
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001900 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001901 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001902 {
1903 return false;
1904 }
1905 }
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001906
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001907 return true;
1908}
1909
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001910bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001911{
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001912 if (constant.type == GL_SAMPLER_2D ||
1913 constant.type == GL_SAMPLER_CUBE)
1914 {
1915 unsigned int samplerIndex = constant.registerIndex;
1916
1917 do
1918 {
1919 if (shader == GL_VERTEX_SHADER)
1920 {
1921 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
1922 {
1923 mSamplersVS[samplerIndex].active = true;
1924 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1925 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
1926 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
1927 }
1928 else
1929 {
1930 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
1931 return false;
1932 }
1933 }
1934 else if (shader == GL_FRAGMENT_SHADER)
1935 {
1936 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1937 {
1938 mSamplersPS[samplerIndex].active = true;
1939 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1940 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1941 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1942 }
1943 else
1944 {
1945 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
1946 return false;
1947 }
1948 }
1949 else UNREACHABLE();
1950
1951 samplerIndex++;
1952 }
1953 while (samplerIndex < constant.registerIndex + constant.arraySize);
1954 }
1955
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001956 Uniform *uniform = NULL;
1957 GLint location = getUniformLocation(constant.name);
1958
1959 if (location >= 0) // Previously defined, types must match
1960 {
1961 uniform = mUniforms[mUniformIndex[location].index];
1962
1963 if (uniform->type != constant.type)
1964 {
1965 return false;
1966 }
1967 }
1968 else
1969 {
1970 uniform = new Uniform(constant.type, constant.name, constant.arraySize);
1971 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001972
1973 if (!uniform)
1974 {
1975 return false;
1976 }
1977
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001978 if (shader == GL_FRAGMENT_SHADER)
1979 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001980 uniform->psRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001981 }
1982 else if (shader == GL_VERTEX_SHADER)
1983 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001984 uniform->vsRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001985 }
1986 else UNREACHABLE();
1987
1988 if (location >= 0)
1989 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001990 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001991 }
1992
1993 mUniforms.push_back(uniform);
1994 unsigned int uniformIndex = mUniforms.size() - 1;
1995
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001996 for (unsigned int i = 0; i < uniform->elementCount(); i++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001997 {
1998 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
1999 }
2000
2001 return true;
2002}
2003
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002004// This method needs to match OutputHLSL::decorate
2005std::string ProgramBinary::decorateAttribute(const std::string &name)
2006{
2007 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2008 {
2009 return "_" + name;
2010 }
2011
2012 return name;
2013}
2014
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002015bool ProgramBinary::isValidated() const
2016{
2017 return mValidated;
2018}
2019
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002020void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2021{
2022 // Skip over inactive attributes
2023 unsigned int activeAttribute = 0;
2024 unsigned int attribute;
2025 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2026 {
2027 if (mLinkedAttribute[attribute].name.empty())
2028 {
2029 continue;
2030 }
2031
2032 if (activeAttribute == index)
2033 {
2034 break;
2035 }
2036
2037 activeAttribute++;
2038 }
2039
2040 if (bufsize > 0)
2041 {
2042 const char *string = mLinkedAttribute[attribute].name.c_str();
2043
2044 strncpy(name, string, bufsize);
2045 name[bufsize - 1] = '\0';
2046
2047 if (length)
2048 {
2049 *length = strlen(name);
2050 }
2051 }
2052
2053 *size = 1; // Always a single 'type' instance
2054
2055 *type = mLinkedAttribute[attribute].type;
2056}
2057
2058GLint ProgramBinary::getActiveAttributeCount()
2059{
2060 int count = 0;
2061
2062 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2063 {
2064 if (!mLinkedAttribute[attributeIndex].name.empty())
2065 {
2066 count++;
2067 }
2068 }
2069
2070 return count;
2071}
2072
2073GLint ProgramBinary::getActiveAttributeMaxLength()
2074{
2075 int maxLength = 0;
2076
2077 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2078 {
2079 if (!mLinkedAttribute[attributeIndex].name.empty())
2080 {
2081 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2082 }
2083 }
2084
2085 return maxLength;
2086}
2087
2088void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2089{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002090 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002091
2092 if (bufsize > 0)
2093 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002094 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002095
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002096 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002097 {
2098 string += "[0]";
2099 }
2100
2101 strncpy(name, string.c_str(), bufsize);
2102 name[bufsize - 1] = '\0';
2103
2104 if (length)
2105 {
2106 *length = strlen(name);
2107 }
2108 }
2109
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002110 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002111
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002112 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002113}
2114
2115GLint ProgramBinary::getActiveUniformCount()
2116{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002117 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002118}
2119
2120GLint ProgramBinary::getActiveUniformMaxLength()
2121{
2122 int maxLength = 0;
2123
2124 unsigned int numUniforms = mUniforms.size();
2125 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2126 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002127 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002128 {
2129 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2130 if (mUniforms[uniformIndex]->isArray())
2131 {
2132 length += 3; // Counting in "[0]".
2133 }
2134 maxLength = std::max(length, maxLength);
2135 }
2136 }
2137
2138 return maxLength;
2139}
2140
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002141void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002142{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002143 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002144 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002145 {
2146 mValidated = false;
2147 }
2148 else
2149 {
2150 mValidated = true;
2151 }
2152}
2153
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002154bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002155{
2156 // if any two active samplers in a program are of different types, but refer to the same
2157 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2158 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2159
2160 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2161 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2162
2163 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2164 {
2165 textureUnitType[i] = TEXTURE_UNKNOWN;
2166 }
2167
2168 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2169 {
2170 if (mSamplersPS[i].active)
2171 {
2172 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2173
2174 if (unit >= maxCombinedTextureImageUnits)
2175 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002176 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002177 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002178 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002179 }
2180
2181 return false;
2182 }
2183
2184 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2185 {
2186 if (mSamplersPS[i].textureType != textureUnitType[unit])
2187 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002188 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002189 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002190 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002191 }
2192
2193 return false;
2194 }
2195 }
2196 else
2197 {
2198 textureUnitType[unit] = mSamplersPS[i].textureType;
2199 }
2200 }
2201 }
2202
2203 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2204 {
2205 if (mSamplersVS[i].active)
2206 {
2207 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2208
2209 if (unit >= maxCombinedTextureImageUnits)
2210 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002211 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002212 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002213 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002214 }
2215
2216 return false;
2217 }
2218
2219 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2220 {
2221 if (mSamplersVS[i].textureType != textureUnitType[unit])
2222 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002223 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002224 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002225 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002226 }
2227
2228 return false;
2229 }
2230 }
2231 else
2232 {
2233 textureUnitType[unit] = mSamplersVS[i].textureType;
2234 }
2235 }
2236 }
2237
2238 return true;
2239}
2240
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002241ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2242{
2243}
2244
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002245}