blob: ad87013bb60cf13a725aa73bd7818157dd2828ba [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
11#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050012#include "libANGLE/Framebuffer.h"
13#include "libANGLE/FramebufferAttachment.h"
14#include "libANGLE/Program.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000015#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/renderer/d3d/DynamicHLSL.h"
17#include "libANGLE/renderer/d3d/RendererD3D.h"
18#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050019#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050020#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050021
Brandon Jonesc9610c52014-08-25 17:02:59 -070022namespace rx
23{
24
Brandon Joneseb994362014-09-24 10:27:28 -070025namespace
26{
27
Brandon Jones1a8a7e32014-10-01 12:49:30 -070028GLenum GetTextureType(GLenum samplerType)
29{
30 switch (samplerType)
31 {
32 case GL_SAMPLER_2D:
33 case GL_INT_SAMPLER_2D:
34 case GL_UNSIGNED_INT_SAMPLER_2D:
35 case GL_SAMPLER_2D_SHADOW:
36 return GL_TEXTURE_2D;
37 case GL_SAMPLER_3D:
38 case GL_INT_SAMPLER_3D:
39 case GL_UNSIGNED_INT_SAMPLER_3D:
40 return GL_TEXTURE_3D;
41 case GL_SAMPLER_CUBE:
42 case GL_SAMPLER_CUBE_SHADOW:
43 return GL_TEXTURE_CUBE_MAP;
44 case GL_INT_SAMPLER_CUBE:
45 case GL_UNSIGNED_INT_SAMPLER_CUBE:
46 return GL_TEXTURE_CUBE_MAP;
47 case GL_SAMPLER_2D_ARRAY:
48 case GL_INT_SAMPLER_2D_ARRAY:
49 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
50 case GL_SAMPLER_2D_ARRAY_SHADOW:
51 return GL_TEXTURE_2D_ARRAY;
52 default: UNREACHABLE();
53 }
54
55 return GL_TEXTURE_2D;
56}
57
Brandon Joneseb994362014-09-24 10:27:28 -070058void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS])
59{
60 size_t layoutIndex = 0;
61 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
62 {
63 ASSERT(layoutIndex < gl::MAX_VERTEX_ATTRIBS);
64
65 const sh::Attribute &shaderAttr = shaderAttributes[attributeIndex];
66
67 if (shaderAttr.type != GL_NONE)
68 {
69 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
70
71 for (size_t rowIndex = 0; static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); rowIndex++, layoutIndex++)
72 {
73 gl::VertexFormat *defaultFormat = &inputLayout[layoutIndex];
74
75 defaultFormat->mType = gl::VariableComponentType(transposedType);
76 defaultFormat->mNormalized = false;
77 defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool
78 defaultFormat->mComponents = gl::VariableColumnCount(transposedType);
79 }
80 }
81 }
82}
83
84std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
85{
Jamie Madillb4463142014-12-19 14:56:54 -050086 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070087
Jamie Madillb4463142014-12-19 14:56:54 -050088 if (!shaderOutputVars.empty())
89 {
90 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
91 }
Brandon Joneseb994362014-09-24 10:27:28 -070092
93 return defaultPixelOutput;
94}
95
Brandon Jones1a8a7e32014-10-01 12:49:30 -070096bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
97{
98 return var.isRowMajorLayout;
99}
100
101bool IsRowMajorLayout(const sh::ShaderVariable &var)
102{
103 return false;
104}
105
Jamie Madill437d2662014-12-05 14:23:35 -0500106struct AttributeSorter
107{
108 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -0500109 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -0500110 {
111 }
112
113 bool operator()(int a, int b)
114 {
Jamie Madill80d934b2015-02-19 10:16:12 -0500115 int indexA = (*originalIndices)[a];
116 int indexB = (*originalIndices)[b];
117
118 if (indexA == -1) return false;
119 if (indexB == -1) return true;
120 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -0500121 }
122
Jamie Madill80d934b2015-02-19 10:16:12 -0500123 const ProgramImpl::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500124};
125
Brandon Joneseb994362014-09-24 10:27:28 -0700126}
127
128ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
129 const GLenum signature[],
Geoff Lang359ef262015-01-05 14:42:29 -0500130 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700131 : mShaderExecutable(shaderExecutable)
132{
133 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
134 {
135 mInputs[attributeIndex] = inputLayout[attributeIndex];
136 mSignature[attributeIndex] = signature[attributeIndex];
137 }
138}
139
140ProgramD3D::VertexExecutable::~VertexExecutable()
141{
142 SafeDelete(mShaderExecutable);
143}
144
145bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
146{
147 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
148 {
149 if (mSignature[attributeIndex] != signature[attributeIndex])
150 {
151 return false;
152 }
153 }
154
155 return true;
156}
157
Geoff Lang359ef262015-01-05 14:42:29 -0500158ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700159 : mOutputSignature(outputSignature),
160 mShaderExecutable(shaderExecutable)
161{
162}
163
164ProgramD3D::PixelExecutable::~PixelExecutable()
165{
166 SafeDelete(mShaderExecutable);
167}
168
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700169ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
170{
171}
172
Geoff Lang7dd2e102014-11-10 15:19:26 -0500173unsigned int ProgramD3D::mCurrentSerial = 1;
174
Jamie Madill93e13fb2014-11-06 15:27:25 -0500175ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700176 : ProgramImpl(),
177 mRenderer(renderer),
178 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700179 mGeometryExecutable(NULL),
180 mVertexWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
181 mPixelWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
Brandon Jones44151a92014-09-10 11:32:25 -0700182 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700183 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700184 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700185 mUsedVertexSamplerRange(0),
186 mUsedPixelSamplerRange(0),
187 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500188 mShaderVersion(100),
189 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700190{
Brandon Joneseb994362014-09-24 10:27:28 -0700191 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700192}
193
194ProgramD3D::~ProgramD3D()
195{
196 reset();
197 SafeDelete(mDynamicHLSL);
198}
199
Brandon Jones44151a92014-09-10 11:32:25 -0700200bool ProgramD3D::usesPointSpriteEmulation() const
201{
202 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
203}
204
205bool ProgramD3D::usesGeometryShader() const
206{
Cooper Partine6664f02015-01-09 16:22:24 -0800207 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
208}
209
210bool ProgramD3D::usesInstancedPointSpriteEmulation() const
211{
212 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700213}
214
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700215GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
216{
217 GLint logicalTextureUnit = -1;
218
219 switch (type)
220 {
221 case gl::SAMPLER_PIXEL:
222 ASSERT(samplerIndex < caps.maxTextureImageUnits);
223 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
224 {
225 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
226 }
227 break;
228 case gl::SAMPLER_VERTEX:
229 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
230 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
231 {
232 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
233 }
234 break;
235 default: UNREACHABLE();
236 }
237
238 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
239 {
240 return logicalTextureUnit;
241 }
242
243 return -1;
244}
245
246// Returns the texture type for a given Direct3D 9 sampler type and
247// index (0-15 for the pixel shader and 0-3 for the vertex shader).
248GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
249{
250 switch (type)
251 {
252 case gl::SAMPLER_PIXEL:
253 ASSERT(samplerIndex < mSamplersPS.size());
254 ASSERT(mSamplersPS[samplerIndex].active);
255 return mSamplersPS[samplerIndex].textureType;
256 case gl::SAMPLER_VERTEX:
257 ASSERT(samplerIndex < mSamplersVS.size());
258 ASSERT(mSamplersVS[samplerIndex].active);
259 return mSamplersVS[samplerIndex].textureType;
260 default: UNREACHABLE();
261 }
262
263 return GL_TEXTURE_2D;
264}
265
266GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
267{
268 switch (type)
269 {
270 case gl::SAMPLER_PIXEL:
271 return mUsedPixelSamplerRange;
272 case gl::SAMPLER_VERTEX:
273 return mUsedVertexSamplerRange;
274 default:
275 UNREACHABLE();
276 return 0;
277 }
278}
279
280void ProgramD3D::updateSamplerMapping()
281{
282 if (!mDirtySamplerMapping)
283 {
284 return;
285 }
286
287 mDirtySamplerMapping = false;
288
289 // Retrieve sampler uniform values
290 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
291 {
292 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
293
294 if (targetUniform->dirty)
295 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500296 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700297 {
298 int count = targetUniform->elementCount();
299 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
300
301 if (targetUniform->isReferencedByFragmentShader())
302 {
303 unsigned int firstIndex = targetUniform->psRegisterIndex;
304
305 for (int i = 0; i < count; i++)
306 {
307 unsigned int samplerIndex = firstIndex + i;
308
309 if (samplerIndex < mSamplersPS.size())
310 {
311 ASSERT(mSamplersPS[samplerIndex].active);
312 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
313 }
314 }
315 }
316
317 if (targetUniform->isReferencedByVertexShader())
318 {
319 unsigned int firstIndex = targetUniform->vsRegisterIndex;
320
321 for (int i = 0; i < count; i++)
322 {
323 unsigned int samplerIndex = firstIndex + i;
324
325 if (samplerIndex < mSamplersVS.size())
326 {
327 ASSERT(mSamplersVS[samplerIndex].active);
328 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
329 }
330 }
331 }
332 }
333 }
334 }
335}
336
337bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
338{
339 // if any two active samplers in a program are of different types, but refer to the same
340 // texture image unit, and this is the current program, then ValidateProgram will fail, and
341 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
342 updateSamplerMapping();
343
344 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
345
346 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
347 {
348 if (mSamplersPS[i].active)
349 {
350 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
351
352 if (unit >= textureUnitTypes.size())
353 {
354 if (infoLog)
355 {
356 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
357 }
358
359 return false;
360 }
361
362 if (textureUnitTypes[unit] != GL_NONE)
363 {
364 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
365 {
366 if (infoLog)
367 {
368 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
369 }
370
371 return false;
372 }
373 }
374 else
375 {
376 textureUnitTypes[unit] = mSamplersPS[i].textureType;
377 }
378 }
379 }
380
381 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
382 {
383 if (mSamplersVS[i].active)
384 {
385 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
386
387 if (unit >= textureUnitTypes.size())
388 {
389 if (infoLog)
390 {
391 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
392 }
393
394 return false;
395 }
396
397 if (textureUnitTypes[unit] != GL_NONE)
398 {
399 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
400 {
401 if (infoLog)
402 {
403 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
404 }
405
406 return false;
407 }
408 }
409 else
410 {
411 textureUnitTypes[unit] = mSamplersVS[i].textureType;
412 }
413 }
414 }
415
416 return true;
417}
418
Geoff Lang7dd2e102014-11-10 15:19:26 -0500419LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700420{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500421 int compileFlags = stream->readInt<int>();
422 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
423 {
424 infoLog.append("Mismatched compilation flags.");
425 return LinkResult(false, gl::Error(GL_NO_ERROR));
426 }
427
Brandon Jones44151a92014-09-10 11:32:25 -0700428 stream->readInt(&mShaderVersion);
429
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700430 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
431 for (unsigned int i = 0; i < psSamplerCount; ++i)
432 {
433 Sampler sampler;
434 stream->readBool(&sampler.active);
435 stream->readInt(&sampler.logicalTextureUnit);
436 stream->readInt(&sampler.textureType);
437 mSamplersPS.push_back(sampler);
438 }
439 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
440 for (unsigned int i = 0; i < vsSamplerCount; ++i)
441 {
442 Sampler sampler;
443 stream->readBool(&sampler.active);
444 stream->readInt(&sampler.logicalTextureUnit);
445 stream->readInt(&sampler.textureType);
446 mSamplersVS.push_back(sampler);
447 }
448
449 stream->readInt(&mUsedVertexSamplerRange);
450 stream->readInt(&mUsedPixelSamplerRange);
451
452 const unsigned int uniformCount = stream->readInt<unsigned int>();
453 if (stream->error())
454 {
455 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500456 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700457 }
458
459 mUniforms.resize(uniformCount);
460 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
461 {
462 GLenum type = stream->readInt<GLenum>();
463 GLenum precision = stream->readInt<GLenum>();
464 std::string name = stream->readString();
465 unsigned int arraySize = stream->readInt<unsigned int>();
466 int blockIndex = stream->readInt<int>();
467
468 int offset = stream->readInt<int>();
469 int arrayStride = stream->readInt<int>();
470 int matrixStride = stream->readInt<int>();
471 bool isRowMajorMatrix = stream->readBool();
472
473 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
474
475 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
476
477 stream->readInt(&uniform->psRegisterIndex);
478 stream->readInt(&uniform->vsRegisterIndex);
479 stream->readInt(&uniform->registerCount);
480 stream->readInt(&uniform->registerElement);
481
482 mUniforms[uniformIndex] = uniform;
483 }
484
485 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
486 if (stream->error())
487 {
488 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500489 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700490 }
491
492 mUniformIndex.resize(uniformIndexCount);
493 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
494 {
495 stream->readString(&mUniformIndex[uniformIndexIndex].name);
496 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
497 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
498 }
499
500 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
501 if (stream->error())
502 {
503 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500504 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700505 }
506
507 mUniformBlocks.resize(uniformBlockCount);
508 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
509 {
510 std::string name = stream->readString();
511 unsigned int elementIndex = stream->readInt<unsigned int>();
512 unsigned int dataSize = stream->readInt<unsigned int>();
513
514 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
515
516 stream->readInt(&uniformBlock->psRegisterIndex);
517 stream->readInt(&uniformBlock->vsRegisterIndex);
518
519 unsigned int numMembers = stream->readInt<unsigned int>();
520 uniformBlock->memberUniformIndexes.resize(numMembers);
521 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
522 {
523 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
524 }
525
526 mUniformBlocks[uniformBlockIndex] = uniformBlock;
527 }
528
Brandon Joneseb994362014-09-24 10:27:28 -0700529 stream->readInt(&mTransformFeedbackBufferMode);
530 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
531 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
532 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
533 {
534 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
535
536 stream->readString(&varying.name);
537 stream->readInt(&varying.type);
538 stream->readInt(&varying.size);
539 stream->readString(&varying.semanticName);
540 stream->readInt(&varying.semanticIndex);
541 stream->readInt(&varying.semanticIndexCount);
542 }
543
Brandon Jones22502d52014-08-29 16:58:36 -0700544 stream->readString(&mVertexHLSL);
545 stream->readInt(&mVertexWorkarounds);
546 stream->readString(&mPixelHLSL);
547 stream->readInt(&mPixelWorkarounds);
548 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700549 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700550
551 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
552 mPixelShaderKey.resize(pixelShaderKeySize);
553 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
554 {
555 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
556 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
557 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
558 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
559 }
560
Brandon Joneseb994362014-09-24 10:27:28 -0700561 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
562
563 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
564 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
565 {
566 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
567
568 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
569 {
570 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
571 stream->readInt(&vertexInput->mType);
572 stream->readInt(&vertexInput->mNormalized);
573 stream->readInt(&vertexInput->mComponents);
574 stream->readBool(&vertexInput->mPureInteger);
575 }
576
577 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
578 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400579
Geoff Lang359ef262015-01-05 14:42:29 -0500580 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400581 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
582 SHADER_VERTEX,
583 mTransformFeedbackLinkedVaryings,
584 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
585 &shaderExecutable);
586 if (error.isError())
587 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500588 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400589 }
590
Brandon Joneseb994362014-09-24 10:27:28 -0700591 if (!shaderExecutable)
592 {
593 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500594 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700595 }
596
597 // generated converted input layout
598 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
599 getInputLayoutSignature(inputLayout, signature);
600
601 // add new binary
602 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
603
604 stream->skip(vertexShaderSize);
605 }
606
607 const size_t pixelShaderCount = stream->readInt<unsigned int>();
608 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
609 {
610 const size_t outputCount = stream->readInt<unsigned int>();
611 std::vector<GLenum> outputs(outputCount);
612 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
613 {
614 stream->readInt(&outputs[outputIndex]);
615 }
616
617 const size_t pixelShaderSize = stream->readInt<unsigned int>();
618 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500619 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400620 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
621 mTransformFeedbackLinkedVaryings,
622 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
623 &shaderExecutable);
624 if (error.isError())
625 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500626 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400627 }
Brandon Joneseb994362014-09-24 10:27:28 -0700628
629 if (!shaderExecutable)
630 {
631 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500632 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700633 }
634
635 // add new binary
636 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
637
638 stream->skip(pixelShaderSize);
639 }
640
641 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
642
643 if (geometryShaderSize > 0)
644 {
645 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400646 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
647 mTransformFeedbackLinkedVaryings,
648 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
649 &mGeometryExecutable);
650 if (error.isError())
651 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500652 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400653 }
Brandon Joneseb994362014-09-24 10:27:28 -0700654
655 if (!mGeometryExecutable)
656 {
657 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500658 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700659 }
660 stream->skip(geometryShaderSize);
661 }
662
Brandon Jones18bd4102014-09-22 14:21:44 -0700663 GUID binaryIdentifier = {0};
664 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
665
666 GUID identifier = mRenderer->getAdapterIdentifier();
667 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
668 {
669 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500670 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700671 }
672
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700673 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500674 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700675
Geoff Lang7dd2e102014-11-10 15:19:26 -0500676 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700677}
678
Geoff Langb543aff2014-09-30 14:52:54 -0400679gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700680{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500681 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
682
Brandon Jones44151a92014-09-10 11:32:25 -0700683 stream->writeInt(mShaderVersion);
684
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685 stream->writeInt(mSamplersPS.size());
686 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
687 {
688 stream->writeInt(mSamplersPS[i].active);
689 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
690 stream->writeInt(mSamplersPS[i].textureType);
691 }
692
693 stream->writeInt(mSamplersVS.size());
694 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
695 {
696 stream->writeInt(mSamplersVS[i].active);
697 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
698 stream->writeInt(mSamplersVS[i].textureType);
699 }
700
701 stream->writeInt(mUsedVertexSamplerRange);
702 stream->writeInt(mUsedPixelSamplerRange);
703
704 stream->writeInt(mUniforms.size());
705 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
706 {
707 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
708
709 stream->writeInt(uniform.type);
710 stream->writeInt(uniform.precision);
711 stream->writeString(uniform.name);
712 stream->writeInt(uniform.arraySize);
713 stream->writeInt(uniform.blockIndex);
714
715 stream->writeInt(uniform.blockInfo.offset);
716 stream->writeInt(uniform.blockInfo.arrayStride);
717 stream->writeInt(uniform.blockInfo.matrixStride);
718 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
719
720 stream->writeInt(uniform.psRegisterIndex);
721 stream->writeInt(uniform.vsRegisterIndex);
722 stream->writeInt(uniform.registerCount);
723 stream->writeInt(uniform.registerElement);
724 }
725
726 stream->writeInt(mUniformIndex.size());
727 for (size_t i = 0; i < mUniformIndex.size(); ++i)
728 {
729 stream->writeString(mUniformIndex[i].name);
730 stream->writeInt(mUniformIndex[i].element);
731 stream->writeInt(mUniformIndex[i].index);
732 }
733
734 stream->writeInt(mUniformBlocks.size());
735 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
736 {
737 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
738
739 stream->writeString(uniformBlock.name);
740 stream->writeInt(uniformBlock.elementIndex);
741 stream->writeInt(uniformBlock.dataSize);
742
743 stream->writeInt(uniformBlock.memberUniformIndexes.size());
744 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
745 {
746 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
747 }
748
749 stream->writeInt(uniformBlock.psRegisterIndex);
750 stream->writeInt(uniformBlock.vsRegisterIndex);
751 }
752
Brandon Joneseb994362014-09-24 10:27:28 -0700753 stream->writeInt(mTransformFeedbackBufferMode);
754 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
755 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
756 {
757 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
758
759 stream->writeString(varying.name);
760 stream->writeInt(varying.type);
761 stream->writeInt(varying.size);
762 stream->writeString(varying.semanticName);
763 stream->writeInt(varying.semanticIndex);
764 stream->writeInt(varying.semanticIndexCount);
765 }
766
Brandon Jones22502d52014-08-29 16:58:36 -0700767 stream->writeString(mVertexHLSL);
768 stream->writeInt(mVertexWorkarounds);
769 stream->writeString(mPixelHLSL);
770 stream->writeInt(mPixelWorkarounds);
771 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700772 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700773
Brandon Joneseb994362014-09-24 10:27:28 -0700774 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700775 stream->writeInt(pixelShaderKey.size());
776 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
777 {
Brandon Joneseb994362014-09-24 10:27:28 -0700778 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700779 stream->writeInt(variable.type);
780 stream->writeString(variable.name);
781 stream->writeString(variable.source);
782 stream->writeInt(variable.outputIndex);
783 }
784
Brandon Joneseb994362014-09-24 10:27:28 -0700785 stream->writeInt(mVertexExecutables.size());
786 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
787 {
788 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
789
790 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
791 {
792 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
793 stream->writeInt(vertexInput.mType);
794 stream->writeInt(vertexInput.mNormalized);
795 stream->writeInt(vertexInput.mComponents);
796 stream->writeInt(vertexInput.mPureInteger);
797 }
798
799 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
800 stream->writeInt(vertexShaderSize);
801
802 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
803 stream->writeBytes(vertexBlob, vertexShaderSize);
804 }
805
806 stream->writeInt(mPixelExecutables.size());
807 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
808 {
809 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
810
811 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
812 stream->writeInt(outputs.size());
813 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
814 {
815 stream->writeInt(outputs[outputIndex]);
816 }
817
818 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
819 stream->writeInt(pixelShaderSize);
820
821 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
822 stream->writeBytes(pixelBlob, pixelShaderSize);
823 }
824
825 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
826 stream->writeInt(geometryShaderSize);
827
828 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
829 {
830 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
831 stream->writeBytes(geometryBlob, geometryShaderSize);
832 }
833
Brandon Jones18bd4102014-09-22 14:21:44 -0700834 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400835 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700836
Geoff Langb543aff2014-09-30 14:52:54 -0400837 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700838}
839
Geoff Lang359ef262015-01-05 14:42:29 -0500840gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700841{
Brandon Joneseb994362014-09-24 10:27:28 -0700842 std::vector<GLenum> outputs;
843
Jamie Madill48faf802014-11-06 15:27:22 -0500844 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700845
846 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
847 {
848 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
849
850 if (colorbuffer)
851 {
852 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
853 }
854 else
855 {
856 outputs.push_back(GL_NONE);
857 }
858 }
859
Jamie Madill97399232014-12-23 12:31:15 -0500860 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700861}
862
Jamie Madill97399232014-12-23 12:31:15 -0500863gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500864 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500865 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700866{
867 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
868 {
869 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
870 {
Geoff Langb543aff2014-09-30 14:52:54 -0400871 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
872 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700873 }
874 }
875
Brandon Jones22502d52014-08-29 16:58:36 -0700876 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
877 outputSignature);
878
879 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500880 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500881
882 gl::InfoLog tempInfoLog;
883 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
884
885 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400886 mTransformFeedbackLinkedVaryings,
887 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
888 mPixelWorkarounds, &pixelExecutable);
889 if (error.isError())
890 {
891 return error;
892 }
Brandon Joneseb994362014-09-24 10:27:28 -0700893
Jamie Madill97399232014-12-23 12:31:15 -0500894 if (pixelExecutable)
895 {
896 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
897 }
898 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700899 {
900 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
901 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
902 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
903 }
Brandon Jones22502d52014-08-29 16:58:36 -0700904
Geoff Langb543aff2014-09-30 14:52:54 -0400905 *outExectuable = pixelExecutable;
906 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700907}
908
Jamie Madill97399232014-12-23 12:31:15 -0500909gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500910 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500911 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700912{
Brandon Joneseb994362014-09-24 10:27:28 -0700913 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
914 getInputLayoutSignature(inputLayout, signature);
915
916 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
917 {
918 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
919 {
Geoff Langb543aff2014-09-30 14:52:54 -0400920 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
921 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700922 }
923 }
924
Brandon Jones22502d52014-08-29 16:58:36 -0700925 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700926 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700927
928 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500929 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500930
931 gl::InfoLog tempInfoLog;
932 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
933
934 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400935 mTransformFeedbackLinkedVaryings,
936 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
937 mVertexWorkarounds, &vertexExecutable);
938 if (error.isError())
939 {
940 return error;
941 }
942
Jamie Madill97399232014-12-23 12:31:15 -0500943 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700944 {
945 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
946 }
Jamie Madill97399232014-12-23 12:31:15 -0500947 else if (!infoLog)
948 {
949 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
950 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
951 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
952 }
Brandon Jones22502d52014-08-29 16:58:36 -0700953
Geoff Langb543aff2014-09-30 14:52:54 -0400954 *outExectuable = vertexExecutable;
955 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700956}
957
Geoff Lang7dd2e102014-11-10 15:19:26 -0500958LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
959 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700960{
Brandon Jones18bd4102014-09-22 14:21:44 -0700961 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
962 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700963
Jamie Madill6df9b372015-02-18 21:28:19 +0000964 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
965 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
966 ShaderExecutableD3D *defaultVertexExecutable = NULL;
967 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
968 if (error.isError())
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800969 {
Jamie Madill6df9b372015-02-18 21:28:19 +0000970 return LinkResult(false, error);
971 }
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800972
Brandon Joneseb994362014-09-24 10:27:28 -0700973 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500974 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill6df9b372015-02-18 21:28:19 +0000975 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400976 if (error.isError())
977 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500978 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400979 }
Brandon Jones44151a92014-09-10 11:32:25 -0700980
Brandon Joneseb994362014-09-24 10:27:28 -0700981 if (usesGeometryShader())
982 {
983 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -0700984
Geoff Langb543aff2014-09-30 14:52:54 -0400985
986 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
987 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
988 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
989 if (error.isError())
990 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500991 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400992 }
Brandon Joneseb994362014-09-24 10:27:28 -0700993 }
994
Brandon Jones091540d2014-10-29 11:32:04 -0700995#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +0200996 if (usesGeometryShader() && mGeometryExecutable)
997 {
998 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
999 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1000 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1001 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1002 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1003 }
1004
1005 if (defaultVertexExecutable)
1006 {
1007 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1008 }
1009
1010 if (defaultPixelExecutable)
1011 {
1012 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1013 }
1014#endif
1015
Geoff Langb543aff2014-09-30 14:52:54 -04001016 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001017 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001018}
1019
Geoff Lang7dd2e102014-11-10 15:19:26 -05001020LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1021 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1022 const std::vector<std::string> &transformFeedbackVaryings,
1023 GLenum transformFeedbackBufferMode,
1024 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1025 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001026{
Brandon Joneseb994362014-09-24 10:27:28 -07001027 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1028 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1029
Jamie Madillde8892b2014-11-11 13:00:22 -05001030 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1031 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001032
Brandon Joneseb994362014-09-24 10:27:28 -07001033 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001034
1035 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
1036 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1037
1038 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1039 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001040 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001041
1042 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001043 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001044 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1045
Geoff Langbdee2d52014-09-17 11:02:51 -04001046 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001047 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001048 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001049 }
1050
Geoff Lang7dd2e102014-11-10 15:19:26 -05001051 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001052 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001053 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001054 }
1055
Jamie Madillde8892b2014-11-11 13:00:22 -05001056 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001057 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1058 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1059 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001060 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001061 }
1062
Brandon Jones44151a92014-09-10 11:32:25 -07001063 mUsesPointSize = vertexShaderD3D->usesPointSize();
1064
Jamie Madill437d2662014-12-05 14:23:35 -05001065 initAttributesByLayout();
1066
Geoff Lang7dd2e102014-11-10 15:19:26 -05001067 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001068}
1069
Brandon Jones44151a92014-09-10 11:32:25 -07001070void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1071{
1072 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1073}
1074
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001075void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001076{
1077 // Compute total default block size
1078 unsigned int vertexRegisters = 0;
1079 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001080 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001081 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001082 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001083
Geoff Lang2ec386b2014-12-03 14:44:38 -05001084 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001085 {
1086 if (uniform.isReferencedByVertexShader())
1087 {
1088 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1089 }
1090 if (uniform.isReferencedByFragmentShader())
1091 {
1092 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1093 }
1094 }
1095 }
1096
1097 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1098 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1099}
1100
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001101gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001102{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001103 updateSamplerMapping();
1104
1105 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1106 if (error.isError())
1107 {
1108 return error;
1109 }
1110
1111 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1112 {
1113 mUniforms[uniformIndex]->dirty = false;
1114 }
1115
1116 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001117}
1118
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001119gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001120{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001121 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1122
Brandon Jones18bd4102014-09-22 14:21:44 -07001123 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1124 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1125
1126 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1127 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1128
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001129 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001130 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001131 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001132 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1133
1134 ASSERT(uniformBlock && uniformBuffer);
1135
1136 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1137 {
1138 // undefined behaviour
1139 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1140 }
1141
1142 // Unnecessary to apply an unreferenced standard or shared UBO
1143 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1144 {
1145 continue;
1146 }
1147
1148 if (uniformBlock->isReferencedByVertexShader())
1149 {
1150 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1151 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1152 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1153 vertexUniformBuffers[registerIndex] = uniformBuffer;
1154 }
1155
1156 if (uniformBlock->isReferencedByFragmentShader())
1157 {
1158 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1159 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1160 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1161 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1162 }
1163 }
1164
1165 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1166}
1167
1168bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1169 unsigned int registerIndex, const gl::Caps &caps)
1170{
1171 if (shader == GL_VERTEX_SHADER)
1172 {
1173 uniformBlock->vsRegisterIndex = registerIndex;
1174 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1175 {
1176 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1177 return false;
1178 }
1179 }
1180 else if (shader == GL_FRAGMENT_SHADER)
1181 {
1182 uniformBlock->psRegisterIndex = registerIndex;
1183 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1184 {
1185 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1186 return false;
1187 }
1188 }
1189 else UNREACHABLE();
1190
1191 return true;
1192}
1193
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001194void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001195{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001196 unsigned int numUniforms = mUniforms.size();
1197 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001198 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001199 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001200 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001201}
1202
1203void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1204{
1205 setUniform(location, count, v, GL_FLOAT);
1206}
1207
1208void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1209{
1210 setUniform(location, count, v, GL_FLOAT_VEC2);
1211}
1212
1213void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1214{
1215 setUniform(location, count, v, GL_FLOAT_VEC3);
1216}
1217
1218void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1219{
1220 setUniform(location, count, v, GL_FLOAT_VEC4);
1221}
1222
1223void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1224{
1225 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1226}
1227
1228void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1229{
1230 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1231}
1232
1233void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1234{
1235 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1236}
1237
1238void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1239{
1240 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1241}
1242
1243void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1244{
1245 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1246}
1247
1248void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1249{
1250 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1251}
1252
1253void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1254{
1255 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1256}
1257
1258void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1259{
1260 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1261}
1262
1263void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1264{
1265 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1266}
1267
1268void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1269{
1270 setUniform(location, count, v, GL_INT);
1271}
1272
1273void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1274{
1275 setUniform(location, count, v, GL_INT_VEC2);
1276}
1277
1278void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1279{
1280 setUniform(location, count, v, GL_INT_VEC3);
1281}
1282
1283void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1284{
1285 setUniform(location, count, v, GL_INT_VEC4);
1286}
1287
1288void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1289{
1290 setUniform(location, count, v, GL_UNSIGNED_INT);
1291}
1292
1293void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1294{
1295 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1296}
1297
1298void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1299{
1300 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1301}
1302
1303void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1304{
1305 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1306}
1307
1308void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1309{
1310 getUniformv(location, params, GL_FLOAT);
1311}
1312
1313void ProgramD3D::getUniformiv(GLint location, GLint *params)
1314{
1315 getUniformv(location, params, GL_INT);
1316}
1317
1318void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1319{
1320 getUniformv(location, params, GL_UNSIGNED_INT);
1321}
1322
1323bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1324 const gl::Caps &caps)
1325{
Jamie Madill30d6c252014-11-13 10:03:33 -05001326 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1327 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001328
1329 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1330 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1331
1332 // Check that uniforms defined in the vertex and fragment shaders are identical
1333 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1334 UniformMap linkedUniforms;
1335
1336 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001337 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001338 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1339 linkedUniforms[vertexUniform.name] = &vertexUniform;
1340 }
1341
1342 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1343 {
1344 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1345 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1346 if (entry != linkedUniforms.end())
1347 {
1348 const sh::Uniform &vertexUniform = *entry->second;
1349 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001350 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001351 {
1352 return false;
1353 }
1354 }
1355 }
1356
1357 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1358 {
1359 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1360
1361 if (uniform.staticUse)
1362 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001363 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001364 }
1365 }
1366
1367 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1368 {
1369 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1370
1371 if (uniform.staticUse)
1372 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001373 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001374 }
1375 }
1376
1377 if (!indexUniforms(infoLog, caps))
1378 {
1379 return false;
1380 }
1381
1382 initializeUniformStorage();
1383
1384 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1385 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1386 {
1387 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1388
1389 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1390 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1391 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1392 }
1393
1394 return true;
1395}
1396
Geoff Lang492a7e42014-11-05 13:27:06 -05001397void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001398{
Geoff Lang492a7e42014-11-05 13:27:06 -05001399 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001400 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1401 encoder.skipRegisters(uniformRegister);
1402
1403 defineUniform(shader, uniform, uniform.name, &encoder);
1404}
1405
Geoff Lang492a7e42014-11-05 13:27:06 -05001406void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1407 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001408{
1409 if (uniform.isStruct())
1410 {
1411 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1412 {
1413 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1414
1415 encoder->enterAggregateType();
1416
1417 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1418 {
1419 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1420 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1421
1422 defineUniform(shader, field, fieldFullName, encoder);
1423 }
1424
1425 encoder->exitAggregateType();
1426 }
1427 }
1428 else // Not a struct
1429 {
1430 // Arrays are treated as aggregate types
1431 if (uniform.isArray())
1432 {
1433 encoder->enterAggregateType();
1434 }
1435
1436 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1437
Jamie Madill2857f482015-02-09 15:35:29 -05001438 // Advance the uniform offset, to track registers allocation for structs
1439 sh::BlockMemberInfo blockInfo = encoder->encodeType(uniform.type, uniform.arraySize, false);
1440
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001441 if (!linkedUniform)
1442 {
1443 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001444 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001445 ASSERT(linkedUniform);
Jamie Madill2857f482015-02-09 15:35:29 -05001446 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001447 mUniforms.push_back(linkedUniform);
1448 }
1449
Geoff Lang492a7e42014-11-05 13:27:06 -05001450 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001451 {
Jamie Madill2857f482015-02-09 15:35:29 -05001452 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001453 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001454 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001455 {
Jamie Madill2857f482015-02-09 15:35:29 -05001456 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001457 }
1458 else UNREACHABLE();
1459
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001460 // Arrays are treated as aggregate types
1461 if (uniform.isArray())
1462 {
1463 encoder->exitAggregateType();
1464 }
1465 }
1466}
1467
1468template <typename T>
1469static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1470{
1471 ASSERT(dest != NULL);
1472 ASSERT(dirtyFlag != NULL);
1473
1474 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1475 *dest = source;
1476}
1477
1478template <typename T>
1479void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1480{
1481 const int components = gl::VariableComponentCount(targetUniformType);
1482 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1483
1484 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1485
1486 int elementCount = targetUniform->elementCount();
1487
1488 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1489
1490 if (targetUniform->type == targetUniformType)
1491 {
1492 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1493
1494 for (int i = 0; i < count; i++)
1495 {
1496 T *dest = target + (i * 4);
1497 const T *source = v + (i * components);
1498
1499 for (int c = 0; c < components; c++)
1500 {
1501 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1502 }
1503 for (int c = components; c < 4; c++)
1504 {
1505 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1506 }
1507 }
1508 }
1509 else if (targetUniform->type == targetBoolType)
1510 {
1511 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1512
1513 for (int i = 0; i < count; i++)
1514 {
1515 GLint *dest = boolParams + (i * 4);
1516 const T *source = v + (i * components);
1517
1518 for (int c = 0; c < components; c++)
1519 {
1520 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1521 }
1522 for (int c = components; c < 4; c++)
1523 {
1524 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1525 }
1526 }
1527 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001528 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001529 {
1530 ASSERT(targetUniformType == GL_INT);
1531
1532 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1533
1534 bool wasDirty = targetUniform->dirty;
1535
1536 for (int i = 0; i < count; i++)
1537 {
1538 GLint *dest = target + (i * 4);
1539 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1540
1541 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1542 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1543 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1544 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1545 }
1546
1547 if (!wasDirty && targetUniform->dirty)
1548 {
1549 mDirtySamplerMapping = true;
1550 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001551 }
1552 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001553}
Brandon Jones18bd4102014-09-22 14:21:44 -07001554
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001555template<typename T>
1556bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1557{
1558 bool dirty = false;
1559 int copyWidth = std::min(targetHeight, srcWidth);
1560 int copyHeight = std::min(targetWidth, srcHeight);
1561
1562 for (int x = 0; x < copyWidth; x++)
1563 {
1564 for (int y = 0; y < copyHeight; y++)
1565 {
1566 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1567 }
1568 }
1569 // clear unfilled right side
1570 for (int y = 0; y < copyWidth; y++)
1571 {
1572 for (int x = copyHeight; x < targetWidth; x++)
1573 {
1574 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1575 }
1576 }
1577 // clear unfilled bottom.
1578 for (int y = copyWidth; y < targetHeight; y++)
1579 {
1580 for (int x = 0; x < targetWidth; x++)
1581 {
1582 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1583 }
1584 }
1585
1586 return dirty;
1587}
1588
1589template<typename T>
1590bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1591{
1592 bool dirty = false;
1593 int copyWidth = std::min(targetWidth, srcWidth);
1594 int copyHeight = std::min(targetHeight, srcHeight);
1595
1596 for (int y = 0; y < copyHeight; y++)
1597 {
1598 for (int x = 0; x < copyWidth; x++)
1599 {
1600 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1601 }
1602 }
1603 // clear unfilled right side
1604 for (int y = 0; y < copyHeight; y++)
1605 {
1606 for (int x = copyWidth; x < targetWidth; x++)
1607 {
1608 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1609 }
1610 }
1611 // clear unfilled bottom.
1612 for (int y = copyHeight; y < targetHeight; y++)
1613 {
1614 for (int x = 0; x < targetWidth; x++)
1615 {
1616 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1617 }
1618 }
1619
1620 return dirty;
1621}
1622
1623template <int cols, int rows>
1624void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1625{
1626 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1627
1628 int elementCount = targetUniform->elementCount();
1629
1630 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1631 const unsigned int targetMatrixStride = (4 * rows);
1632 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1633
1634 for (int i = 0; i < count; i++)
1635 {
1636 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1637 if (transpose == GL_FALSE)
1638 {
1639 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1640 }
1641 else
1642 {
1643 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1644 }
1645 target += targetMatrixStride;
1646 value += cols * rows;
1647 }
1648}
1649
1650template <typename T>
1651void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1652{
1653 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1654
1655 if (gl::IsMatrixType(targetUniform->type))
1656 {
1657 const int rows = gl::VariableRowCount(targetUniform->type);
1658 const int cols = gl::VariableColumnCount(targetUniform->type);
1659 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1660 }
1661 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1662 {
1663 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1664 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1665 size * sizeof(T));
1666 }
1667 else
1668 {
1669 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1670 switch (gl::VariableComponentType(targetUniform->type))
1671 {
1672 case GL_BOOL:
1673 {
1674 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1675
1676 for (unsigned int i = 0; i < size; i++)
1677 {
1678 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1679 }
1680 }
1681 break;
1682
1683 case GL_FLOAT:
1684 {
1685 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1686
1687 for (unsigned int i = 0; i < size; i++)
1688 {
1689 params[i] = static_cast<T>(floatParams[i]);
1690 }
1691 }
1692 break;
1693
1694 case GL_INT:
1695 {
1696 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1697
1698 for (unsigned int i = 0; i < size; i++)
1699 {
1700 params[i] = static_cast<T>(intParams[i]);
1701 }
1702 }
1703 break;
1704
1705 case GL_UNSIGNED_INT:
1706 {
1707 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1708
1709 for (unsigned int i = 0; i < size; i++)
1710 {
1711 params[i] = static_cast<T>(uintParams[i]);
1712 }
1713 }
1714 break;
1715
1716 default: UNREACHABLE();
1717 }
1718 }
1719}
1720
1721template <typename VarT>
1722void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1723 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1724 bool inRowMajorLayout)
1725{
1726 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1727 {
1728 const VarT &field = fields[uniformIndex];
1729 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1730
1731 if (field.isStruct())
1732 {
1733 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1734
1735 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1736 {
1737 encoder->enterAggregateType();
1738
1739 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1740 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1741
1742 encoder->exitAggregateType();
1743 }
1744 }
1745 else
1746 {
1747 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1748
1749 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1750
1751 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1752 blockIndex, memberInfo);
1753
1754 // add to uniform list, but not index, since uniform block uniforms have no location
1755 blockUniformIndexes->push_back(mUniforms.size());
1756 mUniforms.push_back(newUniform);
1757 }
1758 }
1759}
1760
1761bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1762 const gl::Caps &caps)
1763{
Jamie Madill30d6c252014-11-13 10:03:33 -05001764 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001765
1766 // create uniform block entries if they do not exist
1767 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1768 {
1769 std::vector<unsigned int> blockUniformIndexes;
1770 const unsigned int blockIndex = mUniformBlocks.size();
1771
1772 // define member uniforms
1773 sh::BlockLayoutEncoder *encoder = NULL;
1774
1775 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1776 {
1777 encoder = new sh::Std140BlockEncoder;
1778 }
1779 else
1780 {
1781 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1782 }
1783 ASSERT(encoder);
1784
1785 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1786
1787 size_t dataSize = encoder->getBlockSize();
1788
1789 // create all the uniform blocks
1790 if (interfaceBlock.arraySize > 0)
1791 {
1792 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1793 {
1794 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1795 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1796 mUniformBlocks.push_back(newUniformBlock);
1797 }
1798 }
1799 else
1800 {
1801 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1802 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1803 mUniformBlocks.push_back(newUniformBlock);
1804 }
1805 }
1806
1807 if (interfaceBlock.staticUse)
1808 {
1809 // Assign registers to the uniform blocks
1810 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1811 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1812 ASSERT(blockIndex != GL_INVALID_INDEX);
1813 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1814
1815 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1816
1817 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1818 {
1819 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1820 ASSERT(uniformBlock->name == interfaceBlock.name);
1821
1822 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1823 interfaceBlockRegister + uniformBlockElement, caps))
1824 {
1825 return false;
1826 }
1827 }
1828 }
1829
1830 return true;
1831}
1832
1833bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1834 GLenum samplerType,
1835 unsigned int samplerCount,
1836 std::vector<Sampler> &outSamplers,
1837 GLuint *outUsedRange)
1838{
1839 unsigned int samplerIndex = startSamplerIndex;
1840
1841 do
1842 {
1843 if (samplerIndex < outSamplers.size())
1844 {
1845 Sampler& sampler = outSamplers[samplerIndex];
1846 sampler.active = true;
1847 sampler.textureType = GetTextureType(samplerType);
1848 sampler.logicalTextureUnit = 0;
1849 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1850 }
1851 else
1852 {
1853 return false;
1854 }
1855
1856 samplerIndex++;
1857 } while (samplerIndex < startSamplerIndex + samplerCount);
1858
1859 return true;
1860}
1861
1862bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1863{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001864 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001865 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1866
1867 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1868 {
1869 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1870 &mUsedVertexSamplerRange))
1871 {
1872 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1873 mSamplersVS.size());
1874 return false;
1875 }
1876
1877 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1878 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1879 {
1880 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1881 caps.maxVertexUniformVectors);
1882 return false;
1883 }
1884 }
1885
1886 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1887 {
1888 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1889 &mUsedPixelSamplerRange))
1890 {
1891 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1892 mSamplersPS.size());
1893 return false;
1894 }
1895
1896 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1897 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1898 {
1899 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1900 caps.maxFragmentUniformVectors);
1901 return false;
1902 }
1903 }
1904
1905 return true;
1906}
1907
1908bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1909{
1910 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1911 {
1912 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1913
Geoff Lang2ec386b2014-12-03 14:44:38 -05001914 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001915 {
1916 if (!indexSamplerUniform(uniform, infoLog, caps))
1917 {
1918 return false;
1919 }
1920 }
1921
1922 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1923 {
1924 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1925 }
1926 }
1927
1928 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001929}
1930
Brandon Jonesc9610c52014-08-25 17:02:59 -07001931void ProgramD3D::reset()
1932{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001933 ProgramImpl::reset();
1934
Brandon Joneseb994362014-09-24 10:27:28 -07001935 SafeDeleteContainer(mVertexExecutables);
1936 SafeDeleteContainer(mPixelExecutables);
1937 SafeDelete(mGeometryExecutable);
1938
1939 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001940
Brandon Jones22502d52014-08-29 16:58:36 -07001941 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001942 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001943 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001944
1945 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001946 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001947 mUsesFragDepth = false;
1948 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001949 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001950
Brandon Jonesc9610c52014-08-25 17:02:59 -07001951 SafeDelete(mVertexUniformStorage);
1952 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001953
1954 mSamplersPS.clear();
1955 mSamplersVS.clear();
1956
1957 mUsedVertexSamplerRange = 0;
1958 mUsedPixelSamplerRange = 0;
1959 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001960
1961 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001962}
1963
Geoff Lang7dd2e102014-11-10 15:19:26 -05001964unsigned int ProgramD3D::getSerial() const
1965{
1966 return mSerial;
1967}
1968
1969unsigned int ProgramD3D::issueSerial()
1970{
1971 return mCurrentSerial++;
1972}
1973
Jamie Madill437d2662014-12-05 14:23:35 -05001974void ProgramD3D::initAttributesByLayout()
1975{
1976 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1977 {
1978 mAttributesByLayout[i] = i;
1979 }
1980
1981 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
1982}
1983
1984void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
1985 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
1986{
1987 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
1988
1989 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1990 {
1991 oldTranslatedAttributes[i] = attributes[i];
1992 }
1993
1994 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1995 {
1996 int oldIndex = mAttributesByLayout[i];
1997 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
1998 attributes[i] = oldTranslatedAttributes[oldIndex];
1999 }
2000}
2001
Brandon Jonesc9610c52014-08-25 17:02:59 -07002002}