blob: 10283d6bb8125a2ceb668d3d1b9ed4e5192af858 [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00003// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Program.cpp: Implements the gl::Program class. Implements GL program objects
9// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
10
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000011#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000012#include "libGLESv2/ProgramBinary.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000013#include "libGLESv2/renderer/ShaderExecutable.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000014
15#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000016#include "common/version.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000017#include "utilities.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000018
19#include "libGLESv2/main.h"
20#include "libGLESv2/Shader.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000021#include "libGLESv2/Program.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000022#include "libGLESv2/renderer/Renderer.h"
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000023#include "libGLESv2/renderer/VertexDataManager.h"
shannon.woods%transgaming.com@gtempaccount.com0b05b7a2013-04-13 03:34:58 +000024#include "libGLESv2/Context.h"
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000025
daniel@transgaming.com88853c52012-12-20 20:56:40 +000026#undef near
27#undef far
28
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000029namespace gl
30{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000031std::string str(int i)
32{
33 char buffer[20];
34 snprintf(buffer, sizeof(buffer), "%d", i);
35 return buffer;
36}
37
shannonwoods@chromium.org6d7b61c2013-05-30 00:06:38 +000038namespace
39{
40
41unsigned int parseAndStripArrayIndex(std::string* name)
42{
43 unsigned int subscript = GL_INVALID_INDEX;
44
45 // Strip any trailing array operator and retrieve the subscript
46 size_t open = name->find_last_of('[');
47 size_t close = name->find_last_of(']');
48 if (open != std::string::npos && close == name->length() - 1)
49 {
50 subscript = atoi(name->substr(open + 1).c_str());
51 name->erase(open);
52 }
53
54 return subscript;
55}
56
57}
58
daniel@transgaming.comdb019952012-12-20 21:13:32 +000059UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index)
60 : name(name), element(element), index(index)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000061{
62}
63
daniel@transgaming.come87ca002012-07-24 18:30:43 +000064unsigned int ProgramBinary::mCurrentSerial = 1;
65
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000066ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000067{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000068 mPixelExecutable = NULL;
69 mVertexExecutable = NULL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000070 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000071
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000072 mValidated = false;
73
74 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
75 {
76 mSemanticIndex[index] = -1;
77 }
78
79 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
80 {
81 mSamplersPS[index].active = false;
82 }
83
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000084 for (int index = 0; index < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000085 {
86 mSamplersVS[index].active = false;
87 }
88
89 mUsedVertexSamplerRange = 0;
90 mUsedPixelSamplerRange = 0;
shannon.woods@transgaming.com962d4be2013-01-25 21:55:18 +000091 mUsesPointSize = false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000092}
93
94ProgramBinary::~ProgramBinary()
95{
daniel@transgaming.com95892412012-11-28 20:59:09 +000096 delete mPixelExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000097 mPixelExecutable = NULL;
98
daniel@transgaming.com95892412012-11-28 20:59:09 +000099 delete mVertexExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000100 mVertexExecutable = NULL;
101
102 delete mGeometryExecutable;
103 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000104
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000105 while (!mUniforms.empty())
106 {
107 delete mUniforms.back();
108 mUniforms.pop_back();
109 }
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +0000110
111 while (!mUniformBlocks.empty())
112 {
113 delete mUniformBlocks.back();
114 mUniformBlocks.pop_back();
115 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000116}
117
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000118unsigned int ProgramBinary::getSerial() const
119{
120 return mSerial;
121}
122
123unsigned int ProgramBinary::issueSerial()
124{
125 return mCurrentSerial++;
126}
127
daniel@transgaming.com95892412012-11-28 20:59:09 +0000128rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000129{
130 return mPixelExecutable;
131}
132
daniel@transgaming.com95892412012-11-28 20:59:09 +0000133rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000134{
135 return mVertexExecutable;
136}
137
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000138rx::ShaderExecutable *ProgramBinary::getGeometryExecutable()
139{
140 return mGeometryExecutable;
141}
142
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000143GLuint ProgramBinary::getAttributeLocation(const char *name)
144{
145 if (name)
146 {
147 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
148 {
149 if (mLinkedAttribute[index].name == std::string(name))
150 {
151 return index;
152 }
153 }
154 }
155
156 return -1;
157}
158
159int ProgramBinary::getSemanticIndex(int attributeIndex)
160{
161 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
162
163 return mSemanticIndex[attributeIndex];
164}
165
166// Returns one more than the highest sampler index used.
167GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
168{
169 switch (type)
170 {
171 case SAMPLER_PIXEL:
172 return mUsedPixelSamplerRange;
173 case SAMPLER_VERTEX:
174 return mUsedVertexSamplerRange;
175 default:
176 UNREACHABLE();
177 return 0;
178 }
179}
180
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000181bool ProgramBinary::usesPointSize() const
182{
183 return mUsesPointSize;
184}
185
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000186bool ProgramBinary::usesPointSpriteEmulation() const
187{
188 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
189}
190
191bool ProgramBinary::usesGeometryShader() const
192{
193 return usesPointSpriteEmulation();
194}
195
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000196// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
197// index (0-15 for the pixel shader and 0-3 for the vertex shader).
198GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
199{
200 GLint logicalTextureUnit = -1;
201
202 switch (type)
203 {
204 case SAMPLER_PIXEL:
205 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
206
207 if (mSamplersPS[samplerIndex].active)
208 {
209 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
210 }
211 break;
212 case SAMPLER_VERTEX:
213 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
214
215 if (mSamplersVS[samplerIndex].active)
216 {
217 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
218 }
219 break;
220 default: UNREACHABLE();
221 }
222
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000223 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)mRenderer->getMaxCombinedTextureImageUnits())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000224 {
225 return logicalTextureUnit;
226 }
227
228 return -1;
229}
230
231// Returns the texture type for a given Direct3D 9 sampler type and
232// index (0-15 for the pixel shader and 0-3 for the vertex shader).
233TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
234{
235 switch (type)
236 {
237 case SAMPLER_PIXEL:
238 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
239 ASSERT(mSamplersPS[samplerIndex].active);
240 return mSamplersPS[samplerIndex].textureType;
241 case SAMPLER_VERTEX:
242 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
243 ASSERT(mSamplersVS[samplerIndex].active);
244 return mSamplersVS[samplerIndex].textureType;
245 default: UNREACHABLE();
246 }
247
248 return TEXTURE_2D;
249}
250
251GLint ProgramBinary::getUniformLocation(std::string name)
252{
shannonwoods@chromium.org6d7b61c2013-05-30 00:06:38 +0000253 unsigned int subscript = parseAndStripArrayIndex(&name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000254
255 unsigned int numUniforms = mUniformIndex.size();
256 for (unsigned int location = 0; location < numUniforms; location++)
257 {
shannonwoods@chromium.org0ee85f82013-05-30 00:05:47 +0000258 if (mUniformIndex[location].name == name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000259 {
shannonwoods@chromium.org0ee85f82013-05-30 00:05:47 +0000260 const int index = mUniformIndex[location].index;
261 const bool isArray = mUniforms[index]->isArray();
262
263 if ((isArray && mUniformIndex[location].element == subscript) ||
264 (subscript == GL_INVALID_INDEX))
265 {
266 return location;
267 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000268 }
269 }
270
271 return -1;
272}
273
shannonwoods@chromium.orgc2ed9912013-05-30 00:05:33 +0000274GLuint ProgramBinary::getUniformIndex(std::string name)
275{
shannonwoods@chromium.org6d7b61c2013-05-30 00:06:38 +0000276 unsigned int subscript = parseAndStripArrayIndex(&name);
shannonwoods@chromium.orgc2ed9912013-05-30 00:05:33 +0000277
278 // The app is not allowed to specify array indices other than 0 for arrays of basic types
279 if (subscript != 0 && subscript != GL_INVALID_INDEX)
280 {
281 return GL_INVALID_INDEX;
282 }
283
284 unsigned int numUniforms = mUniforms.size();
285 for (unsigned int index = 0; index < numUniforms; index++)
286 {
287 if (mUniforms[index]->name == name)
288 {
289 if (mUniforms[index]->isArray() || subscript == GL_INVALID_INDEX)
290 {
291 return index;
292 }
293 }
294 }
295
296 return GL_INVALID_INDEX;
297}
298
shannonwoods@chromium.org42766252013-05-30 00:07:12 +0000299GLuint ProgramBinary::getUniformBlockIndex(std::string name)
300{
301 unsigned int subscript = parseAndStripArrayIndex(&name);
302
303 unsigned int numUniformBlocks = mUniformBlocks.size();
304 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
305 {
306 const UniformBlock &uniformBlock = *mUniformBlocks[blockIndex];
307 if (uniformBlock.name == name)
308 {
309 const bool arrayElementZero = (subscript == GL_INVALID_INDEX && uniformBlock.elementIndex == 0);
310 if (subscript == uniformBlock.elementIndex || arrayElementZero)
311 {
312 return blockIndex;
313 }
314 }
315 }
316
317 return GL_INVALID_INDEX;
318}
319
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000320template <typename T>
321bool ProgramBinary::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000322{
323 if (location < 0 || location >= (int)mUniformIndex.size())
324 {
325 return false;
326 }
327
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000328 const int components = UniformComponentCount(targetUniformType);
329 const GLenum targetBoolType = UniformBoolVectorType(targetUniformType);
330
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000331 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
332 targetUniform->dirty = true;
333
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000334 int elementCount = targetUniform->elementCount();
335
336 if (elementCount == 1 && count > 1)
337 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
338
339 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
340
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000341 if (targetUniform->type == targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000342 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000343 T *target = (T*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000344
345 for (int i = 0; i < count; i++)
346 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000347 for (int c = 0; c < components; c++)
348 {
349 target[c] = v[c];
350 }
351 for (int c = components; c < 4; c++)
352 {
353 target[c] = 0;
354 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000355 target += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000356 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000357 }
358 }
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000359 else if (targetUniform->type == targetBoolType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000360 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000361 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000362
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000363 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000364 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000365 for (int c = 0; c < components; c++)
366 {
367 boolParams[c] = (v[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
368 }
369 for (int c = components; c < 4; c++)
370 {
371 boolParams[c] = GL_FALSE;
372 }
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000373 boolParams += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000374 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000375 }
376 }
377 else
378 {
379 return false;
380 }
381
382 return true;
383}
384
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000385bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
386{
387 return setUniform(location, count, v, GL_FLOAT);
388}
389
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000390bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
391{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000392 return setUniform(location, count, v, GL_FLOAT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000393}
394
395bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
396{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000397 return setUniform(location, count, v, GL_FLOAT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000398}
399
400bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
401{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000402 return setUniform(location, count, v, GL_FLOAT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000403}
404
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000405template<typename T>
406void transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000407{
408 int copyWidth = std::min(targetWidth, srcWidth);
409 int copyHeight = std::min(targetHeight, srcHeight);
410
411 for (int x = 0; x < copyWidth; x++)
412 {
413 for (int y = 0; y < copyHeight; y++)
414 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000415 target[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000416 }
417 }
418 // clear unfilled right side
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000419 for (int y = 0; y < copyWidth; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000420 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000421 for (int x = copyHeight; x < targetWidth; x++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000422 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000423 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000424 }
425 }
426 // clear unfilled bottom.
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000427 for (int y = copyWidth; y < targetHeight; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000428 {
429 for (int x = 0; x < targetWidth; x++)
430 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000431 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000432 }
433 }
434}
435
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000436template<typename T>
437void expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
438{
439 int copyWidth = std::min(targetWidth, srcWidth);
440 int copyHeight = std::min(targetHeight, srcHeight);
441
442 for (int y = 0; y < copyHeight; y++)
443 {
444 for (int x = 0; x < copyWidth; x++)
445 {
446 target[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
447 }
448 }
449 // clear unfilled right side
450 for (int y = 0; y < copyHeight; y++)
451 {
452 for (int x = copyWidth; x < targetWidth; x++)
453 {
454 target[y * targetWidth + x] = static_cast<T>(0);
455 }
456 }
457 // clear unfilled bottom.
458 for (int y = copyHeight; y < targetHeight; y++)
459 {
460 for (int x = 0; x < targetWidth; x++)
461 {
462 target[y * targetWidth + x] = static_cast<T>(0);
463 }
464 }
465}
466
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000467template <int cols, int rows>
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000468bool ProgramBinary::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000469{
470 if (location < 0 || location >= (int)mUniformIndex.size())
471 {
472 return false;
473 }
474
475 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
476 targetUniform->dirty = true;
477
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000478 if (targetUniform->type != targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000479 {
480 return false;
481 }
482
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000483 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000484
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000485 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000486 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
487
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000488 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000489 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4 * rows);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000490
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000491 for (int i = 0; i < count; i++)
492 {
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000493 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
494 if (transpose == GL_FALSE)
495 {
496 transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols);
497 }
498 else
499 {
500 expandMatrix<GLfloat>(target, value, 4, rows, cols, rows);
501 }
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000502 target += 4 * rows;
503 value += cols * rows;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000504 }
505
506 return true;
507}
508
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000509bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000510{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000511 return setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000512}
513
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000514bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000515{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000516 return setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000517}
518
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000519bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000520{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000521 return setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000522}
523
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000524bool ProgramBinary::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000525{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000526 return setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000527}
528
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000529bool ProgramBinary::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000530{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000531 return setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000532}
533
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000534bool ProgramBinary::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000535{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000536 return setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000537}
538
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000539bool ProgramBinary::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000540{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000541 return setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000542}
543
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000544bool ProgramBinary::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000545{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000546 return setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000547}
548
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000549bool ProgramBinary::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000550{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000551 return setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000552}
553
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000554bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
555{
556 if (location < 0 || location >= (int)mUniformIndex.size())
557 {
558 return false;
559 }
560
561 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
562 targetUniform->dirty = true;
563
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000564 int elementCount = targetUniform->elementCount();
565
566 if (elementCount == 1 && count > 1)
567 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
568
569 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
570
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000571 if (targetUniform->type == GL_INT ||
572 targetUniform->type == GL_SAMPLER_2D ||
573 targetUniform->type == GL_SAMPLER_CUBE)
574 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000575 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000576
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000577 for (int i = 0; i < count; i++)
578 {
579 target[0] = v[0];
580 target[1] = 0;
581 target[2] = 0;
582 target[3] = 0;
583 target += 4;
584 v += 1;
585 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000586 }
587 else if (targetUniform->type == GL_BOOL)
588 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000589 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000590
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000591 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000592 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000593 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
594 boolParams[1] = GL_FALSE;
595 boolParams[2] = GL_FALSE;
596 boolParams[3] = GL_FALSE;
597 boolParams += 4;
598 v += 1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000599 }
600 }
601 else
602 {
603 return false;
604 }
605
606 return true;
607}
608
609bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
610{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000611 return setUniform(location, count, v, GL_INT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000612}
613
614bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
615{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000616 return setUniform(location, count, v, GL_INT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000617}
618
619bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
620{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000621 return setUniform(location, count, v, GL_INT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000622}
623
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000624bool ProgramBinary::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
625{
626 return setUniform(location, count, v, GL_UNSIGNED_INT);
627}
628
629bool ProgramBinary::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
630{
631 return setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
632}
633
634bool ProgramBinary::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
635{
636 return setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
637}
638
639bool ProgramBinary::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
640{
641 return setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
642}
643
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000644template <typename T>
645bool ProgramBinary::getUniformv(GLint location, GLsizei *bufSize, T *params, GLenum uniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000646{
647 if (location < 0 || location >= (int)mUniformIndex.size())
648 {
649 return false;
650 }
651
652 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
653
654 // sized queries -- ensure the provided buffer is large enough
655 if (bufSize)
656 {
657 int requiredBytes = UniformExternalSize(targetUniform->type);
658 if (*bufSize < requiredBytes)
659 {
660 return false;
661 }
662 }
663
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000664 if (IsMatrixType(targetUniform->type))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000665 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000666 const int rows = VariableRowCount(targetUniform->type);
667 const int cols = VariableColumnCount(targetUniform->type);
668 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, cols, rows, 4, rows);
669 }
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000670 else if (uniformType == UniformComponentType(targetUniform->type))
671 {
672 unsigned int size = UniformComponentCount(targetUniform->type);
673 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
674 size * sizeof(T));
675 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000676 else
677 {
678 unsigned int size = UniformComponentCount(targetUniform->type);
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000679 switch (UniformComponentType(targetUniform->type))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000680 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000681 case GL_BOOL:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000682 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000683 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000684
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000685 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000686 {
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000687 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000688 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000689 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000690 break;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000691
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000692 case GL_FLOAT:
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000693 {
694 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
695
696 for (unsigned int i = 0; i < size; i++)
697 {
698 params[i] = static_cast<T>(floatParams[i]);
699 }
700 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000701 break;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000702
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000703 case GL_INT:
704 {
705 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
706
707 for (unsigned int i = 0; i < size; i++)
708 {
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000709 params[i] = static_cast<T>(intParams[i]);
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000710 }
711 }
712 break;
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000713
714 case GL_UNSIGNED_INT:
715 {
716 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000717
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000718 for (unsigned int i = 0; i < size; i++)
719 {
720 params[i] = static_cast<T>(uintParams[i]);
721 }
722 }
723 break;
724
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000725 default: UNREACHABLE();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000726 }
727 }
728
729 return true;
730}
731
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000732bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
733{
734 return getUniformv(location, bufSize, params, GL_FLOAT);
735}
736
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000737bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
738{
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000739 return getUniformv(location, bufSize, params, GL_INT);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000740}
741
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000742bool ProgramBinary::getUniformuiv(GLint location, GLsizei *bufSize, GLuint *params)
743{
744 return getUniformv(location, bufSize, params, GL_UNSIGNED_INT);
745}
746
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000747void ProgramBinary::dirtyAllUniforms()
748{
749 unsigned int numUniforms = mUniforms.size();
750 for (unsigned int index = 0; index < numUniforms; index++)
751 {
752 mUniforms[index]->dirty = true;
753 }
754}
755
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000756// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000757void ProgramBinary::applyUniforms()
758{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000759 // Retrieve sampler uniform values
760 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
761 {
762 Uniform *targetUniform = *ub;
763
764 if (targetUniform->dirty)
765 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000766 if (targetUniform->type == GL_SAMPLER_2D ||
767 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000768 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000769 int count = targetUniform->elementCount();
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000770 GLint (*v)[4] = (GLint(*)[4])targetUniform->data;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000771
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +0000772 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000773 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000774 unsigned int firstIndex = targetUniform->psRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000775
776 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000777 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000778 unsigned int samplerIndex = firstIndex + i;
779
780 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000781 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000782 ASSERT(mSamplersPS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000783 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000784 }
785 }
786 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000787
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +0000788 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000789 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000790 unsigned int firstIndex = targetUniform->vsRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000791
792 for (int i = 0; i < count; i++)
793 {
794 unsigned int samplerIndex = firstIndex + i;
795
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000796 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000797 {
798 ASSERT(mSamplersVS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000799 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000800 }
801 }
802 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000803 }
804 }
805 }
806
shannon.woods@transgaming.com358e88d2013-01-25 21:53:11 +0000807 mRenderer->applyUniforms(this, &mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000808}
809
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000810// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
811// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000812int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000813{
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000814 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000815
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000816 fragmentShader->resetVaryingsRegisterAssignment();
817
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000818 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
819 {
820 int n = VariableRowCount(varying->type) * varying->size;
821 int m = VariableColumnCount(varying->type);
822 bool success = false;
823
824 if (m == 2 || m == 3 || m == 4)
825 {
826 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
827 {
828 bool available = true;
829
830 for (int y = 0; y < n && available; y++)
831 {
832 for (int x = 0; x < m && available; x++)
833 {
834 if (packing[r + y][x])
835 {
836 available = false;
837 }
838 }
839 }
840
841 if (available)
842 {
843 varying->reg = r;
844 varying->col = 0;
845
846 for (int y = 0; y < n; y++)
847 {
848 for (int x = 0; x < m; x++)
849 {
850 packing[r + y][x] = &*varying;
851 }
852 }
853
854 success = true;
855 }
856 }
857
858 if (!success && m == 2)
859 {
860 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
861 {
862 bool available = true;
863
864 for (int y = 0; y < n && available; y++)
865 {
866 for (int x = 2; x < 4 && available; x++)
867 {
868 if (packing[r + y][x])
869 {
870 available = false;
871 }
872 }
873 }
874
875 if (available)
876 {
877 varying->reg = r;
878 varying->col = 2;
879
880 for (int y = 0; y < n; y++)
881 {
882 for (int x = 2; x < 4; x++)
883 {
884 packing[r + y][x] = &*varying;
885 }
886 }
887
888 success = true;
889 }
890 }
891 }
892 }
893 else if (m == 1)
894 {
895 int space[4] = {0};
896
897 for (int y = 0; y < maxVaryingVectors; y++)
898 {
899 for (int x = 0; x < 4; x++)
900 {
901 space[x] += packing[y][x] ? 0 : 1;
902 }
903 }
904
905 int column = 0;
906
907 for (int x = 0; x < 4; x++)
908 {
909 if (space[x] >= n && space[x] < space[column])
910 {
911 column = x;
912 }
913 }
914
915 if (space[column] >= n)
916 {
917 for (int r = 0; r < maxVaryingVectors; r++)
918 {
919 if (!packing[r][column])
920 {
921 varying->reg = r;
922
923 for (int y = r; y < r + n; y++)
924 {
925 packing[y][column] = &*varying;
926 }
927
928 break;
929 }
930 }
931
932 varying->col = column;
933
934 success = true;
935 }
936 }
937 else UNREACHABLE();
938
939 if (!success)
940 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000941 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000942
943 return -1;
944 }
945 }
946
947 // Return the number of used registers
948 int registers = 0;
949
950 for (int r = 0; r < maxVaryingVectors; r++)
951 {
952 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
953 {
954 registers++;
955 }
956 }
957
958 return registers;
959}
960
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000961bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
962 std::string& pixelHLSL, std::string& vertexHLSL,
963 FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000964{
965 if (pixelHLSL.empty() || vertexHLSL.empty())
966 {
967 return false;
968 }
969
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000970 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
971 bool usesFragColor = fragmentShader->mUsesFragColor;
972 bool usesFragData = fragmentShader->mUsesFragData;
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +0000973 if (usesFragColor && usesFragData)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000974 {
975 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
976 return false;
977 }
978
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000979 // Write the HLSL input/output declarations
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +0000980 const int shaderModel = mRenderer->getMajorShaderModel();
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000981 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000982
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000983 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
984
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +0000985 // Two cases when writing to gl_FragColor and using ESSL 1.0:
986 // - with a 3.0 context, the output color is copied to channel 0
987 // - with a 2.0 context, the output color is broadcast to all channels
988 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
989 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
990
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000991 if (registersNeeded > maxVaryingVectors)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000992 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000993 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000994
995 return false;
996 }
997
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000998 vertexShader->resetVaryingsRegisterAssignment();
999
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001000 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1001 {
1002 bool matched = false;
1003
1004 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1005 {
1006 if (output->name == input->name)
1007 {
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00001008 if (output->type != input->type || output->size != input->size || output->interpolation != input->interpolation)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001009 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001010 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 +00001011
1012 return false;
1013 }
1014
1015 output->reg = input->reg;
1016 output->col = input->col;
1017
1018 matched = true;
1019 break;
1020 }
1021 }
1022
1023 if (!matched)
1024 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001025 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001026
1027 return false;
1028 }
1029 }
1030
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001031 mUsesPointSize = vertexShader->mUsesPointSize;
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001032 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001033 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001034 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
1035
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001036 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
1037
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001038 // special varyings that use reserved registers
1039 int reservedRegisterIndex = registers;
1040 std::string fragCoordSemantic;
1041 std::string pointCoordSemantic;
1042
1043 if (fragmentShader->mUsesFragCoord)
1044 {
1045 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1046 }
1047
1048 if (fragmentShader->mUsesPointCoord)
1049 {
1050 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
1051 // In DX11 we compute this in the GS.
1052 if (shaderModel == 3)
1053 {
1054 pointCoordSemantic = "TEXCOORD0";
1055 }
1056 else if (shaderModel >= 4)
1057 {
1058 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1059 }
1060 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001061
1062 vertexHLSL += "struct VS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001063 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001064
1065 int semanticIndex = 0;
1066 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1067 {
1068 switch (attribute->type)
1069 {
1070 case GL_FLOAT: vertexHLSL += " float "; break;
1071 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1072 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1073 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1074 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1075 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1076 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1077 default: UNREACHABLE();
1078 }
1079
1080 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1081
1082 semanticIndex += VariableRowCount(attribute->type);
1083 }
1084
1085 vertexHLSL += "};\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001086 "\n"
1087 "struct VS_OUTPUT\n"
1088 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001089
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001090 if (shaderModel < 4)
1091 {
1092 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1093 }
1094
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001095 vertexHLSL += varyingHLSL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001096
1097 if (fragmentShader->mUsesFragCoord)
1098 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001099 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001100 }
1101
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001102 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001103 {
1104 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1105 }
1106
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001107 if (shaderModel >= 4)
1108 {
1109 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1110 }
1111
1112 vertexHLSL += "};\n"
daniel@transgaming.com9c4a6252013-01-11 04:07:18 +00001113 "\n"
1114 "VS_OUTPUT main(VS_INPUT input)\n"
1115 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001116
1117 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1118 {
1119 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1120
1121 if (VariableRowCount(attribute->type) > 1) // Matrix
1122 {
1123 vertexHLSL += "transpose";
1124 }
1125
1126 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1127 }
1128
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001129 if (shaderModel >= 4)
1130 {
1131 vertexHLSL += "\n"
1132 " gl_main();\n"
1133 "\n"
1134 " VS_OUTPUT output;\n"
1135 " output.gl_Position.x = gl_Position.x;\n"
1136 " output.gl_Position.y = -gl_Position.y;\n"
1137 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1138 " output.gl_Position.w = gl_Position.w;\n";
1139 }
1140 else
1141 {
1142 vertexHLSL += "\n"
1143 " gl_main();\n"
1144 "\n"
1145 " VS_OUTPUT output;\n"
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001146 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
1147 " output.gl_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001148 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1149 " output.gl_Position.w = gl_Position.w;\n";
1150 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001151
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001152 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001153 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001154 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001155 }
1156
1157 if (fragmentShader->mUsesFragCoord)
1158 {
1159 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1160 }
1161
1162 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1163 {
1164 if (varying->reg >= 0)
1165 {
1166 for (int i = 0; i < varying->size; i++)
1167 {
1168 int rows = VariableRowCount(varying->type);
1169
1170 for (int j = 0; j < rows; j++)
1171 {
1172 int r = varying->reg + i * rows + j;
1173 vertexHLSL += " output.v" + str(r);
1174
1175 bool sharedRegister = false; // Register used by multiple varyings
1176
1177 for (int x = 0; x < 4; x++)
1178 {
1179 if (packing[r][x] && packing[r][x] != packing[r][0])
1180 {
1181 sharedRegister = true;
1182 break;
1183 }
1184 }
1185
1186 if(sharedRegister)
1187 {
1188 vertexHLSL += ".";
1189
1190 for (int x = 0; x < 4; x++)
1191 {
1192 if (packing[r][x] == &*varying)
1193 {
1194 switch(x)
1195 {
1196 case 0: vertexHLSL += "x"; break;
1197 case 1: vertexHLSL += "y"; break;
1198 case 2: vertexHLSL += "z"; break;
1199 case 3: vertexHLSL += "w"; break;
1200 }
1201 }
1202 }
1203 }
1204
1205 vertexHLSL += " = " + varying->name;
1206
1207 if (varying->array)
1208 {
1209 vertexHLSL += "[" + str(i) + "]";
1210 }
1211
1212 if (rows > 1)
1213 {
1214 vertexHLSL += "[" + str(j) + "]";
1215 }
1216
1217 vertexHLSL += ";\n";
1218 }
1219 }
1220 }
1221 }
1222
1223 vertexHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001224 " return output;\n"
1225 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001226
1227 pixelHLSL += "struct PS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001228 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001229
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001230 pixelHLSL += varyingHLSL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001231
1232 if (fragmentShader->mUsesFragCoord)
1233 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001234 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001235 }
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001236
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001237 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
1238 {
1239 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
1240 }
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00001241
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001242 // Must consume the PSIZE element if the geometry shader is not active
1243 // We won't know if we use a GS until we draw
1244 if (vertexShader->mUsesPointSize && shaderModel >= 4)
1245 {
1246 pixelHLSL += " float gl_PointSize : PSIZE;\n";
1247 }
1248
1249 if (fragmentShader->mUsesFragCoord)
1250 {
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001251 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001252 {
1253 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1254 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001255 else if (shaderModel >= 3)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001256 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001257 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1258 }
1259 }
1260
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001261 pixelHLSL += "};\n"
1262 "\n"
1263 "struct PS_OUTPUT\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001264 "{\n";
1265
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001266 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001267 {
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001268 pixelHLSL += " float4 gl_Color" + str(renderTargetIndex) + " : " + targetSemantic + str(renderTargetIndex) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001269 }
1270
1271 pixelHLSL += "};\n"
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001272 "\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001273
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001274 if (fragmentShader->mUsesFrontFacing)
1275 {
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001276 if (shaderModel >= 4)
1277 {
1278 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
1279 "{\n";
1280 }
1281 else
1282 {
1283 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
1284 "{\n";
1285 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001286 }
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001287 else
1288 {
1289 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
1290 "{\n";
1291 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001292
1293 if (fragmentShader->mUsesFragCoord)
1294 {
1295 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1296
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001297 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001298 {
1299 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1300 " gl_FragCoord.y = input.dx_VPos.y;\n";
1301 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001302 else if (shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001303 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001304 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001305 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001306 }
1307 else
1308 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001309 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
1310 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
1311 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001312 }
1313
daniel@transgaming.com12985182012-12-20 20:56:31 +00001314 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001315 " gl_FragCoord.w = rhw;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001316 }
1317
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001318 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001319 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001320 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1321 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001322 }
1323
1324 if (fragmentShader->mUsesFrontFacing)
1325 {
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001326 if (shaderModel <= 3)
1327 {
1328 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1329 }
1330 else
1331 {
1332 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1333 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001334 }
1335
1336 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1337 {
1338 if (varying->reg >= 0)
1339 {
1340 for (int i = 0; i < varying->size; i++)
1341 {
1342 int rows = VariableRowCount(varying->type);
1343 for (int j = 0; j < rows; j++)
1344 {
1345 std::string n = str(varying->reg + i * rows + j);
1346 pixelHLSL += " " + varying->name;
1347
1348 if (varying->array)
1349 {
1350 pixelHLSL += "[" + str(i) + "]";
1351 }
1352
1353 if (rows > 1)
1354 {
1355 pixelHLSL += "[" + str(j) + "]";
1356 }
1357
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001358 switch (VariableColumnCount(varying->type))
1359 {
1360 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1361 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1362 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1363 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1364 default: UNREACHABLE();
1365 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001366 }
1367 }
1368 }
1369 else UNREACHABLE();
1370 }
1371
1372 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001373 " gl_main();\n"
1374 "\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001375 " PS_OUTPUT output;\n";
1376
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001377 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001378 {
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001379 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001380
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001381 pixelHLSL += " output.gl_Color" + str(renderTargetIndex) + " = gl_Color[" + str(sourceColorIndex) + "];\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001382 }
1383
1384 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001385 " return output;\n"
1386 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001387
1388 return true;
1389}
1390
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001391std::string ProgramBinary::generateVaryingHLSL(FragmentShader *fragmentShader, const std::string &varyingSemantic) const
1392{
1393 std::string varyingHLSL;
1394
1395 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1396 {
1397 if (varying->reg >= 0)
1398 {
1399 for (int i = 0; i < varying->size; i++)
1400 {
1401 int rows = VariableRowCount(varying->type);
1402 for (int j = 0; j < rows; j++)
1403 {
1404 switch (varying->interpolation)
1405 {
1406 case Smooth: varyingHLSL += " "; break;
1407 case Flat: varyingHLSL += " nointerpolation "; break;
1408 case Centroid: varyingHLSL += " centroid "; break;
1409 default: UNREACHABLE();
1410 }
1411
1412 std::string n = str(varying->reg + i * rows + j);
1413 varyingHLSL += "float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
1414 }
1415 }
1416 }
1417 else UNREACHABLE();
1418 }
1419
1420 return varyingHLSL;
1421}
1422
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001423bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1424{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001425 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001426
1427 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001428 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001429 if (format != GL_PROGRAM_BINARY_ANGLE)
1430 {
1431 infoLog.append("Invalid program binary format.");
1432 return false;
1433 }
1434
1435 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001436 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001437 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001438 {
1439 infoLog.append("Invalid program binary version.");
1440 return false;
1441 }
1442
shannonwoods@chromium.orgf97a0842013-05-30 00:10:33 +00001443 int compileFlags = 0;
1444 stream.read(&compileFlags);
1445 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
1446 {
1447 infoLog.append("Mismatched compilation flags.");
1448 return false;
1449 }
1450
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001451 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1452 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001453 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001454 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001455 stream.read(&name);
1456 mLinkedAttribute[i].name = name;
1457 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001458 }
1459
1460 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1461 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001462 stream.read(&mSamplersPS[i].active);
1463 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001464
1465 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001466 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001467 mSamplersPS[i].textureType = (TextureType) textureType;
1468 }
1469
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001470 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001471 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001472 stream.read(&mSamplersVS[i].active);
1473 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001474
1475 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001476 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001477 mSamplersVS[i].textureType = (TextureType) textureType;
1478 }
1479
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001480 stream.read(&mUsedVertexSamplerRange);
1481 stream.read(&mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001482 stream.read(&mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001483
shannon.woods@transgaming.com45886d62013-02-28 23:19:20 +00001484 size_t size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001485 stream.read(&size);
1486 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001487 {
1488 infoLog.append("Invalid program binary.");
1489 return false;
1490 }
1491
1492 mUniforms.resize(size);
1493 for (unsigned int i = 0; i < size; ++i)
1494 {
1495 GLenum type;
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001496 GLenum precision;
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001497 std::string name;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001498 unsigned int arraySize;
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001499 int blockIndex;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001500
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001501 stream.read(&type);
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001502 stream.read(&precision);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001503 stream.read(&name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001504 stream.read(&arraySize);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001505 stream.read(&blockIndex);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001506
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001507 int offset;
1508 int arrayStride;
1509 int matrixStride;
1510 bool isRowMajorMatrix;
1511
1512 stream.read(&offset);
1513 stream.read(&arrayStride);
1514 stream.read(&matrixStride);
1515 stream.read(&isRowMajorMatrix);
1516
1517 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
1518
1519 mUniforms[i] = new Uniform(type, precision, name, arraySize, blockIndex, blockInfo);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001520
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001521 stream.read(&mUniforms[i]->psRegisterIndex);
1522 stream.read(&mUniforms[i]->vsRegisterIndex);
1523 stream.read(&mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001524 }
1525
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001526 stream.read(&size);
1527 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001528 {
1529 infoLog.append("Invalid program binary.");
1530 return false;
1531 }
1532
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001533 mUniformBlocks.resize(size);
1534 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < size; ++uniformBlockIndex)
1535 {
1536 std::string name;
1537 unsigned int elementIndex;
1538 unsigned int dataSize;
1539
1540 stream.read(&name);
1541 stream.read(&elementIndex);
1542 stream.read(&dataSize);
1543
1544 mUniformBlocks[uniformBlockIndex] = new UniformBlock(name, elementIndex, dataSize);
1545
1546 UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
1547 stream.read(&uniformBlock.psRegisterIndex);
1548 stream.read(&uniformBlock.vsRegisterIndex);
1549
1550 size_t numMembers;
1551 stream.read(&numMembers);
1552 uniformBlock.memberUniformIndexes.resize(numMembers);
1553 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
1554 {
1555 stream.read(&uniformBlock.memberUniformIndexes[blockMemberIndex]);
1556 }
1557 }
1558
1559 stream.read(&size);
1560 if (stream.error())
1561 {
1562 infoLog.append("Invalid program binary.");
1563 return false;
1564 }
1565
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001566 mUniformIndex.resize(size);
1567 for (unsigned int i = 0; i < size; ++i)
1568 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001569 stream.read(&mUniformIndex[i].name);
1570 stream.read(&mUniformIndex[i].element);
1571 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001572 }
1573
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001574 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001575 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001576
1577 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001578 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001579
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001580 unsigned int geometryShaderSize;
1581 stream.read(&geometryShaderSize);
1582
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001583 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001584
daniel@transgaming.com36038542012-11-28 20:59:26 +00001585 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001586 ptr += sizeof(GUID);
1587
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001588 GUID identifier = mRenderer->getAdapterIdentifier();
1589 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001590 {
1591 infoLog.append("Invalid program binary.");
1592 return false;
1593 }
1594
1595 const char *pixelShaderFunction = ptr;
1596 ptr += pixelShaderSize;
1597
1598 const char *vertexShaderFunction = ptr;
1599 ptr += vertexShaderSize;
1600
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001601 const char *geometryShaderFunction = geometryShaderSize > 0 ? ptr : NULL;
1602 ptr += geometryShaderSize;
1603
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001604 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001605 pixelShaderSize, rx::SHADER_PIXEL);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001606 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001607 {
1608 infoLog.append("Could not create pixel shader.");
1609 return false;
1610 }
1611
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001612 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001613 vertexShaderSize, rx::SHADER_VERTEX);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001614 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001615 {
1616 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001617 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001618 mPixelExecutable = NULL;
1619 return false;
1620 }
1621
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001622 if (geometryShaderFunction != NULL && geometryShaderSize > 0)
1623 {
1624 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction),
1625 geometryShaderSize, rx::SHADER_GEOMETRY);
1626 if (!mGeometryExecutable)
1627 {
1628 infoLog.append("Could not create geometry shader.");
1629 delete mPixelExecutable;
1630 mPixelExecutable = NULL;
1631 delete mVertexExecutable;
1632 mVertexExecutable = NULL;
1633 return false;
1634 }
1635 }
1636 else
1637 {
1638 mGeometryExecutable = NULL;
1639 }
1640
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001641 return true;
1642}
1643
1644bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1645{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001646 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001647
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001648 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001649 stream.write(VERSION_DWORD);
shannonwoods@chromium.orgf97a0842013-05-30 00:10:33 +00001650 stream.write(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001651
1652 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1653 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001654 stream.write(mLinkedAttribute[i].type);
1655 stream.write(mLinkedAttribute[i].name);
1656 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001657 }
1658
1659 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1660 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001661 stream.write(mSamplersPS[i].active);
1662 stream.write(mSamplersPS[i].logicalTextureUnit);
1663 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001664 }
1665
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001666 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001667 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001668 stream.write(mSamplersVS[i].active);
1669 stream.write(mSamplersVS[i].logicalTextureUnit);
1670 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001671 }
1672
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001673 stream.write(mUsedVertexSamplerRange);
1674 stream.write(mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001675 stream.write(mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001676
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001677 stream.write(mUniforms.size());
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001678 for (unsigned int uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001679 {
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001680 const Uniform &uniform = *mUniforms[uniformIndex];
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001681
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001682 stream.write(uniform.type);
1683 stream.write(uniform.precision);
1684 stream.write(uniform.name);
1685 stream.write(uniform.arraySize);
1686 stream.write(uniform.blockIndex);
1687
1688 stream.write(uniform.blockInfo.offset);
1689 stream.write(uniform.blockInfo.arrayStride);
1690 stream.write(uniform.blockInfo.matrixStride);
1691 stream.write(uniform.blockInfo.isRowMajorMatrix);
1692
1693 stream.write(uniform.psRegisterIndex);
1694 stream.write(uniform.vsRegisterIndex);
1695 stream.write(uniform.registerCount);
1696 }
1697
1698 stream.write(mUniformBlocks.size());
1699 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
1700 {
1701 const UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
1702
1703 stream.write(uniformBlock.name);
1704 stream.write(uniformBlock.elementIndex);
1705 stream.write(uniformBlock.dataSize);
1706
1707 stream.write(uniformBlock.memberUniformIndexes.size());
1708 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1709 {
1710 stream.write(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1711 }
1712
1713 stream.write(uniformBlock.psRegisterIndex);
1714 stream.write(uniformBlock.vsRegisterIndex);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001715 }
1716
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001717 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001718 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1719 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001720 stream.write(mUniformIndex[i].name);
1721 stream.write(mUniformIndex[i].element);
1722 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001723 }
1724
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001725 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001726 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001727
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001728 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001729 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001730
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001731 UINT geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
1732 stream.write(geometryShaderSize);
1733
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001734 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001735
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001736 GLsizei streamLength = stream.length();
1737 const void *streamData = stream.data();
1738
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001739 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize + geometryShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001740 if (totalLength > bufSize)
1741 {
1742 if (length)
1743 {
1744 *length = 0;
1745 }
1746
1747 return false;
1748 }
1749
1750 if (binary)
1751 {
1752 char *ptr = (char*) binary;
1753
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001754 memcpy(ptr, streamData, streamLength);
1755 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001756
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001757 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001758 ptr += sizeof(GUID);
1759
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001760 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001761 ptr += pixelShaderSize;
1762
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001763 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001764 ptr += vertexShaderSize;
1765
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001766 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
1767 {
1768 memcpy(ptr, mGeometryExecutable->getFunction(), geometryShaderSize);
1769 ptr += geometryShaderSize;
1770 }
1771
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001772 ASSERT(ptr - totalLength == binary);
1773 }
1774
1775 if (length)
1776 {
1777 *length = totalLength;
1778 }
1779
1780 return true;
1781}
1782
1783GLint ProgramBinary::getLength()
1784{
1785 GLint length;
1786 if (save(NULL, INT_MAX, &length))
1787 {
1788 return length;
1789 }
1790 else
1791 {
1792 return 0;
1793 }
1794}
1795
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001796bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001797{
1798 if (!fragmentShader || !fragmentShader->isCompiled())
1799 {
1800 return false;
1801 }
1802
1803 if (!vertexShader || !vertexShader->isCompiled())
1804 {
1805 return false;
1806 }
1807
1808 std::string pixelHLSL = fragmentShader->getHLSL();
1809 std::string vertexHLSL = vertexShader->getHLSL();
1810
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001811 // Map the varyings to the register file
1812 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
1813 int registers = packVaryings(infoLog, packing, fragmentShader);
1814
1815 if (registers < 0)
1816 {
1817 return false;
1818 }
1819
1820 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001821 {
1822 return false;
1823 }
1824
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001825 bool success = true;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001826 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX);
1827 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001828
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001829 if (usesGeometryShader())
1830 {
1831 std::string geometryHLSL = generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader);
1832 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY);
1833 }
1834
1835 if (!mVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001836 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001837 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001838 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001839
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001840 delete mVertexExecutable;
1841 mVertexExecutable = NULL;
1842 delete mPixelExecutable;
1843 mPixelExecutable = NULL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001844 delete mGeometryExecutable;
1845 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001846 }
1847
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001848 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1849 {
1850 success = false;
1851 }
1852
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001853 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001854 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001855 success = false;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001856 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001857
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001858 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1859 if (vertexShader->mUsesDepthRange || fragmentShader->mUsesDepthRange)
1860 {
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001861 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, sh::BlockMemberInfo::defaultBlockInfo));
1862 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, sh::BlockMemberInfo::defaultBlockInfo));
1863 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, sh::BlockMemberInfo::defaultBlockInfo));
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001864 }
1865
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00001866 if (!linkUniformBlocks(infoLog, vertexShader->getInterfaceBlocks(), fragmentShader->getInterfaceBlocks()))
1867 {
1868 success = false;
1869 }
1870
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001871 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001872}
1873
1874// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001875bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001876{
1877 unsigned int usedLocations = 0;
1878
1879 // Link attributes that have a binding location
1880 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1881 {
1882 int location = attributeBindings.getAttributeBinding(attribute->name);
1883
1884 if (location != -1) // Set by glBindAttribLocation
1885 {
1886 if (!mLinkedAttribute[location].name.empty())
1887 {
1888 // Multiple active attributes bound to the same location; not an error
1889 }
1890
1891 mLinkedAttribute[location] = *attribute;
1892
1893 int rows = VariableRowCount(attribute->type);
1894
1895 if (rows + location > MAX_VERTEX_ATTRIBS)
1896 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001897 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 +00001898
1899 return false;
1900 }
1901
1902 for (int i = 0; i < rows; i++)
1903 {
1904 usedLocations |= 1 << (location + i);
1905 }
1906 }
1907 }
1908
1909 // Link attributes that don't have a binding location
1910 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1911 {
1912 int location = attributeBindings.getAttributeBinding(attribute->name);
1913
1914 if (location == -1) // Not set by glBindAttribLocation
1915 {
1916 int rows = VariableRowCount(attribute->type);
1917 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1918
1919 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1920 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001921 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001922
1923 return false; // Fail to link
1924 }
1925
1926 mLinkedAttribute[availableIndex] = *attribute;
1927 }
1928 }
1929
1930 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1931 {
1932 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1933 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1934
1935 for (int r = 0; r < rows; r++)
1936 {
1937 mSemanticIndex[attributeIndex++] = index++;
1938 }
1939 }
1940
1941 return true;
1942}
1943
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001944bool ProgramBinary::areMatchingUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
1945{
1946 if (vertexUniform.type != fragmentUniform.type)
1947 {
1948 infoLog.append("Types for %s differ between vertex and fragment shaders", uniformName.c_str());
1949 return false;
1950 }
1951 else if (vertexUniform.arraySize != fragmentUniform.arraySize)
1952 {
1953 infoLog.append("Array sizes for %s differ between vertex and fragment shaders", uniformName.c_str());
1954 return false;
1955 }
1956 else if (vertexUniform.precision != fragmentUniform.precision)
1957 {
1958 infoLog.append("Precisions for %s differ between vertex and fragment shaders", uniformName.c_str());
1959 return false;
1960 }
1961 else if (vertexUniform.fields.size() != fragmentUniform.fields.size())
1962 {
1963 infoLog.append("Structure lengths for %s differ between vertex and fragment shaders", uniformName.c_str());
1964 }
1965
1966 const unsigned int numMembers = vertexUniform.fields.size();
1967 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1968 {
1969 const sh::Uniform &vertexMember = vertexUniform.fields[memberIndex];
1970 const sh::Uniform &fragmentMember = fragmentUniform.fields[memberIndex];
1971
1972 if (vertexMember.name != fragmentMember.name)
1973 {
1974 infoLog.append("Name mismatch for field %d of %s: (in vertex: '%s', in fragment: '%s')",
1975 memberIndex, uniformName.c_str(), vertexMember.name.c_str(), fragmentMember.name.c_str());
1976 return false;
1977 }
1978
1979 const std::string memberName = uniformName + "." + vertexUniform.name;
1980 if (!areMatchingUniforms(infoLog, memberName, vertexMember, fragmentMember))
1981 {
1982 return false;
1983 }
1984 }
1985
1986 return true;
1987}
1988
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001989bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001990{
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001991 // Check that uniforms defined in the vertex and fragment shaders are identical
1992 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1993 UniformMap linkedUniforms;
1994
1995 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
1996 {
1997 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1998 linkedUniforms[vertexUniform.name] = &vertexUniform;
1999 }
2000
2001 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
2002 {
2003 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
2004 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
2005 if (entry != linkedUniforms.end())
2006 {
2007 const sh::Uniform &vertexUniform = *entry->second;
2008 const std::string &uniformName = "uniform " + vertexUniform.name;
2009 if (!areMatchingUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
2010 {
2011 return false;
2012 }
2013 }
2014 }
2015
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002016 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002017 {
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002018 if (!defineUniform(GL_VERTEX_SHADER, vertexUniforms[uniformIndex], infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002019 {
2020 return false;
2021 }
2022 }
2023
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002024 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002025 {
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002026 if (!defineUniform(GL_FRAGMENT_SHADER, fragmentUniforms[uniformIndex], infoLog))
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002027 {
2028 return false;
2029 }
2030 }
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002031
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002032 return true;
2033}
2034
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00002035int totalRegisterCount(const sh::Uniform &uniform)
2036{
2037 int registerCount = 0;
2038
2039 if (!uniform.fields.empty())
2040 {
2041 for (unsigned int fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2042 {
2043 registerCount += totalRegisterCount(uniform.fields[fieldIndex]);
2044 }
2045 }
2046 else
2047 {
2048 registerCount = 1;
2049 }
2050
2051 return (uniform.arraySize > 0) ? uniform.arraySize * registerCount : registerCount;
2052}
2053
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002054bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002055{
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00002056 if (!constant.fields.empty())
2057 {
2058 if (constant.arraySize > 0)
2059 {
2060 unsigned int elementRegisterIndex = constant.registerIndex;
2061
2062 for (unsigned int elementIndex = 0; elementIndex < constant.arraySize; elementIndex++)
2063 {
2064 for (size_t fieldIndex = 0; fieldIndex < constant.fields.size(); fieldIndex++)
2065 {
2066 const sh::Uniform &field = constant.fields[fieldIndex];
2067 const std::string &uniformName = constant.name + "[" + str(elementIndex) + "]." + field.name;
2068 const sh::Uniform fieldUniform(field.type, field.precision, uniformName.c_str(), field.arraySize, elementRegisterIndex);
2069 if (!defineUniform(shader, fieldUniform, infoLog))
2070 {
2071 return false;
2072 }
2073 elementRegisterIndex += totalRegisterCount(field);
2074 }
2075 }
2076 }
2077 else
2078 {
2079 unsigned int fieldRegisterIndex = constant.registerIndex;
2080
2081 for (size_t fieldIndex = 0; fieldIndex < constant.fields.size(); fieldIndex++)
2082 {
2083 const sh::Uniform &field = constant.fields[fieldIndex];
2084 const std::string &uniformName = constant.name + "." + field.name;
2085
2086 sh::Uniform fieldUniform(field.type, field.precision, uniformName.c_str(), field.arraySize, fieldRegisterIndex);
2087 fieldUniform.fields = field.fields;
2088
2089 if (!defineUniform(shader, fieldUniform, infoLog))
2090 {
2091 return false;
2092 }
2093 fieldRegisterIndex += totalRegisterCount(field);
2094 }
2095 }
2096
2097 return true;
2098 }
2099
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002100 if (constant.type == GL_SAMPLER_2D ||
2101 constant.type == GL_SAMPLER_CUBE)
2102 {
2103 unsigned int samplerIndex = constant.registerIndex;
2104
2105 do
2106 {
2107 if (shader == GL_VERTEX_SHADER)
2108 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002109 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002110 {
2111 mSamplersVS[samplerIndex].active = true;
2112 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2113 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2114 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2115 }
2116 else
2117 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002118 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002119 return false;
2120 }
2121 }
2122 else if (shader == GL_FRAGMENT_SHADER)
2123 {
2124 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2125 {
2126 mSamplersPS[samplerIndex].active = true;
2127 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2128 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2129 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2130 }
2131 else
2132 {
2133 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
2134 return false;
2135 }
2136 }
2137 else UNREACHABLE();
2138
2139 samplerIndex++;
2140 }
2141 while (samplerIndex < constant.registerIndex + constant.arraySize);
2142 }
2143
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002144 Uniform *uniform = NULL;
2145 GLint location = getUniformLocation(constant.name);
2146
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00002147 if (location >= 0) // Previously defined, type and precision must match
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002148 {
2149 uniform = mUniforms[mUniformIndex[location].index];
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002150 }
2151 else
2152 {
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00002153 uniform = new Uniform(constant.type, constant.precision, constant.name, constant.arraySize, -1, sh::BlockMemberInfo::defaultBlockInfo);
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002154 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002155
2156 if (!uniform)
2157 {
2158 return false;
2159 }
2160
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002161 if (shader == GL_FRAGMENT_SHADER)
2162 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00002163 uniform->psRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002164 }
2165 else if (shader == GL_VERTEX_SHADER)
2166 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00002167 uniform->vsRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002168 }
2169 else UNREACHABLE();
2170
2171 if (location >= 0)
2172 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002173 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002174 }
2175
2176 mUniforms.push_back(uniform);
2177 unsigned int uniformIndex = mUniforms.size() - 1;
2178
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002179 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform->elementCount(); arrayElementIndex++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002180 {
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002181 mUniformIndex.push_back(UniformLocation(uniform->name, arrayElementIndex, uniformIndex));
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002182 }
2183
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002184 if (shader == GL_VERTEX_SHADER)
2185 {
2186 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedVertexUniformVectors() + mRenderer->getMaxVertexUniformVectors())
2187 {
2188 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", mRenderer->getMaxVertexUniformVectors());
2189 return false;
2190 }
2191 }
2192 else if (shader == GL_FRAGMENT_SHADER)
2193 {
2194 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedFragmentUniformVectors() + mRenderer->getMaxFragmentUniformVectors())
2195 {
2196 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", mRenderer->getMaxFragmentUniformVectors());
2197 return false;
2198 }
2199 }
2200 else UNREACHABLE();
2201
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002202 return true;
2203}
2204
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002205bool ProgramBinary::areMatchingInterfaceBlocks(InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock, const sh::InterfaceBlock &fragmentInterfaceBlock)
2206{
2207 const char* blockName = vertexInterfaceBlock.name.c_str();
2208
2209 // validate blocks for the same member types
2210 if (vertexInterfaceBlock.activeUniforms.size() != fragmentInterfaceBlock.activeUniforms.size())
2211 {
2212 infoLog.append("Types for interface block '%s' differ between vertex and fragment shaders", blockName);
2213 return false;
2214 }
2215
2216 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2217 {
2218 infoLog.append("Array sizes differ for interface block '%s' between vertex and fragment shaders", blockName);
2219 return false;
2220 }
2221
2222 const unsigned int numBlockMembers = vertexInterfaceBlock.activeUniforms.size();
2223 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2224 {
2225 const sh::Uniform &vertexMember = vertexInterfaceBlock.activeUniforms[blockMemberIndex];
2226 const sh::Uniform &fragmentMember = fragmentInterfaceBlock.activeUniforms[blockMemberIndex];
2227
2228 if (vertexMember.name != fragmentMember.name)
2229 {
2230 infoLog.append("Name mismatch for field %d of interface block %s: (in vertex: '%s', in fragment: '%s')",
2231 blockMemberIndex, blockName, vertexMember.name.c_str(), fragmentMember.name.c_str());
2232 return false;
2233 }
2234
2235 std::string uniformName = "interface block " + vertexInterfaceBlock.name + " member " + vertexMember.name;
2236 if (!areMatchingUniforms(infoLog, uniformName, vertexMember, fragmentMember))
2237 {
2238 return false;
2239 }
2240 }
2241
2242 return true;
2243}
2244
2245bool ProgramBinary::linkUniformBlocks(InfoLog &infoLog, const sh::ActiveInterfaceBlocks &vertexInterfaceBlocks, const sh::ActiveInterfaceBlocks &fragmentInterfaceBlocks)
2246{
2247 // Check that interface blocks defined in the vertex and fragment shaders are identical
2248 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
2249 UniformBlockMap linkedUniformBlocks;
2250
2251 for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
2252 {
2253 const sh::InterfaceBlock &vertexInterfaceBlock = vertexInterfaceBlocks[blockIndex];
2254 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2255 }
2256
2257 for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
2258 {
2259 const sh::InterfaceBlock &fragmentInterfaceBlock = fragmentInterfaceBlocks[blockIndex];
2260 UniformBlockMap::const_iterator entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
2261 if (entry != linkedUniformBlocks.end())
2262 {
2263 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2264 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2265 {
2266 return false;
2267 }
2268 }
2269 }
2270
2271 for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
2272 {
2273 if (!defineUniformBlock(infoLog, GL_VERTEX_SHADER, vertexInterfaceBlocks[blockIndex]))
2274 {
2275 return false;
2276 }
2277 }
2278
2279 for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
2280 {
2281 if (!defineUniformBlock(infoLog, GL_FRAGMENT_SHADER, fragmentInterfaceBlocks[blockIndex]))
2282 {
2283 return false;
2284 }
2285 }
2286
2287 return true;
2288}
2289
2290bool ProgramBinary::defineUniformBlock(InfoLog &infoLog, GLenum shader, const sh::InterfaceBlock &interfaceBlock)
2291{
2292 // create uniform block entries if they do not exist
2293 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
2294 {
2295 std::vector<unsigned int> blockUniformIndexes;
2296 const unsigned int blockIndex = mUniformBlocks.size();
2297
2298 // define member uniforms
2299 for (unsigned int activeUniformIndex = 0; activeUniformIndex < interfaceBlock.activeUniforms.size(); activeUniformIndex++)
2300 {
2301 const sh::Uniform &constant = interfaceBlock.activeUniforms[activeUniformIndex];
2302 Uniform *uniform = new Uniform(constant.type, constant.precision, constant.name, constant.arraySize,
2303 blockIndex, interfaceBlock.blockInfo[activeUniformIndex]);
2304
2305 // add to uniform list, but not index, since uniform block uniforms have no location
2306 blockUniformIndexes.push_back(mUniforms.size());
2307 mUniforms.push_back(uniform);
2308 }
2309
2310 // create all the uniform blocks
2311 if (interfaceBlock.arraySize > 0)
2312 {
2313 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
2314 {
2315 gl::UniformBlock *newUniformBlock = new UniformBlock(interfaceBlock.name, uniformBlockElement, interfaceBlock.dataSize);
2316 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
2317 mUniformBlocks.push_back(newUniformBlock);
2318 }
2319 }
2320 else
2321 {
2322 gl::UniformBlock *newUniformBlock = new UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, interfaceBlock.dataSize);
2323 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
2324 mUniformBlocks.push_back(newUniformBlock);
2325 }
2326 }
2327
2328 // Assign registers to the uniform blocks
2329 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
2330 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
2331 ASSERT(blockIndex != GL_INVALID_INDEX);
2332 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
2333
2334 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
2335 {
2336 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
2337 ASSERT(uniformBlock->name == interfaceBlock.name);
2338
2339 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader, interfaceBlock.registerIndex + uniformBlockElement))
2340 {
2341 return false;
2342 }
2343 }
2344
2345 return true;
2346}
2347
2348bool ProgramBinary::assignUniformBlockRegister(InfoLog &infoLog, UniformBlock *uniformBlock, GLenum shader, unsigned int registerIndex)
2349{
2350 if (shader == GL_VERTEX_SHADER)
2351 {
2352 uniformBlock->vsRegisterIndex = registerIndex;
2353 unsigned int maximumBlocks = mRenderer->getMaxVertexShaderUniformBuffers();
2354
2355 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= maximumBlocks)
2356 {
2357 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", maximumBlocks);
2358 return false;
2359 }
2360 }
2361 else if (shader == GL_FRAGMENT_SHADER)
2362 {
2363 uniformBlock->psRegisterIndex = registerIndex;
2364 unsigned int maximumBlocks = mRenderer->getMaxFragmentShaderUniformBuffers();
2365
2366 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= maximumBlocks)
2367 {
2368 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", maximumBlocks);
2369 return false;
2370 }
2371 }
2372 else UNREACHABLE();
2373
2374 return true;
2375}
2376
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002377std::string ProgramBinary::generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2378{
2379 // for now we only handle point sprite emulation
2380 ASSERT(usesPointSpriteEmulation());
2381 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
2382}
2383
2384std::string ProgramBinary::generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2385{
2386 ASSERT(registers >= 0);
2387 ASSERT(vertexShader->mUsesPointSize);
2388 ASSERT(mRenderer->getMajorShaderModel() >= 4);
2389
2390 std::string geomHLSL;
2391
2392 std::string varyingSemantic = "TEXCOORD";
2393
2394 std::string fragCoordSemantic;
2395 std::string pointCoordSemantic;
2396
2397 int reservedRegisterIndex = registers;
2398
2399 if (fragmentShader->mUsesFragCoord)
2400 {
2401 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2402 }
2403
2404 if (fragmentShader->mUsesPointCoord)
2405 {
2406 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2407 }
2408
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00002409 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
2410 "\n"
2411 "struct GS_INPUT\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002412 "{\n";
2413
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00002414 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002415
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00002416 geomHLSL += varyingHLSL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002417
2418 if (fragmentShader->mUsesFragCoord)
2419 {
2420 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2421 }
2422
2423 geomHLSL += " float gl_PointSize : PSIZE;\n"
2424 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002425 "};\n"
2426 "\n"
2427 "struct GS_OUTPUT\n"
2428 "{\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002429
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00002430 geomHLSL += varyingHLSL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002431
2432 if (fragmentShader->mUsesFragCoord)
2433 {
2434 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2435 }
2436
2437 if (fragmentShader->mUsesPointCoord)
2438 {
2439 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
2440 }
2441
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002442 geomHLSL += " float gl_PointSize : PSIZE;\n"
2443 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002444 "};\n"
2445 "\n"
2446 "static float2 pointSpriteCorners[] = \n"
2447 "{\n"
2448 " float2( 0.5f, -0.5f),\n"
2449 " float2( 0.5f, 0.5f),\n"
2450 " float2(-0.5f, -0.5f),\n"
2451 " float2(-0.5f, 0.5f)\n"
2452 "};\n"
2453 "\n"
2454 "static float2 pointSpriteTexcoords[] = \n"
2455 "{\n"
2456 " float2(1.0f, 1.0f),\n"
2457 " float2(1.0f, 0.0f),\n"
2458 " float2(0.0f, 1.0f),\n"
2459 " float2(0.0f, 0.0f)\n"
2460 "};\n"
2461 "\n"
2462 "static float minPointSize = " + str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
2463 "static float maxPointSize = " + str(mRenderer->getMaxPointSize()) + ".0f;\n"
2464 "\n"
2465 "[maxvertexcount(4)]\n"
2466 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
2467 "{\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002468 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
2469 " output.gl_PointSize = input[0].gl_PointSize;\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002470
2471 for (int r = 0; r < registers; r++)
2472 {
2473 geomHLSL += " output.v" + str(r) + " = input[0].v" + str(r) + ";\n";
2474 }
2475
2476 if (fragmentShader->mUsesFragCoord)
2477 {
2478 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
2479 }
2480
2481 geomHLSL += " \n"
2482 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
2483 " float4 gl_Position = input[0].gl_Position;\n"
shannon.woods@transgaming.com771ca2a2013-02-28 23:14:52 +00002484 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002485
2486 for (int corner = 0; corner < 4; corner++)
2487 {
2488 geomHLSL += " \n"
2489 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
2490
2491 if (fragmentShader->mUsesPointCoord)
2492 {
2493 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + str(corner) + "];\n";
2494 }
2495
2496 geomHLSL += " outStream.Append(output);\n";
2497 }
2498
2499 geomHLSL += " \n"
2500 " outStream.RestartStrip();\n"
2501 "}\n";
2502
2503 return geomHLSL;
2504}
2505
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002506// This method needs to match OutputHLSL::decorate
2507std::string ProgramBinary::decorateAttribute(const std::string &name)
2508{
2509 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2510 {
2511 return "_" + name;
2512 }
2513
2514 return name;
2515}
2516
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002517bool ProgramBinary::isValidated() const
2518{
2519 return mValidated;
2520}
2521
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002522void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002523{
2524 // Skip over inactive attributes
2525 unsigned int activeAttribute = 0;
2526 unsigned int attribute;
2527 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2528 {
2529 if (mLinkedAttribute[attribute].name.empty())
2530 {
2531 continue;
2532 }
2533
2534 if (activeAttribute == index)
2535 {
2536 break;
2537 }
2538
2539 activeAttribute++;
2540 }
2541
2542 if (bufsize > 0)
2543 {
2544 const char *string = mLinkedAttribute[attribute].name.c_str();
2545
2546 strncpy(name, string, bufsize);
2547 name[bufsize - 1] = '\0';
2548
2549 if (length)
2550 {
2551 *length = strlen(name);
2552 }
2553 }
2554
2555 *size = 1; // Always a single 'type' instance
2556
2557 *type = mLinkedAttribute[attribute].type;
2558}
2559
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002560GLint ProgramBinary::getActiveAttributeCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002561{
2562 int count = 0;
2563
2564 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2565 {
2566 if (!mLinkedAttribute[attributeIndex].name.empty())
2567 {
2568 count++;
2569 }
2570 }
2571
2572 return count;
2573}
2574
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002575GLint ProgramBinary::getActiveAttributeMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002576{
2577 int maxLength = 0;
2578
2579 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2580 {
2581 if (!mLinkedAttribute[attributeIndex].name.empty())
2582 {
2583 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2584 }
2585 }
2586
2587 return maxLength;
2588}
2589
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002590void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002591{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002592 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002593
2594 if (bufsize > 0)
2595 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002596 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002597
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002598 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002599 {
2600 string += "[0]";
2601 }
2602
2603 strncpy(name, string.c_str(), bufsize);
2604 name[bufsize - 1] = '\0';
2605
2606 if (length)
2607 {
2608 *length = strlen(name);
2609 }
2610 }
2611
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002612 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002613
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002614 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002615}
2616
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002617GLint ProgramBinary::getActiveUniformCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002618{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002619 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002620}
2621
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002622GLint ProgramBinary::getActiveUniformMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002623{
2624 int maxLength = 0;
2625
2626 unsigned int numUniforms = mUniforms.size();
2627 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2628 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002629 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002630 {
2631 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2632 if (mUniforms[uniformIndex]->isArray())
2633 {
2634 length += 3; // Counting in "[0]".
2635 }
2636 maxLength = std::max(length, maxLength);
2637 }
2638 }
2639
2640 return maxLength;
2641}
2642
shannonwoods@chromium.org2a9a9d22013-05-30 00:05:40 +00002643GLint ProgramBinary::getActiveUniformi(GLuint index, GLenum pname) const
2644{
2645 const gl::Uniform& uniform = *mUniforms[index];
2646
2647 switch (pname)
2648 {
2649 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
2650 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
2651 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00002652 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
shannonwoods@chromium.org2a9a9d22013-05-30 00:05:40 +00002653
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00002654 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
2655 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
2656 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
2657 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
shannonwoods@chromium.org2a9a9d22013-05-30 00:05:40 +00002658
2659 default:
2660 UNREACHABLE();
2661 break;
2662 }
2663 return 0;
2664}
2665
shannonwoods@chromium.orgbeb02782013-05-30 00:07:28 +00002666void ProgramBinary::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
2667{
2668 ASSERT(uniformBlockIndex < mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
2669
2670 const UniformBlock &uniformBlock = *mUniformBlocks[uniformBlockIndex];
2671
2672 if (bufSize > 0)
2673 {
2674 std::string string = uniformBlock.name;
2675
2676 if (uniformBlock.isArrayElement())
2677 {
2678 string += "[" + str(uniformBlock.elementIndex) + "]";
2679 }
2680
2681 strncpy(uniformBlockName, string.c_str(), bufSize);
2682 uniformBlockName[bufSize - 1] = '\0';
2683
2684 if (length)
2685 {
2686 *length = strlen(uniformBlockName);
2687 }
2688 }
2689}
2690
shannonwoods@chromium.orge7317ca2013-05-30 00:07:35 +00002691void ProgramBinary::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
2692{
2693 ASSERT(uniformBlockIndex < mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
2694
2695 const UniformBlock &uniformBlock = *mUniformBlocks[uniformBlockIndex];
2696
2697 switch (pname)
2698 {
2699 case GL_UNIFORM_BLOCK_DATA_SIZE:
2700 *params = static_cast<GLint>(uniformBlock.dataSize);
2701 break;
2702 case GL_UNIFORM_BLOCK_NAME_LENGTH:
2703 *params = static_cast<GLint>(uniformBlock.name.size() + 1);
2704 break;
2705 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
2706 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
2707 break;
2708 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
2709 {
2710 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
2711 {
2712 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
2713 }
2714 }
2715 break;
2716 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
2717 *params = static_cast<GLint>(uniformBlock.isReferencedByVertexShader());
2718 break;
2719 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
2720 *params = static_cast<GLint>(uniformBlock.isReferencedByFragmentShader());
2721 break;
2722 default: UNREACHABLE();
2723 }
2724}
2725
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002726GLuint ProgramBinary::getActiveUniformBlockCount() const
2727{
2728 return mUniformBlocks.size();
2729}
2730
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002731GLuint ProgramBinary::getActiveUniformBlockMaxLength() const
2732{
2733 unsigned int maxLength = 0;
2734
2735 unsigned int numUniformBlocks = mUniformBlocks.size();
2736 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
2737 {
2738 const UniformBlock &uniformBlock = *mUniformBlocks[uniformBlockIndex];
2739 if (!uniformBlock.name.empty())
2740 {
2741 const unsigned int length = uniformBlock.name.length() + 1;
2742
2743 // Counting in "[0]".
2744 const unsigned int arrayLength = (uniformBlock.isArrayElement() ? 3 : 0);
2745
2746 maxLength = std::max(length + arrayLength, maxLength);
2747 }
2748 }
2749
2750 return maxLength;
2751}
2752
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002753void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002754{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002755 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002756 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002757 {
2758 mValidated = false;
2759 }
2760 else
2761 {
2762 mValidated = true;
2763 }
2764}
2765
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002766bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002767{
2768 // if any two active samplers in a program are of different types, but refer to the same
2769 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2770 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2771
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002772 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002773 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002774
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002775 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002776 {
2777 textureUnitType[i] = TEXTURE_UNKNOWN;
2778 }
2779
2780 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2781 {
2782 if (mSamplersPS[i].active)
2783 {
2784 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2785
2786 if (unit >= maxCombinedTextureImageUnits)
2787 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002788 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002789 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002790 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002791 }
2792
2793 return false;
2794 }
2795
2796 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2797 {
2798 if (mSamplersPS[i].textureType != textureUnitType[unit])
2799 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002800 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002801 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002802 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002803 }
2804
2805 return false;
2806 }
2807 }
2808 else
2809 {
2810 textureUnitType[unit] = mSamplersPS[i].textureType;
2811 }
2812 }
2813 }
2814
2815 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2816 {
2817 if (mSamplersVS[i].active)
2818 {
2819 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2820
2821 if (unit >= maxCombinedTextureImageUnits)
2822 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002823 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002824 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002825 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002826 }
2827
2828 return false;
2829 }
2830
2831 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2832 {
2833 if (mSamplersVS[i].textureType != textureUnitType[unit])
2834 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002835 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002836 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002837 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002838 }
2839
2840 return false;
2841 }
2842 }
2843 else
2844 {
2845 textureUnitType[unit] = mSamplersVS[i].textureType;
2846 }
2847 }
2848 }
2849
2850 return true;
2851}
2852
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002853ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2854{
2855}
2856
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002857struct AttributeSorter
2858{
2859 AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS])
2860 : originalIndices(semanticIndices)
2861 {
2862 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2863 {
2864 indices[i] = i;
2865 }
2866
2867 std::sort(&indices[0], &indices[MAX_VERTEX_ATTRIBS], *this);
2868 }
2869
2870 bool operator()(int a, int b)
2871 {
2872 return originalIndices[a] == -1 ? false : originalIndices[a] < originalIndices[b];
2873 }
2874
2875 int indices[MAX_VERTEX_ATTRIBS];
2876 const int (&originalIndices)[MAX_VERTEX_ATTRIBS];
2877};
2878
2879void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
2880{
2881 AttributeSorter sorter(mSemanticIndex);
2882
2883 int oldIndices[MAX_VERTEX_ATTRIBS];
2884 rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
2885
2886 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2887 {
2888 oldIndices[i] = mSemanticIndex[i];
2889 oldTranslatedAttributes[i] = attributes[i];
2890 }
2891
2892 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2893 {
2894 int oldIndex = sorter.indices[i];
2895 sortedSemanticIndices[i] = oldIndices[oldIndex];
2896 attributes[i] = oldTranslatedAttributes[oldIndex];
2897 }
2898}
2899
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002900}