blob: 8525ffbd3cbc8a23a6749cf52be1188e6efc6e22 [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//
Geoff Lang48dcae72014-02-05 16:28:24 -05003// Copyright (c) 2002-2014 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"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000017#include "common/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"
Nicolas Capense6050882013-07-08 10:43:10 -040024#include "libGLESv2/Context.h"
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +000025#include "libGLESv2/Buffer.h"
Jamie Madill5f562732014-02-14 16:41:24 -050026#include "libGLESv2/DynamicHLSL.h"
Jamie Madillc600c8c2014-05-16 11:22:21 -040027#include "common/blocklayout.h"
Jamie Madillc2141fb2013-08-30 13:21:08 -040028
daniel@transgaming.com88853c52012-12-20 20:56:40 +000029#undef near
30#undef far
31
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000032namespace gl
33{
shannonwoods@chromium.orgb4e07b82013-05-30 00:19:31 +000034
Jamie Madill1b2a8f92014-05-14 13:09:39 -040035namespace
shannonwoods@chromium.org6d7b61c2013-05-30 00:06:38 +000036{
37
Jamie Madill8ff21ae2014-02-04 16:04:05 -050038unsigned int ParseAndStripArrayIndex(std::string* name)
shannonwoods@chromium.org6d7b61c2013-05-30 00:06:38 +000039{
40 unsigned int subscript = GL_INVALID_INDEX;
41
42 // Strip any trailing array operator and retrieve the subscript
43 size_t open = name->find_last_of('[');
44 size_t close = name->find_last_of(']');
45 if (open != std::string::npos && close == name->length() - 1)
46 {
47 subscript = atoi(name->substr(open + 1).c_str());
48 name->erase(open);
49 }
50
51 return subscript;
52}
53
Jamie Madill834e8b72014-04-11 13:33:58 -040054void GetInputLayoutFromShader(const std::vector<gl::Attribute> &shaderAttributes, VertexFormat inputLayout[MAX_VERTEX_ATTRIBS])
Jamie Madillc5a83002014-02-14 16:41:25 -050055{
Jamie Madill3b7e2052014-03-17 09:47:43 -040056 size_t layoutIndex = 0;
Jamie Madillc5a83002014-02-14 16:41:25 -050057 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
58 {
Jamie Madill3b7e2052014-03-17 09:47:43 -040059 ASSERT(layoutIndex < MAX_VERTEX_ATTRIBS);
60
Jamie Madill834e8b72014-04-11 13:33:58 -040061 const gl::Attribute &shaderAttr = shaderAttributes[attributeIndex];
Jamie Madillc5a83002014-02-14 16:41:25 -050062
63 if (shaderAttr.type != GL_NONE)
64 {
Jamie Madillac0a2672014-04-11 13:33:56 -040065 GLenum transposedType = TransposeMatrixType(shaderAttr.type);
66
67 for (size_t rowIndex = 0; static_cast<int>(rowIndex) < VariableRowCount(transposedType); rowIndex++, layoutIndex++)
Jamie Madill3b7e2052014-03-17 09:47:43 -040068 {
69 VertexFormat *defaultFormat = &inputLayout[layoutIndex];
70
Jamie Madillac0a2672014-04-11 13:33:56 -040071 defaultFormat->mType = UniformComponentType(transposedType);
Jamie Madill3b7e2052014-03-17 09:47:43 -040072 defaultFormat->mNormalized = false;
73 defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool
Jamie Madillac0a2672014-04-11 13:33:56 -040074 defaultFormat->mComponents = VariableColumnCount(transposedType);
Jamie Madill3b7e2052014-03-17 09:47:43 -040075 }
Jamie Madillc5a83002014-02-14 16:41:25 -050076 }
77 }
78}
79
shannonwoods@chromium.org6d7b61c2013-05-30 00:06:38 +000080}
81
Jamie Madill1b2a8f92014-05-14 13:09:39 -040082VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index)
daniel@transgaming.comdb019952012-12-20 21:13:32 +000083 : name(name), element(element), index(index)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000084{
85}
86
Jamie Madillc5a83002014-02-14 16:41:25 -050087ProgramBinary::VertexExecutable::VertexExecutable(rx::Renderer *const renderer,
88 const VertexFormat inputLayout[],
Jamie Madill7a29e4a2014-05-02 10:41:48 -040089 const GLenum signature[],
Jamie Madillc5a83002014-02-14 16:41:25 -050090 rx::ShaderExecutable *shaderExecutable)
91 : mShaderExecutable(shaderExecutable)
92{
93 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
94 {
95 mInputs[attributeIndex] = inputLayout[attributeIndex];
Jamie Madill7a29e4a2014-05-02 10:41:48 -040096 mSignature[attributeIndex] = signature[attributeIndex];
Jamie Madillc5a83002014-02-14 16:41:25 -050097 }
98}
99
Jamie Madill8b4f8f82014-03-26 14:01:58 -0400100ProgramBinary::VertexExecutable::~VertexExecutable()
101{
102 delete mShaderExecutable;
103}
104
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400105bool ProgramBinary::VertexExecutable::matchesSignature(const GLenum signature[]) const
Jamie Madillc5a83002014-02-14 16:41:25 -0500106{
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400107 for (size_t attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
Jamie Madillc5a83002014-02-14 16:41:25 -0500108 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400109 if (mSignature[attributeIndex] != signature[attributeIndex])
Jamie Madillc5a83002014-02-14 16:41:25 -0500110 {
111 return false;
112 }
113 }
114
115 return true;
116}
117
Geoff Lang48dcae72014-02-05 16:28:24 -0500118LinkedVarying::LinkedVarying()
119{
120}
121
122LinkedVarying::LinkedVarying(const std::string &name, GLenum type, GLsizei size, const std::string &semanticName,
123 unsigned int semanticIndex, unsigned int semanticIndexCount)
124 : name(name), type(type), size(size), semanticName(semanticName), semanticIndex(semanticIndex), semanticIndexCount(semanticIndexCount)
125{
126}
127
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000128unsigned int ProgramBinary::mCurrentSerial = 1;
129
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500130ProgramBinary::ProgramBinary(rx::Renderer *renderer)
131 : RefCountObject(0),
132 mRenderer(renderer),
Jamie Madill5f562732014-02-14 16:41:24 -0500133 mDynamicHLSL(NULL),
Jamie Madillc5a83002014-02-14 16:41:25 -0500134 mVertexWorkarounds(rx::ANGLE_D3D_WORKAROUND_NONE),
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500135 mPixelExecutable(NULL),
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500136 mGeometryExecutable(NULL),
137 mUsedVertexSamplerRange(0),
138 mUsedPixelSamplerRange(0),
139 mUsesPointSize(false),
140 mShaderVersion(100),
141 mVertexUniformStorage(NULL),
142 mFragmentUniformStorage(NULL),
143 mValidated(false),
144 mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000145{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000146 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
147 {
148 mSemanticIndex[index] = -1;
149 }
150
151 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
152 {
153 mSamplersPS[index].active = false;
154 }
155
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000156 for (int index = 0; index < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000157 {
158 mSamplersVS[index].active = false;
159 }
Jamie Madill5f562732014-02-14 16:41:24 -0500160
161 mDynamicHLSL = new DynamicHLSL(renderer);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000162}
163
164ProgramBinary::~ProgramBinary()
165{
Jamie Madillc5a83002014-02-14 16:41:25 -0500166 while (!mVertexExecutables.empty())
167 {
168 delete mVertexExecutables.back();
169 mVertexExecutables.pop_back();
170 }
171
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500172 SafeDelete(mGeometryExecutable);
Jamie Madillc5a83002014-02-14 16:41:25 -0500173 SafeDelete(mPixelExecutable);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000174
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000175 while (!mUniforms.empty())
176 {
177 delete mUniforms.back();
178 mUniforms.pop_back();
179 }
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +0000180
181 while (!mUniformBlocks.empty())
182 {
183 delete mUniformBlocks.back();
184 mUniformBlocks.pop_back();
185 }
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500186
187 SafeDelete(mVertexUniformStorage);
188 SafeDelete(mFragmentUniformStorage);
Jamie Madill5f562732014-02-14 16:41:24 -0500189 SafeDelete(mDynamicHLSL);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000190}
191
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000192unsigned int ProgramBinary::getSerial() const
193{
194 return mSerial;
195}
196
shannonwoods@chromium.org83ac5e82013-05-30 00:15:29 +0000197int ProgramBinary::getShaderVersion() const
198{
199 return mShaderVersion;
200}
201
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000202unsigned int ProgramBinary::issueSerial()
203{
204 return mCurrentSerial++;
205}
206
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500207rx::ShaderExecutable *ProgramBinary::getPixelExecutable() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000208{
209 return mPixelExecutable;
210}
211
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400212rx::ShaderExecutable *ProgramBinary::getVertexExecutableForInputLayout(const VertexFormat inputLayout[MAX_VERTEX_ATTRIBS])
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000213{
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400214 GLenum signature[MAX_VERTEX_ATTRIBS];
215 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
216
Jamie Madillc5a83002014-02-14 16:41:25 -0500217 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
218 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400219 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
Jamie Madillc5a83002014-02-14 16:41:25 -0500220 {
221 return mVertexExecutables[executableIndex]->shaderExecutable();
222 }
223 }
224
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500225 // Generate new dynamic layout with attribute conversions
226 const std::string &layoutHLSL = mDynamicHLSL->generateInputLayoutHLSL(inputLayout, mShaderAttributes);
227
228 // Generate new shader source by replacing the attributes stub with the defined input layout
229 std::string vertexHLSL = mVertexHLSL;
230 size_t insertPos = vertexHLSL.find(DynamicHLSL::VERTEX_ATTRIBUTE_STUB_STRING);
231 vertexHLSL.replace(insertPos, DynamicHLSL::VERTEX_ATTRIBUTE_STUB_STRING.length(), layoutHLSL);
232
Jamie Madillc5a83002014-02-14 16:41:25 -0500233 // Generate new vertex executable
234 InfoLog tempInfoLog;
Geoff Lang48dcae72014-02-05 16:28:24 -0500235 rx::ShaderExecutable *vertexExecutable = mRenderer->compileToExecutable(tempInfoLog, vertexHLSL.c_str(),
236 rx::SHADER_VERTEX,
237 mTransformFeedbackLinkedVaryings,
238 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
239 mVertexWorkarounds);
Jamie Madillc5a83002014-02-14 16:41:25 -0500240
241 if (!vertexExecutable)
242 {
243 std::vector<char> tempCharBuffer(tempInfoLog.getLength()+3);
244 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
245 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
246 }
247 else
248 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400249 mVertexExecutables.push_back(new VertexExecutable(mRenderer, inputLayout, signature, vertexExecutable));
Jamie Madillc5a83002014-02-14 16:41:25 -0500250 }
251
252 return vertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000253}
254
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500255rx::ShaderExecutable *ProgramBinary::getGeometryExecutable() const
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000256{
257 return mGeometryExecutable;
258}
259
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000260GLuint ProgramBinary::getAttributeLocation(const char *name)
261{
262 if (name)
263 {
264 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
265 {
266 if (mLinkedAttribute[index].name == std::string(name))
267 {
268 return index;
269 }
270 }
271 }
272
273 return -1;
274}
275
276int ProgramBinary::getSemanticIndex(int attributeIndex)
277{
278 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400279
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000280 return mSemanticIndex[attributeIndex];
281}
282
283// Returns one more than the highest sampler index used.
284GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
285{
286 switch (type)
287 {
288 case SAMPLER_PIXEL:
289 return mUsedPixelSamplerRange;
290 case SAMPLER_VERTEX:
291 return mUsedVertexSamplerRange;
292 default:
293 UNREACHABLE();
294 return 0;
295 }
296}
297
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000298bool ProgramBinary::usesPointSize() const
299{
300 return mUsesPointSize;
301}
302
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000303bool ProgramBinary::usesPointSpriteEmulation() const
304{
305 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
306}
307
308bool ProgramBinary::usesGeometryShader() const
309{
310 return usesPointSpriteEmulation();
311}
312
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000313// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
314// index (0-15 for the pixel shader and 0-3 for the vertex shader).
315GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
316{
317 GLint logicalTextureUnit = -1;
318
319 switch (type)
320 {
321 case SAMPLER_PIXEL:
322 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
323
324 if (mSamplersPS[samplerIndex].active)
325 {
326 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
327 }
328 break;
329 case SAMPLER_VERTEX:
330 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
331
332 if (mSamplersVS[samplerIndex].active)
333 {
334 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
335 }
336 break;
337 default: UNREACHABLE();
338 }
339
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000340 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)mRenderer->getMaxCombinedTextureImageUnits())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000341 {
342 return logicalTextureUnit;
343 }
344
345 return -1;
346}
347
348// Returns the texture type for a given Direct3D 9 sampler type and
349// index (0-15 for the pixel shader and 0-3 for the vertex shader).
350TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
351{
352 switch (type)
353 {
354 case SAMPLER_PIXEL:
355 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
356 ASSERT(mSamplersPS[samplerIndex].active);
357 return mSamplersPS[samplerIndex].textureType;
358 case SAMPLER_VERTEX:
359 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
360 ASSERT(mSamplersVS[samplerIndex].active);
361 return mSamplersVS[samplerIndex].textureType;
362 default: UNREACHABLE();
363 }
364
365 return TEXTURE_2D;
366}
367
368GLint ProgramBinary::getUniformLocation(std::string name)
369{
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500370 unsigned int subscript = ParseAndStripArrayIndex(&name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000371
372 unsigned int numUniforms = mUniformIndex.size();
373 for (unsigned int location = 0; location < numUniforms; location++)
374 {
shannonwoods@chromium.org0ee85f82013-05-30 00:05:47 +0000375 if (mUniformIndex[location].name == name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000376 {
shannonwoods@chromium.org0ee85f82013-05-30 00:05:47 +0000377 const int index = mUniformIndex[location].index;
378 const bool isArray = mUniforms[index]->isArray();
379
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400380 if ((isArray && mUniformIndex[location].element == subscript) ||
shannonwoods@chromium.org0ee85f82013-05-30 00:05:47 +0000381 (subscript == GL_INVALID_INDEX))
382 {
383 return location;
384 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000385 }
386 }
387
388 return -1;
389}
390
shannonwoods@chromium.orgc2ed9912013-05-30 00:05:33 +0000391GLuint ProgramBinary::getUniformIndex(std::string name)
392{
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500393 unsigned int subscript = ParseAndStripArrayIndex(&name);
shannonwoods@chromium.orgc2ed9912013-05-30 00:05:33 +0000394
395 // The app is not allowed to specify array indices other than 0 for arrays of basic types
396 if (subscript != 0 && subscript != GL_INVALID_INDEX)
397 {
398 return GL_INVALID_INDEX;
399 }
400
401 unsigned int numUniforms = mUniforms.size();
402 for (unsigned int index = 0; index < numUniforms; index++)
403 {
404 if (mUniforms[index]->name == name)
405 {
406 if (mUniforms[index]->isArray() || subscript == GL_INVALID_INDEX)
407 {
408 return index;
409 }
410 }
411 }
412
413 return GL_INVALID_INDEX;
414}
415
shannonwoods@chromium.org42766252013-05-30 00:07:12 +0000416GLuint ProgramBinary::getUniformBlockIndex(std::string name)
417{
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500418 unsigned int subscript = ParseAndStripArrayIndex(&name);
shannonwoods@chromium.org42766252013-05-30 00:07:12 +0000419
420 unsigned int numUniformBlocks = mUniformBlocks.size();
421 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
422 {
423 const UniformBlock &uniformBlock = *mUniformBlocks[blockIndex];
424 if (uniformBlock.name == name)
425 {
426 const bool arrayElementZero = (subscript == GL_INVALID_INDEX && uniformBlock.elementIndex == 0);
427 if (subscript == uniformBlock.elementIndex || arrayElementZero)
428 {
429 return blockIndex;
430 }
431 }
432 }
433
434 return GL_INVALID_INDEX;
435}
436
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000437UniformBlock *ProgramBinary::getUniformBlockByIndex(GLuint blockIndex)
438{
439 ASSERT(blockIndex < mUniformBlocks.size());
440 return mUniformBlocks[blockIndex];
441}
442
Jamie Madilld1e78c92013-06-20 11:55:50 -0400443GLint ProgramBinary::getFragDataLocation(const char *name) const
444{
445 std::string baseName(name);
446 unsigned int arrayIndex;
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500447 arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madilld1e78c92013-06-20 11:55:50 -0400448
449 for (auto locationIt = mOutputVariables.begin(); locationIt != mOutputVariables.end(); locationIt++)
450 {
451 const VariableLocation &outputVariable = locationIt->second;
452
453 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
454 {
455 return static_cast<GLint>(locationIt->first);
456 }
457 }
458
459 return -1;
460}
461
Geoff Lang48dcae72014-02-05 16:28:24 -0500462size_t ProgramBinary::getTransformFeedbackVaryingCount() const
463{
464 return mTransformFeedbackLinkedVaryings.size();
465}
466
467const LinkedVarying &ProgramBinary::getTransformFeedbackVarying(size_t idx) const
468{
469 return mTransformFeedbackLinkedVaryings[idx];
470}
471
472GLenum ProgramBinary::getTransformFeedbackBufferMode() const
473{
474 return mTransformFeedbackBufferMode;
475}
476
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000477template <typename T>
Geoff Langae1990c2014-05-12 16:57:14 -0400478static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
479{
480 ASSERT(dest != NULL);
481 ASSERT(dirtyFlag != NULL);
482
483 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
484 *dest = source;
485}
486
487template <typename T>
Jamie Madill36398922014-05-20 14:51:53 -0400488void ProgramBinary::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000489{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000490 const int components = UniformComponentCount(targetUniformType);
491 const GLenum targetBoolType = UniformBoolVectorType(targetUniformType);
492
Jamie Madill36398922014-05-20 14:51:53 -0400493 LinkedUniform *targetUniform = getUniformByLocation(location);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000494
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000495 int elementCount = targetUniform->elementCount();
496
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000497 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
498
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000499 if (targetUniform->type == targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000500 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000501 T *target = (T*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000502
503 for (int i = 0; i < count; i++)
504 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000505 for (int c = 0; c < components; c++)
506 {
Geoff Langae1990c2014-05-12 16:57:14 -0400507 SetIfDirty(target + c, v[c], &targetUniform->dirty);
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000508 }
509 for (int c = components; c < 4; c++)
510 {
Geoff Langae1990c2014-05-12 16:57:14 -0400511 SetIfDirty(target + c, T(0), &targetUniform->dirty);
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000512 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000513 target += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000514 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000515 }
516 }
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000517 else if (targetUniform->type == targetBoolType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000518 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000519 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000520
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000521 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000522 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000523 for (int c = 0; c < components; c++)
524 {
Geoff Langae1990c2014-05-12 16:57:14 -0400525 SetIfDirty(boolParams + c, (v[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000526 }
527 for (int c = components; c < 4; c++)
528 {
Geoff Langae1990c2014-05-12 16:57:14 -0400529 SetIfDirty(boolParams + c, GL_FALSE, &targetUniform->dirty);
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000530 }
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000531 boolParams += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000532 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000533 }
534 }
Jamie Madill36398922014-05-20 14:51:53 -0400535 else UNREACHABLE();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000536}
537
Jamie Madill36398922014-05-20 14:51:53 -0400538void ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000539{
Jamie Madill36398922014-05-20 14:51:53 -0400540 setUniform(location, count, v, GL_FLOAT);
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000541}
542
Jamie Madill36398922014-05-20 14:51:53 -0400543void ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000544{
Jamie Madill36398922014-05-20 14:51:53 -0400545 setUniform(location, count, v, GL_FLOAT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000546}
547
Jamie Madill36398922014-05-20 14:51:53 -0400548void ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000549{
Jamie Madill36398922014-05-20 14:51:53 -0400550 setUniform(location, count, v, GL_FLOAT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000551}
552
Jamie Madill36398922014-05-20 14:51:53 -0400553void ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000554{
Jamie Madill36398922014-05-20 14:51:53 -0400555 setUniform(location, count, v, GL_FLOAT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000556}
557
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000558template<typename T>
Geoff Langae1990c2014-05-12 16:57:14 -0400559bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000560{
Geoff Langae1990c2014-05-12 16:57:14 -0400561 bool dirty = false;
shannonwoods@chromium.org2c2b82b2013-05-30 00:15:12 +0000562 int copyWidth = std::min(targetHeight, srcWidth);
563 int copyHeight = std::min(targetWidth, srcHeight);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000564
565 for (int x = 0; x < copyWidth; x++)
566 {
567 for (int y = 0; y < copyHeight; y++)
568 {
Geoff Langae1990c2014-05-12 16:57:14 -0400569 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000570 }
571 }
572 // clear unfilled right side
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000573 for (int y = 0; y < copyWidth; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000574 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000575 for (int x = copyHeight; x < targetWidth; x++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000576 {
Geoff Langae1990c2014-05-12 16:57:14 -0400577 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000578 }
579 }
580 // clear unfilled bottom.
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000581 for (int y = copyWidth; y < targetHeight; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000582 {
583 for (int x = 0; x < targetWidth; x++)
584 {
Geoff Langae1990c2014-05-12 16:57:14 -0400585 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000586 }
587 }
Geoff Langae1990c2014-05-12 16:57:14 -0400588
589 return dirty;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000590}
591
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000592template<typename T>
Geoff Langae1990c2014-05-12 16:57:14 -0400593bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000594{
Geoff Langae1990c2014-05-12 16:57:14 -0400595 bool dirty = false;
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000596 int copyWidth = std::min(targetWidth, srcWidth);
597 int copyHeight = std::min(targetHeight, srcHeight);
598
599 for (int y = 0; y < copyHeight; y++)
600 {
601 for (int x = 0; x < copyWidth; x++)
602 {
Geoff Langae1990c2014-05-12 16:57:14 -0400603 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000604 }
605 }
606 // clear unfilled right side
607 for (int y = 0; y < copyHeight; y++)
608 {
609 for (int x = copyWidth; x < targetWidth; x++)
610 {
Geoff Langae1990c2014-05-12 16:57:14 -0400611 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000612 }
613 }
614 // clear unfilled bottom.
615 for (int y = copyHeight; y < targetHeight; y++)
616 {
617 for (int x = 0; x < targetWidth; x++)
618 {
Geoff Langae1990c2014-05-12 16:57:14 -0400619 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000620 }
621 }
Geoff Langae1990c2014-05-12 16:57:14 -0400622
623 return dirty;
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000624}
625
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000626template <int cols, int rows>
Jamie Madill36398922014-05-20 14:51:53 -0400627void ProgramBinary::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000628{
Jamie Madill36398922014-05-20 14:51:53 -0400629 LinkedUniform *targetUniform = getUniformByLocation(location);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000630
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000631 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000632
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000633 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannonwoods@chromium.org2c2b82b2013-05-30 00:15:12 +0000634 const unsigned int targetMatrixStride = (4 * rows);
635 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000636
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000637 for (int i = 0; i < count; i++)
638 {
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000639 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
640 if (transpose == GL_FALSE)
641 {
Geoff Langae1990c2014-05-12 16:57:14 -0400642 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000643 }
644 else
645 {
Geoff Langae1990c2014-05-12 16:57:14 -0400646 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000647 }
shannonwoods@chromium.org2c2b82b2013-05-30 00:15:12 +0000648 target += targetMatrixStride;
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000649 value += cols * rows;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000650 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000651}
652
Jamie Madill36398922014-05-20 14:51:53 -0400653void ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000654{
Jamie Madill36398922014-05-20 14:51:53 -0400655 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000656}
657
Jamie Madill36398922014-05-20 14:51:53 -0400658void ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000659{
Jamie Madill36398922014-05-20 14:51:53 -0400660 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000661}
662
Jamie Madill36398922014-05-20 14:51:53 -0400663void ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000664{
Jamie Madill36398922014-05-20 14:51:53 -0400665 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000666}
667
Jamie Madill36398922014-05-20 14:51:53 -0400668void ProgramBinary::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000669{
Jamie Madill36398922014-05-20 14:51:53 -0400670 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000671}
672
Jamie Madill36398922014-05-20 14:51:53 -0400673void ProgramBinary::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000674{
Jamie Madill36398922014-05-20 14:51:53 -0400675 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000676}
677
Jamie Madill36398922014-05-20 14:51:53 -0400678void ProgramBinary::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000679{
Jamie Madill36398922014-05-20 14:51:53 -0400680 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000681}
682
Jamie Madill36398922014-05-20 14:51:53 -0400683void ProgramBinary::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000684{
Jamie Madill36398922014-05-20 14:51:53 -0400685 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000686}
687
Jamie Madill36398922014-05-20 14:51:53 -0400688void ProgramBinary::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000689{
Jamie Madill36398922014-05-20 14:51:53 -0400690 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000691}
692
Jamie Madill36398922014-05-20 14:51:53 -0400693void ProgramBinary::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000694{
Jamie Madill36398922014-05-20 14:51:53 -0400695 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000696}
697
Jamie Madill36398922014-05-20 14:51:53 -0400698void ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000699{
Jamie Madill834e8b72014-04-11 13:33:58 -0400700 LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000701
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000702 int elementCount = targetUniform->elementCount();
703
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000704 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
705
Nicolas Capense6050882013-07-08 10:43:10 -0400706 if (targetUniform->type == GL_INT || IsSampler(targetUniform->type))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000707 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000708 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000709
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000710 for (int i = 0; i < count; i++)
711 {
Geoff Langae1990c2014-05-12 16:57:14 -0400712 SetIfDirty(target + 0, v[0], &targetUniform->dirty);
713 SetIfDirty(target + 1, 0, &targetUniform->dirty);
714 SetIfDirty(target + 2, 0, &targetUniform->dirty);
715 SetIfDirty(target + 3, 0, &targetUniform->dirty);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000716 target += 4;
717 v += 1;
718 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000719 }
720 else if (targetUniform->type == GL_BOOL)
721 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000722 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000723
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000724 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000725 {
Geoff Langae1990c2014-05-12 16:57:14 -0400726 SetIfDirty(boolParams + 0, (v[0] == 0) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
727 SetIfDirty(boolParams + 1, GL_FALSE, &targetUniform->dirty);
728 SetIfDirty(boolParams + 2, GL_FALSE, &targetUniform->dirty);
729 SetIfDirty(boolParams + 3, GL_FALSE, &targetUniform->dirty);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000730 boolParams += 4;
731 v += 1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000732 }
733 }
Jamie Madill36398922014-05-20 14:51:53 -0400734 else UNREACHABLE();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000735}
736
Jamie Madill36398922014-05-20 14:51:53 -0400737void ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000738{
Jamie Madill36398922014-05-20 14:51:53 -0400739 setUniform(location, count, v, GL_INT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000740}
741
Jamie Madill36398922014-05-20 14:51:53 -0400742void ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000743{
Jamie Madill36398922014-05-20 14:51:53 -0400744 setUniform(location, count, v, GL_INT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000745}
746
Jamie Madill36398922014-05-20 14:51:53 -0400747void ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000748{
Jamie Madill36398922014-05-20 14:51:53 -0400749 setUniform(location, count, v, GL_INT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000750}
751
Jamie Madill36398922014-05-20 14:51:53 -0400752void ProgramBinary::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000753{
Jamie Madill36398922014-05-20 14:51:53 -0400754 setUniform(location, count, v, GL_UNSIGNED_INT);
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000755}
756
Jamie Madill36398922014-05-20 14:51:53 -0400757void ProgramBinary::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000758{
Jamie Madill36398922014-05-20 14:51:53 -0400759 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000760}
761
Jamie Madill36398922014-05-20 14:51:53 -0400762void ProgramBinary::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000763{
Jamie Madill36398922014-05-20 14:51:53 -0400764 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000765}
766
Jamie Madill36398922014-05-20 14:51:53 -0400767void ProgramBinary::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000768{
Jamie Madill36398922014-05-20 14:51:53 -0400769 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000770}
771
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000772template <typename T>
773bool ProgramBinary::getUniformv(GLint location, GLsizei *bufSize, T *params, GLenum uniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000774{
Jamie Madill834e8b72014-04-11 13:33:58 -0400775 LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000776
777 // sized queries -- ensure the provided buffer is large enough
778 if (bufSize)
779 {
780 int requiredBytes = UniformExternalSize(targetUniform->type);
781 if (*bufSize < requiredBytes)
782 {
783 return false;
784 }
785 }
786
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000787 if (IsMatrixType(targetUniform->type))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000788 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000789 const int rows = VariableRowCount(targetUniform->type);
790 const int cols = VariableColumnCount(targetUniform->type);
Jamie Madillf07558a2013-10-31 11:16:22 -0400791 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000792 }
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000793 else if (uniformType == UniformComponentType(targetUniform->type))
794 {
795 unsigned int size = UniformComponentCount(targetUniform->type);
796 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
797 size * sizeof(T));
798 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000799 else
800 {
801 unsigned int size = UniformComponentCount(targetUniform->type);
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000802 switch (UniformComponentType(targetUniform->type))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000803 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000804 case GL_BOOL:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000805 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000806 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000807
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000808 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000809 {
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000810 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000811 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000812 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000813 break;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000814
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000815 case GL_FLOAT:
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000816 {
817 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
818
819 for (unsigned int i = 0; i < size; i++)
820 {
821 params[i] = static_cast<T>(floatParams[i]);
822 }
823 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000824 break;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000825
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000826 case GL_INT:
827 {
828 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
829
830 for (unsigned int i = 0; i < size; i++)
831 {
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000832 params[i] = static_cast<T>(intParams[i]);
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000833 }
834 }
835 break;
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400836
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000837 case GL_UNSIGNED_INT:
838 {
839 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000840
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000841 for (unsigned int i = 0; i < size; i++)
842 {
843 params[i] = static_cast<T>(uintParams[i]);
844 }
845 }
846 break;
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400847
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000848 default: UNREACHABLE();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000849 }
850 }
851
852 return true;
853}
854
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000855bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
856{
857 return getUniformv(location, bufSize, params, GL_FLOAT);
858}
859
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000860bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
861{
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000862 return getUniformv(location, bufSize, params, GL_INT);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000863}
864
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000865bool ProgramBinary::getUniformuiv(GLint location, GLsizei *bufSize, GLuint *params)
866{
867 return getUniformv(location, bufSize, params, GL_UNSIGNED_INT);
868}
869
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000870void ProgramBinary::dirtyAllUniforms()
871{
872 unsigned int numUniforms = mUniforms.size();
873 for (unsigned int index = 0; index < numUniforms; index++)
874 {
875 mUniforms[index]->dirty = true;
876 }
877}
878
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000879// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000880void ProgramBinary::applyUniforms()
881{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000882 // Retrieve sampler uniform values
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500883 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000884 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400885 LinkedUniform *targetUniform = mUniforms[uniformIndex];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000886
887 if (targetUniform->dirty)
888 {
Nicolas Capense6050882013-07-08 10:43:10 -0400889 if (IsSampler(targetUniform->type))
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000890 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000891 int count = targetUniform->elementCount();
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000892 GLint (*v)[4] = (GLint(*)[4])targetUniform->data;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000893
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +0000894 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000895 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000896 unsigned int firstIndex = targetUniform->psRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000897
898 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000899 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000900 unsigned int samplerIndex = firstIndex + i;
901
902 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000903 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000904 ASSERT(mSamplersPS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000905 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000906 }
907 }
908 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000909
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +0000910 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000911 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000912 unsigned int firstIndex = targetUniform->vsRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000913
914 for (int i = 0; i < count; i++)
915 {
916 unsigned int samplerIndex = firstIndex + i;
917
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000918 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000919 {
920 ASSERT(mSamplersVS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000921 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000922 }
923 }
924 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000925 }
926 }
927 }
928
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500929 mRenderer->applyUniforms(*this);
930
931 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
932 {
933 mUniforms[uniformIndex]->dirty = false;
934 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000935}
936
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000937bool ProgramBinary::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers)
938{
939 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
940 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
941
942 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
943 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
944
945 ASSERT(boundBuffers.size() == mUniformBlocks.size());
946
947 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
948 {
949 gl::UniformBlock *uniformBlock = getUniformBlockByIndex(uniformBlockIndex);
950 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
951
952 ASSERT(uniformBlock && uniformBuffer);
953
954 if (uniformBuffer->size() < uniformBlock->dataSize)
955 {
956 // undefined behaviour
957 return false;
958 }
959
960 ASSERT(uniformBlock->isReferencedByVertexShader() || uniformBlock->isReferencedByFragmentShader());
961
962 if (uniformBlock->isReferencedByVertexShader())
963 {
964 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
965 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
966 ASSERT(registerIndex < mRenderer->getMaxVertexShaderUniformBuffers());
967 vertexUniformBuffers[registerIndex] = uniformBuffer;
968 }
969
970 if (uniformBlock->isReferencedByFragmentShader())
971 {
972 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
973 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
974 ASSERT(registerIndex < mRenderer->getMaxFragmentShaderUniformBuffers());
975 fragmentUniformBuffers[registerIndex] = uniformBuffer;
976 }
977 }
978
979 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
980}
981
Jamie Madill5f562732014-02-14 16:41:24 -0500982bool ProgramBinary::linkVaryings(InfoLog &infoLog, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000983{
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400984 std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
985 std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
Jamie Madill5f562732014-02-14 16:41:24 -0500986
987 for (size_t fragVaryingIndex = 0; fragVaryingIndex < fragmentVaryings.size(); fragVaryingIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000988 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400989 PackedVarying *input = &fragmentVaryings[fragVaryingIndex];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000990 bool matched = false;
991
Jamie Madill5f562732014-02-14 16:41:24 -0500992 for (size_t vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000993 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400994 PackedVarying *output = &vertexVaryings[vertVaryingIndex];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000995 if (output->name == input->name)
996 {
Jamie Madill28167c62013-08-30 13:21:10 -0400997 if (!linkValidateVariables(infoLog, output->name, *input, *output))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000998 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000999 return false;
1000 }
1001
Jamie Madill139b9092013-08-30 13:21:06 -04001002 output->registerIndex = input->registerIndex;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001003
1004 matched = true;
1005 break;
1006 }
1007 }
1008
1009 if (!matched)
1010 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001011 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001012 return false;
1013 }
1014 }
1015
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001016 return true;
1017}
1018
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001019bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1020{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001021 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001022
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001023 int format = stream.readInt<int>();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001024 if (format != GL_PROGRAM_BINARY_ANGLE)
1025 {
1026 infoLog.append("Invalid program binary format.");
1027 return false;
1028 }
1029
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001030 int majorVersion = stream.readInt<int>();
1031 int minorVersion = stream.readInt<int>();
Jamie Madill0aa84f62014-02-13 13:17:23 -05001032 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
Jamie Madill049108d2013-11-19 10:41:49 -05001033 {
1034 infoLog.append("Invalid program binary version.");
1035 return false;
1036 }
1037
Jamie Madill0aa84f62014-02-13 13:17:23 -05001038 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001039 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
Jamie Madill0aa84f62014-02-13 13:17:23 -05001040 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001041 {
1042 infoLog.append("Invalid program binary version.");
1043 return false;
1044 }
1045
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001046 int compileFlags = stream.readInt<int>();
shannonwoods@chromium.orgf97a0842013-05-30 00:10:33 +00001047 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
1048 {
1049 infoLog.append("Mismatched compilation flags.");
1050 return false;
1051 }
1052
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001053 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1054 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001055 stream.readInt(&mLinkedAttribute[i].type);
1056 stream.readString(&mLinkedAttribute[i].name);
1057 stream.readInt(&mShaderAttributes[i].type);
1058 stream.readString(&mShaderAttributes[i].name);
1059 stream.readInt(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001060 }
1061
Al Patrick3f2daa82013-08-07 12:58:57 -07001062 initAttributesByLayout();
1063
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001064 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1065 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001066 stream.readBool(&mSamplersPS[i].active);
1067 stream.readInt(&mSamplersPS[i].logicalTextureUnit);
1068 stream.readInt(&mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001069 }
1070
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001071 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001072 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001073 stream.readBool(&mSamplersVS[i].active);
1074 stream.readInt(&mSamplersVS[i].logicalTextureUnit);
1075 stream.readInt(&mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001076 }
1077
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001078 stream.readInt(&mUsedVertexSamplerRange);
1079 stream.readInt(&mUsedPixelSamplerRange);
1080 stream.readBool(&mUsesPointSize);
1081 stream.readInt(&mShaderVersion);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001082
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001083 const unsigned int uniformCount = stream.readInt<unsigned int>();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001084 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001085 {
1086 infoLog.append("Invalid program binary.");
1087 return false;
1088 }
1089
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001090 mUniforms.resize(uniformCount);
1091 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001092 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001093 GLenum type = stream.readInt<GLenum>();
1094 GLenum precision = stream.readInt<GLenum>();
1095 std::string name = stream.readString();
1096 unsigned int arraySize = stream.readInt<unsigned int>();
1097 int blockIndex = stream.readInt<int>();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001098
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001099 int offset = stream.readInt<int>();
1100 int arrayStride = stream.readInt<int>();
1101 int matrixStride = stream.readInt<int>();
1102 bool isRowMajorMatrix = stream.readBool();
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001103
Jamie Madill834e8b72014-04-11 13:33:58 -04001104 const gl::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001105
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001106 LinkedUniform *uniform = new LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
1107
1108 stream.readInt(&uniform->psRegisterIndex);
1109 stream.readInt(&uniform->vsRegisterIndex);
1110 stream.readInt(&uniform->registerCount);
1111 stream.readInt(&uniform->registerElement);
1112
1113 mUniforms[uniformIndex] = uniform;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001114 }
1115
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001116 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001117 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001118 {
1119 infoLog.append("Invalid program binary.");
1120 return false;
1121 }
1122
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001123 mUniformBlocks.resize(uniformBlockCount);
1124 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001125 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001126 std::string name = stream.readString();
1127 unsigned int elementIndex = stream.readInt<unsigned int>();
1128 unsigned int dataSize = stream.readInt<unsigned int>();
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001129
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001130 UniformBlock *uniformBlock = new UniformBlock(name, elementIndex, dataSize);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001131
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001132 stream.readInt(&uniformBlock->psRegisterIndex);
1133 stream.readInt(&uniformBlock->vsRegisterIndex);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001134
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001135 unsigned int numMembers = stream.readInt<unsigned int>();
1136 uniformBlock->memberUniformIndexes.resize(numMembers);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001137 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
1138 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001139 stream.readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001140 }
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001141
1142 mUniformBlocks[uniformBlockIndex] = uniformBlock;
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001143 }
1144
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001145 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001146 if (stream.error())
1147 {
1148 infoLog.append("Invalid program binary.");
1149 return false;
1150 }
1151
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001152 mUniformIndex.resize(uniformIndexCount);
1153 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001154 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001155 stream.readString(&mUniformIndex[uniformIndexIndex].name);
1156 stream.readInt(&mUniformIndex[uniformIndexIndex].element);
1157 stream.readInt(&mUniformIndex[uniformIndexIndex].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001158 }
1159
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001160 stream.readInt(&mTransformFeedbackBufferMode);
1161 const unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
1162 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
1163 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -05001164 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001165 LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -05001166
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001167 stream.readString(&varying.name);
1168 stream.readInt(&varying.type);
1169 stream.readInt(&varying.size);
1170 stream.readString(&varying.semanticName);
1171 stream.readInt(&varying.semanticIndex);
1172 stream.readInt(&varying.semanticIndexCount);
Geoff Lang48dcae72014-02-05 16:28:24 -05001173 }
1174
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001175 stream.readString(&mVertexHLSL);
Jamie Madillc5a83002014-02-14 16:41:25 -05001176
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001177 stream.readInt(&mVertexWorkarounds);
1178
1179 const unsigned int vertexShaderCount = stream.readInt<unsigned int>();
Jamie Madillc5a83002014-02-14 16:41:25 -05001180
1181 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
1182 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001183 VertexFormat inputLayout[MAX_VERTEX_ATTRIBS];
Jamie Madillc5a83002014-02-14 16:41:25 -05001184
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001185 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
Jamie Madillc5a83002014-02-14 16:41:25 -05001186 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001187 VertexFormat *vertexInput = &inputLayout[inputIndex];
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001188 stream.readInt(&vertexInput->mType);
1189 stream.readInt(&vertexInput->mNormalized);
1190 stream.readInt(&vertexInput->mComponents);
1191 stream.readBool(&vertexInput->mPureInteger);
Jamie Madillc5a83002014-02-14 16:41:25 -05001192 }
1193
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001194 unsigned int vertexShaderSize = stream.readInt<unsigned int>();
Jamie Madillc5a83002014-02-14 16:41:25 -05001195
1196 const char *vertexShaderFunction = (const char*) binary + stream.offset();
1197
1198 rx::ShaderExecutable *shaderExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
Geoff Lang48dcae72014-02-05 16:28:24 -05001199 vertexShaderSize, rx::SHADER_VERTEX,
1200 mTransformFeedbackLinkedVaryings,
1201 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS));
Jamie Madillc5a83002014-02-14 16:41:25 -05001202 if (!shaderExecutable)
1203 {
1204 infoLog.append("Could not create vertex shader.");
1205 return false;
1206 }
1207
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001208 // generated converted input layout
1209 GLenum signature[MAX_VERTEX_ATTRIBS];
1210 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1211
1212 // add new binary
1213 mVertexExecutables.push_back(new VertexExecutable(mRenderer, inputLayout, signature, shaderExecutable));
Jamie Madillc5a83002014-02-14 16:41:25 -05001214
1215 stream.skip(vertexShaderSize);
1216 }
1217
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001218 unsigned int pixelShaderSize = stream.readInt<unsigned int>();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001219
Jamie Madillc5a83002014-02-14 16:41:25 -05001220 const char *pixelShaderFunction = (const char*) binary + stream.offset();
1221 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
Geoff Lang48dcae72014-02-05 16:28:24 -05001222 pixelShaderSize, rx::SHADER_PIXEL, mTransformFeedbackLinkedVaryings,
1223 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS));
Jamie Madillc5a83002014-02-14 16:41:25 -05001224 if (!mPixelExecutable)
1225 {
1226 infoLog.append("Could not create pixel shader.");
1227 return false;
1228 }
1229 stream.skip(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001230
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001231 unsigned int geometryShaderSize = stream.readInt<unsigned int>();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001232
Jamie Madillc5a83002014-02-14 16:41:25 -05001233 if (geometryShaderSize > 0)
1234 {
1235 const char *geometryShaderFunction = (const char*) binary + stream.offset();
1236 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction),
Geoff Lang48dcae72014-02-05 16:28:24 -05001237 geometryShaderSize, rx::SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1238 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS));
Jamie Madillc5a83002014-02-14 16:41:25 -05001239 if (!mGeometryExecutable)
1240 {
1241 infoLog.append("Could not create geometry shader.");
1242 SafeDelete(mPixelExecutable);
1243 return false;
1244 }
1245 stream.skip(geometryShaderSize);
1246 }
1247
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001248 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001249
daniel@transgaming.com36038542012-11-28 20:59:26 +00001250 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001251 ptr += sizeof(GUID);
1252
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001253 GUID identifier = mRenderer->getAdapterIdentifier();
1254 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001255 {
1256 infoLog.append("Invalid program binary.");
1257 return false;
1258 }
1259
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001260 initializeUniformStorage();
1261
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001262 return true;
1263}
1264
1265bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1266{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001267 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001268
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001269 stream.writeInt(GL_PROGRAM_BINARY_ANGLE);
1270 stream.writeInt(ANGLE_MAJOR_VERSION);
1271 stream.writeInt(ANGLE_MINOR_VERSION);
1272 stream.writeBytes(reinterpret_cast<unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
1273 stream.writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001274
1275 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1276 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001277 stream.writeInt(mLinkedAttribute[i].type);
1278 stream.writeString(mLinkedAttribute[i].name);
1279 stream.writeInt(mShaderAttributes[i].type);
1280 stream.writeString(mShaderAttributes[i].name);
1281 stream.writeInt(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001282 }
1283
1284 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1285 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001286 stream.writeInt(mSamplersPS[i].active);
1287 stream.writeInt(mSamplersPS[i].logicalTextureUnit);
1288 stream.writeInt(mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001289 }
1290
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001291 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001292 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001293 stream.writeInt(mSamplersVS[i].active);
1294 stream.writeInt(mSamplersVS[i].logicalTextureUnit);
1295 stream.writeInt(mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001296 }
1297
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001298 stream.writeInt(mUsedVertexSamplerRange);
1299 stream.writeInt(mUsedPixelSamplerRange);
1300 stream.writeInt(mUsesPointSize);
1301 stream.writeInt(mShaderVersion);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001302
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001303 stream.writeInt(mUniforms.size());
Jamie Madillc5a83002014-02-14 16:41:25 -05001304 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001305 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001306 const LinkedUniform &uniform = *mUniforms[uniformIndex];
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001307
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001308 stream.writeInt(uniform.type);
1309 stream.writeInt(uniform.precision);
1310 stream.writeString(uniform.name);
1311 stream.writeInt(uniform.arraySize);
1312 stream.writeInt(uniform.blockIndex);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001313
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001314 stream.writeInt(uniform.blockInfo.offset);
1315 stream.writeInt(uniform.blockInfo.arrayStride);
1316 stream.writeInt(uniform.blockInfo.matrixStride);
1317 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001318
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001319 stream.writeInt(uniform.psRegisterIndex);
1320 stream.writeInt(uniform.vsRegisterIndex);
1321 stream.writeInt(uniform.registerCount);
1322 stream.writeInt(uniform.registerElement);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001323 }
1324
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001325 stream.writeInt(mUniformBlocks.size());
Jamie Madillc5a83002014-02-14 16:41:25 -05001326 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001327 {
1328 const UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
1329
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001330 stream.writeString(uniformBlock.name);
1331 stream.writeInt(uniformBlock.elementIndex);
1332 stream.writeInt(uniformBlock.dataSize);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001333
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001334 stream.writeInt(uniformBlock.memberUniformIndexes.size());
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001335 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1336 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001337 stream.writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00001338 }
1339
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001340 stream.writeInt(uniformBlock.psRegisterIndex);
1341 stream.writeInt(uniformBlock.vsRegisterIndex);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001342 }
1343
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001344 stream.writeInt(mUniformIndex.size());
Jamie Madillc5a83002014-02-14 16:41:25 -05001345 for (size_t i = 0; i < mUniformIndex.size(); ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001346 {
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001347 stream.writeString(mUniformIndex[i].name);
1348 stream.writeInt(mUniformIndex[i].element);
1349 stream.writeInt(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001350 }
1351
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001352 stream.writeInt(mTransformFeedbackBufferMode);
1353 stream.writeInt(mTransformFeedbackLinkedVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001354 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
1355 {
1356 const LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
1357
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001358 stream.writeString(varying.name);
1359 stream.writeInt(varying.type);
1360 stream.writeInt(varying.size);
1361 stream.writeString(varying.semanticName);
1362 stream.writeInt(varying.semanticIndex);
1363 stream.writeInt(varying.semanticIndexCount);
Geoff Lang48dcae72014-02-05 16:28:24 -05001364 }
1365
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001366 stream.writeString(mVertexHLSL);
1367 stream.writeInt(mVertexWorkarounds);
Jamie Madillc5a83002014-02-14 16:41:25 -05001368
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001369 stream.writeInt(mVertexExecutables.size());
Jamie Madillc5a83002014-02-14 16:41:25 -05001370 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
1371 {
1372 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1373
1374 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
1375 {
1376 const VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001377 stream.writeInt(vertexInput.mType);
1378 stream.writeInt(vertexInput.mNormalized);
1379 stream.writeInt(vertexInput.mComponents);
1380 stream.writeInt(vertexInput.mPureInteger);
Jamie Madillc5a83002014-02-14 16:41:25 -05001381 }
1382
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001383 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1384 stream.writeInt(vertexShaderSize);
Jamie Madillc5a83002014-02-14 16:41:25 -05001385
1386 unsigned char *vertexBlob = static_cast<unsigned char *>(vertexExecutable->shaderExecutable()->getFunction());
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001387 stream.writeBytes(vertexBlob, vertexShaderSize);
Jamie Madillc5a83002014-02-14 16:41:25 -05001388 }
1389
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001390 size_t pixelShaderSize = mPixelExecutable->getLength();
1391 stream.writeInt(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001392
Jamie Madillc5a83002014-02-14 16:41:25 -05001393 unsigned char *pixelBlob = static_cast<unsigned char *>(mPixelExecutable->getFunction());
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001394 stream.writeBytes(pixelBlob, pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001395
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001396 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
1397 stream.writeInt(geometryShaderSize);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001398
Jamie Madillc5a83002014-02-14 16:41:25 -05001399 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
1400 {
1401 unsigned char *geometryBlob = static_cast<unsigned char *>(mGeometryExecutable->getFunction());
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001402 stream.writeBytes(geometryBlob, geometryShaderSize);
Jamie Madillc5a83002014-02-14 16:41:25 -05001403 }
1404
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001405 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001406
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001407 GLsizei streamLength = stream.length();
1408 const void *streamData = stream.data();
1409
Jamie Madillc5a83002014-02-14 16:41:25 -05001410 GLsizei totalLength = streamLength + sizeof(GUID);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001411 if (totalLength > bufSize)
1412 {
1413 if (length)
1414 {
1415 *length = 0;
1416 }
1417
1418 return false;
1419 }
1420
1421 if (binary)
1422 {
1423 char *ptr = (char*) binary;
1424
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001425 memcpy(ptr, streamData, streamLength);
1426 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001427
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001428 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001429 ptr += sizeof(GUID);
1430
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001431 ASSERT(ptr - totalLength == binary);
1432 }
1433
1434 if (length)
1435 {
1436 *length = totalLength;
1437 }
1438
1439 return true;
1440}
1441
1442GLint ProgramBinary::getLength()
1443{
1444 GLint length;
1445 if (save(NULL, INT_MAX, &length))
1446 {
1447 return length;
1448 }
1449 else
1450 {
1451 return 0;
1452 }
1453}
1454
Geoff Lang48dcae72014-02-05 16:28:24 -05001455bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader,
1456 const std::vector<std::string>& transformFeedbackVaryings, GLenum transformFeedbackBufferMode)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001457{
1458 if (!fragmentShader || !fragmentShader->isCompiled())
1459 {
1460 return false;
1461 }
1462
1463 if (!vertexShader || !vertexShader->isCompiled())
1464 {
1465 return false;
1466 }
1467
Geoff Lang48dcae72014-02-05 16:28:24 -05001468 mTransformFeedbackLinkedVaryings.clear();
1469 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
1470
shannonwoods@chromium.org83ac5e82013-05-30 00:15:29 +00001471 mShaderVersion = vertexShader->getShaderVersion();
1472
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001473 std::string pixelHLSL = fragmentShader->getHLSL();
Jamie Madillc5a83002014-02-14 16:41:25 -05001474 mVertexHLSL = vertexShader->getHLSL();
1475 mVertexWorkarounds = vertexShader->getD3DWorkarounds();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001476
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001477 // Map the varyings to the register file
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001478 VaryingPacking packing = { NULL };
Geoff Lang48dcae72014-02-05 16:28:24 -05001479 int registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShader, vertexShader, transformFeedbackVaryings);
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001480
1481 if (registers < 0)
1482 {
1483 return false;
1484 }
1485
Jamie Madill5f562732014-02-14 16:41:24 -05001486 if (!linkVaryings(infoLog, fragmentShader, vertexShader))
1487 {
1488 return false;
1489 }
1490
1491 mUsesPointSize = vertexShader->usesPointSize();
Geoff Lang48dcae72014-02-05 16:28:24 -05001492 std::vector<LinkedVarying> linkedVaryings;
Jamie Madillc5a83002014-02-14 16:41:25 -05001493 if (!mDynamicHLSL->generateShaderLinkHLSL(infoLog, registers, packing, pixelHLSL, mVertexHLSL,
Geoff Lang48dcae72014-02-05 16:28:24 -05001494 fragmentShader, vertexShader, transformFeedbackVaryings,
1495 &linkedVaryings, &mOutputVariables))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001496 {
1497 return false;
1498 }
1499
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001500 bool success = true;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001501
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001502 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1503 {
1504 success = false;
1505 }
1506
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001507 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001508 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001509 success = false;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001510 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001511
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001512 // special case for gl_DepthRange, the only built-in uniform (also a struct)
Jamie Madill5f562732014-02-14 16:41:24 -05001513 if (vertexShader->usesDepthRange() || fragmentShader->usesDepthRange())
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001514 {
Jamie Madill07d68852014-05-06 16:31:14 -04001515 mUniforms.push_back(new LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, BlockMemberInfo::getDefaultBlockInfo()));
1516 mUniforms.push_back(new LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, BlockMemberInfo::getDefaultBlockInfo()));
1517 mUniforms.push_back(new LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, BlockMemberInfo::getDefaultBlockInfo()));
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001518 }
1519
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00001520 if (!linkUniformBlocks(infoLog, vertexShader->getInterfaceBlocks(), fragmentShader->getInterfaceBlocks()))
1521 {
1522 success = false;
1523 }
1524
Geoff Lang48dcae72014-02-05 16:28:24 -05001525 if (!gatherTransformFeedbackLinkedVaryings(infoLog, linkedVaryings, transformFeedbackVaryings,
1526 transformFeedbackBufferMode, &mTransformFeedbackLinkedVaryings))
1527 {
1528 success = false;
1529 }
1530
Jamie Madilld1ac3c92013-06-25 10:40:30 -04001531 if (success)
1532 {
Jamie Madillc5a83002014-02-14 16:41:25 -05001533 VertexFormat defaultInputLayout[MAX_VERTEX_ATTRIBS];
1534 GetInputLayoutFromShader(vertexShader->activeAttributes(), defaultInputLayout);
1535
1536 rx::ShaderExecutable *defaultVertexExecutable = getVertexExecutableForInputLayout(defaultInputLayout);
Geoff Lang48dcae72014-02-05 16:28:24 -05001537 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL,
1538 mTransformFeedbackLinkedVaryings,
1539 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
1540 fragmentShader->getD3DWorkarounds());
Jamie Madilld1ac3c92013-06-25 10:40:30 -04001541
1542 if (usesGeometryShader())
1543 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001544 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShader, vertexShader);
Geoff Lang48dcae72014-02-05 16:28:24 -05001545 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY,
1546 mTransformFeedbackLinkedVaryings,
1547 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
1548 rx::ANGLE_D3D_WORKAROUND_NONE);
Jamie Madilld1ac3c92013-06-25 10:40:30 -04001549 }
1550
Jamie Madillc5a83002014-02-14 16:41:25 -05001551 if (!defaultVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable))
Jamie Madilld1ac3c92013-06-25 10:40:30 -04001552 {
1553 infoLog.append("Failed to create D3D shaders.");
1554 success = false;
1555
Jamie Madillc5a83002014-02-14 16:41:25 -05001556 while (!mVertexExecutables.empty())
1557 {
1558 delete mVertexExecutables.back();
1559 mVertexExecutables.pop_back();
1560 }
1561
1562 SafeDelete(mGeometryExecutable);
1563 SafeDelete(mPixelExecutable);
Geoff Lang48dcae72014-02-05 16:28:24 -05001564
1565 mTransformFeedbackLinkedVaryings.clear();
Jamie Madilld1ac3c92013-06-25 10:40:30 -04001566 }
1567 }
1568
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001569 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001570}
1571
1572// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001573bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001574{
1575 unsigned int usedLocations = 0;
Jamie Madill834e8b72014-04-11 13:33:58 -04001576 const std::vector<gl::Attribute> &activeAttributes = vertexShader->activeAttributes();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001577
1578 // Link attributes that have a binding location
Jamie Madilldefb6742013-06-20 11:55:51 -04001579 for (unsigned int attributeIndex = 0; attributeIndex < activeAttributes.size(); attributeIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001580 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001581 const gl::Attribute &attribute = activeAttributes[attributeIndex];
Jamie Madilleba4eff2013-06-20 11:55:51 -04001582 const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001583
Jamie Madill2c7d84a2014-02-14 16:41:26 -05001584 mShaderAttributes[attributeIndex] = attribute;
1585
Jamie Madilleba4eff2013-06-20 11:55:51 -04001586 if (location != -1) // Set by glBindAttribLocation or by location layout qualifier
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001587 {
Jamie Madilleba4eff2013-06-20 11:55:51 -04001588 const int rows = AttributeRegisterCount(attribute.type);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001589
1590 if (rows + location > MAX_VERTEX_ATTRIBS)
1591 {
Jamie Madilldefb6742013-06-20 11:55:51 -04001592 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 +00001593
1594 return false;
1595 }
1596
Jamie Madilleba4eff2013-06-20 11:55:51 -04001597 for (int row = 0; row < rows; row++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001598 {
Jamie Madilleba4eff2013-06-20 11:55:51 -04001599 const int rowLocation = location + row;
Jamie Madill834e8b72014-04-11 13:33:58 -04001600 gl::ShaderVariable &linkedAttribute = mLinkedAttribute[rowLocation];
Jamie Madilleba4eff2013-06-20 11:55:51 -04001601
1602 // In GLSL 3.00, attribute aliasing produces a link error
1603 // In GLSL 1.00, attribute aliasing is allowed
1604 if (mShaderVersion >= 300)
1605 {
1606 if (!linkedAttribute.name.empty())
1607 {
1608 infoLog.append("Attribute '%s' aliases attribute '%s' at location %d", attribute.name.c_str(), linkedAttribute.name.c_str(), rowLocation);
1609 return false;
1610 }
1611 }
1612
1613 linkedAttribute = attribute;
1614 usedLocations |= 1 << rowLocation;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001615 }
1616 }
1617 }
1618
1619 // Link attributes that don't have a binding location
Jamie Madilldefb6742013-06-20 11:55:51 -04001620 for (unsigned int attributeIndex = 0; attributeIndex < activeAttributes.size(); attributeIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001621 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001622 const gl::Attribute &attribute = activeAttributes[attributeIndex];
Jamie Madilleba4eff2013-06-20 11:55:51 -04001623 const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001624
Jamie Madilleba4eff2013-06-20 11:55:51 -04001625 if (location == -1) // Not set by glBindAttribLocation or by location layout qualifier
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001626 {
Jamie Madilldefb6742013-06-20 11:55:51 -04001627 int rows = AttributeRegisterCount(attribute.type);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001628 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1629
1630 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1631 {
Jamie Madilldefb6742013-06-20 11:55:51 -04001632 infoLog.append("Too many active attributes (%s)", attribute.name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001633
1634 return false; // Fail to link
1635 }
1636
Jamie Madilldefb6742013-06-20 11:55:51 -04001637 mLinkedAttribute[availableIndex] = attribute;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001638 }
1639 }
1640
1641 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1642 {
1643 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
Jamie Madillbb22e502013-06-20 11:55:51 -04001644 int rows = AttributeRegisterCount(mLinkedAttribute[attributeIndex].type);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001645
1646 for (int r = 0; r < rows; r++)
1647 {
1648 mSemanticIndex[attributeIndex++] = index++;
1649 }
1650 }
1651
Al Patrick3f2daa82013-08-07 12:58:57 -07001652 initAttributesByLayout();
1653
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001654 return true;
1655}
1656
Jamie Madill834e8b72014-04-11 13:33:58 -04001657bool ProgramBinary::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const gl::ShaderVariable &vertexVariable, const gl::ShaderVariable &fragmentVariable, bool validatePrecision)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001658{
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001659 if (vertexVariable.type != fragmentVariable.type)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001660 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001661 infoLog.append("Types for %s differ between vertex and fragment shaders", variableName.c_str());
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001662 return false;
1663 }
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001664 if (vertexVariable.arraySize != fragmentVariable.arraySize)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001665 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001666 infoLog.append("Array sizes for %s differ between vertex and fragment shaders", variableName.c_str());
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001667 return false;
1668 }
Jamie Madill28167c62013-08-30 13:21:10 -04001669 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001670 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001671 infoLog.append("Precisions for %s differ between vertex and fragment shaders", variableName.c_str());
Jamie Madill010fffa2013-06-20 11:55:53 -04001672 return false;
1673 }
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001674
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001675 return true;
1676}
1677
1678template <class ShaderVarType>
1679bool ProgramBinary::linkValidateFields(InfoLog &infoLog, const std::string &varName, const ShaderVarType &vertexVar, const ShaderVarType &fragmentVar)
1680{
1681 if (vertexVar.fields.size() != fragmentVar.fields.size())
1682 {
1683 infoLog.append("Structure lengths for %s differ between vertex and fragment shaders", varName.c_str());
1684 return false;
1685 }
1686 const unsigned int numMembers = vertexVar.fields.size();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001687 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1688 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001689 const ShaderVarType &vertexMember = vertexVar.fields[memberIndex];
1690 const ShaderVarType &fragmentMember = fragmentVar.fields[memberIndex];
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001691
1692 if (vertexMember.name != fragmentMember.name)
1693 {
Jamie Madill28167c62013-08-30 13:21:10 -04001694 infoLog.append("Name mismatch for field '%d' of %s: (in vertex: '%s', in fragment: '%s')",
1695 memberIndex, varName.c_str(), vertexMember.name.c_str(), fragmentMember.name.c_str());
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001696 return false;
1697 }
1698
Jamie Madill28167c62013-08-30 13:21:10 -04001699 const std::string memberName = varName.substr(0, varName.length()-1) + "." + vertexVar.name + "'";
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001700 if (!linkValidateVariables(infoLog, memberName, vertexMember, fragmentMember))
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001701 {
1702 return false;
1703 }
1704 }
1705
1706 return true;
1707}
1708
Jamie Madill834e8b72014-04-11 13:33:58 -04001709bool ProgramBinary::linkValidateVariables(InfoLog &infoLog, const std::string &uniformName, const gl::Uniform &vertexUniform, const gl::Uniform &fragmentUniform)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001710{
Jamie Madill28167c62013-08-30 13:21:10 -04001711 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001712 {
1713 return false;
1714 }
1715
Jamie Madill834e8b72014-04-11 13:33:58 -04001716 if (!linkValidateFields<gl::Uniform>(infoLog, uniformName, vertexUniform, fragmentUniform))
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001717 {
1718 return false;
1719 }
1720
1721 return true;
1722}
1723
Jamie Madill834e8b72014-04-11 13:33:58 -04001724bool ProgramBinary::linkValidateVariables(InfoLog &infoLog, const std::string &varyingName, const gl::Varying &vertexVarying, const gl::Varying &fragmentVarying)
Jamie Madill28167c62013-08-30 13:21:10 -04001725{
1726 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
1727 {
1728 return false;
1729 }
1730
1731 if (vertexVarying.interpolation != fragmentVarying.interpolation)
1732 {
1733 infoLog.append("Interpolation types for %s differ between vertex and fragment shaders", varyingName.c_str());
1734 return false;
1735 }
1736
Jamie Madill834e8b72014-04-11 13:33:58 -04001737 if (!linkValidateFields<gl::Varying>(infoLog, varyingName, vertexVarying, fragmentVarying))
Jamie Madill28167c62013-08-30 13:21:10 -04001738 {
1739 return false;
1740 }
1741
1742 return true;
1743}
1744
Jamie Madill834e8b72014-04-11 13:33:58 -04001745bool ProgramBinary::linkValidateVariables(InfoLog &infoLog, const std::string &uniformName, const gl::InterfaceBlockField &vertexUniform, const gl::InterfaceBlockField &fragmentUniform)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001746{
Jamie Madill28167c62013-08-30 13:21:10 -04001747 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001748 {
1749 return false;
1750 }
1751
1752 if (vertexUniform.isRowMajorMatrix != fragmentUniform.isRowMajorMatrix)
1753 {
1754 infoLog.append("Matrix packings for %s differ between vertex and fragment shaders", uniformName.c_str());
1755 return false;
1756 }
1757
Jamie Madill834e8b72014-04-11 13:33:58 -04001758 if (!linkValidateFields<gl::InterfaceBlockField>(infoLog, uniformName, vertexUniform, fragmentUniform))
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001759 {
1760 return false;
1761 }
1762
1763 return true;
1764}
1765
Jamie Madill834e8b72014-04-11 13:33:58 -04001766bool ProgramBinary::linkUniforms(InfoLog &infoLog, const std::vector<gl::Uniform> &vertexUniforms, const std::vector<gl::Uniform> &fragmentUniforms)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001767{
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001768 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill834e8b72014-04-11 13:33:58 -04001769 typedef std::map<std::string, const gl::Uniform*> UniformMap;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001770 UniformMap linkedUniforms;
1771
1772 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
1773 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001774 const gl::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001775 linkedUniforms[vertexUniform.name] = &vertexUniform;
1776 }
1777
1778 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1779 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001780 const gl::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001781 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1782 if (entry != linkedUniforms.end())
1783 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001784 const gl::Uniform &vertexUniform = *entry->second;
Jamie Madill28167c62013-08-30 13:21:10 -04001785 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Jamie Madill9d2ffb12013-08-30 13:21:04 -04001786 if (!linkValidateVariables(infoLog, uniformName, vertexUniform, fragmentUniform))
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001787 {
1788 return false;
1789 }
1790 }
1791 }
1792
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00001793 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001794 {
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00001795 if (!defineUniform(GL_VERTEX_SHADER, vertexUniforms[uniformIndex], infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001796 {
1797 return false;
1798 }
1799 }
1800
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00001801 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001802 {
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00001803 if (!defineUniform(GL_FRAGMENT_SHADER, fragmentUniforms[uniformIndex], infoLog))
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001804 {
1805 return false;
1806 }
1807 }
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001808
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001809 initializeUniformStorage();
1810
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001811 return true;
1812}
1813
Nicolas Capens43e8ba82013-07-09 10:35:15 -04001814TextureType ProgramBinary::getTextureType(GLenum samplerType, InfoLog &infoLog)
1815{
1816 switch(samplerType)
1817 {
1818 case GL_SAMPLER_2D:
1819 case GL_INT_SAMPLER_2D:
1820 case GL_UNSIGNED_INT_SAMPLER_2D:
Nicolas Capenscb127d32013-07-15 17:26:18 -04001821 case GL_SAMPLER_2D_SHADOW:
Nicolas Capens43e8ba82013-07-09 10:35:15 -04001822 return TEXTURE_2D;
1823 case GL_SAMPLER_3D:
1824 case GL_INT_SAMPLER_3D:
1825 case GL_UNSIGNED_INT_SAMPLER_3D:
1826 return TEXTURE_3D;
1827 case GL_SAMPLER_CUBE:
Nicolas Capenscb127d32013-07-15 17:26:18 -04001828 case GL_SAMPLER_CUBE_SHADOW:
Nicolas Capens43e8ba82013-07-09 10:35:15 -04001829 return TEXTURE_CUBE;
1830 case GL_INT_SAMPLER_CUBE:
1831 case GL_UNSIGNED_INT_SAMPLER_CUBE:
Nicolas Capens43e8ba82013-07-09 10:35:15 -04001832 return TEXTURE_CUBE;
1833 case GL_SAMPLER_2D_ARRAY:
1834 case GL_INT_SAMPLER_2D_ARRAY:
1835 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
Nicolas Capenscb127d32013-07-15 17:26:18 -04001836 case GL_SAMPLER_2D_ARRAY_SHADOW:
Nicolas Capens43e8ba82013-07-09 10:35:15 -04001837 return TEXTURE_2D_ARRAY;
1838 default: UNREACHABLE();
1839 }
1840
1841 return TEXTURE_2D;
1842}
1843
Jamie Madill834e8b72014-04-11 13:33:58 -04001844bool ProgramBinary::defineUniform(GLenum shader, const gl::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001845{
Jamie Madillfcef3362013-11-13 19:37:19 -05001846 if (constant.isStruct())
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001847 {
1848 if (constant.arraySize > 0)
1849 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04001850 ShShaderOutput outputType = Shader::getCompilerOutputType(shader);
1851 const unsigned int elementRegisterCount = HLSLVariableRegisterCount(constant, outputType) / constant.arraySize;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001852
1853 for (unsigned int elementIndex = 0; elementIndex < constant.arraySize; elementIndex++)
1854 {
Jamie Madillc600c8c2014-05-16 11:22:21 -04001855 const unsigned int elementRegisterOffset = elementRegisterCount * elementIndex;
1856
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001857 for (size_t fieldIndex = 0; fieldIndex < constant.fields.size(); fieldIndex++)
1858 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001859 const gl::Uniform &field = constant.fields[fieldIndex];
Jamie Madill5f562732014-02-14 16:41:24 -05001860 const std::string &uniformName = constant.name + ArrayString(elementIndex) + "." + field.name;
Jamie Madillc600c8c2014-05-16 11:22:21 -04001861 const unsigned int fieldRegisterIndex = field.registerIndex + elementRegisterOffset;
Jamie Madill834e8b72014-04-11 13:33:58 -04001862 gl::Uniform fieldUniform(field.type, field.precision, uniformName.c_str(), field.arraySize,
Jamie Madillc600c8c2014-05-16 11:22:21 -04001863 fieldRegisterIndex, field.elementIndex);
Jamie Madillfcef3362013-11-13 19:37:19 -05001864
1865 fieldUniform.fields = field.fields;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001866 if (!defineUniform(shader, fieldUniform, infoLog))
1867 {
1868 return false;
1869 }
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001870 }
1871 }
1872 }
1873 else
1874 {
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001875 for (size_t fieldIndex = 0; fieldIndex < constant.fields.size(); fieldIndex++)
1876 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001877 const gl::Uniform &field = constant.fields[fieldIndex];
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001878 const std::string &uniformName = constant.name + "." + field.name;
1879
Jamie Madill834e8b72014-04-11 13:33:58 -04001880 gl::Uniform fieldUniform(field.type, field.precision, uniformName.c_str(), field.arraySize,
Jamie Madill56093782013-08-30 13:21:11 -04001881 field.registerIndex, field.elementIndex);
1882
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001883 fieldUniform.fields = field.fields;
1884
1885 if (!defineUniform(shader, fieldUniform, infoLog))
1886 {
1887 return false;
1888 }
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00001889 }
1890 }
1891
1892 return true;
1893 }
1894
Nicolas Capense6050882013-07-08 10:43:10 -04001895 if (IsSampler(constant.type))
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001896 {
1897 unsigned int samplerIndex = constant.registerIndex;
Jamie Madill1b2a8f92014-05-14 13:09:39 -04001898
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001899 do
1900 {
1901 if (shader == GL_VERTEX_SHADER)
1902 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001903 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001904 {
1905 mSamplersVS[samplerIndex].active = true;
Nicolas Capens43e8ba82013-07-09 10:35:15 -04001906 mSamplersVS[samplerIndex].textureType = getTextureType(constant.type, infoLog);
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001907 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
1908 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
1909 }
1910 else
1911 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001912 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001913 return false;
1914 }
1915 }
1916 else if (shader == GL_FRAGMENT_SHADER)
1917 {
1918 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1919 {
1920 mSamplersPS[samplerIndex].active = true;
Nicolas Capens43e8ba82013-07-09 10:35:15 -04001921 mSamplersPS[samplerIndex].textureType = getTextureType(constant.type, infoLog);
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001922 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1923 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1924 }
1925 else
1926 {
1927 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
1928 return false;
1929 }
1930 }
1931 else UNREACHABLE();
1932
1933 samplerIndex++;
1934 }
1935 while (samplerIndex < constant.registerIndex + constant.arraySize);
1936 }
1937
Jamie Madill834e8b72014-04-11 13:33:58 -04001938 LinkedUniform *uniform = NULL;
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001939 GLint location = getUniformLocation(constant.name);
1940
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001941 if (location >= 0) // Previously defined, type and precision must match
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001942 {
1943 uniform = mUniforms[mUniformIndex[location].index];
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001944 }
1945 else
1946 {
Jamie Madill07d68852014-05-06 16:31:14 -04001947 uniform = new LinkedUniform(constant.type, constant.precision, constant.name, constant.arraySize,
1948 -1, BlockMemberInfo::getDefaultBlockInfo());
Jamie Madill56093782013-08-30 13:21:11 -04001949 uniform->registerElement = constant.elementIndex;
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001950 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001951
1952 if (!uniform)
1953 {
1954 return false;
1955 }
1956
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001957 if (shader == GL_FRAGMENT_SHADER)
1958 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001959 uniform->psRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001960 }
1961 else if (shader == GL_VERTEX_SHADER)
1962 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001963 uniform->vsRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001964 }
1965 else UNREACHABLE();
1966
1967 if (location >= 0)
1968 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001969 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001970 }
1971
1972 mUniforms.push_back(uniform);
1973 unsigned int uniformIndex = mUniforms.size() - 1;
1974
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00001975 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform->elementCount(); arrayElementIndex++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001976 {
Jamie Madill63491ea2013-06-06 11:56:45 -04001977 mUniformIndex.push_back(VariableLocation(uniform->name, arrayElementIndex, uniformIndex));
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001978 }
1979
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001980 if (shader == GL_VERTEX_SHADER)
1981 {
1982 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedVertexUniformVectors() + mRenderer->getMaxVertexUniformVectors())
1983 {
1984 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", mRenderer->getMaxVertexUniformVectors());
1985 return false;
1986 }
1987 }
1988 else if (shader == GL_FRAGMENT_SHADER)
1989 {
1990 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedFragmentUniformVectors() + mRenderer->getMaxFragmentUniformVectors())
1991 {
1992 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", mRenderer->getMaxFragmentUniformVectors());
1993 return false;
1994 }
1995 }
1996 else UNREACHABLE();
1997
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001998 return true;
1999}
2000
Jamie Madill834e8b72014-04-11 13:33:58 -04002001bool ProgramBinary::areMatchingInterfaceBlocks(InfoLog &infoLog, const gl::InterfaceBlock &vertexInterfaceBlock, const gl::InterfaceBlock &fragmentInterfaceBlock)
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002002{
2003 const char* blockName = vertexInterfaceBlock.name.c_str();
2004
2005 // validate blocks for the same member types
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002006 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002007 {
2008 infoLog.append("Types for interface block '%s' differ between vertex and fragment shaders", blockName);
2009 return false;
2010 }
2011
2012 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2013 {
2014 infoLog.append("Array sizes differ for interface block '%s' between vertex and fragment shaders", blockName);
2015 return false;
2016 }
2017
Jamie Madill9060a4e2013-08-12 16:22:57 -07002018 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2019 {
2020 infoLog.append("Layout qualifiers differ for interface block '%s' between vertex and fragment shaders", blockName);
2021 return false;
2022 }
2023
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002024 const unsigned int numBlockMembers = vertexInterfaceBlock.fields.size();
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002025 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2026 {
Jamie Madill834e8b72014-04-11 13:33:58 -04002027 const gl::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2028 const gl::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002029
2030 if (vertexMember.name != fragmentMember.name)
2031 {
Jamie Madill28167c62013-08-30 13:21:10 -04002032 infoLog.append("Name mismatch for field %d of interface block '%s': (in vertex: '%s', in fragment: '%s')",
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002033 blockMemberIndex, blockName, vertexMember.name.c_str(), fragmentMember.name.c_str());
2034 return false;
2035 }
2036
Jamie Madill28167c62013-08-30 13:21:10 -04002037 std::string uniformName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002038 if (!linkValidateVariables(infoLog, uniformName, vertexMember, fragmentMember))
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002039 {
2040 return false;
2041 }
2042 }
2043
2044 return true;
2045}
2046
Jamie Madill834e8b72014-04-11 13:33:58 -04002047bool ProgramBinary::linkUniformBlocks(InfoLog &infoLog, const std::vector<gl::InterfaceBlock> &vertexInterfaceBlocks,
2048 const std::vector<gl::InterfaceBlock> &fragmentInterfaceBlocks)
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002049{
2050 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jamie Madill834e8b72014-04-11 13:33:58 -04002051 typedef std::map<std::string, const gl::InterfaceBlock*> UniformBlockMap;
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002052 UniformBlockMap linkedUniformBlocks;
2053
2054 for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
2055 {
Jamie Madill834e8b72014-04-11 13:33:58 -04002056 const gl::InterfaceBlock &vertexInterfaceBlock = vertexInterfaceBlocks[blockIndex];
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002057 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2058 }
2059
2060 for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
2061 {
Jamie Madill834e8b72014-04-11 13:33:58 -04002062 const gl::InterfaceBlock &fragmentInterfaceBlock = fragmentInterfaceBlocks[blockIndex];
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002063 UniformBlockMap::const_iterator entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
2064 if (entry != linkedUniformBlocks.end())
2065 {
Jamie Madill834e8b72014-04-11 13:33:58 -04002066 const gl::InterfaceBlock &vertexInterfaceBlock = *entry->second;
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002067 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2068 {
2069 return false;
2070 }
2071 }
2072 }
2073
2074 for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
2075 {
2076 if (!defineUniformBlock(infoLog, GL_VERTEX_SHADER, vertexInterfaceBlocks[blockIndex]))
2077 {
2078 return false;
2079 }
2080 }
2081
2082 for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
2083 {
2084 if (!defineUniformBlock(infoLog, GL_FRAGMENT_SHADER, fragmentInterfaceBlocks[blockIndex]))
2085 {
2086 return false;
2087 }
2088 }
2089
2090 return true;
2091}
2092
Geoff Lang48dcae72014-02-05 16:28:24 -05002093bool ProgramBinary::gatherTransformFeedbackLinkedVaryings(InfoLog &infoLog, const std::vector<LinkedVarying> &linkedVaryings,
2094 const std::vector<std::string> &transformFeedbackVaryingNames,
2095 GLenum transformFeedbackBufferMode,
2096 std::vector<LinkedVarying> *outTransformFeedbackLinkedVaryings) const
2097{
2098 size_t totalComponents = 0;
2099 const size_t maxSeparateComponents = mRenderer->getMaxTransformFeedbackSeparateComponents();
2100 const size_t maxInterleavedComponents = mRenderer->getMaxTransformFeedbackInterleavedComponents();
2101
2102 // Gather the linked varyings that are used for transform feedback, they should all exist.
2103 outTransformFeedbackLinkedVaryings->clear();
2104 for (size_t i = 0; i < transformFeedbackVaryingNames.size(); i++)
2105 {
2106 bool found = false;
2107 for (size_t j = 0; j < linkedVaryings.size(); j++)
2108 {
2109 if (transformFeedbackVaryingNames[i] == linkedVaryings[j].name)
2110 {
2111 for (size_t k = 0; k < outTransformFeedbackLinkedVaryings->size(); k++)
2112 {
2113 if (outTransformFeedbackLinkedVaryings->at(k).name == linkedVaryings[j].name)
2114 {
2115 infoLog.append("Two transform feedback varyings specify the same output variable (%s).", linkedVaryings[j].name.c_str());
2116 return false;
2117 }
2118 }
2119
2120 size_t componentCount = linkedVaryings[j].semanticIndexCount * 4;
2121 if (transformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
2122 componentCount > maxSeparateComponents)
2123 {
2124 infoLog.append("Transform feedback varying's %s components (%u) exceed the maximum separate components (%u).",
2125 linkedVaryings[j].name.c_str(), componentCount, maxSeparateComponents);
2126 return false;
2127 }
2128
2129 totalComponents += componentCount;
2130
2131 outTransformFeedbackLinkedVaryings->push_back(linkedVaryings[j]);
2132 found = true;
2133 break;
2134 }
2135 }
2136
2137 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2138 ASSERT(found);
2139 }
2140
2141 if (transformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS && totalComponents > maxInterleavedComponents)
2142 {
2143 infoLog.append("Transform feedback varying total components (%u) exceed the maximum interleaved components (%u).",
2144 totalComponents, maxInterleavedComponents);
2145 return false;
2146 }
2147
2148 return true;
2149}
2150
Jamie Madill834e8b72014-04-11 13:33:58 -04002151void ProgramBinary::defineUniformBlockMembers(const std::vector<gl::InterfaceBlockField> &fields, const std::string &prefix, int blockIndex, BlockInfoItr *blockInfoItr, std::vector<unsigned int> *blockUniformIndexes)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00002152{
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002153 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00002154 {
Jamie Madill834e8b72014-04-11 13:33:58 -04002155 const gl::InterfaceBlockField &field = fields[uniformIndex];
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002156 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00002157
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002158 if (!field.fields.empty())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00002159 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002160 if (field.arraySize > 0)
shannonwoods@chromium.org5f8d9b42013-05-30 00:17:57 +00002161 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002162 for (unsigned int arrayElement = 0; arrayElement < field.arraySize; arrayElement++)
shannonwoods@chromium.org5f8d9b42013-05-30 00:17:57 +00002163 {
Jamie Madill5f562732014-02-14 16:41:24 -05002164 const std::string uniformElementName = fieldName + ArrayString(arrayElement);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002165 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, blockInfoItr, blockUniformIndexes);
shannonwoods@chromium.org5f8d9b42013-05-30 00:17:57 +00002166 }
2167 }
2168 else
2169 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002170 defineUniformBlockMembers(field.fields, fieldName, blockIndex, blockInfoItr, blockUniformIndexes);
shannonwoods@chromium.org5f8d9b42013-05-30 00:17:57 +00002171 }
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00002172 }
2173 else
2174 {
Jamie Madill834e8b72014-04-11 13:33:58 -04002175 LinkedUniform *newUniform = new LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
2176 blockIndex, **blockInfoItr);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00002177
2178 // add to uniform list, but not index, since uniform block uniforms have no location
2179 blockUniformIndexes->push_back(mUniforms.size());
2180 mUniforms.push_back(newUniform);
2181 (*blockInfoItr)++;
2182 }
2183 }
2184}
2185
Jamie Madill834e8b72014-04-11 13:33:58 -04002186bool ProgramBinary::defineUniformBlock(InfoLog &infoLog, GLenum shader, const gl::InterfaceBlock &interfaceBlock)
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002187{
2188 // create uniform block entries if they do not exist
2189 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
2190 {
2191 std::vector<unsigned int> blockUniformIndexes;
2192 const unsigned int blockIndex = mUniformBlocks.size();
2193
2194 // define member uniforms
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00002195 BlockInfoItr blockInfoItr = interfaceBlock.blockInfo.cbegin();
Jamie Madill9d2ffb12013-08-30 13:21:04 -04002196 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, &blockInfoItr, &blockUniformIndexes);
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002197
2198 // create all the uniform blocks
2199 if (interfaceBlock.arraySize > 0)
2200 {
2201 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
2202 {
2203 gl::UniformBlock *newUniformBlock = new UniformBlock(interfaceBlock.name, uniformBlockElement, interfaceBlock.dataSize);
2204 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
2205 mUniformBlocks.push_back(newUniformBlock);
2206 }
2207 }
2208 else
2209 {
2210 gl::UniformBlock *newUniformBlock = new UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, interfaceBlock.dataSize);
2211 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
2212 mUniformBlocks.push_back(newUniformBlock);
2213 }
2214 }
2215
2216 // Assign registers to the uniform blocks
2217 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
2218 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
2219 ASSERT(blockIndex != GL_INVALID_INDEX);
2220 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
2221
2222 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
2223 {
2224 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
2225 ASSERT(uniformBlock->name == interfaceBlock.name);
2226
2227 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader, interfaceBlock.registerIndex + uniformBlockElement))
2228 {
2229 return false;
2230 }
2231 }
2232
2233 return true;
2234}
2235
2236bool ProgramBinary::assignUniformBlockRegister(InfoLog &infoLog, UniformBlock *uniformBlock, GLenum shader, unsigned int registerIndex)
2237{
2238 if (shader == GL_VERTEX_SHADER)
2239 {
2240 uniformBlock->vsRegisterIndex = registerIndex;
2241 unsigned int maximumBlocks = mRenderer->getMaxVertexShaderUniformBuffers();
2242
2243 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= maximumBlocks)
2244 {
2245 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", maximumBlocks);
2246 return false;
2247 }
2248 }
2249 else if (shader == GL_FRAGMENT_SHADER)
2250 {
2251 uniformBlock->psRegisterIndex = registerIndex;
2252 unsigned int maximumBlocks = mRenderer->getMaxFragmentShaderUniformBuffers();
2253
2254 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= maximumBlocks)
2255 {
Jamie Madill6fb09f62013-06-21 09:15:31 -04002256 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", maximumBlocks);
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +00002257 return false;
2258 }
2259 }
2260 else UNREACHABLE();
2261
2262 return true;
2263}
2264
Jamie Madill1b2a8f92014-05-14 13:09:39 -04002265bool ProgramBinary::isValidated() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002266{
2267 return mValidated;
2268}
2269
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002270void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002271{
2272 // Skip over inactive attributes
2273 unsigned int activeAttribute = 0;
2274 unsigned int attribute;
2275 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2276 {
2277 if (mLinkedAttribute[attribute].name.empty())
2278 {
2279 continue;
2280 }
2281
2282 if (activeAttribute == index)
2283 {
2284 break;
2285 }
2286
2287 activeAttribute++;
2288 }
2289
2290 if (bufsize > 0)
2291 {
2292 const char *string = mLinkedAttribute[attribute].name.c_str();
2293
2294 strncpy(name, string, bufsize);
2295 name[bufsize - 1] = '\0';
2296
2297 if (length)
2298 {
2299 *length = strlen(name);
2300 }
2301 }
2302
2303 *size = 1; // Always a single 'type' instance
2304
2305 *type = mLinkedAttribute[attribute].type;
2306}
2307
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002308GLint ProgramBinary::getActiveAttributeCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002309{
2310 int count = 0;
2311
2312 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2313 {
2314 if (!mLinkedAttribute[attributeIndex].name.empty())
2315 {
2316 count++;
2317 }
2318 }
2319
2320 return count;
2321}
2322
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002323GLint ProgramBinary::getActiveAttributeMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002324{
2325 int maxLength = 0;
2326
2327 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2328 {
2329 if (!mLinkedAttribute[attributeIndex].name.empty())
2330 {
2331 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2332 }
2333 }
2334
2335 return maxLength;
2336}
2337
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002338void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002339{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002340 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002341
2342 if (bufsize > 0)
2343 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002344 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002345
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002346 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002347 {
2348 string += "[0]";
2349 }
2350
2351 strncpy(name, string.c_str(), bufsize);
2352 name[bufsize - 1] = '\0';
2353
2354 if (length)
2355 {
2356 *length = strlen(name);
2357 }
2358 }
2359
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002360 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002361
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002362 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002363}
2364
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002365GLint ProgramBinary::getActiveUniformCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002366{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002367 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002368}
2369
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002370GLint ProgramBinary::getActiveUniformMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002371{
2372 int maxLength = 0;
2373
2374 unsigned int numUniforms = mUniforms.size();
2375 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2376 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002377 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002378 {
2379 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2380 if (mUniforms[uniformIndex]->isArray())
2381 {
2382 length += 3; // Counting in "[0]".
2383 }
2384 maxLength = std::max(length, maxLength);
2385 }
2386 }
2387
2388 return maxLength;
2389}
2390
shannonwoods@chromium.org2a9a9d22013-05-30 00:05:40 +00002391GLint ProgramBinary::getActiveUniformi(GLuint index, GLenum pname) const
2392{
Jamie Madill834e8b72014-04-11 13:33:58 -04002393 const gl::LinkedUniform& uniform = *mUniforms[index];
shannonwoods@chromium.org2a9a9d22013-05-30 00:05:40 +00002394
2395 switch (pname)
2396 {
2397 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
2398 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
shannonwoods@chromium.orgb1dc1b62013-05-30 00:15:43 +00002399 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00002400 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
shannonwoods@chromium.org2a9a9d22013-05-30 00:05:40 +00002401
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +00002402 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
2403 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
2404 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
2405 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
shannonwoods@chromium.org2a9a9d22013-05-30 00:05:40 +00002406
2407 default:
2408 UNREACHABLE();
2409 break;
2410 }
2411 return 0;
2412}
2413
Jamie Madill36398922014-05-20 14:51:53 -04002414bool ProgramBinary::isValidUniformLocation(GLint location) const
2415{
2416 ASSERT(rx::IsIntegerCastSafe<GLint>(mUniformIndex.size()));
2417 return (location >= 0 && location < static_cast<GLint>(mUniformIndex.size()));
2418}
2419
2420LinkedUniform *ProgramBinary::getUniformByLocation(GLint location) const
2421{
2422 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformIndex.size());
2423 return mUniforms[mUniformIndex[location].index];
2424}
2425
shannonwoods@chromium.orgbeb02782013-05-30 00:07:28 +00002426void ProgramBinary::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
2427{
2428 ASSERT(uniformBlockIndex < mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
2429
2430 const UniformBlock &uniformBlock = *mUniformBlocks[uniformBlockIndex];
2431
2432 if (bufSize > 0)
2433 {
2434 std::string string = uniformBlock.name;
2435
2436 if (uniformBlock.isArrayElement())
2437 {
Jamie Madill5f562732014-02-14 16:41:24 -05002438 string += ArrayString(uniformBlock.elementIndex);
shannonwoods@chromium.orgbeb02782013-05-30 00:07:28 +00002439 }
2440
2441 strncpy(uniformBlockName, string.c_str(), bufSize);
2442 uniformBlockName[bufSize - 1] = '\0';
2443
2444 if (length)
2445 {
2446 *length = strlen(uniformBlockName);
2447 }
2448 }
2449}
2450
shannonwoods@chromium.orge7317ca2013-05-30 00:07:35 +00002451void ProgramBinary::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
2452{
2453 ASSERT(uniformBlockIndex < mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
2454
2455 const UniformBlock &uniformBlock = *mUniformBlocks[uniformBlockIndex];
2456
2457 switch (pname)
2458 {
2459 case GL_UNIFORM_BLOCK_DATA_SIZE:
2460 *params = static_cast<GLint>(uniformBlock.dataSize);
2461 break;
2462 case GL_UNIFORM_BLOCK_NAME_LENGTH:
shannonwoods@chromium.org932cc6b2013-05-30 00:15:37 +00002463 *params = static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArrayElement() ? 3 : 0));
shannonwoods@chromium.orge7317ca2013-05-30 00:07:35 +00002464 break;
2465 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
2466 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
2467 break;
2468 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
2469 {
2470 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
2471 {
2472 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
2473 }
2474 }
2475 break;
2476 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
2477 *params = static_cast<GLint>(uniformBlock.isReferencedByVertexShader());
2478 break;
2479 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
2480 *params = static_cast<GLint>(uniformBlock.isReferencedByFragmentShader());
2481 break;
2482 default: UNREACHABLE();
2483 }
2484}
2485
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002486GLuint ProgramBinary::getActiveUniformBlockCount() const
2487{
2488 return mUniformBlocks.size();
2489}
2490
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002491GLuint ProgramBinary::getActiveUniformBlockMaxLength() const
2492{
2493 unsigned int maxLength = 0;
2494
2495 unsigned int numUniformBlocks = mUniformBlocks.size();
2496 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
2497 {
2498 const UniformBlock &uniformBlock = *mUniformBlocks[uniformBlockIndex];
2499 if (!uniformBlock.name.empty())
2500 {
2501 const unsigned int length = uniformBlock.name.length() + 1;
2502
2503 // Counting in "[0]".
2504 const unsigned int arrayLength = (uniformBlock.isArrayElement() ? 3 : 0);
2505
2506 maxLength = std::max(length + arrayLength, maxLength);
2507 }
2508 }
2509
2510 return maxLength;
2511}
2512
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002513void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002514{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002515 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002516 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002517 {
2518 mValidated = false;
2519 }
2520 else
2521 {
2522 mValidated = true;
2523 }
2524}
2525
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002526bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002527{
2528 // if any two active samplers in a program are of different types, but refer to the same
2529 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2530 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2531
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002532 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002533 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002534
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002535 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002536 {
2537 textureUnitType[i] = TEXTURE_UNKNOWN;
2538 }
2539
2540 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2541 {
2542 if (mSamplersPS[i].active)
2543 {
2544 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
Jamie Madill1b2a8f92014-05-14 13:09:39 -04002545
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002546 if (unit >= maxCombinedTextureImageUnits)
2547 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002548 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002549 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002550 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002551 }
2552
2553 return false;
2554 }
2555
2556 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2557 {
2558 if (mSamplersPS[i].textureType != textureUnitType[unit])
2559 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002560 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002561 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002562 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002563 }
2564
2565 return false;
2566 }
2567 }
2568 else
2569 {
2570 textureUnitType[unit] = mSamplersPS[i].textureType;
2571 }
2572 }
2573 }
2574
2575 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2576 {
2577 if (mSamplersVS[i].active)
2578 {
2579 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
Jamie Madill1b2a8f92014-05-14 13:09:39 -04002580
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002581 if (unit >= maxCombinedTextureImageUnits)
2582 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002583 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002584 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002585 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002586 }
2587
2588 return false;
2589 }
2590
2591 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2592 {
2593 if (mSamplersVS[i].textureType != textureUnitType[unit])
2594 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002595 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002596 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002597 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002598 }
2599
2600 return false;
2601 }
2602 }
2603 else
2604 {
2605 textureUnitType[unit] = mSamplersVS[i].textureType;
2606 }
2607 }
2608 }
2609
2610 return true;
2611}
2612
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002613ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2614{
2615}
2616
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002617struct AttributeSorter
2618{
2619 AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS])
2620 : originalIndices(semanticIndices)
2621 {
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002622 }
2623
2624 bool operator()(int a, int b)
2625 {
Jamie Madill03181ab2013-12-18 12:56:06 -05002626 if (originalIndices[a] == -1) return false;
2627 if (originalIndices[b] == -1) return true;
2628 return (originalIndices[a] < originalIndices[b]);
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002629 }
2630
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002631 const int (&originalIndices)[MAX_VERTEX_ATTRIBS];
2632};
2633
Al Patrick3f2daa82013-08-07 12:58:57 -07002634void ProgramBinary::initAttributesByLayout()
2635{
2636 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2637 {
2638 mAttributesByLayout[i] = i;
2639 }
2640
2641 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2642}
2643
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002644void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
2645{
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002646 rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
2647
2648 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2649 {
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002650 oldTranslatedAttributes[i] = attributes[i];
2651 }
2652
2653 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2654 {
Al Patrick3f2daa82013-08-07 12:58:57 -07002655 int oldIndex = mAttributesByLayout[i];
2656 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002657 attributes[i] = oldTranslatedAttributes[oldIndex];
2658 }
2659}
2660
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002661void ProgramBinary::initializeUniformStorage()
2662{
2663 // Compute total default block size
2664 unsigned int vertexRegisters = 0;
2665 unsigned int fragmentRegisters = 0;
2666 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
2667 {
Jamie Madill834e8b72014-04-11 13:33:58 -04002668 const LinkedUniform &uniform = *mUniforms[uniformIndex];
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002669
2670 if (!IsSampler(uniform.type))
2671 {
2672 if (uniform.isReferencedByVertexShader())
2673 {
2674 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
2675 }
2676 if (uniform.isReferencedByFragmentShader())
2677 {
2678 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
2679 }
2680 }
2681 }
2682
2683 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
2684 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
2685}
2686
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002687}