blob: 29b7b98225d3a7f29ee481318adf4137f33673b7 [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 Madill437d2662014-12-05 14:23:35 -050015#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
Austin Kinross6d51f262015-01-26 16:34:48 -080022#include <future> // For std::async
23
Brandon Jonesc9610c52014-08-25 17:02:59 -070024namespace rx
25{
26
Brandon Joneseb994362014-09-24 10:27:28 -070027namespace
28{
29
Brandon Jones1a8a7e32014-10-01 12:49:30 -070030GLenum GetTextureType(GLenum samplerType)
31{
32 switch (samplerType)
33 {
34 case GL_SAMPLER_2D:
35 case GL_INT_SAMPLER_2D:
36 case GL_UNSIGNED_INT_SAMPLER_2D:
37 case GL_SAMPLER_2D_SHADOW:
38 return GL_TEXTURE_2D;
39 case GL_SAMPLER_3D:
40 case GL_INT_SAMPLER_3D:
41 case GL_UNSIGNED_INT_SAMPLER_3D:
42 return GL_TEXTURE_3D;
43 case GL_SAMPLER_CUBE:
44 case GL_SAMPLER_CUBE_SHADOW:
45 return GL_TEXTURE_CUBE_MAP;
46 case GL_INT_SAMPLER_CUBE:
47 case GL_UNSIGNED_INT_SAMPLER_CUBE:
48 return GL_TEXTURE_CUBE_MAP;
49 case GL_SAMPLER_2D_ARRAY:
50 case GL_INT_SAMPLER_2D_ARRAY:
51 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
52 case GL_SAMPLER_2D_ARRAY_SHADOW:
53 return GL_TEXTURE_2D_ARRAY;
54 default: UNREACHABLE();
55 }
56
57 return GL_TEXTURE_2D;
58}
59
Brandon Joneseb994362014-09-24 10:27:28 -070060void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS])
61{
62 size_t layoutIndex = 0;
63 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
64 {
65 ASSERT(layoutIndex < gl::MAX_VERTEX_ATTRIBS);
66
67 const sh::Attribute &shaderAttr = shaderAttributes[attributeIndex];
68
69 if (shaderAttr.type != GL_NONE)
70 {
71 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
72
73 for (size_t rowIndex = 0; static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); rowIndex++, layoutIndex++)
74 {
75 gl::VertexFormat *defaultFormat = &inputLayout[layoutIndex];
76
77 defaultFormat->mType = gl::VariableComponentType(transposedType);
78 defaultFormat->mNormalized = false;
79 defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool
80 defaultFormat->mComponents = gl::VariableColumnCount(transposedType);
81 }
82 }
83 }
84}
85
86std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
87{
Jamie Madillb4463142014-12-19 14:56:54 -050088 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070089
Jamie Madillb4463142014-12-19 14:56:54 -050090 if (!shaderOutputVars.empty())
91 {
92 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
93 }
Brandon Joneseb994362014-09-24 10:27:28 -070094
95 return defaultPixelOutput;
96}
97
Brandon Jones1a8a7e32014-10-01 12:49:30 -070098bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
99{
100 return var.isRowMajorLayout;
101}
102
103bool IsRowMajorLayout(const sh::ShaderVariable &var)
104{
105 return false;
106}
107
Jamie Madill437d2662014-12-05 14:23:35 -0500108struct AttributeSorter
109{
110 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
111 : originalIndices(semanticIndices)
112 {
113 }
114
115 bool operator()(int a, int b)
116 {
117 if (originalIndices[a] == -1) return false;
118 if (originalIndices[b] == -1) return true;
119 return (originalIndices[a] < originalIndices[b]);
120 }
121
122 const ProgramImpl::SemanticIndexArray &originalIndices;
123};
124
Brandon Joneseb994362014-09-24 10:27:28 -0700125}
126
127ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
128 const GLenum signature[],
Geoff Lang359ef262015-01-05 14:42:29 -0500129 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700130 : mShaderExecutable(shaderExecutable)
131{
132 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
133 {
134 mInputs[attributeIndex] = inputLayout[attributeIndex];
135 mSignature[attributeIndex] = signature[attributeIndex];
136 }
137}
138
139ProgramD3D::VertexExecutable::~VertexExecutable()
140{
141 SafeDelete(mShaderExecutable);
142}
143
144bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
145{
146 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
147 {
148 if (mSignature[attributeIndex] != signature[attributeIndex])
149 {
150 return false;
151 }
152 }
153
154 return true;
155}
156
Geoff Lang359ef262015-01-05 14:42:29 -0500157ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700158 : mOutputSignature(outputSignature),
159 mShaderExecutable(shaderExecutable)
160{
161}
162
163ProgramD3D::PixelExecutable::~PixelExecutable()
164{
165 SafeDelete(mShaderExecutable);
166}
167
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700168ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
169{
170}
171
Geoff Lang7dd2e102014-11-10 15:19:26 -0500172unsigned int ProgramD3D::mCurrentSerial = 1;
173
Jamie Madill93e13fb2014-11-06 15:27:25 -0500174ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700175 : ProgramImpl(),
176 mRenderer(renderer),
177 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700178 mGeometryExecutable(NULL),
179 mVertexWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
180 mPixelWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
Brandon Jones44151a92014-09-10 11:32:25 -0700181 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700182 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700183 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700184 mUsedVertexSamplerRange(0),
185 mUsedPixelSamplerRange(0),
186 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500187 mShaderVersion(100),
188 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700189{
Brandon Joneseb994362014-09-24 10:27:28 -0700190 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700191}
192
193ProgramD3D::~ProgramD3D()
194{
195 reset();
196 SafeDelete(mDynamicHLSL);
197}
198
199ProgramD3D *ProgramD3D::makeProgramD3D(ProgramImpl *impl)
200{
201 ASSERT(HAS_DYNAMIC_TYPE(ProgramD3D*, impl));
202 return static_cast<ProgramD3D*>(impl);
203}
204
205const ProgramD3D *ProgramD3D::makeProgramD3D(const ProgramImpl *impl)
206{
207 ASSERT(HAS_DYNAMIC_TYPE(const ProgramD3D*, impl));
208 return static_cast<const ProgramD3D*>(impl);
209}
210
Brandon Jones44151a92014-09-10 11:32:25 -0700211bool ProgramD3D::usesPointSpriteEmulation() const
212{
213 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
214}
215
216bool ProgramD3D::usesGeometryShader() const
217{
Cooper Partine6664f02015-01-09 16:22:24 -0800218 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
219}
220
221bool ProgramD3D::usesInstancedPointSpriteEmulation() const
222{
223 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700224}
225
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700226GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
227{
228 GLint logicalTextureUnit = -1;
229
230 switch (type)
231 {
232 case gl::SAMPLER_PIXEL:
233 ASSERT(samplerIndex < caps.maxTextureImageUnits);
234 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
235 {
236 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
237 }
238 break;
239 case gl::SAMPLER_VERTEX:
240 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
241 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
242 {
243 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
244 }
245 break;
246 default: UNREACHABLE();
247 }
248
249 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
250 {
251 return logicalTextureUnit;
252 }
253
254 return -1;
255}
256
257// Returns the texture type for a given Direct3D 9 sampler type and
258// index (0-15 for the pixel shader and 0-3 for the vertex shader).
259GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
260{
261 switch (type)
262 {
263 case gl::SAMPLER_PIXEL:
264 ASSERT(samplerIndex < mSamplersPS.size());
265 ASSERT(mSamplersPS[samplerIndex].active);
266 return mSamplersPS[samplerIndex].textureType;
267 case gl::SAMPLER_VERTEX:
268 ASSERT(samplerIndex < mSamplersVS.size());
269 ASSERT(mSamplersVS[samplerIndex].active);
270 return mSamplersVS[samplerIndex].textureType;
271 default: UNREACHABLE();
272 }
273
274 return GL_TEXTURE_2D;
275}
276
277GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
278{
279 switch (type)
280 {
281 case gl::SAMPLER_PIXEL:
282 return mUsedPixelSamplerRange;
283 case gl::SAMPLER_VERTEX:
284 return mUsedVertexSamplerRange;
285 default:
286 UNREACHABLE();
287 return 0;
288 }
289}
290
291void ProgramD3D::updateSamplerMapping()
292{
293 if (!mDirtySamplerMapping)
294 {
295 return;
296 }
297
298 mDirtySamplerMapping = false;
299
300 // Retrieve sampler uniform values
301 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
302 {
303 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
304
305 if (targetUniform->dirty)
306 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500307 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700308 {
309 int count = targetUniform->elementCount();
310 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
311
312 if (targetUniform->isReferencedByFragmentShader())
313 {
314 unsigned int firstIndex = targetUniform->psRegisterIndex;
315
316 for (int i = 0; i < count; i++)
317 {
318 unsigned int samplerIndex = firstIndex + i;
319
320 if (samplerIndex < mSamplersPS.size())
321 {
322 ASSERT(mSamplersPS[samplerIndex].active);
323 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
324 }
325 }
326 }
327
328 if (targetUniform->isReferencedByVertexShader())
329 {
330 unsigned int firstIndex = targetUniform->vsRegisterIndex;
331
332 for (int i = 0; i < count; i++)
333 {
334 unsigned int samplerIndex = firstIndex + i;
335
336 if (samplerIndex < mSamplersVS.size())
337 {
338 ASSERT(mSamplersVS[samplerIndex].active);
339 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
340 }
341 }
342 }
343 }
344 }
345 }
346}
347
348bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
349{
350 // if any two active samplers in a program are of different types, but refer to the same
351 // texture image unit, and this is the current program, then ValidateProgram will fail, and
352 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
353 updateSamplerMapping();
354
355 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
356
357 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
358 {
359 if (mSamplersPS[i].active)
360 {
361 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
362
363 if (unit >= textureUnitTypes.size())
364 {
365 if (infoLog)
366 {
367 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
368 }
369
370 return false;
371 }
372
373 if (textureUnitTypes[unit] != GL_NONE)
374 {
375 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
376 {
377 if (infoLog)
378 {
379 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
380 }
381
382 return false;
383 }
384 }
385 else
386 {
387 textureUnitTypes[unit] = mSamplersPS[i].textureType;
388 }
389 }
390 }
391
392 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
393 {
394 if (mSamplersVS[i].active)
395 {
396 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
397
398 if (unit >= textureUnitTypes.size())
399 {
400 if (infoLog)
401 {
402 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
403 }
404
405 return false;
406 }
407
408 if (textureUnitTypes[unit] != GL_NONE)
409 {
410 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
411 {
412 if (infoLog)
413 {
414 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
415 }
416
417 return false;
418 }
419 }
420 else
421 {
422 textureUnitTypes[unit] = mSamplersVS[i].textureType;
423 }
424 }
425 }
426
427 return true;
428}
429
Geoff Lang7dd2e102014-11-10 15:19:26 -0500430LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700431{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500432 int compileFlags = stream->readInt<int>();
433 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
434 {
435 infoLog.append("Mismatched compilation flags.");
436 return LinkResult(false, gl::Error(GL_NO_ERROR));
437 }
438
Brandon Jones44151a92014-09-10 11:32:25 -0700439 stream->readInt(&mShaderVersion);
440
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700441 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
442 for (unsigned int i = 0; i < psSamplerCount; ++i)
443 {
444 Sampler sampler;
445 stream->readBool(&sampler.active);
446 stream->readInt(&sampler.logicalTextureUnit);
447 stream->readInt(&sampler.textureType);
448 mSamplersPS.push_back(sampler);
449 }
450 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
451 for (unsigned int i = 0; i < vsSamplerCount; ++i)
452 {
453 Sampler sampler;
454 stream->readBool(&sampler.active);
455 stream->readInt(&sampler.logicalTextureUnit);
456 stream->readInt(&sampler.textureType);
457 mSamplersVS.push_back(sampler);
458 }
459
460 stream->readInt(&mUsedVertexSamplerRange);
461 stream->readInt(&mUsedPixelSamplerRange);
462
463 const unsigned int uniformCount = stream->readInt<unsigned int>();
464 if (stream->error())
465 {
466 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500467 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700468 }
469
470 mUniforms.resize(uniformCount);
471 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
472 {
473 GLenum type = stream->readInt<GLenum>();
474 GLenum precision = stream->readInt<GLenum>();
475 std::string name = stream->readString();
476 unsigned int arraySize = stream->readInt<unsigned int>();
477 int blockIndex = stream->readInt<int>();
478
479 int offset = stream->readInt<int>();
480 int arrayStride = stream->readInt<int>();
481 int matrixStride = stream->readInt<int>();
482 bool isRowMajorMatrix = stream->readBool();
483
484 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
485
486 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
487
488 stream->readInt(&uniform->psRegisterIndex);
489 stream->readInt(&uniform->vsRegisterIndex);
490 stream->readInt(&uniform->registerCount);
491 stream->readInt(&uniform->registerElement);
492
493 mUniforms[uniformIndex] = uniform;
494 }
495
496 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
497 if (stream->error())
498 {
499 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500500 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700501 }
502
503 mUniformIndex.resize(uniformIndexCount);
504 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
505 {
506 stream->readString(&mUniformIndex[uniformIndexIndex].name);
507 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
508 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
509 }
510
511 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
512 if (stream->error())
513 {
514 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500515 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700516 }
517
518 mUniformBlocks.resize(uniformBlockCount);
519 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
520 {
521 std::string name = stream->readString();
522 unsigned int elementIndex = stream->readInt<unsigned int>();
523 unsigned int dataSize = stream->readInt<unsigned int>();
524
525 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
526
527 stream->readInt(&uniformBlock->psRegisterIndex);
528 stream->readInt(&uniformBlock->vsRegisterIndex);
529
530 unsigned int numMembers = stream->readInt<unsigned int>();
531 uniformBlock->memberUniformIndexes.resize(numMembers);
532 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
533 {
534 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
535 }
536
537 mUniformBlocks[uniformBlockIndex] = uniformBlock;
538 }
539
Brandon Joneseb994362014-09-24 10:27:28 -0700540 stream->readInt(&mTransformFeedbackBufferMode);
541 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
542 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
543 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
544 {
545 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
546
547 stream->readString(&varying.name);
548 stream->readInt(&varying.type);
549 stream->readInt(&varying.size);
550 stream->readString(&varying.semanticName);
551 stream->readInt(&varying.semanticIndex);
552 stream->readInt(&varying.semanticIndexCount);
553 }
554
Brandon Jones22502d52014-08-29 16:58:36 -0700555 stream->readString(&mVertexHLSL);
556 stream->readInt(&mVertexWorkarounds);
557 stream->readString(&mPixelHLSL);
558 stream->readInt(&mPixelWorkarounds);
559 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700560 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700561
562 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
563 mPixelShaderKey.resize(pixelShaderKeySize);
564 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
565 {
566 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
567 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
568 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
569 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
570 }
571
Brandon Joneseb994362014-09-24 10:27:28 -0700572 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
573
574 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
575 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
576 {
577 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
578
579 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
580 {
581 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
582 stream->readInt(&vertexInput->mType);
583 stream->readInt(&vertexInput->mNormalized);
584 stream->readInt(&vertexInput->mComponents);
585 stream->readBool(&vertexInput->mPureInteger);
586 }
587
588 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
589 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400590
Geoff Lang359ef262015-01-05 14:42:29 -0500591 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400592 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
593 SHADER_VERTEX,
594 mTransformFeedbackLinkedVaryings,
595 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
596 &shaderExecutable);
597 if (error.isError())
598 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500599 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400600 }
601
Brandon Joneseb994362014-09-24 10:27:28 -0700602 if (!shaderExecutable)
603 {
604 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500605 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700606 }
607
608 // generated converted input layout
609 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
610 getInputLayoutSignature(inputLayout, signature);
611
612 // add new binary
613 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
614
615 stream->skip(vertexShaderSize);
616 }
617
618 const size_t pixelShaderCount = stream->readInt<unsigned int>();
619 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
620 {
621 const size_t outputCount = stream->readInt<unsigned int>();
622 std::vector<GLenum> outputs(outputCount);
623 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
624 {
625 stream->readInt(&outputs[outputIndex]);
626 }
627
628 const size_t pixelShaderSize = stream->readInt<unsigned int>();
629 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500630 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400631 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
632 mTransformFeedbackLinkedVaryings,
633 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
634 &shaderExecutable);
635 if (error.isError())
636 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500637 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400638 }
Brandon Joneseb994362014-09-24 10:27:28 -0700639
640 if (!shaderExecutable)
641 {
642 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500643 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700644 }
645
646 // add new binary
647 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
648
649 stream->skip(pixelShaderSize);
650 }
651
652 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
653
654 if (geometryShaderSize > 0)
655 {
656 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400657 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
658 mTransformFeedbackLinkedVaryings,
659 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
660 &mGeometryExecutable);
661 if (error.isError())
662 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500663 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400664 }
Brandon Joneseb994362014-09-24 10:27:28 -0700665
666 if (!mGeometryExecutable)
667 {
668 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500669 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700670 }
671 stream->skip(geometryShaderSize);
672 }
673
Brandon Jones18bd4102014-09-22 14:21:44 -0700674 GUID binaryIdentifier = {0};
675 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
676
677 GUID identifier = mRenderer->getAdapterIdentifier();
678 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
679 {
680 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500681 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700682 }
683
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700684 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500685 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700686
Geoff Lang7dd2e102014-11-10 15:19:26 -0500687 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700688}
689
Geoff Langb543aff2014-09-30 14:52:54 -0400690gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700691{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500692 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
693
Brandon Jones44151a92014-09-10 11:32:25 -0700694 stream->writeInt(mShaderVersion);
695
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700696 stream->writeInt(mSamplersPS.size());
697 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
698 {
699 stream->writeInt(mSamplersPS[i].active);
700 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
701 stream->writeInt(mSamplersPS[i].textureType);
702 }
703
704 stream->writeInt(mSamplersVS.size());
705 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
706 {
707 stream->writeInt(mSamplersVS[i].active);
708 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
709 stream->writeInt(mSamplersVS[i].textureType);
710 }
711
712 stream->writeInt(mUsedVertexSamplerRange);
713 stream->writeInt(mUsedPixelSamplerRange);
714
715 stream->writeInt(mUniforms.size());
716 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
717 {
718 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
719
720 stream->writeInt(uniform.type);
721 stream->writeInt(uniform.precision);
722 stream->writeString(uniform.name);
723 stream->writeInt(uniform.arraySize);
724 stream->writeInt(uniform.blockIndex);
725
726 stream->writeInt(uniform.blockInfo.offset);
727 stream->writeInt(uniform.blockInfo.arrayStride);
728 stream->writeInt(uniform.blockInfo.matrixStride);
729 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
730
731 stream->writeInt(uniform.psRegisterIndex);
732 stream->writeInt(uniform.vsRegisterIndex);
733 stream->writeInt(uniform.registerCount);
734 stream->writeInt(uniform.registerElement);
735 }
736
737 stream->writeInt(mUniformIndex.size());
738 for (size_t i = 0; i < mUniformIndex.size(); ++i)
739 {
740 stream->writeString(mUniformIndex[i].name);
741 stream->writeInt(mUniformIndex[i].element);
742 stream->writeInt(mUniformIndex[i].index);
743 }
744
745 stream->writeInt(mUniformBlocks.size());
746 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
747 {
748 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
749
750 stream->writeString(uniformBlock.name);
751 stream->writeInt(uniformBlock.elementIndex);
752 stream->writeInt(uniformBlock.dataSize);
753
754 stream->writeInt(uniformBlock.memberUniformIndexes.size());
755 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
756 {
757 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
758 }
759
760 stream->writeInt(uniformBlock.psRegisterIndex);
761 stream->writeInt(uniformBlock.vsRegisterIndex);
762 }
763
Brandon Joneseb994362014-09-24 10:27:28 -0700764 stream->writeInt(mTransformFeedbackBufferMode);
765 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
766 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
767 {
768 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
769
770 stream->writeString(varying.name);
771 stream->writeInt(varying.type);
772 stream->writeInt(varying.size);
773 stream->writeString(varying.semanticName);
774 stream->writeInt(varying.semanticIndex);
775 stream->writeInt(varying.semanticIndexCount);
776 }
777
Brandon Jones22502d52014-08-29 16:58:36 -0700778 stream->writeString(mVertexHLSL);
779 stream->writeInt(mVertexWorkarounds);
780 stream->writeString(mPixelHLSL);
781 stream->writeInt(mPixelWorkarounds);
782 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700783 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700784
Brandon Joneseb994362014-09-24 10:27:28 -0700785 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700786 stream->writeInt(pixelShaderKey.size());
787 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
788 {
Brandon Joneseb994362014-09-24 10:27:28 -0700789 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700790 stream->writeInt(variable.type);
791 stream->writeString(variable.name);
792 stream->writeString(variable.source);
793 stream->writeInt(variable.outputIndex);
794 }
795
Brandon Joneseb994362014-09-24 10:27:28 -0700796 stream->writeInt(mVertexExecutables.size());
797 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
798 {
799 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
800
801 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
802 {
803 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
804 stream->writeInt(vertexInput.mType);
805 stream->writeInt(vertexInput.mNormalized);
806 stream->writeInt(vertexInput.mComponents);
807 stream->writeInt(vertexInput.mPureInteger);
808 }
809
810 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
811 stream->writeInt(vertexShaderSize);
812
813 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
814 stream->writeBytes(vertexBlob, vertexShaderSize);
815 }
816
817 stream->writeInt(mPixelExecutables.size());
818 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
819 {
820 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
821
822 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
823 stream->writeInt(outputs.size());
824 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
825 {
826 stream->writeInt(outputs[outputIndex]);
827 }
828
829 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
830 stream->writeInt(pixelShaderSize);
831
832 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
833 stream->writeBytes(pixelBlob, pixelShaderSize);
834 }
835
836 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
837 stream->writeInt(geometryShaderSize);
838
839 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
840 {
841 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
842 stream->writeBytes(geometryBlob, geometryShaderSize);
843 }
844
Brandon Jones18bd4102014-09-22 14:21:44 -0700845 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400846 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700847
Geoff Langb543aff2014-09-30 14:52:54 -0400848 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700849}
850
Geoff Lang359ef262015-01-05 14:42:29 -0500851gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700852{
Brandon Joneseb994362014-09-24 10:27:28 -0700853 std::vector<GLenum> outputs;
854
Jamie Madill48faf802014-11-06 15:27:22 -0500855 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700856
857 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
858 {
859 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
860
861 if (colorbuffer)
862 {
863 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
864 }
865 else
866 {
867 outputs.push_back(GL_NONE);
868 }
869 }
870
Jamie Madill97399232014-12-23 12:31:15 -0500871 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700872}
873
Jamie Madill97399232014-12-23 12:31:15 -0500874gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500875 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500876 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700877{
878 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
879 {
880 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
881 {
Geoff Langb543aff2014-09-30 14:52:54 -0400882 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
883 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700884 }
885 }
886
Brandon Jones22502d52014-08-29 16:58:36 -0700887 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
888 outputSignature);
889
890 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500891 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500892
893 gl::InfoLog tempInfoLog;
894 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
895
896 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400897 mTransformFeedbackLinkedVaryings,
898 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
899 mPixelWorkarounds, &pixelExecutable);
900 if (error.isError())
901 {
902 return error;
903 }
Brandon Joneseb994362014-09-24 10:27:28 -0700904
Jamie Madill97399232014-12-23 12:31:15 -0500905 if (pixelExecutable)
906 {
907 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
908 }
909 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700910 {
911 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
912 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
913 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
914 }
Brandon Jones22502d52014-08-29 16:58:36 -0700915
Geoff Langb543aff2014-09-30 14:52:54 -0400916 *outExectuable = pixelExecutable;
917 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700918}
919
Jamie Madill97399232014-12-23 12:31:15 -0500920gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500921 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500922 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700923{
Brandon Joneseb994362014-09-24 10:27:28 -0700924 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
925 getInputLayoutSignature(inputLayout, signature);
926
927 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
928 {
929 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
930 {
Geoff Langb543aff2014-09-30 14:52:54 -0400931 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
932 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700933 }
934 }
935
Brandon Jones22502d52014-08-29 16:58:36 -0700936 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700937 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700938
939 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500940 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500941
942 gl::InfoLog tempInfoLog;
943 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
944
945 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400946 mTransformFeedbackLinkedVaryings,
947 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
948 mVertexWorkarounds, &vertexExecutable);
949 if (error.isError())
950 {
951 return error;
952 }
953
Jamie Madill97399232014-12-23 12:31:15 -0500954 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700955 {
956 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
957 }
Jamie Madill97399232014-12-23 12:31:15 -0500958 else if (!infoLog)
959 {
Austin Kinross6d51f262015-01-26 16:34:48 -0800960 // 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 -0500961 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
962 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
963 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
964 }
Brandon Jones22502d52014-08-29 16:58:36 -0700965
Geoff Langb543aff2014-09-30 14:52:54 -0400966 *outExectuable = vertexExecutable;
967 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700968}
969
Geoff Lang7dd2e102014-11-10 15:19:26 -0500970LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
971 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700972{
Brandon Jones18bd4102014-09-22 14:21:44 -0700973 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
974 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700975
Austin Kinross6d51f262015-01-26 16:34:48 -0800976 gl::Error vertexShaderTaskResult(GL_NO_ERROR);
977 gl::InfoLog tempVertexShaderInfoLog;
978
979 std::future<ShaderExecutableD3D*> vertexShaderTask = std::async([this, vertexShader, &tempVertexShaderInfoLog, &vertexShaderTaskResult]()
Geoff Langb543aff2014-09-30 14:52:54 -0400980 {
Austin Kinross6d51f262015-01-26 16:34:48 -0800981 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
982 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
983 ShaderExecutableD3D *defaultVertexExecutable = NULL;
984 vertexShaderTaskResult = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &tempVertexShaderInfoLog);
985 return defaultVertexExecutable;
986 });
Brandon Jones44151a92014-09-10 11:32:25 -0700987
Brandon Joneseb994362014-09-24 10:27:28 -0700988 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500989 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Austin Kinross6d51f262015-01-26 16:34:48 -0800990 gl::Error error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
991
992 ShaderExecutableD3D *defaultVertexExecutable = vertexShaderTask.get();
993
994 // Combine the temporary infoLog with the real one
995 if (tempVertexShaderInfoLog.getLength() > 0)
996 {
997 std::vector<char> tempCharBuffer(tempVertexShaderInfoLog.getLength() + 3);
998 tempVertexShaderInfoLog.getLog(tempVertexShaderInfoLog.getLength(), NULL, &tempCharBuffer[0]);
999 infoLog.append(&tempCharBuffer[0]);
1000 }
1001
1002 if (vertexShaderTaskResult.isError())
1003 {
1004 return LinkResult(false, vertexShaderTaskResult);
1005 }
1006
1007 // If the pixel shader compilation failed, then return error
Geoff Langb543aff2014-09-30 14:52:54 -04001008 if (error.isError())
1009 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001010 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001011 }
Brandon Jones44151a92014-09-10 11:32:25 -07001012
Brandon Joneseb994362014-09-24 10:27:28 -07001013 if (usesGeometryShader())
1014 {
1015 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001016
Geoff Langb543aff2014-09-30 14:52:54 -04001017
1018 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1019 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
1020 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
1021 if (error.isError())
1022 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001023 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001024 }
Brandon Joneseb994362014-09-24 10:27:28 -07001025 }
1026
Brandon Jones091540d2014-10-29 11:32:04 -07001027#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001028 if (usesGeometryShader() && mGeometryExecutable)
1029 {
1030 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1031 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1032 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1033 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1034 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1035 }
1036
1037 if (defaultVertexExecutable)
1038 {
1039 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1040 }
1041
1042 if (defaultPixelExecutable)
1043 {
1044 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1045 }
1046#endif
1047
Geoff Langb543aff2014-09-30 14:52:54 -04001048 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001049 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001050}
1051
Geoff Lang7dd2e102014-11-10 15:19:26 -05001052LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1053 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1054 const std::vector<std::string> &transformFeedbackVaryings,
1055 GLenum transformFeedbackBufferMode,
1056 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1057 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001058{
Brandon Joneseb994362014-09-24 10:27:28 -07001059 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1060 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1061
Jamie Madillde8892b2014-11-11 13:00:22 -05001062 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1063 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001064
Brandon Joneseb994362014-09-24 10:27:28 -07001065 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001066
1067 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
1068 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1069
1070 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1071 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001072 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001073
1074 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001075 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001076 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1077
Geoff Langbdee2d52014-09-17 11:02:51 -04001078 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001079 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001080 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001081 }
1082
Geoff Lang7dd2e102014-11-10 15:19:26 -05001083 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001084 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001085 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001086 }
1087
Jamie Madillde8892b2014-11-11 13:00:22 -05001088 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001089 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1090 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1091 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001092 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001093 }
1094
Brandon Jones44151a92014-09-10 11:32:25 -07001095 mUsesPointSize = vertexShaderD3D->usesPointSize();
1096
Jamie Madill437d2662014-12-05 14:23:35 -05001097 initAttributesByLayout();
1098
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001100}
1101
Brandon Jones44151a92014-09-10 11:32:25 -07001102void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1103{
1104 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1105}
1106
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001107void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001108{
1109 // Compute total default block size
1110 unsigned int vertexRegisters = 0;
1111 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001112 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001113 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001114 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001115
Geoff Lang2ec386b2014-12-03 14:44:38 -05001116 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001117 {
1118 if (uniform.isReferencedByVertexShader())
1119 {
1120 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1121 }
1122 if (uniform.isReferencedByFragmentShader())
1123 {
1124 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1125 }
1126 }
1127 }
1128
1129 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1130 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1131}
1132
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001133gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001134{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001135 updateSamplerMapping();
1136
1137 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1138 if (error.isError())
1139 {
1140 return error;
1141 }
1142
1143 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1144 {
1145 mUniforms[uniformIndex]->dirty = false;
1146 }
1147
1148 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001149}
1150
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001151gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001152{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001153 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1154
Brandon Jones18bd4102014-09-22 14:21:44 -07001155 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1156 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1157
1158 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1159 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1160
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001161 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001162 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001163 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001164 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1165
1166 ASSERT(uniformBlock && uniformBuffer);
1167
1168 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1169 {
1170 // undefined behaviour
1171 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1172 }
1173
1174 // Unnecessary to apply an unreferenced standard or shared UBO
1175 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1176 {
1177 continue;
1178 }
1179
1180 if (uniformBlock->isReferencedByVertexShader())
1181 {
1182 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1183 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1184 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1185 vertexUniformBuffers[registerIndex] = uniformBuffer;
1186 }
1187
1188 if (uniformBlock->isReferencedByFragmentShader())
1189 {
1190 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1191 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1192 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1193 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1194 }
1195 }
1196
1197 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1198}
1199
1200bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1201 unsigned int registerIndex, const gl::Caps &caps)
1202{
1203 if (shader == GL_VERTEX_SHADER)
1204 {
1205 uniformBlock->vsRegisterIndex = registerIndex;
1206 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1207 {
1208 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1209 return false;
1210 }
1211 }
1212 else if (shader == GL_FRAGMENT_SHADER)
1213 {
1214 uniformBlock->psRegisterIndex = registerIndex;
1215 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1216 {
1217 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1218 return false;
1219 }
1220 }
1221 else UNREACHABLE();
1222
1223 return true;
1224}
1225
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001226void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001227{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001228 unsigned int numUniforms = mUniforms.size();
1229 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001230 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001231 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001232 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001233}
1234
1235void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1236{
1237 setUniform(location, count, v, GL_FLOAT);
1238}
1239
1240void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1241{
1242 setUniform(location, count, v, GL_FLOAT_VEC2);
1243}
1244
1245void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1246{
1247 setUniform(location, count, v, GL_FLOAT_VEC3);
1248}
1249
1250void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1251{
1252 setUniform(location, count, v, GL_FLOAT_VEC4);
1253}
1254
1255void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1256{
1257 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1258}
1259
1260void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1261{
1262 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1263}
1264
1265void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1266{
1267 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1268}
1269
1270void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1271{
1272 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1273}
1274
1275void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1276{
1277 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1278}
1279
1280void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1281{
1282 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1283}
1284
1285void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1286{
1287 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1288}
1289
1290void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1291{
1292 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1293}
1294
1295void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1296{
1297 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1298}
1299
1300void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1301{
1302 setUniform(location, count, v, GL_INT);
1303}
1304
1305void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1306{
1307 setUniform(location, count, v, GL_INT_VEC2);
1308}
1309
1310void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1311{
1312 setUniform(location, count, v, GL_INT_VEC3);
1313}
1314
1315void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1316{
1317 setUniform(location, count, v, GL_INT_VEC4);
1318}
1319
1320void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1321{
1322 setUniform(location, count, v, GL_UNSIGNED_INT);
1323}
1324
1325void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1326{
1327 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1328}
1329
1330void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1331{
1332 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1333}
1334
1335void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1336{
1337 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1338}
1339
1340void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1341{
1342 getUniformv(location, params, GL_FLOAT);
1343}
1344
1345void ProgramD3D::getUniformiv(GLint location, GLint *params)
1346{
1347 getUniformv(location, params, GL_INT);
1348}
1349
1350void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1351{
1352 getUniformv(location, params, GL_UNSIGNED_INT);
1353}
1354
1355bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1356 const gl::Caps &caps)
1357{
Jamie Madill30d6c252014-11-13 10:03:33 -05001358 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1359 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001360
1361 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1362 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1363
1364 // Check that uniforms defined in the vertex and fragment shaders are identical
1365 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1366 UniformMap linkedUniforms;
1367
1368 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001369 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001370 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1371 linkedUniforms[vertexUniform.name] = &vertexUniform;
1372 }
1373
1374 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1375 {
1376 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1377 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1378 if (entry != linkedUniforms.end())
1379 {
1380 const sh::Uniform &vertexUniform = *entry->second;
1381 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001383 {
1384 return false;
1385 }
1386 }
1387 }
1388
1389 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1390 {
1391 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1392
1393 if (uniform.staticUse)
1394 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001395 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001396 }
1397 }
1398
1399 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1400 {
1401 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1402
1403 if (uniform.staticUse)
1404 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001405 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001406 }
1407 }
1408
1409 if (!indexUniforms(infoLog, caps))
1410 {
1411 return false;
1412 }
1413
1414 initializeUniformStorage();
1415
1416 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1417 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1418 {
1419 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1420
1421 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1422 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1423 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1424 }
1425
1426 return true;
1427}
1428
Geoff Lang492a7e42014-11-05 13:27:06 -05001429void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001430{
Geoff Lang492a7e42014-11-05 13:27:06 -05001431 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001432 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1433 encoder.skipRegisters(uniformRegister);
1434
1435 defineUniform(shader, uniform, uniform.name, &encoder);
1436}
1437
Geoff Lang492a7e42014-11-05 13:27:06 -05001438void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1439 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001440{
1441 if (uniform.isStruct())
1442 {
1443 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1444 {
1445 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1446
1447 encoder->enterAggregateType();
1448
1449 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1450 {
1451 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1452 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1453
1454 defineUniform(shader, field, fieldFullName, encoder);
1455 }
1456
1457 encoder->exitAggregateType();
1458 }
1459 }
1460 else // Not a struct
1461 {
1462 // Arrays are treated as aggregate types
1463 if (uniform.isArray())
1464 {
1465 encoder->enterAggregateType();
1466 }
1467
1468 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1469
1470 if (!linkedUniform)
1471 {
1472 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
1473 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
1474 ASSERT(linkedUniform);
1475 linkedUniform->registerElement = encoder->getCurrentElement();
1476 mUniforms.push_back(linkedUniform);
1477 }
1478
1479 ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
1480
Geoff Lang492a7e42014-11-05 13:27:06 -05001481 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001482 {
1483 linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
1484 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001485 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001486 {
1487 linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
1488 }
1489 else UNREACHABLE();
1490
1491 // Advance the uniform offset, to track registers allocation for structs
1492 encoder->encodeType(uniform.type, uniform.arraySize, false);
1493
1494 // Arrays are treated as aggregate types
1495 if (uniform.isArray())
1496 {
1497 encoder->exitAggregateType();
1498 }
1499 }
1500}
1501
1502template <typename T>
1503static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1504{
1505 ASSERT(dest != NULL);
1506 ASSERT(dirtyFlag != NULL);
1507
1508 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1509 *dest = source;
1510}
1511
1512template <typename T>
1513void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1514{
1515 const int components = gl::VariableComponentCount(targetUniformType);
1516 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1517
1518 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1519
1520 int elementCount = targetUniform->elementCount();
1521
1522 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1523
1524 if (targetUniform->type == targetUniformType)
1525 {
1526 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1527
1528 for (int i = 0; i < count; i++)
1529 {
1530 T *dest = target + (i * 4);
1531 const T *source = v + (i * components);
1532
1533 for (int c = 0; c < components; c++)
1534 {
1535 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1536 }
1537 for (int c = components; c < 4; c++)
1538 {
1539 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1540 }
1541 }
1542 }
1543 else if (targetUniform->type == targetBoolType)
1544 {
1545 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1546
1547 for (int i = 0; i < count; i++)
1548 {
1549 GLint *dest = boolParams + (i * 4);
1550 const T *source = v + (i * components);
1551
1552 for (int c = 0; c < components; c++)
1553 {
1554 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1555 }
1556 for (int c = components; c < 4; c++)
1557 {
1558 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1559 }
1560 }
1561 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001562 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001563 {
1564 ASSERT(targetUniformType == GL_INT);
1565
1566 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1567
1568 bool wasDirty = targetUniform->dirty;
1569
1570 for (int i = 0; i < count; i++)
1571 {
1572 GLint *dest = target + (i * 4);
1573 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1574
1575 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1576 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1577 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1578 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1579 }
1580
1581 if (!wasDirty && targetUniform->dirty)
1582 {
1583 mDirtySamplerMapping = true;
1584 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001585 }
1586 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001587}
Brandon Jones18bd4102014-09-22 14:21:44 -07001588
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001589template<typename T>
1590bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1591{
1592 bool dirty = false;
1593 int copyWidth = std::min(targetHeight, srcWidth);
1594 int copyHeight = std::min(targetWidth, srcHeight);
1595
1596 for (int x = 0; x < copyWidth; x++)
1597 {
1598 for (int y = 0; y < copyHeight; y++)
1599 {
1600 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1601 }
1602 }
1603 // clear unfilled right side
1604 for (int y = 0; y < copyWidth; y++)
1605 {
1606 for (int x = copyHeight; 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 = copyWidth; 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<typename T>
1624bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1625{
1626 bool dirty = false;
1627 int copyWidth = std::min(targetWidth, srcWidth);
1628 int copyHeight = std::min(targetHeight, srcHeight);
1629
1630 for (int y = 0; y < copyHeight; y++)
1631 {
1632 for (int x = 0; x < copyWidth; x++)
1633 {
1634 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1635 }
1636 }
1637 // clear unfilled right side
1638 for (int y = 0; y < copyHeight; y++)
1639 {
1640 for (int x = copyWidth; x < targetWidth; x++)
1641 {
1642 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1643 }
1644 }
1645 // clear unfilled bottom.
1646 for (int y = copyHeight; y < targetHeight; y++)
1647 {
1648 for (int x = 0; x < targetWidth; x++)
1649 {
1650 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1651 }
1652 }
1653
1654 return dirty;
1655}
1656
1657template <int cols, int rows>
1658void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1659{
1660 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1661
1662 int elementCount = targetUniform->elementCount();
1663
1664 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1665 const unsigned int targetMatrixStride = (4 * rows);
1666 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1667
1668 for (int i = 0; i < count; i++)
1669 {
1670 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1671 if (transpose == GL_FALSE)
1672 {
1673 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1674 }
1675 else
1676 {
1677 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1678 }
1679 target += targetMatrixStride;
1680 value += cols * rows;
1681 }
1682}
1683
1684template <typename T>
1685void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1686{
1687 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1688
1689 if (gl::IsMatrixType(targetUniform->type))
1690 {
1691 const int rows = gl::VariableRowCount(targetUniform->type);
1692 const int cols = gl::VariableColumnCount(targetUniform->type);
1693 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1694 }
1695 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1696 {
1697 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1698 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1699 size * sizeof(T));
1700 }
1701 else
1702 {
1703 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1704 switch (gl::VariableComponentType(targetUniform->type))
1705 {
1706 case GL_BOOL:
1707 {
1708 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1709
1710 for (unsigned int i = 0; i < size; i++)
1711 {
1712 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1713 }
1714 }
1715 break;
1716
1717 case GL_FLOAT:
1718 {
1719 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1720
1721 for (unsigned int i = 0; i < size; i++)
1722 {
1723 params[i] = static_cast<T>(floatParams[i]);
1724 }
1725 }
1726 break;
1727
1728 case GL_INT:
1729 {
1730 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1731
1732 for (unsigned int i = 0; i < size; i++)
1733 {
1734 params[i] = static_cast<T>(intParams[i]);
1735 }
1736 }
1737 break;
1738
1739 case GL_UNSIGNED_INT:
1740 {
1741 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1742
1743 for (unsigned int i = 0; i < size; i++)
1744 {
1745 params[i] = static_cast<T>(uintParams[i]);
1746 }
1747 }
1748 break;
1749
1750 default: UNREACHABLE();
1751 }
1752 }
1753}
1754
1755template <typename VarT>
1756void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1757 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1758 bool inRowMajorLayout)
1759{
1760 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1761 {
1762 const VarT &field = fields[uniformIndex];
1763 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1764
1765 if (field.isStruct())
1766 {
1767 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1768
1769 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1770 {
1771 encoder->enterAggregateType();
1772
1773 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1774 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1775
1776 encoder->exitAggregateType();
1777 }
1778 }
1779 else
1780 {
1781 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1782
1783 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1784
1785 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1786 blockIndex, memberInfo);
1787
1788 // add to uniform list, but not index, since uniform block uniforms have no location
1789 blockUniformIndexes->push_back(mUniforms.size());
1790 mUniforms.push_back(newUniform);
1791 }
1792 }
1793}
1794
1795bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1796 const gl::Caps &caps)
1797{
Jamie Madill30d6c252014-11-13 10:03:33 -05001798 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001799
1800 // create uniform block entries if they do not exist
1801 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1802 {
1803 std::vector<unsigned int> blockUniformIndexes;
1804 const unsigned int blockIndex = mUniformBlocks.size();
1805
1806 // define member uniforms
1807 sh::BlockLayoutEncoder *encoder = NULL;
1808
1809 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1810 {
1811 encoder = new sh::Std140BlockEncoder;
1812 }
1813 else
1814 {
1815 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1816 }
1817 ASSERT(encoder);
1818
1819 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1820
1821 size_t dataSize = encoder->getBlockSize();
1822
1823 // create all the uniform blocks
1824 if (interfaceBlock.arraySize > 0)
1825 {
1826 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1827 {
1828 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1829 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1830 mUniformBlocks.push_back(newUniformBlock);
1831 }
1832 }
1833 else
1834 {
1835 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1836 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1837 mUniformBlocks.push_back(newUniformBlock);
1838 }
1839 }
1840
1841 if (interfaceBlock.staticUse)
1842 {
1843 // Assign registers to the uniform blocks
1844 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1845 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1846 ASSERT(blockIndex != GL_INVALID_INDEX);
1847 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1848
1849 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1850
1851 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1852 {
1853 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1854 ASSERT(uniformBlock->name == interfaceBlock.name);
1855
1856 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1857 interfaceBlockRegister + uniformBlockElement, caps))
1858 {
1859 return false;
1860 }
1861 }
1862 }
1863
1864 return true;
1865}
1866
1867bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1868 GLenum samplerType,
1869 unsigned int samplerCount,
1870 std::vector<Sampler> &outSamplers,
1871 GLuint *outUsedRange)
1872{
1873 unsigned int samplerIndex = startSamplerIndex;
1874
1875 do
1876 {
1877 if (samplerIndex < outSamplers.size())
1878 {
1879 Sampler& sampler = outSamplers[samplerIndex];
1880 sampler.active = true;
1881 sampler.textureType = GetTextureType(samplerType);
1882 sampler.logicalTextureUnit = 0;
1883 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1884 }
1885 else
1886 {
1887 return false;
1888 }
1889
1890 samplerIndex++;
1891 } while (samplerIndex < startSamplerIndex + samplerCount);
1892
1893 return true;
1894}
1895
1896bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1897{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001898 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001899 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1900
1901 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1902 {
1903 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1904 &mUsedVertexSamplerRange))
1905 {
1906 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1907 mSamplersVS.size());
1908 return false;
1909 }
1910
1911 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1912 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1913 {
1914 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1915 caps.maxVertexUniformVectors);
1916 return false;
1917 }
1918 }
1919
1920 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1921 {
1922 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1923 &mUsedPixelSamplerRange))
1924 {
1925 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1926 mSamplersPS.size());
1927 return false;
1928 }
1929
1930 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1931 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1932 {
1933 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1934 caps.maxFragmentUniformVectors);
1935 return false;
1936 }
1937 }
1938
1939 return true;
1940}
1941
1942bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1943{
1944 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1945 {
1946 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1947
Geoff Lang2ec386b2014-12-03 14:44:38 -05001948 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001949 {
1950 if (!indexSamplerUniform(uniform, infoLog, caps))
1951 {
1952 return false;
1953 }
1954 }
1955
1956 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1957 {
1958 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1959 }
1960 }
1961
1962 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001963}
1964
Brandon Jonesc9610c52014-08-25 17:02:59 -07001965void ProgramD3D::reset()
1966{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001967 ProgramImpl::reset();
1968
Brandon Joneseb994362014-09-24 10:27:28 -07001969 SafeDeleteContainer(mVertexExecutables);
1970 SafeDeleteContainer(mPixelExecutables);
1971 SafeDelete(mGeometryExecutable);
1972
1973 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001974
Brandon Jones22502d52014-08-29 16:58:36 -07001975 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001976 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001977 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001978
1979 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001980 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001981 mUsesFragDepth = false;
1982 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001983 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001984
Brandon Jonesc9610c52014-08-25 17:02:59 -07001985 SafeDelete(mVertexUniformStorage);
1986 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001987
1988 mSamplersPS.clear();
1989 mSamplersVS.clear();
1990
1991 mUsedVertexSamplerRange = 0;
1992 mUsedPixelSamplerRange = 0;
1993 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001994
1995 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001996}
1997
Geoff Lang7dd2e102014-11-10 15:19:26 -05001998unsigned int ProgramD3D::getSerial() const
1999{
2000 return mSerial;
2001}
2002
2003unsigned int ProgramD3D::issueSerial()
2004{
2005 return mCurrentSerial++;
2006}
2007
Jamie Madill437d2662014-12-05 14:23:35 -05002008void ProgramD3D::initAttributesByLayout()
2009{
2010 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2011 {
2012 mAttributesByLayout[i] = i;
2013 }
2014
2015 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2016}
2017
2018void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
2019 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
2020{
2021 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
2022
2023 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2024 {
2025 oldTranslatedAttributes[i] = attributes[i];
2026 }
2027
2028 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2029 {
2030 int oldIndex = mAttributesByLayout[i];
2031 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2032 attributes[i] = oldTranslatedAttributes[oldIndex];
2033 }
2034}
2035
Brandon Jonesc9610c52014-08-25 17:02:59 -07002036}