blob: 6eaac91e284c330729a23f5e9a97f4c907f8b13f [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
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000057 for (int index = 0; index < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000058 {
59 mSamplersVS[index].active = false;
60 }
61
62 mUsedVertexSamplerRange = 0;
63 mUsedPixelSamplerRange = 0;
shannon.woods@transgaming.com962d4be2013-01-25 21:55:18 +000064 mUsesPointSize = false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000065}
66
67ProgramBinary::~ProgramBinary()
68{
daniel@transgaming.com95892412012-11-28 20:59:09 +000069 delete mPixelExecutable;
70 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000071
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000072 while (!mUniforms.empty())
73 {
74 delete mUniforms.back();
75 mUniforms.pop_back();
76 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000077}
78
daniel@transgaming.come87ca002012-07-24 18:30:43 +000079unsigned int ProgramBinary::getSerial() const
80{
81 return mSerial;
82}
83
84unsigned int ProgramBinary::issueSerial()
85{
86 return mCurrentSerial++;
87}
88
daniel@transgaming.com95892412012-11-28 20:59:09 +000089rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000090{
91 return mPixelExecutable;
92}
93
daniel@transgaming.com95892412012-11-28 20:59:09 +000094rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000095{
96 return mVertexExecutable;
97}
98
99GLuint ProgramBinary::getAttributeLocation(const char *name)
100{
101 if (name)
102 {
103 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
104 {
105 if (mLinkedAttribute[index].name == std::string(name))
106 {
107 return index;
108 }
109 }
110 }
111
112 return -1;
113}
114
115int ProgramBinary::getSemanticIndex(int attributeIndex)
116{
117 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
118
119 return mSemanticIndex[attributeIndex];
120}
121
122// Returns one more than the highest sampler index used.
123GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
124{
125 switch (type)
126 {
127 case SAMPLER_PIXEL:
128 return mUsedPixelSamplerRange;
129 case SAMPLER_VERTEX:
130 return mUsedVertexSamplerRange;
131 default:
132 UNREACHABLE();
133 return 0;
134 }
135}
136
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000137bool ProgramBinary::usesPointSize() const
138{
139 return mUsesPointSize;
140}
141
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000142// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
143// index (0-15 for the pixel shader and 0-3 for the vertex shader).
144GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
145{
146 GLint logicalTextureUnit = -1;
147
148 switch (type)
149 {
150 case SAMPLER_PIXEL:
151 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
152
153 if (mSamplersPS[samplerIndex].active)
154 {
155 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
156 }
157 break;
158 case SAMPLER_VERTEX:
159 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
160
161 if (mSamplersVS[samplerIndex].active)
162 {
163 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
164 }
165 break;
166 default: UNREACHABLE();
167 }
168
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000169 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)mRenderer->getMaxCombinedTextureImageUnits())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000170 {
171 return logicalTextureUnit;
172 }
173
174 return -1;
175}
176
177// Returns the texture type for a given Direct3D 9 sampler type and
178// index (0-15 for the pixel shader and 0-3 for the vertex shader).
179TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
180{
181 switch (type)
182 {
183 case SAMPLER_PIXEL:
184 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
185 ASSERT(mSamplersPS[samplerIndex].active);
186 return mSamplersPS[samplerIndex].textureType;
187 case SAMPLER_VERTEX:
188 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
189 ASSERT(mSamplersVS[samplerIndex].active);
190 return mSamplersVS[samplerIndex].textureType;
191 default: UNREACHABLE();
192 }
193
194 return TEXTURE_2D;
195}
196
197GLint ProgramBinary::getUniformLocation(std::string name)
198{
199 unsigned int subscript = 0;
200
201 // Strip any trailing array operator and retrieve the subscript
202 size_t open = name.find_last_of('[');
203 size_t close = name.find_last_of(']');
204 if (open != std::string::npos && close == name.length() - 1)
205 {
206 subscript = atoi(name.substr(open + 1).c_str());
207 name.erase(open);
208 }
209
210 unsigned int numUniforms = mUniformIndex.size();
211 for (unsigned int location = 0; location < numUniforms; location++)
212 {
213 if (mUniformIndex[location].name == name &&
214 mUniformIndex[location].element == subscript)
215 {
216 return location;
217 }
218 }
219
220 return -1;
221}
222
223bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
224{
225 if (location < 0 || location >= (int)mUniformIndex.size())
226 {
227 return false;
228 }
229
230 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
231 targetUniform->dirty = true;
232
233 if (targetUniform->type == GL_FLOAT)
234 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000235 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000236
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000237 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000238 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
239
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000240 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000241
242 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
243
244 for (int i = 0; i < count; i++)
245 {
246 target[0] = v[0];
247 target[1] = 0;
248 target[2] = 0;
249 target[3] = 0;
250 target += 4;
251 v += 1;
252 }
253 }
254 else if (targetUniform->type == GL_BOOL)
255 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000256 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000257
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000258 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000259 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
260
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000261 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000262 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
263
264 for (int i = 0; i < count; ++i)
265 {
266 if (v[i] == 0.0f)
267 {
268 boolParams[i] = GL_FALSE;
269 }
270 else
271 {
272 boolParams[i] = GL_TRUE;
273 }
274 }
275 }
276 else
277 {
278 return false;
279 }
280
281 return true;
282}
283
284bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
285{
286 if (location < 0 || location >= (int)mUniformIndex.size())
287 {
288 return false;
289 }
290
291 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
292 targetUniform->dirty = true;
293
294 if (targetUniform->type == GL_FLOAT_VEC2)
295 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000296 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000297
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000298 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000299 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
300
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000301 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000302
303 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
304
305 for (int i = 0; i < count; i++)
306 {
307 target[0] = v[0];
308 target[1] = v[1];
309 target[2] = 0;
310 target[3] = 0;
311 target += 4;
312 v += 2;
313 }
314 }
315 else if (targetUniform->type == GL_BOOL_VEC2)
316 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000317 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000318
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000319 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000320 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
321
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000322 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000323
324 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
325
326 for (int i = 0; i < count * 2; ++i)
327 {
328 if (v[i] == 0.0f)
329 {
330 boolParams[i] = GL_FALSE;
331 }
332 else
333 {
334 boolParams[i] = GL_TRUE;
335 }
336 }
337 }
338 else
339 {
340 return false;
341 }
342
343 return true;
344}
345
346bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
347{
348 if (location < 0 || location >= (int)mUniformIndex.size())
349 {
350 return false;
351 }
352
353 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
354 targetUniform->dirty = true;
355
356 if (targetUniform->type == GL_FLOAT_VEC3)
357 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000358 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000359
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000360 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000361 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
362
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000363 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000364
365 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
366
367 for (int i = 0; i < count; i++)
368 {
369 target[0] = v[0];
370 target[1] = v[1];
371 target[2] = v[2];
372 target[3] = 0;
373 target += 4;
374 v += 3;
375 }
376 }
377 else if (targetUniform->type == GL_BOOL_VEC3)
378 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000379 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000380
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000381 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000382 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
383
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000384 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000385 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
386
387 for (int i = 0; i < count * 3; ++i)
388 {
389 if (v[i] == 0.0f)
390 {
391 boolParams[i] = GL_FALSE;
392 }
393 else
394 {
395 boolParams[i] = GL_TRUE;
396 }
397 }
398 }
399 else
400 {
401 return false;
402 }
403
404 return true;
405}
406
407bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
408{
409 if (location < 0 || location >= (int)mUniformIndex.size())
410 {
411 return false;
412 }
413
414 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
415 targetUniform->dirty = true;
416
417 if (targetUniform->type == GL_FLOAT_VEC4)
418 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000419 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000420
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000421 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000422 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
423
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000424 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000425
426 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
427 v, 4 * sizeof(GLfloat) * count);
428 }
429 else if (targetUniform->type == GL_BOOL_VEC4)
430 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000431 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000432
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000433 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000434 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
435
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000436 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000437 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
438
439 for (int i = 0; i < count * 4; ++i)
440 {
441 if (v[i] == 0.0f)
442 {
443 boolParams[i] = GL_FALSE;
444 }
445 else
446 {
447 boolParams[i] = GL_TRUE;
448 }
449 }
450 }
451 else
452 {
453 return false;
454 }
455
456 return true;
457}
458
459template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
460void transposeMatrix(T *target, const GLfloat *value)
461{
462 int copyWidth = std::min(targetWidth, srcWidth);
463 int copyHeight = std::min(targetHeight, srcHeight);
464
465 for (int x = 0; x < copyWidth; x++)
466 {
467 for (int y = 0; y < copyHeight; y++)
468 {
469 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
470 }
471 }
472 // clear unfilled right side
473 for (int y = 0; y < copyHeight; y++)
474 {
475 for (int x = srcWidth; x < targetWidth; x++)
476 {
477 target[y * targetWidth + x] = (T)0;
478 }
479 }
480 // clear unfilled bottom.
481 for (int y = srcHeight; y < targetHeight; y++)
482 {
483 for (int x = 0; x < targetWidth; x++)
484 {
485 target[y * targetWidth + x] = (T)0;
486 }
487 }
488}
489
490bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
491{
492 if (location < 0 || location >= (int)mUniformIndex.size())
493 {
494 return false;
495 }
496
497 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
498 targetUniform->dirty = true;
499
500 if (targetUniform->type != GL_FLOAT_MAT2)
501 {
502 return false;
503 }
504
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000505 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000506
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000507 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000508 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
509
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000510 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000511
512 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
513 for (int i = 0; i < count; i++)
514 {
515 transposeMatrix<GLfloat,4,2,2,2>(target, value);
516 target += 8;
517 value += 4;
518 }
519
520 return true;
521}
522
523bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
524{
525 if (location < 0 || location >= (int)mUniformIndex.size())
526 {
527 return false;
528 }
529
530 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
531 targetUniform->dirty = true;
532
533 if (targetUniform->type != GL_FLOAT_MAT3)
534 {
535 return false;
536 }
537
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000538 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000539
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000540 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000541 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
542
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000543 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000544
545 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
546 for (int i = 0; i < count; i++)
547 {
548 transposeMatrix<GLfloat,4,3,3,3>(target, value);
549 target += 12;
550 value += 9;
551 }
552
553 return true;
554}
555
556
557bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
558{
559 if (location < 0 || location >= (int)mUniformIndex.size())
560 {
561 return false;
562 }
563
564 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
565 targetUniform->dirty = true;
566
567 if (targetUniform->type != GL_FLOAT_MAT4)
568 {
569 return false;
570 }
571
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000572 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000573
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000574 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000575 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
576
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000577 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000578
579 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
580 for (int i = 0; i < count; i++)
581 {
582 transposeMatrix<GLfloat,4,4,4,4>(target, value);
583 target += 16;
584 value += 16;
585 }
586
587 return true;
588}
589
590bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
591{
592 if (location < 0 || location >= (int)mUniformIndex.size())
593 {
594 return false;
595 }
596
597 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
598 targetUniform->dirty = true;
599
600 if (targetUniform->type == GL_INT ||
601 targetUniform->type == GL_SAMPLER_2D ||
602 targetUniform->type == GL_SAMPLER_CUBE)
603 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000604 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000605
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000606 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000607 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
608
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000609 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000610
611 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
612 v, sizeof(GLint) * count);
613 }
614 else if (targetUniform->type == GL_BOOL)
615 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000616 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000617
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000618 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000619 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
620
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000621 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000622 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
623
624 for (int i = 0; i < count; ++i)
625 {
626 if (v[i] == 0)
627 {
628 boolParams[i] = GL_FALSE;
629 }
630 else
631 {
632 boolParams[i] = GL_TRUE;
633 }
634 }
635 }
636 else
637 {
638 return false;
639 }
640
641 return true;
642}
643
644bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
645{
646 if (location < 0 || location >= (int)mUniformIndex.size())
647 {
648 return false;
649 }
650
651 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
652 targetUniform->dirty = true;
653
654 if (targetUniform->type == GL_INT_VEC2)
655 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000656 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000657
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000658 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000659 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
660
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000661 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000662
663 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
664 v, 2 * sizeof(GLint) * count);
665 }
666 else if (targetUniform->type == GL_BOOL_VEC2)
667 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000668 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000669
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000670 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000671 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
672
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000673 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000674 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
675
676 for (int i = 0; i < count * 2; ++i)
677 {
678 if (v[i] == 0)
679 {
680 boolParams[i] = GL_FALSE;
681 }
682 else
683 {
684 boolParams[i] = GL_TRUE;
685 }
686 }
687 }
688 else
689 {
690 return false;
691 }
692
693 return true;
694}
695
696bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
697{
698 if (location < 0 || location >= (int)mUniformIndex.size())
699 {
700 return false;
701 }
702
703 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
704 targetUniform->dirty = true;
705
706 if (targetUniform->type == GL_INT_VEC3)
707 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000708 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000709
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000710 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000711 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
712
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000713 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000714
715 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
716 v, 3 * sizeof(GLint) * count);
717 }
718 else if (targetUniform->type == GL_BOOL_VEC3)
719 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000720 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000721
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000722 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000723 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
724
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000725 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000726 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
727
728 for (int i = 0; i < count * 3; ++i)
729 {
730 if (v[i] == 0)
731 {
732 boolParams[i] = GL_FALSE;
733 }
734 else
735 {
736 boolParams[i] = GL_TRUE;
737 }
738 }
739 }
740 else
741 {
742 return false;
743 }
744
745 return true;
746}
747
748bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
749{
750 if (location < 0 || location >= (int)mUniformIndex.size())
751 {
752 return false;
753 }
754
755 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
756 targetUniform->dirty = true;
757
758 if (targetUniform->type == GL_INT_VEC4)
759 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000760 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000761
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000762 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000763 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
764
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000765 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000766
767 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
768 v, 4 * sizeof(GLint) * count);
769 }
770 else if (targetUniform->type == GL_BOOL_VEC4)
771 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000772 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000773
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000774 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000775 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
776
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000777 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000778 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
779
780 for (int i = 0; i < count * 4; ++i)
781 {
782 if (v[i] == 0)
783 {
784 boolParams[i] = GL_FALSE;
785 }
786 else
787 {
788 boolParams[i] = GL_TRUE;
789 }
790 }
791 }
792 else
793 {
794 return false;
795 }
796
797 return true;
798}
799
800bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
801{
802 if (location < 0 || location >= (int)mUniformIndex.size())
803 {
804 return false;
805 }
806
807 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
808
809 // sized queries -- ensure the provided buffer is large enough
810 if (bufSize)
811 {
812 int requiredBytes = UniformExternalSize(targetUniform->type);
813 if (*bufSize < requiredBytes)
814 {
815 return false;
816 }
817 }
818
819 switch (targetUniform->type)
820 {
821 case GL_FLOAT_MAT2:
822 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
823 break;
824 case GL_FLOAT_MAT3:
825 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
826 break;
827 case GL_FLOAT_MAT4:
828 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
829 break;
830 default:
831 {
832 unsigned int count = UniformExternalComponentCount(targetUniform->type);
833 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
834
835 switch (UniformComponentType(targetUniform->type))
836 {
837 case GL_BOOL:
838 {
839 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
840
841 for (unsigned int i = 0; i < count; ++i)
842 {
843 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
844 }
845 }
846 break;
847 case GL_FLOAT:
848 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
849 count * sizeof(GLfloat));
850 break;
851 case GL_INT:
852 {
853 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
854
855 for (unsigned int i = 0; i < count; ++i)
856 {
857 params[i] = (float)intParams[i];
858 }
859 }
860 break;
861 default: UNREACHABLE();
862 }
863 }
864 }
865
866 return true;
867}
868
869bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
870{
871 if (location < 0 || location >= (int)mUniformIndex.size())
872 {
873 return false;
874 }
875
876 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
877
878 // sized queries -- ensure the provided buffer is large enough
879 if (bufSize)
880 {
881 int requiredBytes = UniformExternalSize(targetUniform->type);
882 if (*bufSize < requiredBytes)
883 {
884 return false;
885 }
886 }
887
888 switch (targetUniform->type)
889 {
890 case GL_FLOAT_MAT2:
891 {
892 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
893 }
894 break;
895 case GL_FLOAT_MAT3:
896 {
897 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
898 }
899 break;
900 case GL_FLOAT_MAT4:
901 {
902 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
903 }
904 break;
905 default:
906 {
907 unsigned int count = UniformExternalComponentCount(targetUniform->type);
908 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
909
910 switch (UniformComponentType(targetUniform->type))
911 {
912 case GL_BOOL:
913 {
914 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
915
916 for (unsigned int i = 0; i < count; ++i)
917 {
918 params[i] = (GLint)boolParams[i];
919 }
920 }
921 break;
922 case GL_FLOAT:
923 {
924 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
925
926 for (unsigned int i = 0; i < count; ++i)
927 {
928 params[i] = (GLint)floatParams[i];
929 }
930 }
931 break;
932 case GL_INT:
933 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
934 count * sizeof(GLint));
935 break;
936 default: UNREACHABLE();
937 }
938 }
939 }
940
941 return true;
942}
943
944void ProgramBinary::dirtyAllUniforms()
945{
946 unsigned int numUniforms = mUniforms.size();
947 for (unsigned int index = 0; index < numUniforms; index++)
948 {
949 mUniforms[index]->dirty = true;
950 }
951}
952
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000953// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000954void ProgramBinary::applyUniforms()
955{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000956 // Retrieve sampler uniform values
957 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
958 {
959 Uniform *targetUniform = *ub;
960
961 if (targetUniform->dirty)
962 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000963 if (targetUniform->type == GL_SAMPLER_2D ||
964 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000965 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000966 int count = targetUniform->elementCount();
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000967 GLint *v = (GLint*)targetUniform->data;
968
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000969 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000970 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000971 unsigned int firstIndex = targetUniform->psRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000972
973 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000974 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000975 unsigned int samplerIndex = firstIndex + i;
976
977 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000978 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000979 ASSERT(mSamplersPS[samplerIndex].active);
980 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000981 }
982 }
983 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000984
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000985 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000986 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000987 unsigned int firstIndex = targetUniform->vsRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000988
989 for (int i = 0; i < count; i++)
990 {
991 unsigned int samplerIndex = firstIndex + i;
992
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000993 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000994 {
995 ASSERT(mSamplersVS[samplerIndex].active);
996 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
997 }
998 }
999 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001000 }
1001 }
1002 }
1003
shannon.woods@transgaming.com358e88d2013-01-25 21:53:11 +00001004 mRenderer->applyUniforms(this, &mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001005}
1006
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001007// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1008// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001009int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001010{
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001011 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001012
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001013 fragmentShader->resetVaryingsRegisterAssignment();
1014
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001015 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1016 {
1017 int n = VariableRowCount(varying->type) * varying->size;
1018 int m = VariableColumnCount(varying->type);
1019 bool success = false;
1020
1021 if (m == 2 || m == 3 || m == 4)
1022 {
1023 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1024 {
1025 bool available = true;
1026
1027 for (int y = 0; y < n && available; y++)
1028 {
1029 for (int x = 0; x < m && available; x++)
1030 {
1031 if (packing[r + y][x])
1032 {
1033 available = false;
1034 }
1035 }
1036 }
1037
1038 if (available)
1039 {
1040 varying->reg = r;
1041 varying->col = 0;
1042
1043 for (int y = 0; y < n; y++)
1044 {
1045 for (int x = 0; x < m; x++)
1046 {
1047 packing[r + y][x] = &*varying;
1048 }
1049 }
1050
1051 success = true;
1052 }
1053 }
1054
1055 if (!success && m == 2)
1056 {
1057 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1058 {
1059 bool available = true;
1060
1061 for (int y = 0; y < n && available; y++)
1062 {
1063 for (int x = 2; x < 4 && available; x++)
1064 {
1065 if (packing[r + y][x])
1066 {
1067 available = false;
1068 }
1069 }
1070 }
1071
1072 if (available)
1073 {
1074 varying->reg = r;
1075 varying->col = 2;
1076
1077 for (int y = 0; y < n; y++)
1078 {
1079 for (int x = 2; x < 4; x++)
1080 {
1081 packing[r + y][x] = &*varying;
1082 }
1083 }
1084
1085 success = true;
1086 }
1087 }
1088 }
1089 }
1090 else if (m == 1)
1091 {
1092 int space[4] = {0};
1093
1094 for (int y = 0; y < maxVaryingVectors; y++)
1095 {
1096 for (int x = 0; x < 4; x++)
1097 {
1098 space[x] += packing[y][x] ? 0 : 1;
1099 }
1100 }
1101
1102 int column = 0;
1103
1104 for (int x = 0; x < 4; x++)
1105 {
1106 if (space[x] >= n && space[x] < space[column])
1107 {
1108 column = x;
1109 }
1110 }
1111
1112 if (space[column] >= n)
1113 {
1114 for (int r = 0; r < maxVaryingVectors; r++)
1115 {
1116 if (!packing[r][column])
1117 {
1118 varying->reg = r;
1119
1120 for (int y = r; y < r + n; y++)
1121 {
1122 packing[y][column] = &*varying;
1123 }
1124
1125 break;
1126 }
1127 }
1128
1129 varying->col = column;
1130
1131 success = true;
1132 }
1133 }
1134 else UNREACHABLE();
1135
1136 if (!success)
1137 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001138 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001139
1140 return -1;
1141 }
1142 }
1143
1144 // Return the number of used registers
1145 int registers = 0;
1146
1147 for (int r = 0; r < maxVaryingVectors; r++)
1148 {
1149 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1150 {
1151 registers++;
1152 }
1153 }
1154
1155 return registers;
1156}
1157
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001158bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
1159 std::string& pixelHLSL, std::string& vertexHLSL,
1160 FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001161{
1162 if (pixelHLSL.empty() || vertexHLSL.empty())
1163 {
1164 return false;
1165 }
1166
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001167 // Write the HLSL input/output declarations
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001168 const int shaderModel = mRenderer->getMajorShaderModel();
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001169 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001170
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001171 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
1172
1173 if (registersNeeded > maxVaryingVectors)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001174 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001175 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001176
1177 return false;
1178 }
1179
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001180 vertexShader->resetVaryingsRegisterAssignment();
1181
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001182 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1183 {
1184 bool matched = false;
1185
1186 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1187 {
1188 if (output->name == input->name)
1189 {
1190 if (output->type != input->type || output->size != input->size)
1191 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001192 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 +00001193
1194 return false;
1195 }
1196
1197 output->reg = input->reg;
1198 output->col = input->col;
1199
1200 matched = true;
1201 break;
1202 }
1203 }
1204
1205 if (!matched)
1206 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001207 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001208
1209 return false;
1210 }
1211 }
1212
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001213 mUsesPointSize = vertexShader->mUsesPointSize;
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001214 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001215 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001216 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
1217
1218 // special varyings that use reserved registers
1219 int reservedRegisterIndex = registers;
1220 std::string fragCoordSemantic;
1221 std::string pointCoordSemantic;
1222
1223 if (fragmentShader->mUsesFragCoord)
1224 {
1225 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1226 }
1227
1228 if (fragmentShader->mUsesPointCoord)
1229 {
1230 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
1231 // In DX11 we compute this in the GS.
1232 if (shaderModel == 3)
1233 {
1234 pointCoordSemantic = "TEXCOORD0";
1235 }
1236 else if (shaderModel >= 4)
1237 {
1238 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1239 }
1240 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001241
1242 vertexHLSL += "struct VS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001243 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001244
1245 int semanticIndex = 0;
1246 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1247 {
1248 switch (attribute->type)
1249 {
1250 case GL_FLOAT: vertexHLSL += " float "; break;
1251 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1252 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1253 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1254 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1255 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1256 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1257 default: UNREACHABLE();
1258 }
1259
1260 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1261
1262 semanticIndex += VariableRowCount(attribute->type);
1263 }
1264
1265 vertexHLSL += "};\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001266 "\n"
1267 "struct VS_OUTPUT\n"
1268 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001269
1270 for (int r = 0; r < registers; r++)
1271 {
1272 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1273
1274 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1275 }
1276
1277 if (fragmentShader->mUsesFragCoord)
1278 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001279 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001280 }
1281
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001282 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001283 {
1284 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1285 }
1286
daniel@transgaming.com9c4a6252013-01-11 04:07:18 +00001287 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n"
1288 "};\n"
1289 "\n"
1290 "VS_OUTPUT main(VS_INPUT input)\n"
1291 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001292
1293 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1294 {
1295 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1296
1297 if (VariableRowCount(attribute->type) > 1) // Matrix
1298 {
1299 vertexHLSL += "transpose";
1300 }
1301
1302 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1303 }
1304
1305 vertexHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001306 " gl_main();\n"
1307 "\n"
1308 " VS_OUTPUT output;\n"
1309 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
1310 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
1311 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1312 " output.gl_Position.w = gl_Position.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001313
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001314 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001315 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001316 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001317 }
1318
1319 if (fragmentShader->mUsesFragCoord)
1320 {
1321 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1322 }
1323
1324 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1325 {
1326 if (varying->reg >= 0)
1327 {
1328 for (int i = 0; i < varying->size; i++)
1329 {
1330 int rows = VariableRowCount(varying->type);
1331
1332 for (int j = 0; j < rows; j++)
1333 {
1334 int r = varying->reg + i * rows + j;
1335 vertexHLSL += " output.v" + str(r);
1336
1337 bool sharedRegister = false; // Register used by multiple varyings
1338
1339 for (int x = 0; x < 4; x++)
1340 {
1341 if (packing[r][x] && packing[r][x] != packing[r][0])
1342 {
1343 sharedRegister = true;
1344 break;
1345 }
1346 }
1347
1348 if(sharedRegister)
1349 {
1350 vertexHLSL += ".";
1351
1352 for (int x = 0; x < 4; x++)
1353 {
1354 if (packing[r][x] == &*varying)
1355 {
1356 switch(x)
1357 {
1358 case 0: vertexHLSL += "x"; break;
1359 case 1: vertexHLSL += "y"; break;
1360 case 2: vertexHLSL += "z"; break;
1361 case 3: vertexHLSL += "w"; break;
1362 }
1363 }
1364 }
1365 }
1366
1367 vertexHLSL += " = " + varying->name;
1368
1369 if (varying->array)
1370 {
1371 vertexHLSL += "[" + str(i) + "]";
1372 }
1373
1374 if (rows > 1)
1375 {
1376 vertexHLSL += "[" + str(j) + "]";
1377 }
1378
1379 vertexHLSL += ";\n";
1380 }
1381 }
1382 }
1383 }
1384
1385 vertexHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001386 " return output;\n"
1387 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001388
1389 pixelHLSL += "struct PS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001390 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001391
1392 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1393 {
1394 if (varying->reg >= 0)
1395 {
1396 for (int i = 0; i < varying->size; i++)
1397 {
1398 int rows = VariableRowCount(varying->type);
1399 for (int j = 0; j < rows; j++)
1400 {
1401 std::string n = str(varying->reg + i * rows + j);
daniel@transgaming.com00c0d152013-01-11 04:07:23 +00001402 pixelHLSL += " float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001403 }
1404 }
1405 }
1406 else UNREACHABLE();
1407 }
1408
1409 if (fragmentShader->mUsesFragCoord)
1410 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001411 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001412
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001413 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001414 {
1415 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1416 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001417 else if (shaderModel >= 3)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001418 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001419 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1420 }
1421 }
1422
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001423 if (fragmentShader->mUsesPointCoord && shaderModel == 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001424 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001425 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001426 }
1427
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001428 pixelHLSL += "};\n"
1429 "\n"
1430 "struct PS_OUTPUT\n"
1431 "{\n"
1432 " float4 gl_Color[1] : " + targetSemantic + ";\n"
1433 "};\n"
1434 "\n";
1435
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001436 if (fragmentShader->mUsesFrontFacing)
1437 {
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001438 if (shaderModel >= 4)
1439 {
1440 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
1441 "{\n";
1442 }
1443 else
1444 {
1445 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
1446 "{\n";
1447 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001448 }
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001449 else
1450 {
1451 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
1452 "{\n";
1453 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001454
1455 if (fragmentShader->mUsesFragCoord)
1456 {
1457 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1458
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001459 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001460 {
1461 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1462 " gl_FragCoord.y = input.dx_VPos.y;\n";
1463 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001464 else if (shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001465 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001466 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001467 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001468 }
1469 else
1470 {
1471 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1472 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001473 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001474 }
1475
daniel@transgaming.com12985182012-12-20 20:56:31 +00001476 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001477 " gl_FragCoord.w = rhw;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001478 }
1479
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001480 if (fragmentShader->mUsesPointCoord && shaderModel == 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001481 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001482 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1483 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001484 }
1485
1486 if (fragmentShader->mUsesFrontFacing)
1487 {
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001488 if (shaderModel <= 3)
1489 {
1490 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1491 }
1492 else
1493 {
1494 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1495 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001496 }
1497
1498 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1499 {
1500 if (varying->reg >= 0)
1501 {
1502 for (int i = 0; i < varying->size; i++)
1503 {
1504 int rows = VariableRowCount(varying->type);
1505 for (int j = 0; j < rows; j++)
1506 {
1507 std::string n = str(varying->reg + i * rows + j);
1508 pixelHLSL += " " + varying->name;
1509
1510 if (varying->array)
1511 {
1512 pixelHLSL += "[" + str(i) + "]";
1513 }
1514
1515 if (rows > 1)
1516 {
1517 pixelHLSL += "[" + str(j) + "]";
1518 }
1519
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001520 switch (VariableColumnCount(varying->type))
1521 {
1522 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1523 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1524 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1525 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1526 default: UNREACHABLE();
1527 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001528 }
1529 }
1530 }
1531 else UNREACHABLE();
1532 }
1533
1534 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001535 " gl_main();\n"
1536 "\n"
1537 " PS_OUTPUT output;\n"
1538 " output.gl_Color[0] = gl_Color[0];\n"
1539 "\n"
1540 " return output;\n"
1541 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001542
1543 return true;
1544}
1545
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001546bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1547{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001548 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001549
1550 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001551 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001552 if (format != GL_PROGRAM_BINARY_ANGLE)
1553 {
1554 infoLog.append("Invalid program binary format.");
1555 return false;
1556 }
1557
1558 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001559 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001560 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001561 {
1562 infoLog.append("Invalid program binary version.");
1563 return false;
1564 }
1565
1566 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1567 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001568 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001569 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001570 stream.read(&name);
1571 mLinkedAttribute[i].name = name;
1572 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001573 }
1574
1575 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1576 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001577 stream.read(&mSamplersPS[i].active);
1578 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001579
1580 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001581 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001582 mSamplersPS[i].textureType = (TextureType) textureType;
1583 }
1584
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001585 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001586 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001587 stream.read(&mSamplersVS[i].active);
1588 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001589
1590 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001591 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001592 mSamplersVS[i].textureType = (TextureType) textureType;
1593 }
1594
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001595 stream.read(&mUsedVertexSamplerRange);
1596 stream.read(&mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001597 stream.read(&mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001598
1599 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001600 stream.read(&size);
1601 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001602 {
1603 infoLog.append("Invalid program binary.");
1604 return false;
1605 }
1606
1607 mUniforms.resize(size);
1608 for (unsigned int i = 0; i < size; ++i)
1609 {
1610 GLenum type;
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001611 std::string name;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001612 unsigned int arraySize;
1613
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001614 stream.read(&type);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001615 stream.read(&name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001616 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001617
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001618 mUniforms[i] = new Uniform(type, name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001619
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001620 stream.read(&mUniforms[i]->psRegisterIndex);
1621 stream.read(&mUniforms[i]->vsRegisterIndex);
1622 stream.read(&mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001623 }
1624
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001625 stream.read(&size);
1626 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001627 {
1628 infoLog.append("Invalid program binary.");
1629 return false;
1630 }
1631
1632 mUniformIndex.resize(size);
1633 for (unsigned int i = 0; i < size; ++i)
1634 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001635 stream.read(&mUniformIndex[i].name);
1636 stream.read(&mUniformIndex[i].element);
1637 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001638 }
1639
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001640 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001641 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001642
1643 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001644 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001645
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001646 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001647
daniel@transgaming.com36038542012-11-28 20:59:26 +00001648 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001649 ptr += sizeof(GUID);
1650
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001651 GUID identifier = mRenderer->getAdapterIdentifier();
1652 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001653 {
1654 infoLog.append("Invalid program binary.");
1655 return false;
1656 }
1657
1658 const char *pixelShaderFunction = ptr;
1659 ptr += pixelShaderSize;
1660
1661 const char *vertexShaderFunction = ptr;
1662 ptr += vertexShaderSize;
1663
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001664 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001665 pixelShaderSize, rx::SHADER_PIXEL);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001666 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001667 {
1668 infoLog.append("Could not create pixel shader.");
1669 return false;
1670 }
1671
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001672 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001673 vertexShaderSize, rx::SHADER_VERTEX);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001674 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001675 {
1676 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001677 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001678 mPixelExecutable = NULL;
1679 return false;
1680 }
1681
1682 return true;
1683}
1684
1685bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1686{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001687 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001688
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001689 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001690 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001691
1692 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1693 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001694 stream.write(mLinkedAttribute[i].type);
1695 stream.write(mLinkedAttribute[i].name);
1696 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001697 }
1698
1699 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1700 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001701 stream.write(mSamplersPS[i].active);
1702 stream.write(mSamplersPS[i].logicalTextureUnit);
1703 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001704 }
1705
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001706 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001707 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001708 stream.write(mSamplersVS[i].active);
1709 stream.write(mSamplersVS[i].logicalTextureUnit);
1710 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001711 }
1712
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001713 stream.write(mUsedVertexSamplerRange);
1714 stream.write(mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001715 stream.write(mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001716
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001717 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001718 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1719 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001720 stream.write(mUniforms[i]->type);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001721 stream.write(mUniforms[i]->name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001722 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001723
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001724 stream.write(mUniforms[i]->psRegisterIndex);
1725 stream.write(mUniforms[i]->vsRegisterIndex);
1726 stream.write(mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001727 }
1728
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001729 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001730 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1731 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001732 stream.write(mUniformIndex[i].name);
1733 stream.write(mUniformIndex[i].element);
1734 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001735 }
1736
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001737 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001738 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001739
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001740 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001741 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001742
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001743 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001744
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001745 GLsizei streamLength = stream.length();
1746 const void *streamData = stream.data();
1747
1748 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001749 if (totalLength > bufSize)
1750 {
1751 if (length)
1752 {
1753 *length = 0;
1754 }
1755
1756 return false;
1757 }
1758
1759 if (binary)
1760 {
1761 char *ptr = (char*) binary;
1762
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001763 memcpy(ptr, streamData, streamLength);
1764 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001765
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001766 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001767 ptr += sizeof(GUID);
1768
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001769 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001770 ptr += pixelShaderSize;
1771
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001772 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001773 ptr += vertexShaderSize;
1774
1775 ASSERT(ptr - totalLength == binary);
1776 }
1777
1778 if (length)
1779 {
1780 *length = totalLength;
1781 }
1782
1783 return true;
1784}
1785
1786GLint ProgramBinary::getLength()
1787{
1788 GLint length;
1789 if (save(NULL, INT_MAX, &length))
1790 {
1791 return length;
1792 }
1793 else
1794 {
1795 return 0;
1796 }
1797}
1798
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001799bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001800{
1801 if (!fragmentShader || !fragmentShader->isCompiled())
1802 {
1803 return false;
1804 }
1805
1806 if (!vertexShader || !vertexShader->isCompiled())
1807 {
1808 return false;
1809 }
1810
1811 std::string pixelHLSL = fragmentShader->getHLSL();
1812 std::string vertexHLSL = vertexShader->getHLSL();
1813
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001814 // Map the varyings to the register file
1815 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
1816 int registers = packVaryings(infoLog, packing, fragmentShader);
1817
1818 if (registers < 0)
1819 {
1820 return false;
1821 }
1822
1823 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001824 {
1825 return false;
1826 }
1827
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001828 bool success = true;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001829 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX);
1830 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001831
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001832 if (!mVertexExecutable || !mPixelExecutable)
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001833 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001834 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001835 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001836
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001837 delete mVertexExecutable;
1838 mVertexExecutable = NULL;
1839 delete mPixelExecutable;
1840 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001841 }
1842
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001843 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1844 {
1845 success = false;
1846 }
1847
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001848 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001849 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001850 success = false;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001851 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001852
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001853 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001854}
1855
1856// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001857bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001858{
1859 unsigned int usedLocations = 0;
1860
1861 // Link attributes that have a binding location
1862 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1863 {
1864 int location = attributeBindings.getAttributeBinding(attribute->name);
1865
1866 if (location != -1) // Set by glBindAttribLocation
1867 {
1868 if (!mLinkedAttribute[location].name.empty())
1869 {
1870 // Multiple active attributes bound to the same location; not an error
1871 }
1872
1873 mLinkedAttribute[location] = *attribute;
1874
1875 int rows = VariableRowCount(attribute->type);
1876
1877 if (rows + location > MAX_VERTEX_ATTRIBS)
1878 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001879 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 +00001880
1881 return false;
1882 }
1883
1884 for (int i = 0; i < rows; i++)
1885 {
1886 usedLocations |= 1 << (location + i);
1887 }
1888 }
1889 }
1890
1891 // Link attributes that don't have a binding location
1892 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1893 {
1894 int location = attributeBindings.getAttributeBinding(attribute->name);
1895
1896 if (location == -1) // Not set by glBindAttribLocation
1897 {
1898 int rows = VariableRowCount(attribute->type);
1899 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1900
1901 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1902 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001903 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001904
1905 return false; // Fail to link
1906 }
1907
1908 mLinkedAttribute[availableIndex] = *attribute;
1909 }
1910 }
1911
1912 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1913 {
1914 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1915 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1916
1917 for (int r = 0; r < rows; r++)
1918 {
1919 mSemanticIndex[attributeIndex++] = index++;
1920 }
1921 }
1922
1923 return true;
1924}
1925
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001926bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001927{
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001928 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001929 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001930 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001931 {
1932 return false;
1933 }
1934 }
1935
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001936 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++)
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001937 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001938 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001939 {
1940 return false;
1941 }
1942 }
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001943
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001944 return true;
1945}
1946
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001947bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001948{
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001949 if (constant.type == GL_SAMPLER_2D ||
1950 constant.type == GL_SAMPLER_CUBE)
1951 {
1952 unsigned int samplerIndex = constant.registerIndex;
1953
1954 do
1955 {
1956 if (shader == GL_VERTEX_SHADER)
1957 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001958 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001959 {
1960 mSamplersVS[samplerIndex].active = true;
1961 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1962 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
1963 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
1964 }
1965 else
1966 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001967 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001968 return false;
1969 }
1970 }
1971 else if (shader == GL_FRAGMENT_SHADER)
1972 {
1973 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1974 {
1975 mSamplersPS[samplerIndex].active = true;
1976 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1977 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1978 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1979 }
1980 else
1981 {
1982 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
1983 return false;
1984 }
1985 }
1986 else UNREACHABLE();
1987
1988 samplerIndex++;
1989 }
1990 while (samplerIndex < constant.registerIndex + constant.arraySize);
1991 }
1992
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001993 Uniform *uniform = NULL;
1994 GLint location = getUniformLocation(constant.name);
1995
1996 if (location >= 0) // Previously defined, types must match
1997 {
1998 uniform = mUniforms[mUniformIndex[location].index];
1999
2000 if (uniform->type != constant.type)
2001 {
2002 return false;
2003 }
2004 }
2005 else
2006 {
2007 uniform = new Uniform(constant.type, constant.name, constant.arraySize);
2008 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002009
2010 if (!uniform)
2011 {
2012 return false;
2013 }
2014
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002015 if (shader == GL_FRAGMENT_SHADER)
2016 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00002017 uniform->psRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002018 }
2019 else if (shader == GL_VERTEX_SHADER)
2020 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00002021 uniform->vsRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002022 }
2023 else UNREACHABLE();
2024
2025 if (location >= 0)
2026 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002027 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002028 }
2029
2030 mUniforms.push_back(uniform);
2031 unsigned int uniformIndex = mUniforms.size() - 1;
2032
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002033 for (unsigned int i = 0; i < uniform->elementCount(); i++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002034 {
2035 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
2036 }
2037
2038 return true;
2039}
2040
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002041// This method needs to match OutputHLSL::decorate
2042std::string ProgramBinary::decorateAttribute(const std::string &name)
2043{
2044 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2045 {
2046 return "_" + name;
2047 }
2048
2049 return name;
2050}
2051
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002052bool ProgramBinary::isValidated() const
2053{
2054 return mValidated;
2055}
2056
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002057void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2058{
2059 // Skip over inactive attributes
2060 unsigned int activeAttribute = 0;
2061 unsigned int attribute;
2062 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2063 {
2064 if (mLinkedAttribute[attribute].name.empty())
2065 {
2066 continue;
2067 }
2068
2069 if (activeAttribute == index)
2070 {
2071 break;
2072 }
2073
2074 activeAttribute++;
2075 }
2076
2077 if (bufsize > 0)
2078 {
2079 const char *string = mLinkedAttribute[attribute].name.c_str();
2080
2081 strncpy(name, string, bufsize);
2082 name[bufsize - 1] = '\0';
2083
2084 if (length)
2085 {
2086 *length = strlen(name);
2087 }
2088 }
2089
2090 *size = 1; // Always a single 'type' instance
2091
2092 *type = mLinkedAttribute[attribute].type;
2093}
2094
2095GLint ProgramBinary::getActiveAttributeCount()
2096{
2097 int count = 0;
2098
2099 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2100 {
2101 if (!mLinkedAttribute[attributeIndex].name.empty())
2102 {
2103 count++;
2104 }
2105 }
2106
2107 return count;
2108}
2109
2110GLint ProgramBinary::getActiveAttributeMaxLength()
2111{
2112 int maxLength = 0;
2113
2114 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2115 {
2116 if (!mLinkedAttribute[attributeIndex].name.empty())
2117 {
2118 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2119 }
2120 }
2121
2122 return maxLength;
2123}
2124
2125void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2126{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002127 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002128
2129 if (bufsize > 0)
2130 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002131 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002132
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002133 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002134 {
2135 string += "[0]";
2136 }
2137
2138 strncpy(name, string.c_str(), bufsize);
2139 name[bufsize - 1] = '\0';
2140
2141 if (length)
2142 {
2143 *length = strlen(name);
2144 }
2145 }
2146
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002147 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002148
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002149 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002150}
2151
2152GLint ProgramBinary::getActiveUniformCount()
2153{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002154 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002155}
2156
2157GLint ProgramBinary::getActiveUniformMaxLength()
2158{
2159 int maxLength = 0;
2160
2161 unsigned int numUniforms = mUniforms.size();
2162 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2163 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002164 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002165 {
2166 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2167 if (mUniforms[uniformIndex]->isArray())
2168 {
2169 length += 3; // Counting in "[0]".
2170 }
2171 maxLength = std::max(length, maxLength);
2172 }
2173 }
2174
2175 return maxLength;
2176}
2177
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002178void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002179{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002180 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002181 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002182 {
2183 mValidated = false;
2184 }
2185 else
2186 {
2187 mValidated = true;
2188 }
2189}
2190
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002191bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002192{
2193 // if any two active samplers in a program are of different types, but refer to the same
2194 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2195 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2196
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002197 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002198 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002199
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002200 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002201 {
2202 textureUnitType[i] = TEXTURE_UNKNOWN;
2203 }
2204
2205 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2206 {
2207 if (mSamplersPS[i].active)
2208 {
2209 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2210
2211 if (unit >= maxCombinedTextureImageUnits)
2212 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002213 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002214 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002215 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002216 }
2217
2218 return false;
2219 }
2220
2221 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2222 {
2223 if (mSamplersPS[i].textureType != textureUnitType[unit])
2224 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002225 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002226 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002227 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002228 }
2229
2230 return false;
2231 }
2232 }
2233 else
2234 {
2235 textureUnitType[unit] = mSamplersPS[i].textureType;
2236 }
2237 }
2238 }
2239
2240 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2241 {
2242 if (mSamplersVS[i].active)
2243 {
2244 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2245
2246 if (unit >= maxCombinedTextureImageUnits)
2247 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002248 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002249 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002250 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002251 }
2252
2253 return false;
2254 }
2255
2256 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2257 {
2258 if (mSamplersVS[i].textureType != textureUnitType[unit])
2259 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002260 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002261 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002262 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002263 }
2264
2265 return false;
2266 }
2267 }
2268 else
2269 {
2270 textureUnitType[unit] = mSamplersVS[i].textureType;
2271 }
2272 }
2273 }
2274
2275 return true;
2276}
2277
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002278ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2279{
2280}
2281
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002282}