blob: 7a069ab2a1555c648ff44fc3d30f92846c26c8e1 [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;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000044 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000045
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000046 mValidated = false;
47
48 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
49 {
50 mSemanticIndex[index] = -1;
51 }
52
53 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
54 {
55 mSamplersPS[index].active = false;
56 }
57
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000058 for (int index = 0; index < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000059 {
60 mSamplersVS[index].active = false;
61 }
62
63 mUsedVertexSamplerRange = 0;
64 mUsedPixelSamplerRange = 0;
shannon.woods@transgaming.com962d4be2013-01-25 21:55:18 +000065 mUsesPointSize = false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000066}
67
68ProgramBinary::~ProgramBinary()
69{
daniel@transgaming.com95892412012-11-28 20:59:09 +000070 delete mPixelExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000071 mPixelExecutable = NULL;
72
daniel@transgaming.com95892412012-11-28 20:59:09 +000073 delete mVertexExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000074 mVertexExecutable = NULL;
75
76 delete mGeometryExecutable;
77 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000078
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000079 while (!mUniforms.empty())
80 {
81 delete mUniforms.back();
82 mUniforms.pop_back();
83 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000084}
85
daniel@transgaming.come87ca002012-07-24 18:30:43 +000086unsigned int ProgramBinary::getSerial() const
87{
88 return mSerial;
89}
90
91unsigned int ProgramBinary::issueSerial()
92{
93 return mCurrentSerial++;
94}
95
daniel@transgaming.com95892412012-11-28 20:59:09 +000096rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000097{
98 return mPixelExecutable;
99}
100
daniel@transgaming.com95892412012-11-28 20:59:09 +0000101rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000102{
103 return mVertexExecutable;
104}
105
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000106rx::ShaderExecutable *ProgramBinary::getGeometryExecutable()
107{
108 return mGeometryExecutable;
109}
110
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000111GLuint ProgramBinary::getAttributeLocation(const char *name)
112{
113 if (name)
114 {
115 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
116 {
117 if (mLinkedAttribute[index].name == std::string(name))
118 {
119 return index;
120 }
121 }
122 }
123
124 return -1;
125}
126
127int ProgramBinary::getSemanticIndex(int attributeIndex)
128{
129 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
130
131 return mSemanticIndex[attributeIndex];
132}
133
134// Returns one more than the highest sampler index used.
135GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
136{
137 switch (type)
138 {
139 case SAMPLER_PIXEL:
140 return mUsedPixelSamplerRange;
141 case SAMPLER_VERTEX:
142 return mUsedVertexSamplerRange;
143 default:
144 UNREACHABLE();
145 return 0;
146 }
147}
148
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000149bool ProgramBinary::usesPointSize() const
150{
151 return mUsesPointSize;
152}
153
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000154bool ProgramBinary::usesPointSpriteEmulation() const
155{
156 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
157}
158
159bool ProgramBinary::usesGeometryShader() const
160{
161 return usesPointSpriteEmulation();
162}
163
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000164// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
165// index (0-15 for the pixel shader and 0-3 for the vertex shader).
166GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
167{
168 GLint logicalTextureUnit = -1;
169
170 switch (type)
171 {
172 case SAMPLER_PIXEL:
173 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
174
175 if (mSamplersPS[samplerIndex].active)
176 {
177 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
178 }
179 break;
180 case SAMPLER_VERTEX:
181 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
182
183 if (mSamplersVS[samplerIndex].active)
184 {
185 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
186 }
187 break;
188 default: UNREACHABLE();
189 }
190
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000191 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)mRenderer->getMaxCombinedTextureImageUnits())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000192 {
193 return logicalTextureUnit;
194 }
195
196 return -1;
197}
198
199// Returns the texture type for a given Direct3D 9 sampler type and
200// index (0-15 for the pixel shader and 0-3 for the vertex shader).
201TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
202{
203 switch (type)
204 {
205 case SAMPLER_PIXEL:
206 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
207 ASSERT(mSamplersPS[samplerIndex].active);
208 return mSamplersPS[samplerIndex].textureType;
209 case SAMPLER_VERTEX:
210 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
211 ASSERT(mSamplersVS[samplerIndex].active);
212 return mSamplersVS[samplerIndex].textureType;
213 default: UNREACHABLE();
214 }
215
216 return TEXTURE_2D;
217}
218
219GLint ProgramBinary::getUniformLocation(std::string name)
220{
221 unsigned int subscript = 0;
222
223 // Strip any trailing array operator and retrieve the subscript
224 size_t open = name.find_last_of('[');
225 size_t close = name.find_last_of(']');
226 if (open != std::string::npos && close == name.length() - 1)
227 {
228 subscript = atoi(name.substr(open + 1).c_str());
229 name.erase(open);
230 }
231
232 unsigned int numUniforms = mUniformIndex.size();
233 for (unsigned int location = 0; location < numUniforms; location++)
234 {
235 if (mUniformIndex[location].name == name &&
236 mUniformIndex[location].element == subscript)
237 {
238 return location;
239 }
240 }
241
242 return -1;
243}
244
245bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
246{
247 if (location < 0 || location >= (int)mUniformIndex.size())
248 {
249 return false;
250 }
251
252 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
253 targetUniform->dirty = true;
254
255 if (targetUniform->type == GL_FLOAT)
256 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000257 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000258
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000259 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000260 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
261
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000262 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000263
264 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
265
266 for (int i = 0; i < count; i++)
267 {
268 target[0] = v[0];
269 target[1] = 0;
270 target[2] = 0;
271 target[3] = 0;
272 target += 4;
273 v += 1;
274 }
275 }
276 else if (targetUniform->type == GL_BOOL)
277 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000278 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000279
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000280 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000281 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
282
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000283 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000284 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000285
286 for (int i = 0; i < count; ++i)
287 {
288 if (v[i] == 0.0f)
289 {
290 boolParams[i] = GL_FALSE;
291 }
292 else
293 {
294 boolParams[i] = GL_TRUE;
295 }
296 }
297 }
298 else
299 {
300 return false;
301 }
302
303 return true;
304}
305
306bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
307{
308 if (location < 0 || location >= (int)mUniformIndex.size())
309 {
310 return false;
311 }
312
313 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
314 targetUniform->dirty = true;
315
316 if (targetUniform->type == GL_FLOAT_VEC2)
317 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000318 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000319
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000320 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000321 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
322
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000323 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000324
325 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
326
327 for (int i = 0; i < count; i++)
328 {
329 target[0] = v[0];
330 target[1] = v[1];
331 target[2] = 0;
332 target[3] = 0;
333 target += 4;
334 v += 2;
335 }
336 }
337 else if (targetUniform->type == GL_BOOL_VEC2)
338 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000339 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000340
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000341 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000342 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
343
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000344 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000345
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000346 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 2;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000347
348 for (int i = 0; i < count * 2; ++i)
349 {
350 if (v[i] == 0.0f)
351 {
352 boolParams[i] = GL_FALSE;
353 }
354 else
355 {
356 boolParams[i] = GL_TRUE;
357 }
358 }
359 }
360 else
361 {
362 return false;
363 }
364
365 return true;
366}
367
368bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
369{
370 if (location < 0 || location >= (int)mUniformIndex.size())
371 {
372 return false;
373 }
374
375 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
376 targetUniform->dirty = true;
377
378 if (targetUniform->type == GL_FLOAT_VEC3)
379 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000380 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000381
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000382 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000383 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
384
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000385 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000386
387 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
388
389 for (int i = 0; i < count; i++)
390 {
391 target[0] = v[0];
392 target[1] = v[1];
393 target[2] = v[2];
394 target[3] = 0;
395 target += 4;
396 v += 3;
397 }
398 }
399 else if (targetUniform->type == GL_BOOL_VEC3)
400 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000401 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000402
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000403 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000404 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
405
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000406 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000407 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 3;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000408
409 for (int i = 0; i < count * 3; ++i)
410 {
411 if (v[i] == 0.0f)
412 {
413 boolParams[i] = GL_FALSE;
414 }
415 else
416 {
417 boolParams[i] = GL_TRUE;
418 }
419 }
420 }
421 else
422 {
423 return false;
424 }
425
426 return true;
427}
428
429bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
430{
431 if (location < 0 || location >= (int)mUniformIndex.size())
432 {
433 return false;
434 }
435
436 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
437 targetUniform->dirty = true;
438
439 if (targetUniform->type == GL_FLOAT_VEC4)
440 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000441 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000442
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000443 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000444 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
445
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000446 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000447
448 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
449 v, 4 * sizeof(GLfloat) * count);
450 }
451 else if (targetUniform->type == GL_BOOL_VEC4)
452 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000453 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000454
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000455 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000456 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
457
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000458 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000459 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000460
461 for (int i = 0; i < count * 4; ++i)
462 {
463 if (v[i] == 0.0f)
464 {
465 boolParams[i] = GL_FALSE;
466 }
467 else
468 {
469 boolParams[i] = GL_TRUE;
470 }
471 }
472 }
473 else
474 {
475 return false;
476 }
477
478 return true;
479}
480
481template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
482void transposeMatrix(T *target, const GLfloat *value)
483{
484 int copyWidth = std::min(targetWidth, srcWidth);
485 int copyHeight = std::min(targetHeight, srcHeight);
486
487 for (int x = 0; x < copyWidth; x++)
488 {
489 for (int y = 0; y < copyHeight; y++)
490 {
491 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
492 }
493 }
494 // clear unfilled right side
495 for (int y = 0; y < copyHeight; y++)
496 {
497 for (int x = srcWidth; x < targetWidth; x++)
498 {
499 target[y * targetWidth + x] = (T)0;
500 }
501 }
502 // clear unfilled bottom.
503 for (int y = srcHeight; y < targetHeight; y++)
504 {
505 for (int x = 0; x < targetWidth; x++)
506 {
507 target[y * targetWidth + x] = (T)0;
508 }
509 }
510}
511
512bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
513{
514 if (location < 0 || location >= (int)mUniformIndex.size())
515 {
516 return false;
517 }
518
519 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
520 targetUniform->dirty = true;
521
522 if (targetUniform->type != GL_FLOAT_MAT2)
523 {
524 return false;
525 }
526
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000527 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000528
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000529 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000530 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
531
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000532 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000533
534 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
535 for (int i = 0; i < count; i++)
536 {
537 transposeMatrix<GLfloat,4,2,2,2>(target, value);
538 target += 8;
539 value += 4;
540 }
541
542 return true;
543}
544
545bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
546{
547 if (location < 0 || location >= (int)mUniformIndex.size())
548 {
549 return false;
550 }
551
552 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
553 targetUniform->dirty = true;
554
555 if (targetUniform->type != GL_FLOAT_MAT3)
556 {
557 return false;
558 }
559
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000560 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000561
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000562 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000563 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
564
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000565 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000566
567 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
568 for (int i = 0; i < count; i++)
569 {
570 transposeMatrix<GLfloat,4,3,3,3>(target, value);
571 target += 12;
572 value += 9;
573 }
574
575 return true;
576}
577
578
579bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
580{
581 if (location < 0 || location >= (int)mUniformIndex.size())
582 {
583 return false;
584 }
585
586 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
587 targetUniform->dirty = true;
588
589 if (targetUniform->type != GL_FLOAT_MAT4)
590 {
591 return false;
592 }
593
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000594 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000595
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000596 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000597 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
598
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000599 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000600
601 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
602 for (int i = 0; i < count; i++)
603 {
604 transposeMatrix<GLfloat,4,4,4,4>(target, value);
605 target += 16;
606 value += 16;
607 }
608
609 return true;
610}
611
612bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
613{
614 if (location < 0 || location >= (int)mUniformIndex.size())
615 {
616 return false;
617 }
618
619 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
620 targetUniform->dirty = true;
621
622 if (targetUniform->type == GL_INT ||
623 targetUniform->type == GL_SAMPLER_2D ||
624 targetUniform->type == GL_SAMPLER_CUBE)
625 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000626 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000627
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000628 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000629 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
630
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000631 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000632
633 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
634 v, sizeof(GLint) * count);
635 }
636 else if (targetUniform->type == GL_BOOL)
637 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000638 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000639
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000640 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000641 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
642
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000643 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000644 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000645
646 for (int i = 0; i < count; ++i)
647 {
648 if (v[i] == 0)
649 {
650 boolParams[i] = GL_FALSE;
651 }
652 else
653 {
654 boolParams[i] = GL_TRUE;
655 }
656 }
657 }
658 else
659 {
660 return false;
661 }
662
663 return true;
664}
665
666bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
667{
668 if (location < 0 || location >= (int)mUniformIndex.size())
669 {
670 return false;
671 }
672
673 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
674 targetUniform->dirty = true;
675
676 if (targetUniform->type == GL_INT_VEC2)
677 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000678 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000679
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000680 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000681 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
682
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000683 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000684
685 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
686 v, 2 * sizeof(GLint) * count);
687 }
688 else if (targetUniform->type == GL_BOOL_VEC2)
689 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000690 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000691
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000692 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000693 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
694
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000695 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000696 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 2;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000697
698 for (int i = 0; i < count * 2; ++i)
699 {
700 if (v[i] == 0)
701 {
702 boolParams[i] = GL_FALSE;
703 }
704 else
705 {
706 boolParams[i] = GL_TRUE;
707 }
708 }
709 }
710 else
711 {
712 return false;
713 }
714
715 return true;
716}
717
718bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
719{
720 if (location < 0 || location >= (int)mUniformIndex.size())
721 {
722 return false;
723 }
724
725 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
726 targetUniform->dirty = true;
727
728 if (targetUniform->type == GL_INT_VEC3)
729 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000730 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000731
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000732 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000733 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
734
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000735 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000736
737 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
738 v, 3 * sizeof(GLint) * count);
739 }
740 else if (targetUniform->type == GL_BOOL_VEC3)
741 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000742 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000743
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000744 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000745 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
746
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000747 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000748 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 3;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000749
750 for (int i = 0; i < count * 3; ++i)
751 {
752 if (v[i] == 0)
753 {
754 boolParams[i] = GL_FALSE;
755 }
756 else
757 {
758 boolParams[i] = GL_TRUE;
759 }
760 }
761 }
762 else
763 {
764 return false;
765 }
766
767 return true;
768}
769
770bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
771{
772 if (location < 0 || location >= (int)mUniformIndex.size())
773 {
774 return false;
775 }
776
777 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
778 targetUniform->dirty = true;
779
780 if (targetUniform->type == GL_INT_VEC4)
781 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000782 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000783
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000784 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000785 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
786
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000787 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000788
789 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
790 v, 4 * sizeof(GLint) * count);
791 }
792 else if (targetUniform->type == GL_BOOL_VEC4)
793 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000794 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000795
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000796 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000797 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
798
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000799 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000800 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000801
802 for (int i = 0; i < count * 4; ++i)
803 {
804 if (v[i] == 0)
805 {
806 boolParams[i] = GL_FALSE;
807 }
808 else
809 {
810 boolParams[i] = GL_TRUE;
811 }
812 }
813 }
814 else
815 {
816 return false;
817 }
818
819 return true;
820}
821
822bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
823{
824 if (location < 0 || location >= (int)mUniformIndex.size())
825 {
826 return false;
827 }
828
829 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
830
831 // sized queries -- ensure the provided buffer is large enough
832 if (bufSize)
833 {
834 int requiredBytes = UniformExternalSize(targetUniform->type);
835 if (*bufSize < requiredBytes)
836 {
837 return false;
838 }
839 }
840
841 switch (targetUniform->type)
842 {
843 case GL_FLOAT_MAT2:
844 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
845 break;
846 case GL_FLOAT_MAT3:
847 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
848 break;
849 case GL_FLOAT_MAT4:
850 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
851 break;
852 default:
853 {
854 unsigned int count = UniformExternalComponentCount(targetUniform->type);
855 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
856
857 switch (UniformComponentType(targetUniform->type))
858 {
859 case GL_BOOL:
860 {
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000861 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000862
863 for (unsigned int i = 0; i < count; ++i)
864 {
865 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
866 }
867 }
868 break;
869 case GL_FLOAT:
870 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
871 count * sizeof(GLfloat));
872 break;
873 case GL_INT:
874 {
875 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
876
877 for (unsigned int i = 0; i < count; ++i)
878 {
879 params[i] = (float)intParams[i];
880 }
881 }
882 break;
883 default: UNREACHABLE();
884 }
885 }
886 }
887
888 return true;
889}
890
891bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
892{
893 if (location < 0 || location >= (int)mUniformIndex.size())
894 {
895 return false;
896 }
897
898 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
899
900 // sized queries -- ensure the provided buffer is large enough
901 if (bufSize)
902 {
903 int requiredBytes = UniformExternalSize(targetUniform->type);
904 if (*bufSize < requiredBytes)
905 {
906 return false;
907 }
908 }
909
910 switch (targetUniform->type)
911 {
912 case GL_FLOAT_MAT2:
913 {
914 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
915 }
916 break;
917 case GL_FLOAT_MAT3:
918 {
919 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
920 }
921 break;
922 case GL_FLOAT_MAT4:
923 {
924 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
925 }
926 break;
927 default:
928 {
929 unsigned int count = UniformExternalComponentCount(targetUniform->type);
930 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
931
932 switch (UniformComponentType(targetUniform->type))
933 {
934 case GL_BOOL:
935 {
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000936 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000937
938 for (unsigned int i = 0; i < count; ++i)
939 {
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000940 params[i] = boolParams[i];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000941 }
942 }
943 break;
944 case GL_FLOAT:
945 {
946 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
947
948 for (unsigned int i = 0; i < count; ++i)
949 {
950 params[i] = (GLint)floatParams[i];
951 }
952 }
953 break;
954 case GL_INT:
955 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
956 count * sizeof(GLint));
957 break;
958 default: UNREACHABLE();
959 }
960 }
961 }
962
963 return true;
964}
965
966void ProgramBinary::dirtyAllUniforms()
967{
968 unsigned int numUniforms = mUniforms.size();
969 for (unsigned int index = 0; index < numUniforms; index++)
970 {
971 mUniforms[index]->dirty = true;
972 }
973}
974
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000975// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000976void ProgramBinary::applyUniforms()
977{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000978 // Retrieve sampler uniform values
979 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
980 {
981 Uniform *targetUniform = *ub;
982
983 if (targetUniform->dirty)
984 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000985 if (targetUniform->type == GL_SAMPLER_2D ||
986 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000987 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000988 int count = targetUniform->elementCount();
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000989 GLint *v = (GLint*)targetUniform->data;
990
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000991 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000992 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000993 unsigned int firstIndex = targetUniform->psRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000994
995 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000996 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000997 unsigned int samplerIndex = firstIndex + i;
998
999 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001000 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001001 ASSERT(mSamplersPS[samplerIndex].active);
1002 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001003 }
1004 }
1005 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001006
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001007 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001008 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001009 unsigned int firstIndex = targetUniform->vsRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001010
1011 for (int i = 0; i < count; i++)
1012 {
1013 unsigned int samplerIndex = firstIndex + i;
1014
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001015 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001016 {
1017 ASSERT(mSamplersVS[samplerIndex].active);
1018 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
1019 }
1020 }
1021 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001022 }
1023 }
1024 }
1025
shannon.woods@transgaming.com358e88d2013-01-25 21:53:11 +00001026 mRenderer->applyUniforms(this, &mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001027}
1028
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001029// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1030// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001031int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001032{
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001033 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001034
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001035 fragmentShader->resetVaryingsRegisterAssignment();
1036
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001037 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1038 {
1039 int n = VariableRowCount(varying->type) * varying->size;
1040 int m = VariableColumnCount(varying->type);
1041 bool success = false;
1042
1043 if (m == 2 || m == 3 || m == 4)
1044 {
1045 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1046 {
1047 bool available = true;
1048
1049 for (int y = 0; y < n && available; y++)
1050 {
1051 for (int x = 0; x < m && available; x++)
1052 {
1053 if (packing[r + y][x])
1054 {
1055 available = false;
1056 }
1057 }
1058 }
1059
1060 if (available)
1061 {
1062 varying->reg = r;
1063 varying->col = 0;
1064
1065 for (int y = 0; y < n; y++)
1066 {
1067 for (int x = 0; x < m; x++)
1068 {
1069 packing[r + y][x] = &*varying;
1070 }
1071 }
1072
1073 success = true;
1074 }
1075 }
1076
1077 if (!success && m == 2)
1078 {
1079 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1080 {
1081 bool available = true;
1082
1083 for (int y = 0; y < n && available; y++)
1084 {
1085 for (int x = 2; x < 4 && available; x++)
1086 {
1087 if (packing[r + y][x])
1088 {
1089 available = false;
1090 }
1091 }
1092 }
1093
1094 if (available)
1095 {
1096 varying->reg = r;
1097 varying->col = 2;
1098
1099 for (int y = 0; y < n; y++)
1100 {
1101 for (int x = 2; x < 4; x++)
1102 {
1103 packing[r + y][x] = &*varying;
1104 }
1105 }
1106
1107 success = true;
1108 }
1109 }
1110 }
1111 }
1112 else if (m == 1)
1113 {
1114 int space[4] = {0};
1115
1116 for (int y = 0; y < maxVaryingVectors; y++)
1117 {
1118 for (int x = 0; x < 4; x++)
1119 {
1120 space[x] += packing[y][x] ? 0 : 1;
1121 }
1122 }
1123
1124 int column = 0;
1125
1126 for (int x = 0; x < 4; x++)
1127 {
1128 if (space[x] >= n && space[x] < space[column])
1129 {
1130 column = x;
1131 }
1132 }
1133
1134 if (space[column] >= n)
1135 {
1136 for (int r = 0; r < maxVaryingVectors; r++)
1137 {
1138 if (!packing[r][column])
1139 {
1140 varying->reg = r;
1141
1142 for (int y = r; y < r + n; y++)
1143 {
1144 packing[y][column] = &*varying;
1145 }
1146
1147 break;
1148 }
1149 }
1150
1151 varying->col = column;
1152
1153 success = true;
1154 }
1155 }
1156 else UNREACHABLE();
1157
1158 if (!success)
1159 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001160 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001161
1162 return -1;
1163 }
1164 }
1165
1166 // Return the number of used registers
1167 int registers = 0;
1168
1169 for (int r = 0; r < maxVaryingVectors; r++)
1170 {
1171 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1172 {
1173 registers++;
1174 }
1175 }
1176
1177 return registers;
1178}
1179
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001180bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
1181 std::string& pixelHLSL, std::string& vertexHLSL,
1182 FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001183{
1184 if (pixelHLSL.empty() || vertexHLSL.empty())
1185 {
1186 return false;
1187 }
1188
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001189 // Write the HLSL input/output declarations
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001190 const int shaderModel = mRenderer->getMajorShaderModel();
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001191 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001192
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001193 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
1194
1195 if (registersNeeded > maxVaryingVectors)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001196 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001197 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001198
1199 return false;
1200 }
1201
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001202 vertexShader->resetVaryingsRegisterAssignment();
1203
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001204 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1205 {
1206 bool matched = false;
1207
1208 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1209 {
1210 if (output->name == input->name)
1211 {
1212 if (output->type != input->type || output->size != input->size)
1213 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001214 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 +00001215
1216 return false;
1217 }
1218
1219 output->reg = input->reg;
1220 output->col = input->col;
1221
1222 matched = true;
1223 break;
1224 }
1225 }
1226
1227 if (!matched)
1228 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001229 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001230
1231 return false;
1232 }
1233 }
1234
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001235 mUsesPointSize = vertexShader->mUsesPointSize;
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001236 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001237 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001238 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
1239
1240 // special varyings that use reserved registers
1241 int reservedRegisterIndex = registers;
1242 std::string fragCoordSemantic;
1243 std::string pointCoordSemantic;
1244
1245 if (fragmentShader->mUsesFragCoord)
1246 {
1247 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1248 }
1249
1250 if (fragmentShader->mUsesPointCoord)
1251 {
1252 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
1253 // In DX11 we compute this in the GS.
1254 if (shaderModel == 3)
1255 {
1256 pointCoordSemantic = "TEXCOORD0";
1257 }
1258 else if (shaderModel >= 4)
1259 {
1260 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1261 }
1262 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001263
1264 vertexHLSL += "struct VS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001265 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001266
1267 int semanticIndex = 0;
1268 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1269 {
1270 switch (attribute->type)
1271 {
1272 case GL_FLOAT: vertexHLSL += " float "; break;
1273 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1274 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1275 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1276 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1277 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1278 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1279 default: UNREACHABLE();
1280 }
1281
1282 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1283
1284 semanticIndex += VariableRowCount(attribute->type);
1285 }
1286
1287 vertexHLSL += "};\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001288 "\n"
1289 "struct VS_OUTPUT\n"
1290 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001291
1292 for (int r = 0; r < registers; r++)
1293 {
1294 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1295
1296 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1297 }
1298
1299 if (fragmentShader->mUsesFragCoord)
1300 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001301 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001302 }
1303
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001304 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001305 {
1306 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1307 }
1308
daniel@transgaming.com9c4a6252013-01-11 04:07:18 +00001309 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n"
1310 "};\n"
1311 "\n"
1312 "VS_OUTPUT main(VS_INPUT input)\n"
1313 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001314
1315 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1316 {
1317 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1318
1319 if (VariableRowCount(attribute->type) > 1) // Matrix
1320 {
1321 vertexHLSL += "transpose";
1322 }
1323
1324 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1325 }
1326
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001327 if (shaderModel >= 4)
1328 {
1329 vertexHLSL += "\n"
1330 " gl_main();\n"
1331 "\n"
1332 " VS_OUTPUT output;\n"
1333 " output.gl_Position.x = gl_Position.x;\n"
1334 " output.gl_Position.y = -gl_Position.y;\n"
1335 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1336 " output.gl_Position.w = gl_Position.w;\n";
1337 }
1338 else
1339 {
1340 vertexHLSL += "\n"
1341 " gl_main();\n"
1342 "\n"
1343 " VS_OUTPUT output;\n"
1344 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
1345 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
1346 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1347 " output.gl_Position.w = gl_Position.w;\n";
1348 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001349
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001350 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001351 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001352 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001353 }
1354
1355 if (fragmentShader->mUsesFragCoord)
1356 {
1357 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1358 }
1359
1360 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1361 {
1362 if (varying->reg >= 0)
1363 {
1364 for (int i = 0; i < varying->size; i++)
1365 {
1366 int rows = VariableRowCount(varying->type);
1367
1368 for (int j = 0; j < rows; j++)
1369 {
1370 int r = varying->reg + i * rows + j;
1371 vertexHLSL += " output.v" + str(r);
1372
1373 bool sharedRegister = false; // Register used by multiple varyings
1374
1375 for (int x = 0; x < 4; x++)
1376 {
1377 if (packing[r][x] && packing[r][x] != packing[r][0])
1378 {
1379 sharedRegister = true;
1380 break;
1381 }
1382 }
1383
1384 if(sharedRegister)
1385 {
1386 vertexHLSL += ".";
1387
1388 for (int x = 0; x < 4; x++)
1389 {
1390 if (packing[r][x] == &*varying)
1391 {
1392 switch(x)
1393 {
1394 case 0: vertexHLSL += "x"; break;
1395 case 1: vertexHLSL += "y"; break;
1396 case 2: vertexHLSL += "z"; break;
1397 case 3: vertexHLSL += "w"; break;
1398 }
1399 }
1400 }
1401 }
1402
1403 vertexHLSL += " = " + varying->name;
1404
1405 if (varying->array)
1406 {
1407 vertexHLSL += "[" + str(i) + "]";
1408 }
1409
1410 if (rows > 1)
1411 {
1412 vertexHLSL += "[" + str(j) + "]";
1413 }
1414
1415 vertexHLSL += ";\n";
1416 }
1417 }
1418 }
1419 }
1420
1421 vertexHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001422 " return output;\n"
1423 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001424
1425 pixelHLSL += "struct PS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001426 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001427
1428 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1429 {
1430 if (varying->reg >= 0)
1431 {
1432 for (int i = 0; i < varying->size; i++)
1433 {
1434 int rows = VariableRowCount(varying->type);
1435 for (int j = 0; j < rows; j++)
1436 {
1437 std::string n = str(varying->reg + i * rows + j);
daniel@transgaming.com00c0d152013-01-11 04:07:23 +00001438 pixelHLSL += " float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001439 }
1440 }
1441 }
1442 else UNREACHABLE();
1443 }
1444
1445 if (fragmentShader->mUsesFragCoord)
1446 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001447 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001448
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001449 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001450 {
1451 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1452 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001453 else if (shaderModel >= 3)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001454 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001455 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1456 }
1457 }
1458
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001459 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001460 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001461 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001462 }
1463
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001464 pixelHLSL += "};\n"
1465 "\n"
1466 "struct PS_OUTPUT\n"
1467 "{\n"
1468 " float4 gl_Color[1] : " + targetSemantic + ";\n"
1469 "};\n"
1470 "\n";
1471
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001472 if (fragmentShader->mUsesFrontFacing)
1473 {
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001474 if (shaderModel >= 4)
1475 {
1476 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
1477 "{\n";
1478 }
1479 else
1480 {
1481 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
1482 "{\n";
1483 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001484 }
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001485 else
1486 {
1487 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
1488 "{\n";
1489 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001490
1491 if (fragmentShader->mUsesFragCoord)
1492 {
1493 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1494
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001495 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001496 {
1497 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1498 " gl_FragCoord.y = input.dx_VPos.y;\n";
1499 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001500 else if (shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001501 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001502 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001503 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001504 }
1505 else
1506 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001507 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
1508 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
1509 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001510 }
1511
daniel@transgaming.com12985182012-12-20 20:56:31 +00001512 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001513 " gl_FragCoord.w = rhw;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001514 }
1515
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001516 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001517 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001518 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1519 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001520 }
1521
1522 if (fragmentShader->mUsesFrontFacing)
1523 {
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001524 if (shaderModel <= 3)
1525 {
1526 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1527 }
1528 else
1529 {
1530 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1531 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001532 }
1533
1534 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1535 {
1536 if (varying->reg >= 0)
1537 {
1538 for (int i = 0; i < varying->size; i++)
1539 {
1540 int rows = VariableRowCount(varying->type);
1541 for (int j = 0; j < rows; j++)
1542 {
1543 std::string n = str(varying->reg + i * rows + j);
1544 pixelHLSL += " " + varying->name;
1545
1546 if (varying->array)
1547 {
1548 pixelHLSL += "[" + str(i) + "]";
1549 }
1550
1551 if (rows > 1)
1552 {
1553 pixelHLSL += "[" + str(j) + "]";
1554 }
1555
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001556 switch (VariableColumnCount(varying->type))
1557 {
1558 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1559 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1560 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1561 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1562 default: UNREACHABLE();
1563 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001564 }
1565 }
1566 }
1567 else UNREACHABLE();
1568 }
1569
1570 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001571 " gl_main();\n"
1572 "\n"
1573 " PS_OUTPUT output;\n"
1574 " output.gl_Color[0] = gl_Color[0];\n"
1575 "\n"
1576 " return output;\n"
1577 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001578
1579 return true;
1580}
1581
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001582bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1583{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001584 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001585
1586 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001587 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001588 if (format != GL_PROGRAM_BINARY_ANGLE)
1589 {
1590 infoLog.append("Invalid program binary format.");
1591 return false;
1592 }
1593
1594 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001595 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001596 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001597 {
1598 infoLog.append("Invalid program binary version.");
1599 return false;
1600 }
1601
1602 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1603 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001604 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001605 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001606 stream.read(&name);
1607 mLinkedAttribute[i].name = name;
1608 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001609 }
1610
1611 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1612 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001613 stream.read(&mSamplersPS[i].active);
1614 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001615
1616 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001617 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001618 mSamplersPS[i].textureType = (TextureType) textureType;
1619 }
1620
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001621 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001622 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001623 stream.read(&mSamplersVS[i].active);
1624 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001625
1626 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001627 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001628 mSamplersVS[i].textureType = (TextureType) textureType;
1629 }
1630
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001631 stream.read(&mUsedVertexSamplerRange);
1632 stream.read(&mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001633 stream.read(&mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001634
1635 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001636 stream.read(&size);
1637 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001638 {
1639 infoLog.append("Invalid program binary.");
1640 return false;
1641 }
1642
1643 mUniforms.resize(size);
1644 for (unsigned int i = 0; i < size; ++i)
1645 {
1646 GLenum type;
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001647 std::string name;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001648 unsigned int arraySize;
1649
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001650 stream.read(&type);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001651 stream.read(&name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001652 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001653
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001654 mUniforms[i] = new Uniform(type, name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001655
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001656 stream.read(&mUniforms[i]->psRegisterIndex);
1657 stream.read(&mUniforms[i]->vsRegisterIndex);
1658 stream.read(&mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001659 }
1660
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001661 stream.read(&size);
1662 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001663 {
1664 infoLog.append("Invalid program binary.");
1665 return false;
1666 }
1667
1668 mUniformIndex.resize(size);
1669 for (unsigned int i = 0; i < size; ++i)
1670 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001671 stream.read(&mUniformIndex[i].name);
1672 stream.read(&mUniformIndex[i].element);
1673 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001674 }
1675
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001676 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001677 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001678
1679 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001680 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001681
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001682 unsigned int geometryShaderSize;
1683 stream.read(&geometryShaderSize);
1684
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001685 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001686
daniel@transgaming.com36038542012-11-28 20:59:26 +00001687 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001688 ptr += sizeof(GUID);
1689
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001690 GUID identifier = mRenderer->getAdapterIdentifier();
1691 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001692 {
1693 infoLog.append("Invalid program binary.");
1694 return false;
1695 }
1696
1697 const char *pixelShaderFunction = ptr;
1698 ptr += pixelShaderSize;
1699
1700 const char *vertexShaderFunction = ptr;
1701 ptr += vertexShaderSize;
1702
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001703 const char *geometryShaderFunction = geometryShaderSize > 0 ? ptr : NULL;
1704 ptr += geometryShaderSize;
1705
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001706 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001707 pixelShaderSize, rx::SHADER_PIXEL);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001708 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001709 {
1710 infoLog.append("Could not create pixel shader.");
1711 return false;
1712 }
1713
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001714 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001715 vertexShaderSize, rx::SHADER_VERTEX);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001716 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001717 {
1718 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001719 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001720 mPixelExecutable = NULL;
1721 return false;
1722 }
1723
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001724 if (geometryShaderFunction != NULL && geometryShaderSize > 0)
1725 {
1726 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction),
1727 geometryShaderSize, rx::SHADER_GEOMETRY);
1728 if (!mGeometryExecutable)
1729 {
1730 infoLog.append("Could not create geometry shader.");
1731 delete mPixelExecutable;
1732 mPixelExecutable = NULL;
1733 delete mVertexExecutable;
1734 mVertexExecutable = NULL;
1735 return false;
1736 }
1737 }
1738 else
1739 {
1740 mGeometryExecutable = NULL;
1741 }
1742
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001743 return true;
1744}
1745
1746bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1747{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001748 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001749
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001750 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001751 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001752
1753 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1754 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001755 stream.write(mLinkedAttribute[i].type);
1756 stream.write(mLinkedAttribute[i].name);
1757 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001758 }
1759
1760 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1761 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001762 stream.write(mSamplersPS[i].active);
1763 stream.write(mSamplersPS[i].logicalTextureUnit);
1764 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001765 }
1766
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001767 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001768 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001769 stream.write(mSamplersVS[i].active);
1770 stream.write(mSamplersVS[i].logicalTextureUnit);
1771 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001772 }
1773
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001774 stream.write(mUsedVertexSamplerRange);
1775 stream.write(mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001776 stream.write(mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001777
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001778 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001779 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1780 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001781 stream.write(mUniforms[i]->type);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001782 stream.write(mUniforms[i]->name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001783 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001784
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001785 stream.write(mUniforms[i]->psRegisterIndex);
1786 stream.write(mUniforms[i]->vsRegisterIndex);
1787 stream.write(mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001788 }
1789
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001790 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001791 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1792 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001793 stream.write(mUniformIndex[i].name);
1794 stream.write(mUniformIndex[i].element);
1795 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001796 }
1797
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001798 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001799 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001800
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001801 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001802 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001803
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001804 UINT geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
1805 stream.write(geometryShaderSize);
1806
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001807 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001808
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001809 GLsizei streamLength = stream.length();
1810 const void *streamData = stream.data();
1811
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001812 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize + geometryShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001813 if (totalLength > bufSize)
1814 {
1815 if (length)
1816 {
1817 *length = 0;
1818 }
1819
1820 return false;
1821 }
1822
1823 if (binary)
1824 {
1825 char *ptr = (char*) binary;
1826
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001827 memcpy(ptr, streamData, streamLength);
1828 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001829
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001830 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001831 ptr += sizeof(GUID);
1832
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001833 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001834 ptr += pixelShaderSize;
1835
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001836 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001837 ptr += vertexShaderSize;
1838
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001839 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
1840 {
1841 memcpy(ptr, mGeometryExecutable->getFunction(), geometryShaderSize);
1842 ptr += geometryShaderSize;
1843 }
1844
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001845 ASSERT(ptr - totalLength == binary);
1846 }
1847
1848 if (length)
1849 {
1850 *length = totalLength;
1851 }
1852
1853 return true;
1854}
1855
1856GLint ProgramBinary::getLength()
1857{
1858 GLint length;
1859 if (save(NULL, INT_MAX, &length))
1860 {
1861 return length;
1862 }
1863 else
1864 {
1865 return 0;
1866 }
1867}
1868
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001869bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001870{
1871 if (!fragmentShader || !fragmentShader->isCompiled())
1872 {
1873 return false;
1874 }
1875
1876 if (!vertexShader || !vertexShader->isCompiled())
1877 {
1878 return false;
1879 }
1880
1881 std::string pixelHLSL = fragmentShader->getHLSL();
1882 std::string vertexHLSL = vertexShader->getHLSL();
1883
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001884 // Map the varyings to the register file
1885 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
1886 int registers = packVaryings(infoLog, packing, fragmentShader);
1887
1888 if (registers < 0)
1889 {
1890 return false;
1891 }
1892
1893 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001894 {
1895 return false;
1896 }
1897
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001898 bool success = true;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001899 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX);
1900 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001901
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001902 if (usesGeometryShader())
1903 {
1904 std::string geometryHLSL = generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader);
1905 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY);
1906 }
1907
1908 if (!mVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001909 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001910 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001911 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001912
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001913 delete mVertexExecutable;
1914 mVertexExecutable = NULL;
1915 delete mPixelExecutable;
1916 mPixelExecutable = NULL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001917 delete mGeometryExecutable;
1918 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001919 }
1920
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001921 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1922 {
1923 success = false;
1924 }
1925
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001926 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001927 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001928 success = false;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001929 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001930
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001931 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001932}
1933
1934// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001935bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001936{
1937 unsigned int usedLocations = 0;
1938
1939 // Link attributes that have a binding location
1940 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1941 {
1942 int location = attributeBindings.getAttributeBinding(attribute->name);
1943
1944 if (location != -1) // Set by glBindAttribLocation
1945 {
1946 if (!mLinkedAttribute[location].name.empty())
1947 {
1948 // Multiple active attributes bound to the same location; not an error
1949 }
1950
1951 mLinkedAttribute[location] = *attribute;
1952
1953 int rows = VariableRowCount(attribute->type);
1954
1955 if (rows + location > MAX_VERTEX_ATTRIBS)
1956 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001957 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 +00001958
1959 return false;
1960 }
1961
1962 for (int i = 0; i < rows; i++)
1963 {
1964 usedLocations |= 1 << (location + i);
1965 }
1966 }
1967 }
1968
1969 // Link attributes that don't have a binding location
1970 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1971 {
1972 int location = attributeBindings.getAttributeBinding(attribute->name);
1973
1974 if (location == -1) // Not set by glBindAttribLocation
1975 {
1976 int rows = VariableRowCount(attribute->type);
1977 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1978
1979 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1980 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001981 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001982
1983 return false; // Fail to link
1984 }
1985
1986 mLinkedAttribute[availableIndex] = *attribute;
1987 }
1988 }
1989
1990 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1991 {
1992 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1993 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1994
1995 for (int r = 0; r < rows; r++)
1996 {
1997 mSemanticIndex[attributeIndex++] = index++;
1998 }
1999 }
2000
2001 return true;
2002}
2003
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002004bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002005{
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002006 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002007 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002008 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002009 {
2010 return false;
2011 }
2012 }
2013
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002014 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++)
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002015 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002016 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002017 {
2018 return false;
2019 }
2020 }
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002021
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022 return true;
2023}
2024
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002025bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002026{
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002027 if (constant.type == GL_SAMPLER_2D ||
2028 constant.type == GL_SAMPLER_CUBE)
2029 {
2030 unsigned int samplerIndex = constant.registerIndex;
2031
2032 do
2033 {
2034 if (shader == GL_VERTEX_SHADER)
2035 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002036 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002037 {
2038 mSamplersVS[samplerIndex].active = true;
2039 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2040 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2041 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2042 }
2043 else
2044 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002045 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002046 return false;
2047 }
2048 }
2049 else if (shader == GL_FRAGMENT_SHADER)
2050 {
2051 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2052 {
2053 mSamplersPS[samplerIndex].active = true;
2054 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2055 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2056 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2057 }
2058 else
2059 {
2060 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
2061 return false;
2062 }
2063 }
2064 else UNREACHABLE();
2065
2066 samplerIndex++;
2067 }
2068 while (samplerIndex < constant.registerIndex + constant.arraySize);
2069 }
2070
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002071 Uniform *uniform = NULL;
2072 GLint location = getUniformLocation(constant.name);
2073
2074 if (location >= 0) // Previously defined, types must match
2075 {
2076 uniform = mUniforms[mUniformIndex[location].index];
2077
2078 if (uniform->type != constant.type)
2079 {
2080 return false;
2081 }
2082 }
2083 else
2084 {
2085 uniform = new Uniform(constant.type, constant.name, constant.arraySize);
2086 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002087
2088 if (!uniform)
2089 {
2090 return false;
2091 }
2092
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002093 if (shader == GL_FRAGMENT_SHADER)
2094 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00002095 uniform->psRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002096 }
2097 else if (shader == GL_VERTEX_SHADER)
2098 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00002099 uniform->vsRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002100 }
2101 else UNREACHABLE();
2102
2103 if (location >= 0)
2104 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002105 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002106 }
2107
2108 mUniforms.push_back(uniform);
2109 unsigned int uniformIndex = mUniforms.size() - 1;
2110
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002111 for (unsigned int i = 0; i < uniform->elementCount(); i++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002112 {
2113 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
2114 }
2115
2116 return true;
2117}
2118
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002119std::string ProgramBinary::generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2120{
2121 // for now we only handle point sprite emulation
2122 ASSERT(usesPointSpriteEmulation());
2123 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
2124}
2125
2126std::string ProgramBinary::generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2127{
2128 ASSERT(registers >= 0);
2129 ASSERT(vertexShader->mUsesPointSize);
2130 ASSERT(mRenderer->getMajorShaderModel() >= 4);
2131
2132 std::string geomHLSL;
2133
2134 std::string varyingSemantic = "TEXCOORD";
2135
2136 std::string fragCoordSemantic;
2137 std::string pointCoordSemantic;
2138
2139 int reservedRegisterIndex = registers;
2140
2141 if (fragmentShader->mUsesFragCoord)
2142 {
2143 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2144 }
2145
2146 if (fragmentShader->mUsesPointCoord)
2147 {
2148 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2149 }
2150
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00002151 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
2152 "\n"
2153 "struct GS_INPUT\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002154 "{\n";
2155
2156 for (int r = 0; r < registers; r++)
2157 {
2158 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
2159
2160 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
2161 }
2162
2163 if (fragmentShader->mUsesFragCoord)
2164 {
2165 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2166 }
2167
2168 geomHLSL += " float gl_PointSize : PSIZE;\n"
2169 " float4 gl_Position : SV_Position;\n"
2170 "};\n"
2171 "\n"
2172 "struct GS_OUTPUT\n"
2173 "{\n";
2174
2175 for (int r = 0; r < registers; r++)
2176 {
2177 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
2178
2179 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
2180 }
2181
2182 if (fragmentShader->mUsesFragCoord)
2183 {
2184 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2185 }
2186
2187 if (fragmentShader->mUsesPointCoord)
2188 {
2189 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
2190 }
2191
2192 geomHLSL += " float4 gl_Position : SV_Position;\n"
2193 "};\n"
2194 "\n"
2195 "static float2 pointSpriteCorners[] = \n"
2196 "{\n"
2197 " float2( 0.5f, -0.5f),\n"
2198 " float2( 0.5f, 0.5f),\n"
2199 " float2(-0.5f, -0.5f),\n"
2200 " float2(-0.5f, 0.5f)\n"
2201 "};\n"
2202 "\n"
2203 "static float2 pointSpriteTexcoords[] = \n"
2204 "{\n"
2205 " float2(1.0f, 1.0f),\n"
2206 " float2(1.0f, 0.0f),\n"
2207 " float2(0.0f, 1.0f),\n"
2208 " float2(0.0f, 0.0f)\n"
2209 "};\n"
2210 "\n"
2211 "static float minPointSize = " + str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
2212 "static float maxPointSize = " + str(mRenderer->getMaxPointSize()) + ".0f;\n"
2213 "\n"
2214 "[maxvertexcount(4)]\n"
2215 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
2216 "{\n"
2217 " GS_OUTPUT output = (GS_OUTPUT)0;\n";
2218
2219 for (int r = 0; r < registers; r++)
2220 {
2221 geomHLSL += " output.v" + str(r) + " = input[0].v" + str(r) + ";\n";
2222 }
2223
2224 if (fragmentShader->mUsesFragCoord)
2225 {
2226 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
2227 }
2228
2229 geomHLSL += " \n"
2230 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
2231 " float4 gl_Position = input[0].gl_Position;\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00002232 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y);\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002233
2234 for (int corner = 0; corner < 4; corner++)
2235 {
2236 geomHLSL += " \n"
2237 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
2238
2239 if (fragmentShader->mUsesPointCoord)
2240 {
2241 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + str(corner) + "];\n";
2242 }
2243
2244 geomHLSL += " outStream.Append(output);\n";
2245 }
2246
2247 geomHLSL += " \n"
2248 " outStream.RestartStrip();\n"
2249 "}\n";
2250
2251 return geomHLSL;
2252}
2253
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002254// This method needs to match OutputHLSL::decorate
2255std::string ProgramBinary::decorateAttribute(const std::string &name)
2256{
2257 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2258 {
2259 return "_" + name;
2260 }
2261
2262 return name;
2263}
2264
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002265bool ProgramBinary::isValidated() const
2266{
2267 return mValidated;
2268}
2269
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002270void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2271{
2272 // Skip over inactive attributes
2273 unsigned int activeAttribute = 0;
2274 unsigned int attribute;
2275 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2276 {
2277 if (mLinkedAttribute[attribute].name.empty())
2278 {
2279 continue;
2280 }
2281
2282 if (activeAttribute == index)
2283 {
2284 break;
2285 }
2286
2287 activeAttribute++;
2288 }
2289
2290 if (bufsize > 0)
2291 {
2292 const char *string = mLinkedAttribute[attribute].name.c_str();
2293
2294 strncpy(name, string, bufsize);
2295 name[bufsize - 1] = '\0';
2296
2297 if (length)
2298 {
2299 *length = strlen(name);
2300 }
2301 }
2302
2303 *size = 1; // Always a single 'type' instance
2304
2305 *type = mLinkedAttribute[attribute].type;
2306}
2307
2308GLint ProgramBinary::getActiveAttributeCount()
2309{
2310 int count = 0;
2311
2312 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2313 {
2314 if (!mLinkedAttribute[attributeIndex].name.empty())
2315 {
2316 count++;
2317 }
2318 }
2319
2320 return count;
2321}
2322
2323GLint ProgramBinary::getActiveAttributeMaxLength()
2324{
2325 int maxLength = 0;
2326
2327 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2328 {
2329 if (!mLinkedAttribute[attributeIndex].name.empty())
2330 {
2331 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2332 }
2333 }
2334
2335 return maxLength;
2336}
2337
2338void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2339{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002340 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002341
2342 if (bufsize > 0)
2343 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002344 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002345
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002346 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002347 {
2348 string += "[0]";
2349 }
2350
2351 strncpy(name, string.c_str(), bufsize);
2352 name[bufsize - 1] = '\0';
2353
2354 if (length)
2355 {
2356 *length = strlen(name);
2357 }
2358 }
2359
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002360 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002361
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002362 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002363}
2364
2365GLint ProgramBinary::getActiveUniformCount()
2366{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002367 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002368}
2369
2370GLint ProgramBinary::getActiveUniformMaxLength()
2371{
2372 int maxLength = 0;
2373
2374 unsigned int numUniforms = mUniforms.size();
2375 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2376 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002377 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002378 {
2379 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2380 if (mUniforms[uniformIndex]->isArray())
2381 {
2382 length += 3; // Counting in "[0]".
2383 }
2384 maxLength = std::max(length, maxLength);
2385 }
2386 }
2387
2388 return maxLength;
2389}
2390
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002391void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002392{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002393 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002394 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002395 {
2396 mValidated = false;
2397 }
2398 else
2399 {
2400 mValidated = true;
2401 }
2402}
2403
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002404bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002405{
2406 // if any two active samplers in a program are of different types, but refer to the same
2407 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2408 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2409
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002410 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002411 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002412
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002413 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002414 {
2415 textureUnitType[i] = TEXTURE_UNKNOWN;
2416 }
2417
2418 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2419 {
2420 if (mSamplersPS[i].active)
2421 {
2422 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2423
2424 if (unit >= maxCombinedTextureImageUnits)
2425 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002426 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002427 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002428 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002429 }
2430
2431 return false;
2432 }
2433
2434 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2435 {
2436 if (mSamplersPS[i].textureType != textureUnitType[unit])
2437 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002438 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002439 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002440 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002441 }
2442
2443 return false;
2444 }
2445 }
2446 else
2447 {
2448 textureUnitType[unit] = mSamplersPS[i].textureType;
2449 }
2450 }
2451 }
2452
2453 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2454 {
2455 if (mSamplersVS[i].active)
2456 {
2457 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2458
2459 if (unit >= maxCombinedTextureImageUnits)
2460 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002461 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002462 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002463 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002464 }
2465
2466 return false;
2467 }
2468
2469 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2470 {
2471 if (mSamplersVS[i].textureType != textureUnitType[unit])
2472 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002473 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002474 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002475 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002476 }
2477
2478 return false;
2479 }
2480 }
2481 else
2482 {
2483 textureUnitType[unit] = mSamplersVS[i].textureType;
2484 }
2485 }
2486 }
2487
2488 return true;
2489}
2490
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002491ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2492{
2493}
2494
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002495}