blob: 2cf504d1bbbbb1198f6fd8e28f26e81b7b4dd9ad [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
Austin Kinross31018482015-01-30 13:06:52 -08009// <future> requires _HAS_EXCEPTIONS to be defined
10#ifdef _HAS_EXCEPTIONS
11#undef _HAS_EXCEPTIONS
12#endif // _HAS_EXCEPTIONS
13#define _HAS_EXCEPTIONS 1
14#include <future> // For std::async
15
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050017
18#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/Framebuffer.h"
20#include "libANGLE/FramebufferAttachment.h"
21#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050022#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050023#include "libANGLE/renderer/d3d/DynamicHLSL.h"
24#include "libANGLE/renderer/d3d/RendererD3D.h"
25#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050026#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050027#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050028
Brandon Jonesc9610c52014-08-25 17:02:59 -070029namespace rx
30{
31
Brandon Joneseb994362014-09-24 10:27:28 -070032namespace
33{
34
Brandon Jones1a8a7e32014-10-01 12:49:30 -070035GLenum GetTextureType(GLenum samplerType)
36{
37 switch (samplerType)
38 {
39 case GL_SAMPLER_2D:
40 case GL_INT_SAMPLER_2D:
41 case GL_UNSIGNED_INT_SAMPLER_2D:
42 case GL_SAMPLER_2D_SHADOW:
43 return GL_TEXTURE_2D;
44 case GL_SAMPLER_3D:
45 case GL_INT_SAMPLER_3D:
46 case GL_UNSIGNED_INT_SAMPLER_3D:
47 return GL_TEXTURE_3D;
48 case GL_SAMPLER_CUBE:
49 case GL_SAMPLER_CUBE_SHADOW:
50 return GL_TEXTURE_CUBE_MAP;
51 case GL_INT_SAMPLER_CUBE:
52 case GL_UNSIGNED_INT_SAMPLER_CUBE:
53 return GL_TEXTURE_CUBE_MAP;
54 case GL_SAMPLER_2D_ARRAY:
55 case GL_INT_SAMPLER_2D_ARRAY:
56 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
57 case GL_SAMPLER_2D_ARRAY_SHADOW:
58 return GL_TEXTURE_2D_ARRAY;
59 default: UNREACHABLE();
60 }
61
62 return GL_TEXTURE_2D;
63}
64
Brandon Joneseb994362014-09-24 10:27:28 -070065void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS])
66{
67 size_t layoutIndex = 0;
68 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
69 {
70 ASSERT(layoutIndex < gl::MAX_VERTEX_ATTRIBS);
71
72 const sh::Attribute &shaderAttr = shaderAttributes[attributeIndex];
73
74 if (shaderAttr.type != GL_NONE)
75 {
76 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
77
78 for (size_t rowIndex = 0; static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); rowIndex++, layoutIndex++)
79 {
80 gl::VertexFormat *defaultFormat = &inputLayout[layoutIndex];
81
82 defaultFormat->mType = gl::VariableComponentType(transposedType);
83 defaultFormat->mNormalized = false;
84 defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool
85 defaultFormat->mComponents = gl::VariableColumnCount(transposedType);
86 }
87 }
88 }
89}
90
91std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
92{
Jamie Madillb4463142014-12-19 14:56:54 -050093 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070094
Jamie Madillb4463142014-12-19 14:56:54 -050095 if (!shaderOutputVars.empty())
96 {
97 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
98 }
Brandon Joneseb994362014-09-24 10:27:28 -070099
100 return defaultPixelOutput;
101}
102
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700103bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
104{
105 return var.isRowMajorLayout;
106}
107
108bool IsRowMajorLayout(const sh::ShaderVariable &var)
109{
110 return false;
111}
112
Jamie Madill437d2662014-12-05 14:23:35 -0500113struct AttributeSorter
114{
115 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
116 : originalIndices(semanticIndices)
117 {
118 }
119
120 bool operator()(int a, int b)
121 {
122 if (originalIndices[a] == -1) return false;
123 if (originalIndices[b] == -1) return true;
124 return (originalIndices[a] < originalIndices[b]);
125 }
126
127 const ProgramImpl::SemanticIndexArray &originalIndices;
128};
129
Brandon Joneseb994362014-09-24 10:27:28 -0700130}
131
132ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
133 const GLenum signature[],
Geoff Lang359ef262015-01-05 14:42:29 -0500134 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700135 : mShaderExecutable(shaderExecutable)
136{
137 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
138 {
139 mInputs[attributeIndex] = inputLayout[attributeIndex];
140 mSignature[attributeIndex] = signature[attributeIndex];
141 }
142}
143
144ProgramD3D::VertexExecutable::~VertexExecutable()
145{
146 SafeDelete(mShaderExecutable);
147}
148
149bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
150{
151 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
152 {
153 if (mSignature[attributeIndex] != signature[attributeIndex])
154 {
155 return false;
156 }
157 }
158
159 return true;
160}
161
Geoff Lang359ef262015-01-05 14:42:29 -0500162ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700163 : mOutputSignature(outputSignature),
164 mShaderExecutable(shaderExecutable)
165{
166}
167
168ProgramD3D::PixelExecutable::~PixelExecutable()
169{
170 SafeDelete(mShaderExecutable);
171}
172
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700173ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
174{
175}
176
Geoff Lang7dd2e102014-11-10 15:19:26 -0500177unsigned int ProgramD3D::mCurrentSerial = 1;
178
Jamie Madill93e13fb2014-11-06 15:27:25 -0500179ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700180 : ProgramImpl(),
181 mRenderer(renderer),
182 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700183 mGeometryExecutable(NULL),
184 mVertexWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
185 mPixelWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
Brandon Jones44151a92014-09-10 11:32:25 -0700186 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700187 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700188 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700189 mUsedVertexSamplerRange(0),
190 mUsedPixelSamplerRange(0),
191 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500192 mShaderVersion(100),
193 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700194{
Brandon Joneseb994362014-09-24 10:27:28 -0700195 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700196}
197
198ProgramD3D::~ProgramD3D()
199{
200 reset();
201 SafeDelete(mDynamicHLSL);
202}
203
Brandon Jones44151a92014-09-10 11:32:25 -0700204bool ProgramD3D::usesPointSpriteEmulation() const
205{
206 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
207}
208
209bool ProgramD3D::usesGeometryShader() const
210{
Cooper Partine6664f02015-01-09 16:22:24 -0800211 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
212}
213
214bool ProgramD3D::usesInstancedPointSpriteEmulation() const
215{
216 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700217}
218
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700219GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
220{
221 GLint logicalTextureUnit = -1;
222
223 switch (type)
224 {
225 case gl::SAMPLER_PIXEL:
226 ASSERT(samplerIndex < caps.maxTextureImageUnits);
227 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
228 {
229 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
230 }
231 break;
232 case gl::SAMPLER_VERTEX:
233 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
234 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
235 {
236 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
237 }
238 break;
239 default: UNREACHABLE();
240 }
241
242 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
243 {
244 return logicalTextureUnit;
245 }
246
247 return -1;
248}
249
250// Returns the texture type for a given Direct3D 9 sampler type and
251// index (0-15 for the pixel shader and 0-3 for the vertex shader).
252GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
253{
254 switch (type)
255 {
256 case gl::SAMPLER_PIXEL:
257 ASSERT(samplerIndex < mSamplersPS.size());
258 ASSERT(mSamplersPS[samplerIndex].active);
259 return mSamplersPS[samplerIndex].textureType;
260 case gl::SAMPLER_VERTEX:
261 ASSERT(samplerIndex < mSamplersVS.size());
262 ASSERT(mSamplersVS[samplerIndex].active);
263 return mSamplersVS[samplerIndex].textureType;
264 default: UNREACHABLE();
265 }
266
267 return GL_TEXTURE_2D;
268}
269
270GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
271{
272 switch (type)
273 {
274 case gl::SAMPLER_PIXEL:
275 return mUsedPixelSamplerRange;
276 case gl::SAMPLER_VERTEX:
277 return mUsedVertexSamplerRange;
278 default:
279 UNREACHABLE();
280 return 0;
281 }
282}
283
284void ProgramD3D::updateSamplerMapping()
285{
286 if (!mDirtySamplerMapping)
287 {
288 return;
289 }
290
291 mDirtySamplerMapping = false;
292
293 // Retrieve sampler uniform values
294 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
295 {
296 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
297
298 if (targetUniform->dirty)
299 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500300 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700301 {
302 int count = targetUniform->elementCount();
303 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
304
305 if (targetUniform->isReferencedByFragmentShader())
306 {
307 unsigned int firstIndex = targetUniform->psRegisterIndex;
308
309 for (int i = 0; i < count; i++)
310 {
311 unsigned int samplerIndex = firstIndex + i;
312
313 if (samplerIndex < mSamplersPS.size())
314 {
315 ASSERT(mSamplersPS[samplerIndex].active);
316 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
317 }
318 }
319 }
320
321 if (targetUniform->isReferencedByVertexShader())
322 {
323 unsigned int firstIndex = targetUniform->vsRegisterIndex;
324
325 for (int i = 0; i < count; i++)
326 {
327 unsigned int samplerIndex = firstIndex + i;
328
329 if (samplerIndex < mSamplersVS.size())
330 {
331 ASSERT(mSamplersVS[samplerIndex].active);
332 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
333 }
334 }
335 }
336 }
337 }
338 }
339}
340
341bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
342{
343 // if any two active samplers in a program are of different types, but refer to the same
344 // texture image unit, and this is the current program, then ValidateProgram will fail, and
345 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
346 updateSamplerMapping();
347
348 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
349
350 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
351 {
352 if (mSamplersPS[i].active)
353 {
354 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
355
356 if (unit >= textureUnitTypes.size())
357 {
358 if (infoLog)
359 {
360 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
361 }
362
363 return false;
364 }
365
366 if (textureUnitTypes[unit] != GL_NONE)
367 {
368 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
369 {
370 if (infoLog)
371 {
372 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
373 }
374
375 return false;
376 }
377 }
378 else
379 {
380 textureUnitTypes[unit] = mSamplersPS[i].textureType;
381 }
382 }
383 }
384
385 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
386 {
387 if (mSamplersVS[i].active)
388 {
389 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
390
391 if (unit >= textureUnitTypes.size())
392 {
393 if (infoLog)
394 {
395 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
396 }
397
398 return false;
399 }
400
401 if (textureUnitTypes[unit] != GL_NONE)
402 {
403 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
404 {
405 if (infoLog)
406 {
407 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
408 }
409
410 return false;
411 }
412 }
413 else
414 {
415 textureUnitTypes[unit] = mSamplersVS[i].textureType;
416 }
417 }
418 }
419
420 return true;
421}
422
Geoff Lang7dd2e102014-11-10 15:19:26 -0500423LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700424{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500425 int compileFlags = stream->readInt<int>();
426 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
427 {
428 infoLog.append("Mismatched compilation flags.");
429 return LinkResult(false, gl::Error(GL_NO_ERROR));
430 }
431
Brandon Jones44151a92014-09-10 11:32:25 -0700432 stream->readInt(&mShaderVersion);
433
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700434 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
435 for (unsigned int i = 0; i < psSamplerCount; ++i)
436 {
437 Sampler sampler;
438 stream->readBool(&sampler.active);
439 stream->readInt(&sampler.logicalTextureUnit);
440 stream->readInt(&sampler.textureType);
441 mSamplersPS.push_back(sampler);
442 }
443 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
444 for (unsigned int i = 0; i < vsSamplerCount; ++i)
445 {
446 Sampler sampler;
447 stream->readBool(&sampler.active);
448 stream->readInt(&sampler.logicalTextureUnit);
449 stream->readInt(&sampler.textureType);
450 mSamplersVS.push_back(sampler);
451 }
452
453 stream->readInt(&mUsedVertexSamplerRange);
454 stream->readInt(&mUsedPixelSamplerRange);
455
456 const unsigned int uniformCount = stream->readInt<unsigned int>();
457 if (stream->error())
458 {
459 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500460 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700461 }
462
463 mUniforms.resize(uniformCount);
464 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
465 {
466 GLenum type = stream->readInt<GLenum>();
467 GLenum precision = stream->readInt<GLenum>();
468 std::string name = stream->readString();
469 unsigned int arraySize = stream->readInt<unsigned int>();
470 int blockIndex = stream->readInt<int>();
471
472 int offset = stream->readInt<int>();
473 int arrayStride = stream->readInt<int>();
474 int matrixStride = stream->readInt<int>();
475 bool isRowMajorMatrix = stream->readBool();
476
477 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
478
479 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
480
481 stream->readInt(&uniform->psRegisterIndex);
482 stream->readInt(&uniform->vsRegisterIndex);
483 stream->readInt(&uniform->registerCount);
484 stream->readInt(&uniform->registerElement);
485
486 mUniforms[uniformIndex] = uniform;
487 }
488
489 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
490 if (stream->error())
491 {
492 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500493 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700494 }
495
496 mUniformIndex.resize(uniformIndexCount);
497 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
498 {
499 stream->readString(&mUniformIndex[uniformIndexIndex].name);
500 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
501 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
502 }
503
504 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
505 if (stream->error())
506 {
507 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500508 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700509 }
510
511 mUniformBlocks.resize(uniformBlockCount);
512 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
513 {
514 std::string name = stream->readString();
515 unsigned int elementIndex = stream->readInt<unsigned int>();
516 unsigned int dataSize = stream->readInt<unsigned int>();
517
518 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
519
520 stream->readInt(&uniformBlock->psRegisterIndex);
521 stream->readInt(&uniformBlock->vsRegisterIndex);
522
523 unsigned int numMembers = stream->readInt<unsigned int>();
524 uniformBlock->memberUniformIndexes.resize(numMembers);
525 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
526 {
527 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
528 }
529
530 mUniformBlocks[uniformBlockIndex] = uniformBlock;
531 }
532
Brandon Joneseb994362014-09-24 10:27:28 -0700533 stream->readInt(&mTransformFeedbackBufferMode);
534 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
535 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
536 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
537 {
538 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
539
540 stream->readString(&varying.name);
541 stream->readInt(&varying.type);
542 stream->readInt(&varying.size);
543 stream->readString(&varying.semanticName);
544 stream->readInt(&varying.semanticIndex);
545 stream->readInt(&varying.semanticIndexCount);
546 }
547
Brandon Jones22502d52014-08-29 16:58:36 -0700548 stream->readString(&mVertexHLSL);
549 stream->readInt(&mVertexWorkarounds);
550 stream->readString(&mPixelHLSL);
551 stream->readInt(&mPixelWorkarounds);
552 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700553 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700554
555 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
556 mPixelShaderKey.resize(pixelShaderKeySize);
557 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
558 {
559 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
560 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
561 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
562 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
563 }
564
Brandon Joneseb994362014-09-24 10:27:28 -0700565 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
566
567 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
568 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
569 {
570 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
571
572 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
573 {
574 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
575 stream->readInt(&vertexInput->mType);
576 stream->readInt(&vertexInput->mNormalized);
577 stream->readInt(&vertexInput->mComponents);
578 stream->readBool(&vertexInput->mPureInteger);
579 }
580
581 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
582 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400583
Geoff Lang359ef262015-01-05 14:42:29 -0500584 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400585 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
586 SHADER_VERTEX,
587 mTransformFeedbackLinkedVaryings,
588 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
589 &shaderExecutable);
590 if (error.isError())
591 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500592 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400593 }
594
Brandon Joneseb994362014-09-24 10:27:28 -0700595 if (!shaderExecutable)
596 {
597 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500598 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700599 }
600
601 // generated converted input layout
602 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
603 getInputLayoutSignature(inputLayout, signature);
604
605 // add new binary
606 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
607
608 stream->skip(vertexShaderSize);
609 }
610
611 const size_t pixelShaderCount = stream->readInt<unsigned int>();
612 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
613 {
614 const size_t outputCount = stream->readInt<unsigned int>();
615 std::vector<GLenum> outputs(outputCount);
616 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
617 {
618 stream->readInt(&outputs[outputIndex]);
619 }
620
621 const size_t pixelShaderSize = stream->readInt<unsigned int>();
622 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500623 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400624 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
625 mTransformFeedbackLinkedVaryings,
626 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
627 &shaderExecutable);
628 if (error.isError())
629 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500630 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400631 }
Brandon Joneseb994362014-09-24 10:27:28 -0700632
633 if (!shaderExecutable)
634 {
635 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500636 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700637 }
638
639 // add new binary
640 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
641
642 stream->skip(pixelShaderSize);
643 }
644
645 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
646
647 if (geometryShaderSize > 0)
648 {
649 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400650 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
651 mTransformFeedbackLinkedVaryings,
652 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
653 &mGeometryExecutable);
654 if (error.isError())
655 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500656 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400657 }
Brandon Joneseb994362014-09-24 10:27:28 -0700658
659 if (!mGeometryExecutable)
660 {
661 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500662 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700663 }
664 stream->skip(geometryShaderSize);
665 }
666
Brandon Jones18bd4102014-09-22 14:21:44 -0700667 GUID binaryIdentifier = {0};
668 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
669
670 GUID identifier = mRenderer->getAdapterIdentifier();
671 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
672 {
673 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500674 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700675 }
676
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700677 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500678 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700679
Geoff Lang7dd2e102014-11-10 15:19:26 -0500680 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700681}
682
Geoff Langb543aff2014-09-30 14:52:54 -0400683gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700684{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500685 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
686
Brandon Jones44151a92014-09-10 11:32:25 -0700687 stream->writeInt(mShaderVersion);
688
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689 stream->writeInt(mSamplersPS.size());
690 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
691 {
692 stream->writeInt(mSamplersPS[i].active);
693 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
694 stream->writeInt(mSamplersPS[i].textureType);
695 }
696
697 stream->writeInt(mSamplersVS.size());
698 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
699 {
700 stream->writeInt(mSamplersVS[i].active);
701 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
702 stream->writeInt(mSamplersVS[i].textureType);
703 }
704
705 stream->writeInt(mUsedVertexSamplerRange);
706 stream->writeInt(mUsedPixelSamplerRange);
707
708 stream->writeInt(mUniforms.size());
709 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
710 {
711 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
712
713 stream->writeInt(uniform.type);
714 stream->writeInt(uniform.precision);
715 stream->writeString(uniform.name);
716 stream->writeInt(uniform.arraySize);
717 stream->writeInt(uniform.blockIndex);
718
719 stream->writeInt(uniform.blockInfo.offset);
720 stream->writeInt(uniform.blockInfo.arrayStride);
721 stream->writeInt(uniform.blockInfo.matrixStride);
722 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
723
724 stream->writeInt(uniform.psRegisterIndex);
725 stream->writeInt(uniform.vsRegisterIndex);
726 stream->writeInt(uniform.registerCount);
727 stream->writeInt(uniform.registerElement);
728 }
729
730 stream->writeInt(mUniformIndex.size());
731 for (size_t i = 0; i < mUniformIndex.size(); ++i)
732 {
733 stream->writeString(mUniformIndex[i].name);
734 stream->writeInt(mUniformIndex[i].element);
735 stream->writeInt(mUniformIndex[i].index);
736 }
737
738 stream->writeInt(mUniformBlocks.size());
739 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
740 {
741 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
742
743 stream->writeString(uniformBlock.name);
744 stream->writeInt(uniformBlock.elementIndex);
745 stream->writeInt(uniformBlock.dataSize);
746
747 stream->writeInt(uniformBlock.memberUniformIndexes.size());
748 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
749 {
750 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
751 }
752
753 stream->writeInt(uniformBlock.psRegisterIndex);
754 stream->writeInt(uniformBlock.vsRegisterIndex);
755 }
756
Brandon Joneseb994362014-09-24 10:27:28 -0700757 stream->writeInt(mTransformFeedbackBufferMode);
758 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
759 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
760 {
761 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
762
763 stream->writeString(varying.name);
764 stream->writeInt(varying.type);
765 stream->writeInt(varying.size);
766 stream->writeString(varying.semanticName);
767 stream->writeInt(varying.semanticIndex);
768 stream->writeInt(varying.semanticIndexCount);
769 }
770
Brandon Jones22502d52014-08-29 16:58:36 -0700771 stream->writeString(mVertexHLSL);
772 stream->writeInt(mVertexWorkarounds);
773 stream->writeString(mPixelHLSL);
774 stream->writeInt(mPixelWorkarounds);
775 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700776 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700777
Brandon Joneseb994362014-09-24 10:27:28 -0700778 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700779 stream->writeInt(pixelShaderKey.size());
780 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
781 {
Brandon Joneseb994362014-09-24 10:27:28 -0700782 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700783 stream->writeInt(variable.type);
784 stream->writeString(variable.name);
785 stream->writeString(variable.source);
786 stream->writeInt(variable.outputIndex);
787 }
788
Brandon Joneseb994362014-09-24 10:27:28 -0700789 stream->writeInt(mVertexExecutables.size());
790 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
791 {
792 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
793
794 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
795 {
796 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
797 stream->writeInt(vertexInput.mType);
798 stream->writeInt(vertexInput.mNormalized);
799 stream->writeInt(vertexInput.mComponents);
800 stream->writeInt(vertexInput.mPureInteger);
801 }
802
803 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
804 stream->writeInt(vertexShaderSize);
805
806 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
807 stream->writeBytes(vertexBlob, vertexShaderSize);
808 }
809
810 stream->writeInt(mPixelExecutables.size());
811 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
812 {
813 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
814
815 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
816 stream->writeInt(outputs.size());
817 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
818 {
819 stream->writeInt(outputs[outputIndex]);
820 }
821
822 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
823 stream->writeInt(pixelShaderSize);
824
825 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
826 stream->writeBytes(pixelBlob, pixelShaderSize);
827 }
828
829 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
830 stream->writeInt(geometryShaderSize);
831
832 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
833 {
834 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
835 stream->writeBytes(geometryBlob, geometryShaderSize);
836 }
837
Brandon Jones18bd4102014-09-22 14:21:44 -0700838 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400839 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700840
Geoff Langb543aff2014-09-30 14:52:54 -0400841 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700842}
843
Geoff Lang359ef262015-01-05 14:42:29 -0500844gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700845{
Brandon Joneseb994362014-09-24 10:27:28 -0700846 std::vector<GLenum> outputs;
847
Jamie Madill48faf802014-11-06 15:27:22 -0500848 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700849
850 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
851 {
852 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
853
854 if (colorbuffer)
855 {
856 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
857 }
858 else
859 {
860 outputs.push_back(GL_NONE);
861 }
862 }
863
Jamie Madill97399232014-12-23 12:31:15 -0500864 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700865}
866
Jamie Madill97399232014-12-23 12:31:15 -0500867gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500868 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500869 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700870{
871 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
872 {
873 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
874 {
Geoff Langb543aff2014-09-30 14:52:54 -0400875 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
876 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700877 }
878 }
879
Brandon Jones22502d52014-08-29 16:58:36 -0700880 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
881 outputSignature);
882
883 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500884 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500885
886 gl::InfoLog tempInfoLog;
887 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
888
889 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400890 mTransformFeedbackLinkedVaryings,
891 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
892 mPixelWorkarounds, &pixelExecutable);
893 if (error.isError())
894 {
895 return error;
896 }
Brandon Joneseb994362014-09-24 10:27:28 -0700897
Jamie Madill97399232014-12-23 12:31:15 -0500898 if (pixelExecutable)
899 {
900 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
901 }
902 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700903 {
904 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
905 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
906 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
907 }
Brandon Jones22502d52014-08-29 16:58:36 -0700908
Geoff Langb543aff2014-09-30 14:52:54 -0400909 *outExectuable = pixelExecutable;
910 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700911}
912
Jamie Madill97399232014-12-23 12:31:15 -0500913gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500914 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500915 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700916{
Brandon Joneseb994362014-09-24 10:27:28 -0700917 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
918 getInputLayoutSignature(inputLayout, signature);
919
920 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
921 {
922 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
923 {
Geoff Langb543aff2014-09-30 14:52:54 -0400924 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
925 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700926 }
927 }
928
Brandon Jones22502d52014-08-29 16:58:36 -0700929 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700930 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700931
932 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500933 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500934
935 gl::InfoLog tempInfoLog;
936 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
937
938 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400939 mTransformFeedbackLinkedVaryings,
940 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
941 mVertexWorkarounds, &vertexExecutable);
942 if (error.isError())
943 {
944 return error;
945 }
946
Jamie Madill97399232014-12-23 12:31:15 -0500947 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700948 {
949 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
950 }
Jamie Madill97399232014-12-23 12:31:15 -0500951 else if (!infoLog)
952 {
Austin Kinross31018482015-01-30 13:06:52 -0800953 // This isn't thread-safe, so we should ensure that we always pass in an infoLog if using multiple threads.
Jamie Madill97399232014-12-23 12:31:15 -0500954 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
955 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
956 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
957 }
Brandon Jones22502d52014-08-29 16:58:36 -0700958
Geoff Langb543aff2014-09-30 14:52:54 -0400959 *outExectuable = vertexExecutable;
960 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700961}
962
Geoff Lang7dd2e102014-11-10 15:19:26 -0500963LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
964 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700965{
Brandon Jones18bd4102014-09-22 14:21:44 -0700966 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
967 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700968
Austin Kinross31018482015-01-30 13:06:52 -0800969 gl::Error vertexShaderTaskResult(GL_NO_ERROR);
970 gl::InfoLog tempVertexShaderInfoLog;
Brandon Jones44151a92014-09-10 11:32:25 -0700971
Austin Kinross31018482015-01-30 13:06:52 -0800972 // Use an async task to begin compiling the vertex shader asynchronously on its own task.
973 std::future<ShaderExecutableD3D*> vertexShaderTask = std::async([this, vertexShader, &tempVertexShaderInfoLog, &vertexShaderTaskResult]()
974 {
975 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
976 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
977 ShaderExecutableD3D *defaultVertexExecutable = NULL;
978 vertexShaderTaskResult = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &tempVertexShaderInfoLog);
979 return defaultVertexExecutable;
980 });
981
982 // Continue to compile the pixel shader on the main thread
Brandon Joneseb994362014-09-24 10:27:28 -0700983 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500984 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Austin Kinross31018482015-01-30 13:06:52 -0800985 gl::Error error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
986
987 // Call .get() on the vertex shader compilation. This waits until the task is complete before returning
988 ShaderExecutableD3D *defaultVertexExecutable = vertexShaderTask.get();
989
990 // Combine the temporary infoLog with the real one
991 if (tempVertexShaderInfoLog.getLength() > 0)
992 {
993 std::vector<char> tempCharBuffer(tempVertexShaderInfoLog.getLength() + 3);
994 tempVertexShaderInfoLog.getLog(tempVertexShaderInfoLog.getLength(), NULL, &tempCharBuffer[0]);
995 infoLog.append(&tempCharBuffer[0]);
996 }
997
998 if (vertexShaderTaskResult.isError())
999 {
1000 return LinkResult(false, vertexShaderTaskResult);
1001 }
1002
1003 // If the pixel shader compilation failed, then return error
Geoff Langb543aff2014-09-30 14:52:54 -04001004 if (error.isError())
1005 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001006 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001007 }
Brandon Jones44151a92014-09-10 11:32:25 -07001008
Brandon Joneseb994362014-09-24 10:27:28 -07001009 if (usesGeometryShader())
1010 {
1011 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001012
Geoff Langb543aff2014-09-30 14:52:54 -04001013
1014 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1015 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
1016 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
1017 if (error.isError())
1018 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001019 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001020 }
Brandon Joneseb994362014-09-24 10:27:28 -07001021 }
1022
Brandon Jones091540d2014-10-29 11:32:04 -07001023#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001024 if (usesGeometryShader() && mGeometryExecutable)
1025 {
1026 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1027 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1028 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1029 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1030 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1031 }
1032
1033 if (defaultVertexExecutable)
1034 {
1035 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1036 }
1037
1038 if (defaultPixelExecutable)
1039 {
1040 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1041 }
1042#endif
1043
Geoff Langb543aff2014-09-30 14:52:54 -04001044 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001045 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001046}
1047
Geoff Lang7dd2e102014-11-10 15:19:26 -05001048LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1049 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1050 const std::vector<std::string> &transformFeedbackVaryings,
1051 GLenum transformFeedbackBufferMode,
1052 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1053 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001054{
Brandon Joneseb994362014-09-24 10:27:28 -07001055 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1056 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1057
Jamie Madillde8892b2014-11-11 13:00:22 -05001058 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1059 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001060
Brandon Joneseb994362014-09-24 10:27:28 -07001061 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001062
1063 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
1064 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1065
1066 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1067 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001068 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001069
1070 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001071 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001072 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1073
Geoff Langbdee2d52014-09-17 11:02:51 -04001074 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001075 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001077 }
1078
Geoff Lang7dd2e102014-11-10 15:19:26 -05001079 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001080 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001082 }
1083
Jamie Madillde8892b2014-11-11 13:00:22 -05001084 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001085 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1086 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1087 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001088 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001089 }
1090
Brandon Jones44151a92014-09-10 11:32:25 -07001091 mUsesPointSize = vertexShaderD3D->usesPointSize();
1092
Jamie Madill437d2662014-12-05 14:23:35 -05001093 initAttributesByLayout();
1094
Geoff Lang7dd2e102014-11-10 15:19:26 -05001095 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001096}
1097
Brandon Jones44151a92014-09-10 11:32:25 -07001098void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1099{
1100 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1101}
1102
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001103void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001104{
1105 // Compute total default block size
1106 unsigned int vertexRegisters = 0;
1107 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001108 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001109 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001110 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001111
Geoff Lang2ec386b2014-12-03 14:44:38 -05001112 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001113 {
1114 if (uniform.isReferencedByVertexShader())
1115 {
1116 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1117 }
1118 if (uniform.isReferencedByFragmentShader())
1119 {
1120 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1121 }
1122 }
1123 }
1124
1125 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1126 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1127}
1128
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001129gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001130{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001131 updateSamplerMapping();
1132
1133 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1134 if (error.isError())
1135 {
1136 return error;
1137 }
1138
1139 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1140 {
1141 mUniforms[uniformIndex]->dirty = false;
1142 }
1143
1144 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001145}
1146
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001147gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001148{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001149 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1150
Brandon Jones18bd4102014-09-22 14:21:44 -07001151 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1152 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1153
1154 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1155 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1156
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001157 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001158 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001159 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001160 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1161
1162 ASSERT(uniformBlock && uniformBuffer);
1163
1164 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1165 {
1166 // undefined behaviour
1167 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1168 }
1169
1170 // Unnecessary to apply an unreferenced standard or shared UBO
1171 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1172 {
1173 continue;
1174 }
1175
1176 if (uniformBlock->isReferencedByVertexShader())
1177 {
1178 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1179 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1180 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1181 vertexUniformBuffers[registerIndex] = uniformBuffer;
1182 }
1183
1184 if (uniformBlock->isReferencedByFragmentShader())
1185 {
1186 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1187 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1188 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1189 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1190 }
1191 }
1192
1193 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1194}
1195
1196bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1197 unsigned int registerIndex, const gl::Caps &caps)
1198{
1199 if (shader == GL_VERTEX_SHADER)
1200 {
1201 uniformBlock->vsRegisterIndex = registerIndex;
1202 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1203 {
1204 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1205 return false;
1206 }
1207 }
1208 else if (shader == GL_FRAGMENT_SHADER)
1209 {
1210 uniformBlock->psRegisterIndex = registerIndex;
1211 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1212 {
1213 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1214 return false;
1215 }
1216 }
1217 else UNREACHABLE();
1218
1219 return true;
1220}
1221
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001222void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001223{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001224 unsigned int numUniforms = mUniforms.size();
1225 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001226 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001227 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001228 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001229}
1230
1231void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1232{
1233 setUniform(location, count, v, GL_FLOAT);
1234}
1235
1236void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1237{
1238 setUniform(location, count, v, GL_FLOAT_VEC2);
1239}
1240
1241void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1242{
1243 setUniform(location, count, v, GL_FLOAT_VEC3);
1244}
1245
1246void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1247{
1248 setUniform(location, count, v, GL_FLOAT_VEC4);
1249}
1250
1251void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1252{
1253 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1254}
1255
1256void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1257{
1258 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1259}
1260
1261void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1262{
1263 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1264}
1265
1266void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1267{
1268 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1269}
1270
1271void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1272{
1273 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1274}
1275
1276void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1277{
1278 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1279}
1280
1281void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1282{
1283 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1284}
1285
1286void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1287{
1288 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1289}
1290
1291void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1292{
1293 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1294}
1295
1296void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1297{
1298 setUniform(location, count, v, GL_INT);
1299}
1300
1301void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1302{
1303 setUniform(location, count, v, GL_INT_VEC2);
1304}
1305
1306void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1307{
1308 setUniform(location, count, v, GL_INT_VEC3);
1309}
1310
1311void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1312{
1313 setUniform(location, count, v, GL_INT_VEC4);
1314}
1315
1316void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1317{
1318 setUniform(location, count, v, GL_UNSIGNED_INT);
1319}
1320
1321void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1322{
1323 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1324}
1325
1326void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1327{
1328 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1329}
1330
1331void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1332{
1333 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1334}
1335
1336void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1337{
1338 getUniformv(location, params, GL_FLOAT);
1339}
1340
1341void ProgramD3D::getUniformiv(GLint location, GLint *params)
1342{
1343 getUniformv(location, params, GL_INT);
1344}
1345
1346void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1347{
1348 getUniformv(location, params, GL_UNSIGNED_INT);
1349}
1350
1351bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1352 const gl::Caps &caps)
1353{
Jamie Madill30d6c252014-11-13 10:03:33 -05001354 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1355 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001356
1357 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1358 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1359
1360 // Check that uniforms defined in the vertex and fragment shaders are identical
1361 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1362 UniformMap linkedUniforms;
1363
1364 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001365 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001366 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1367 linkedUniforms[vertexUniform.name] = &vertexUniform;
1368 }
1369
1370 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1371 {
1372 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1373 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1374 if (entry != linkedUniforms.end())
1375 {
1376 const sh::Uniform &vertexUniform = *entry->second;
1377 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001379 {
1380 return false;
1381 }
1382 }
1383 }
1384
1385 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1386 {
1387 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1388
1389 if (uniform.staticUse)
1390 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001391 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001392 }
1393 }
1394
1395 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1396 {
1397 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1398
1399 if (uniform.staticUse)
1400 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001401 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001402 }
1403 }
1404
1405 if (!indexUniforms(infoLog, caps))
1406 {
1407 return false;
1408 }
1409
1410 initializeUniformStorage();
1411
1412 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1413 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1414 {
1415 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1416
1417 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1418 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1419 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1420 }
1421
1422 return true;
1423}
1424
Geoff Lang492a7e42014-11-05 13:27:06 -05001425void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001426{
Geoff Lang492a7e42014-11-05 13:27:06 -05001427 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001428 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1429 encoder.skipRegisters(uniformRegister);
1430
1431 defineUniform(shader, uniform, uniform.name, &encoder);
1432}
1433
Geoff Lang492a7e42014-11-05 13:27:06 -05001434void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1435 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001436{
1437 if (uniform.isStruct())
1438 {
1439 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1440 {
1441 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1442
1443 encoder->enterAggregateType();
1444
1445 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1446 {
1447 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1448 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1449
1450 defineUniform(shader, field, fieldFullName, encoder);
1451 }
1452
1453 encoder->exitAggregateType();
1454 }
1455 }
1456 else // Not a struct
1457 {
1458 // Arrays are treated as aggregate types
1459 if (uniform.isArray())
1460 {
1461 encoder->enterAggregateType();
1462 }
1463
1464 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1465
1466 if (!linkedUniform)
1467 {
1468 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
1469 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
1470 ASSERT(linkedUniform);
1471 linkedUniform->registerElement = encoder->getCurrentElement();
1472 mUniforms.push_back(linkedUniform);
1473 }
1474
1475 ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
1476
Geoff Lang492a7e42014-11-05 13:27:06 -05001477 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001478 {
1479 linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
1480 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001481 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001482 {
1483 linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
1484 }
1485 else UNREACHABLE();
1486
1487 // Advance the uniform offset, to track registers allocation for structs
1488 encoder->encodeType(uniform.type, uniform.arraySize, false);
1489
1490 // Arrays are treated as aggregate types
1491 if (uniform.isArray())
1492 {
1493 encoder->exitAggregateType();
1494 }
1495 }
1496}
1497
1498template <typename T>
1499static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1500{
1501 ASSERT(dest != NULL);
1502 ASSERT(dirtyFlag != NULL);
1503
1504 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1505 *dest = source;
1506}
1507
1508template <typename T>
1509void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1510{
1511 const int components = gl::VariableComponentCount(targetUniformType);
1512 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1513
1514 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1515
1516 int elementCount = targetUniform->elementCount();
1517
1518 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1519
1520 if (targetUniform->type == targetUniformType)
1521 {
1522 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1523
1524 for (int i = 0; i < count; i++)
1525 {
1526 T *dest = target + (i * 4);
1527 const T *source = v + (i * components);
1528
1529 for (int c = 0; c < components; c++)
1530 {
1531 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1532 }
1533 for (int c = components; c < 4; c++)
1534 {
1535 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1536 }
1537 }
1538 }
1539 else if (targetUniform->type == targetBoolType)
1540 {
1541 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1542
1543 for (int i = 0; i < count; i++)
1544 {
1545 GLint *dest = boolParams + (i * 4);
1546 const T *source = v + (i * components);
1547
1548 for (int c = 0; c < components; c++)
1549 {
1550 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1551 }
1552 for (int c = components; c < 4; c++)
1553 {
1554 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1555 }
1556 }
1557 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001558 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001559 {
1560 ASSERT(targetUniformType == GL_INT);
1561
1562 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1563
1564 bool wasDirty = targetUniform->dirty;
1565
1566 for (int i = 0; i < count; i++)
1567 {
1568 GLint *dest = target + (i * 4);
1569 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1570
1571 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1572 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1573 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1574 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1575 }
1576
1577 if (!wasDirty && targetUniform->dirty)
1578 {
1579 mDirtySamplerMapping = true;
1580 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001581 }
1582 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001583}
Brandon Jones18bd4102014-09-22 14:21:44 -07001584
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001585template<typename T>
1586bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1587{
1588 bool dirty = false;
1589 int copyWidth = std::min(targetHeight, srcWidth);
1590 int copyHeight = std::min(targetWidth, srcHeight);
1591
1592 for (int x = 0; x < copyWidth; x++)
1593 {
1594 for (int y = 0; y < copyHeight; y++)
1595 {
1596 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1597 }
1598 }
1599 // clear unfilled right side
1600 for (int y = 0; y < copyWidth; y++)
1601 {
1602 for (int x = copyHeight; x < targetWidth; x++)
1603 {
1604 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1605 }
1606 }
1607 // clear unfilled bottom.
1608 for (int y = copyWidth; y < targetHeight; y++)
1609 {
1610 for (int x = 0; x < targetWidth; x++)
1611 {
1612 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1613 }
1614 }
1615
1616 return dirty;
1617}
1618
1619template<typename T>
1620bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1621{
1622 bool dirty = false;
1623 int copyWidth = std::min(targetWidth, srcWidth);
1624 int copyHeight = std::min(targetHeight, srcHeight);
1625
1626 for (int y = 0; y < copyHeight; y++)
1627 {
1628 for (int x = 0; x < copyWidth; x++)
1629 {
1630 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1631 }
1632 }
1633 // clear unfilled right side
1634 for (int y = 0; y < copyHeight; y++)
1635 {
1636 for (int x = copyWidth; x < targetWidth; x++)
1637 {
1638 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1639 }
1640 }
1641 // clear unfilled bottom.
1642 for (int y = copyHeight; y < targetHeight; y++)
1643 {
1644 for (int x = 0; x < targetWidth; x++)
1645 {
1646 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1647 }
1648 }
1649
1650 return dirty;
1651}
1652
1653template <int cols, int rows>
1654void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1655{
1656 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1657
1658 int elementCount = targetUniform->elementCount();
1659
1660 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1661 const unsigned int targetMatrixStride = (4 * rows);
1662 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1663
1664 for (int i = 0; i < count; i++)
1665 {
1666 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1667 if (transpose == GL_FALSE)
1668 {
1669 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1670 }
1671 else
1672 {
1673 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1674 }
1675 target += targetMatrixStride;
1676 value += cols * rows;
1677 }
1678}
1679
1680template <typename T>
1681void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1682{
1683 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1684
1685 if (gl::IsMatrixType(targetUniform->type))
1686 {
1687 const int rows = gl::VariableRowCount(targetUniform->type);
1688 const int cols = gl::VariableColumnCount(targetUniform->type);
1689 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1690 }
1691 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1692 {
1693 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1694 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1695 size * sizeof(T));
1696 }
1697 else
1698 {
1699 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1700 switch (gl::VariableComponentType(targetUniform->type))
1701 {
1702 case GL_BOOL:
1703 {
1704 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1705
1706 for (unsigned int i = 0; i < size; i++)
1707 {
1708 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1709 }
1710 }
1711 break;
1712
1713 case GL_FLOAT:
1714 {
1715 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1716
1717 for (unsigned int i = 0; i < size; i++)
1718 {
1719 params[i] = static_cast<T>(floatParams[i]);
1720 }
1721 }
1722 break;
1723
1724 case GL_INT:
1725 {
1726 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1727
1728 for (unsigned int i = 0; i < size; i++)
1729 {
1730 params[i] = static_cast<T>(intParams[i]);
1731 }
1732 }
1733 break;
1734
1735 case GL_UNSIGNED_INT:
1736 {
1737 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1738
1739 for (unsigned int i = 0; i < size; i++)
1740 {
1741 params[i] = static_cast<T>(uintParams[i]);
1742 }
1743 }
1744 break;
1745
1746 default: UNREACHABLE();
1747 }
1748 }
1749}
1750
1751template <typename VarT>
1752void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1753 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1754 bool inRowMajorLayout)
1755{
1756 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1757 {
1758 const VarT &field = fields[uniformIndex];
1759 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1760
1761 if (field.isStruct())
1762 {
1763 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1764
1765 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1766 {
1767 encoder->enterAggregateType();
1768
1769 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1770 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1771
1772 encoder->exitAggregateType();
1773 }
1774 }
1775 else
1776 {
1777 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1778
1779 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1780
1781 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1782 blockIndex, memberInfo);
1783
1784 // add to uniform list, but not index, since uniform block uniforms have no location
1785 blockUniformIndexes->push_back(mUniforms.size());
1786 mUniforms.push_back(newUniform);
1787 }
1788 }
1789}
1790
1791bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1792 const gl::Caps &caps)
1793{
Jamie Madill30d6c252014-11-13 10:03:33 -05001794 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001795
1796 // create uniform block entries if they do not exist
1797 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1798 {
1799 std::vector<unsigned int> blockUniformIndexes;
1800 const unsigned int blockIndex = mUniformBlocks.size();
1801
1802 // define member uniforms
1803 sh::BlockLayoutEncoder *encoder = NULL;
1804
1805 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1806 {
1807 encoder = new sh::Std140BlockEncoder;
1808 }
1809 else
1810 {
1811 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1812 }
1813 ASSERT(encoder);
1814
1815 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1816
1817 size_t dataSize = encoder->getBlockSize();
1818
1819 // create all the uniform blocks
1820 if (interfaceBlock.arraySize > 0)
1821 {
1822 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1823 {
1824 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1825 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1826 mUniformBlocks.push_back(newUniformBlock);
1827 }
1828 }
1829 else
1830 {
1831 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1832 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1833 mUniformBlocks.push_back(newUniformBlock);
1834 }
1835 }
1836
1837 if (interfaceBlock.staticUse)
1838 {
1839 // Assign registers to the uniform blocks
1840 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1841 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1842 ASSERT(blockIndex != GL_INVALID_INDEX);
1843 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1844
1845 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1846
1847 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1848 {
1849 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1850 ASSERT(uniformBlock->name == interfaceBlock.name);
1851
1852 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1853 interfaceBlockRegister + uniformBlockElement, caps))
1854 {
1855 return false;
1856 }
1857 }
1858 }
1859
1860 return true;
1861}
1862
1863bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1864 GLenum samplerType,
1865 unsigned int samplerCount,
1866 std::vector<Sampler> &outSamplers,
1867 GLuint *outUsedRange)
1868{
1869 unsigned int samplerIndex = startSamplerIndex;
1870
1871 do
1872 {
1873 if (samplerIndex < outSamplers.size())
1874 {
1875 Sampler& sampler = outSamplers[samplerIndex];
1876 sampler.active = true;
1877 sampler.textureType = GetTextureType(samplerType);
1878 sampler.logicalTextureUnit = 0;
1879 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1880 }
1881 else
1882 {
1883 return false;
1884 }
1885
1886 samplerIndex++;
1887 } while (samplerIndex < startSamplerIndex + samplerCount);
1888
1889 return true;
1890}
1891
1892bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1893{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001894 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001895 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1896
1897 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1898 {
1899 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1900 &mUsedVertexSamplerRange))
1901 {
1902 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1903 mSamplersVS.size());
1904 return false;
1905 }
1906
1907 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1908 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1909 {
1910 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1911 caps.maxVertexUniformVectors);
1912 return false;
1913 }
1914 }
1915
1916 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1917 {
1918 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1919 &mUsedPixelSamplerRange))
1920 {
1921 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1922 mSamplersPS.size());
1923 return false;
1924 }
1925
1926 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1927 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1928 {
1929 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1930 caps.maxFragmentUniformVectors);
1931 return false;
1932 }
1933 }
1934
1935 return true;
1936}
1937
1938bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1939{
1940 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1941 {
1942 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1943
Geoff Lang2ec386b2014-12-03 14:44:38 -05001944 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945 {
1946 if (!indexSamplerUniform(uniform, infoLog, caps))
1947 {
1948 return false;
1949 }
1950 }
1951
1952 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1953 {
1954 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1955 }
1956 }
1957
1958 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001959}
1960
Brandon Jonesc9610c52014-08-25 17:02:59 -07001961void ProgramD3D::reset()
1962{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001963 ProgramImpl::reset();
1964
Brandon Joneseb994362014-09-24 10:27:28 -07001965 SafeDeleteContainer(mVertexExecutables);
1966 SafeDeleteContainer(mPixelExecutables);
1967 SafeDelete(mGeometryExecutable);
1968
1969 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001970
Brandon Jones22502d52014-08-29 16:58:36 -07001971 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001972 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001973 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001974
1975 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001976 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001977 mUsesFragDepth = false;
1978 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001979 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001980
Brandon Jonesc9610c52014-08-25 17:02:59 -07001981 SafeDelete(mVertexUniformStorage);
1982 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001983
1984 mSamplersPS.clear();
1985 mSamplersVS.clear();
1986
1987 mUsedVertexSamplerRange = 0;
1988 mUsedPixelSamplerRange = 0;
1989 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001990
1991 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001992}
1993
Geoff Lang7dd2e102014-11-10 15:19:26 -05001994unsigned int ProgramD3D::getSerial() const
1995{
1996 return mSerial;
1997}
1998
1999unsigned int ProgramD3D::issueSerial()
2000{
2001 return mCurrentSerial++;
2002}
2003
Jamie Madill437d2662014-12-05 14:23:35 -05002004void ProgramD3D::initAttributesByLayout()
2005{
2006 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2007 {
2008 mAttributesByLayout[i] = i;
2009 }
2010
2011 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2012}
2013
2014void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
2015 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
2016{
2017 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
2018
2019 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2020 {
2021 oldTranslatedAttributes[i] = attributes[i];
2022 }
2023
2024 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2025 {
2026 int oldIndex = mAttributesByLayout[i];
2027 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2028 attributes[i] = oldTranslatedAttributes[oldIndex];
2029 }
2030}
2031
Brandon Jonesc9610c52014-08-25 17:02:59 -07002032}