blob: dd39a6d49201dfa405562a6b3ce73187f40a9166 [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
204ProgramD3D *ProgramD3D::makeProgramD3D(ProgramImpl *impl)
205{
206 ASSERT(HAS_DYNAMIC_TYPE(ProgramD3D*, impl));
207 return static_cast<ProgramD3D*>(impl);
208}
209
210const ProgramD3D *ProgramD3D::makeProgramD3D(const ProgramImpl *impl)
211{
212 ASSERT(HAS_DYNAMIC_TYPE(const ProgramD3D*, impl));
213 return static_cast<const ProgramD3D*>(impl);
214}
215
Brandon Jones44151a92014-09-10 11:32:25 -0700216bool ProgramD3D::usesPointSpriteEmulation() const
217{
218 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
219}
220
221bool ProgramD3D::usesGeometryShader() const
222{
Cooper Partine6664f02015-01-09 16:22:24 -0800223 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
224}
225
226bool ProgramD3D::usesInstancedPointSpriteEmulation() const
227{
228 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700229}
230
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700231GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
232{
233 GLint logicalTextureUnit = -1;
234
235 switch (type)
236 {
237 case gl::SAMPLER_PIXEL:
238 ASSERT(samplerIndex < caps.maxTextureImageUnits);
239 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
240 {
241 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
242 }
243 break;
244 case gl::SAMPLER_VERTEX:
245 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
246 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
247 {
248 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
249 }
250 break;
251 default: UNREACHABLE();
252 }
253
254 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
255 {
256 return logicalTextureUnit;
257 }
258
259 return -1;
260}
261
262// Returns the texture type for a given Direct3D 9 sampler type and
263// index (0-15 for the pixel shader and 0-3 for the vertex shader).
264GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
265{
266 switch (type)
267 {
268 case gl::SAMPLER_PIXEL:
269 ASSERT(samplerIndex < mSamplersPS.size());
270 ASSERT(mSamplersPS[samplerIndex].active);
271 return mSamplersPS[samplerIndex].textureType;
272 case gl::SAMPLER_VERTEX:
273 ASSERT(samplerIndex < mSamplersVS.size());
274 ASSERT(mSamplersVS[samplerIndex].active);
275 return mSamplersVS[samplerIndex].textureType;
276 default: UNREACHABLE();
277 }
278
279 return GL_TEXTURE_2D;
280}
281
282GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
283{
284 switch (type)
285 {
286 case gl::SAMPLER_PIXEL:
287 return mUsedPixelSamplerRange;
288 case gl::SAMPLER_VERTEX:
289 return mUsedVertexSamplerRange;
290 default:
291 UNREACHABLE();
292 return 0;
293 }
294}
295
296void ProgramD3D::updateSamplerMapping()
297{
298 if (!mDirtySamplerMapping)
299 {
300 return;
301 }
302
303 mDirtySamplerMapping = false;
304
305 // Retrieve sampler uniform values
306 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
307 {
308 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
309
310 if (targetUniform->dirty)
311 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500312 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700313 {
314 int count = targetUniform->elementCount();
315 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
316
317 if (targetUniform->isReferencedByFragmentShader())
318 {
319 unsigned int firstIndex = targetUniform->psRegisterIndex;
320
321 for (int i = 0; i < count; i++)
322 {
323 unsigned int samplerIndex = firstIndex + i;
324
325 if (samplerIndex < mSamplersPS.size())
326 {
327 ASSERT(mSamplersPS[samplerIndex].active);
328 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
329 }
330 }
331 }
332
333 if (targetUniform->isReferencedByVertexShader())
334 {
335 unsigned int firstIndex = targetUniform->vsRegisterIndex;
336
337 for (int i = 0; i < count; i++)
338 {
339 unsigned int samplerIndex = firstIndex + i;
340
341 if (samplerIndex < mSamplersVS.size())
342 {
343 ASSERT(mSamplersVS[samplerIndex].active);
344 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
345 }
346 }
347 }
348 }
349 }
350 }
351}
352
353bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
354{
355 // if any two active samplers in a program are of different types, but refer to the same
356 // texture image unit, and this is the current program, then ValidateProgram will fail, and
357 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
358 updateSamplerMapping();
359
360 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
361
362 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
363 {
364 if (mSamplersPS[i].active)
365 {
366 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
367
368 if (unit >= textureUnitTypes.size())
369 {
370 if (infoLog)
371 {
372 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
373 }
374
375 return false;
376 }
377
378 if (textureUnitTypes[unit] != GL_NONE)
379 {
380 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
381 {
382 if (infoLog)
383 {
384 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
385 }
386
387 return false;
388 }
389 }
390 else
391 {
392 textureUnitTypes[unit] = mSamplersPS[i].textureType;
393 }
394 }
395 }
396
397 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
398 {
399 if (mSamplersVS[i].active)
400 {
401 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
402
403 if (unit >= textureUnitTypes.size())
404 {
405 if (infoLog)
406 {
407 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
408 }
409
410 return false;
411 }
412
413 if (textureUnitTypes[unit] != GL_NONE)
414 {
415 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
416 {
417 if (infoLog)
418 {
419 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
420 }
421
422 return false;
423 }
424 }
425 else
426 {
427 textureUnitTypes[unit] = mSamplersVS[i].textureType;
428 }
429 }
430 }
431
432 return true;
433}
434
Geoff Lang7dd2e102014-11-10 15:19:26 -0500435LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700436{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500437 int compileFlags = stream->readInt<int>();
438 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
439 {
440 infoLog.append("Mismatched compilation flags.");
441 return LinkResult(false, gl::Error(GL_NO_ERROR));
442 }
443
Brandon Jones44151a92014-09-10 11:32:25 -0700444 stream->readInt(&mShaderVersion);
445
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700446 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
447 for (unsigned int i = 0; i < psSamplerCount; ++i)
448 {
449 Sampler sampler;
450 stream->readBool(&sampler.active);
451 stream->readInt(&sampler.logicalTextureUnit);
452 stream->readInt(&sampler.textureType);
453 mSamplersPS.push_back(sampler);
454 }
455 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
456 for (unsigned int i = 0; i < vsSamplerCount; ++i)
457 {
458 Sampler sampler;
459 stream->readBool(&sampler.active);
460 stream->readInt(&sampler.logicalTextureUnit);
461 stream->readInt(&sampler.textureType);
462 mSamplersVS.push_back(sampler);
463 }
464
465 stream->readInt(&mUsedVertexSamplerRange);
466 stream->readInt(&mUsedPixelSamplerRange);
467
468 const unsigned int uniformCount = stream->readInt<unsigned int>();
469 if (stream->error())
470 {
471 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500472 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700473 }
474
475 mUniforms.resize(uniformCount);
476 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
477 {
478 GLenum type = stream->readInt<GLenum>();
479 GLenum precision = stream->readInt<GLenum>();
480 std::string name = stream->readString();
481 unsigned int arraySize = stream->readInt<unsigned int>();
482 int blockIndex = stream->readInt<int>();
483
484 int offset = stream->readInt<int>();
485 int arrayStride = stream->readInt<int>();
486 int matrixStride = stream->readInt<int>();
487 bool isRowMajorMatrix = stream->readBool();
488
489 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
490
491 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
492
493 stream->readInt(&uniform->psRegisterIndex);
494 stream->readInt(&uniform->vsRegisterIndex);
495 stream->readInt(&uniform->registerCount);
496 stream->readInt(&uniform->registerElement);
497
498 mUniforms[uniformIndex] = uniform;
499 }
500
501 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
502 if (stream->error())
503 {
504 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500505 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700506 }
507
508 mUniformIndex.resize(uniformIndexCount);
509 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
510 {
511 stream->readString(&mUniformIndex[uniformIndexIndex].name);
512 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
513 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
514 }
515
516 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
517 if (stream->error())
518 {
519 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500520 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700521 }
522
523 mUniformBlocks.resize(uniformBlockCount);
524 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
525 {
526 std::string name = stream->readString();
527 unsigned int elementIndex = stream->readInt<unsigned int>();
528 unsigned int dataSize = stream->readInt<unsigned int>();
529
530 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
531
532 stream->readInt(&uniformBlock->psRegisterIndex);
533 stream->readInt(&uniformBlock->vsRegisterIndex);
534
535 unsigned int numMembers = stream->readInt<unsigned int>();
536 uniformBlock->memberUniformIndexes.resize(numMembers);
537 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
538 {
539 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
540 }
541
542 mUniformBlocks[uniformBlockIndex] = uniformBlock;
543 }
544
Brandon Joneseb994362014-09-24 10:27:28 -0700545 stream->readInt(&mTransformFeedbackBufferMode);
546 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
547 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
548 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
549 {
550 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
551
552 stream->readString(&varying.name);
553 stream->readInt(&varying.type);
554 stream->readInt(&varying.size);
555 stream->readString(&varying.semanticName);
556 stream->readInt(&varying.semanticIndex);
557 stream->readInt(&varying.semanticIndexCount);
558 }
559
Brandon Jones22502d52014-08-29 16:58:36 -0700560 stream->readString(&mVertexHLSL);
561 stream->readInt(&mVertexWorkarounds);
562 stream->readString(&mPixelHLSL);
563 stream->readInt(&mPixelWorkarounds);
564 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700565 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700566
567 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
568 mPixelShaderKey.resize(pixelShaderKeySize);
569 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
570 {
571 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
572 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
573 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
574 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
575 }
576
Brandon Joneseb994362014-09-24 10:27:28 -0700577 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
578
579 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
580 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
581 {
582 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
583
584 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
585 {
586 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
587 stream->readInt(&vertexInput->mType);
588 stream->readInt(&vertexInput->mNormalized);
589 stream->readInt(&vertexInput->mComponents);
590 stream->readBool(&vertexInput->mPureInteger);
591 }
592
593 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
594 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400595
Geoff Lang359ef262015-01-05 14:42:29 -0500596 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400597 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
598 SHADER_VERTEX,
599 mTransformFeedbackLinkedVaryings,
600 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
601 &shaderExecutable);
602 if (error.isError())
603 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500604 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400605 }
606
Brandon Joneseb994362014-09-24 10:27:28 -0700607 if (!shaderExecutable)
608 {
609 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500610 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700611 }
612
613 // generated converted input layout
614 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
615 getInputLayoutSignature(inputLayout, signature);
616
617 // add new binary
618 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
619
620 stream->skip(vertexShaderSize);
621 }
622
623 const size_t pixelShaderCount = stream->readInt<unsigned int>();
624 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
625 {
626 const size_t outputCount = stream->readInt<unsigned int>();
627 std::vector<GLenum> outputs(outputCount);
628 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
629 {
630 stream->readInt(&outputs[outputIndex]);
631 }
632
633 const size_t pixelShaderSize = stream->readInt<unsigned int>();
634 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500635 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400636 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
637 mTransformFeedbackLinkedVaryings,
638 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
639 &shaderExecutable);
640 if (error.isError())
641 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500642 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400643 }
Brandon Joneseb994362014-09-24 10:27:28 -0700644
645 if (!shaderExecutable)
646 {
647 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500648 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700649 }
650
651 // add new binary
652 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
653
654 stream->skip(pixelShaderSize);
655 }
656
657 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
658
659 if (geometryShaderSize > 0)
660 {
661 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400662 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
663 mTransformFeedbackLinkedVaryings,
664 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
665 &mGeometryExecutable);
666 if (error.isError())
667 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500668 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400669 }
Brandon Joneseb994362014-09-24 10:27:28 -0700670
671 if (!mGeometryExecutable)
672 {
673 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500674 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700675 }
676 stream->skip(geometryShaderSize);
677 }
678
Brandon Jones18bd4102014-09-22 14:21:44 -0700679 GUID binaryIdentifier = {0};
680 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
681
682 GUID identifier = mRenderer->getAdapterIdentifier();
683 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
684 {
685 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500686 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700687 }
688
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500690 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700691
Geoff Lang7dd2e102014-11-10 15:19:26 -0500692 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700693}
694
Geoff Langb543aff2014-09-30 14:52:54 -0400695gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700696{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500697 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
698
Brandon Jones44151a92014-09-10 11:32:25 -0700699 stream->writeInt(mShaderVersion);
700
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700701 stream->writeInt(mSamplersPS.size());
702 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
703 {
704 stream->writeInt(mSamplersPS[i].active);
705 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
706 stream->writeInt(mSamplersPS[i].textureType);
707 }
708
709 stream->writeInt(mSamplersVS.size());
710 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
711 {
712 stream->writeInt(mSamplersVS[i].active);
713 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
714 stream->writeInt(mSamplersVS[i].textureType);
715 }
716
717 stream->writeInt(mUsedVertexSamplerRange);
718 stream->writeInt(mUsedPixelSamplerRange);
719
720 stream->writeInt(mUniforms.size());
721 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
722 {
723 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
724
725 stream->writeInt(uniform.type);
726 stream->writeInt(uniform.precision);
727 stream->writeString(uniform.name);
728 stream->writeInt(uniform.arraySize);
729 stream->writeInt(uniform.blockIndex);
730
731 stream->writeInt(uniform.blockInfo.offset);
732 stream->writeInt(uniform.blockInfo.arrayStride);
733 stream->writeInt(uniform.blockInfo.matrixStride);
734 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
735
736 stream->writeInt(uniform.psRegisterIndex);
737 stream->writeInt(uniform.vsRegisterIndex);
738 stream->writeInt(uniform.registerCount);
739 stream->writeInt(uniform.registerElement);
740 }
741
742 stream->writeInt(mUniformIndex.size());
743 for (size_t i = 0; i < mUniformIndex.size(); ++i)
744 {
745 stream->writeString(mUniformIndex[i].name);
746 stream->writeInt(mUniformIndex[i].element);
747 stream->writeInt(mUniformIndex[i].index);
748 }
749
750 stream->writeInt(mUniformBlocks.size());
751 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
752 {
753 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
754
755 stream->writeString(uniformBlock.name);
756 stream->writeInt(uniformBlock.elementIndex);
757 stream->writeInt(uniformBlock.dataSize);
758
759 stream->writeInt(uniformBlock.memberUniformIndexes.size());
760 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
761 {
762 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
763 }
764
765 stream->writeInt(uniformBlock.psRegisterIndex);
766 stream->writeInt(uniformBlock.vsRegisterIndex);
767 }
768
Brandon Joneseb994362014-09-24 10:27:28 -0700769 stream->writeInt(mTransformFeedbackBufferMode);
770 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
771 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
772 {
773 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
774
775 stream->writeString(varying.name);
776 stream->writeInt(varying.type);
777 stream->writeInt(varying.size);
778 stream->writeString(varying.semanticName);
779 stream->writeInt(varying.semanticIndex);
780 stream->writeInt(varying.semanticIndexCount);
781 }
782
Brandon Jones22502d52014-08-29 16:58:36 -0700783 stream->writeString(mVertexHLSL);
784 stream->writeInt(mVertexWorkarounds);
785 stream->writeString(mPixelHLSL);
786 stream->writeInt(mPixelWorkarounds);
787 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700788 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700789
Brandon Joneseb994362014-09-24 10:27:28 -0700790 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700791 stream->writeInt(pixelShaderKey.size());
792 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
793 {
Brandon Joneseb994362014-09-24 10:27:28 -0700794 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700795 stream->writeInt(variable.type);
796 stream->writeString(variable.name);
797 stream->writeString(variable.source);
798 stream->writeInt(variable.outputIndex);
799 }
800
Brandon Joneseb994362014-09-24 10:27:28 -0700801 stream->writeInt(mVertexExecutables.size());
802 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
803 {
804 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
805
806 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
807 {
808 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
809 stream->writeInt(vertexInput.mType);
810 stream->writeInt(vertexInput.mNormalized);
811 stream->writeInt(vertexInput.mComponents);
812 stream->writeInt(vertexInput.mPureInteger);
813 }
814
815 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
816 stream->writeInt(vertexShaderSize);
817
818 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
819 stream->writeBytes(vertexBlob, vertexShaderSize);
820 }
821
822 stream->writeInt(mPixelExecutables.size());
823 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
824 {
825 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
826
827 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
828 stream->writeInt(outputs.size());
829 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
830 {
831 stream->writeInt(outputs[outputIndex]);
832 }
833
834 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
835 stream->writeInt(pixelShaderSize);
836
837 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
838 stream->writeBytes(pixelBlob, pixelShaderSize);
839 }
840
841 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
842 stream->writeInt(geometryShaderSize);
843
844 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
845 {
846 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
847 stream->writeBytes(geometryBlob, geometryShaderSize);
848 }
849
Brandon Jones18bd4102014-09-22 14:21:44 -0700850 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400851 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700852
Geoff Langb543aff2014-09-30 14:52:54 -0400853 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700854}
855
Geoff Lang359ef262015-01-05 14:42:29 -0500856gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700857{
Brandon Joneseb994362014-09-24 10:27:28 -0700858 std::vector<GLenum> outputs;
859
Jamie Madill48faf802014-11-06 15:27:22 -0500860 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700861
862 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
863 {
864 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
865
866 if (colorbuffer)
867 {
868 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
869 }
870 else
871 {
872 outputs.push_back(GL_NONE);
873 }
874 }
875
Jamie Madill97399232014-12-23 12:31:15 -0500876 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700877}
878
Jamie Madill97399232014-12-23 12:31:15 -0500879gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500880 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500881 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700882{
883 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
884 {
885 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
886 {
Geoff Langb543aff2014-09-30 14:52:54 -0400887 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
888 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700889 }
890 }
891
Brandon Jones22502d52014-08-29 16:58:36 -0700892 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
893 outputSignature);
894
895 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500896 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500897
898 gl::InfoLog tempInfoLog;
899 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
900
901 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400902 mTransformFeedbackLinkedVaryings,
903 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
904 mPixelWorkarounds, &pixelExecutable);
905 if (error.isError())
906 {
907 return error;
908 }
Brandon Joneseb994362014-09-24 10:27:28 -0700909
Jamie Madill97399232014-12-23 12:31:15 -0500910 if (pixelExecutable)
911 {
912 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
913 }
914 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700915 {
916 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
917 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
918 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
919 }
Brandon Jones22502d52014-08-29 16:58:36 -0700920
Geoff Langb543aff2014-09-30 14:52:54 -0400921 *outExectuable = pixelExecutable;
922 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700923}
924
Jamie Madill97399232014-12-23 12:31:15 -0500925gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500926 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500927 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700928{
Brandon Joneseb994362014-09-24 10:27:28 -0700929 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
930 getInputLayoutSignature(inputLayout, signature);
931
932 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
933 {
934 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
935 {
Geoff Langb543aff2014-09-30 14:52:54 -0400936 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
937 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700938 }
939 }
940
Brandon Jones22502d52014-08-29 16:58:36 -0700941 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700942 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700943
944 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500945 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500946
947 gl::InfoLog tempInfoLog;
948 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
949
950 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400951 mTransformFeedbackLinkedVaryings,
952 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
953 mVertexWorkarounds, &vertexExecutable);
954 if (error.isError())
955 {
956 return error;
957 }
958
Jamie Madill97399232014-12-23 12:31:15 -0500959 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700960 {
961 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
962 }
Jamie Madill97399232014-12-23 12:31:15 -0500963 else if (!infoLog)
964 {
Austin Kinross31018482015-01-30 13:06:52 -0800965 // 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 -0500966 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
967 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
968 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
969 }
Brandon Jones22502d52014-08-29 16:58:36 -0700970
Geoff Langb543aff2014-09-30 14:52:54 -0400971 *outExectuable = vertexExecutable;
972 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700973}
974
Geoff Lang7dd2e102014-11-10 15:19:26 -0500975LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
976 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700977{
Brandon Jones18bd4102014-09-22 14:21:44 -0700978 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
979 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700980
Austin Kinross31018482015-01-30 13:06:52 -0800981 gl::Error vertexShaderTaskResult(GL_NO_ERROR);
982 gl::InfoLog tempVertexShaderInfoLog;
Brandon Jones44151a92014-09-10 11:32:25 -0700983
Austin Kinross31018482015-01-30 13:06:52 -0800984 // Use an async task to begin compiling the vertex shader asynchronously on its own task.
985 std::future<ShaderExecutableD3D*> vertexShaderTask = std::async([this, vertexShader, &tempVertexShaderInfoLog, &vertexShaderTaskResult]()
986 {
987 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
988 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
989 ShaderExecutableD3D *defaultVertexExecutable = NULL;
990 vertexShaderTaskResult = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &tempVertexShaderInfoLog);
991 return defaultVertexExecutable;
992 });
993
994 // Continue to compile the pixel shader on the main thread
Brandon Joneseb994362014-09-24 10:27:28 -0700995 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500996 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Austin Kinross31018482015-01-30 13:06:52 -0800997 gl::Error error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
998
999 // Call .get() on the vertex shader compilation. This waits until the task is complete before returning
1000 ShaderExecutableD3D *defaultVertexExecutable = vertexShaderTask.get();
1001
1002 // Combine the temporary infoLog with the real one
1003 if (tempVertexShaderInfoLog.getLength() > 0)
1004 {
1005 std::vector<char> tempCharBuffer(tempVertexShaderInfoLog.getLength() + 3);
1006 tempVertexShaderInfoLog.getLog(tempVertexShaderInfoLog.getLength(), NULL, &tempCharBuffer[0]);
1007 infoLog.append(&tempCharBuffer[0]);
1008 }
1009
1010 if (vertexShaderTaskResult.isError())
1011 {
1012 return LinkResult(false, vertexShaderTaskResult);
1013 }
1014
1015 // If the pixel shader compilation failed, then return error
Geoff Langb543aff2014-09-30 14:52:54 -04001016 if (error.isError())
1017 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001018 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001019 }
Brandon Jones44151a92014-09-10 11:32:25 -07001020
Brandon Joneseb994362014-09-24 10:27:28 -07001021 if (usesGeometryShader())
1022 {
1023 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001024
Geoff Langb543aff2014-09-30 14:52:54 -04001025
1026 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1027 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
1028 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
1029 if (error.isError())
1030 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001031 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001032 }
Brandon Joneseb994362014-09-24 10:27:28 -07001033 }
1034
Brandon Jones091540d2014-10-29 11:32:04 -07001035#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001036 if (usesGeometryShader() && mGeometryExecutable)
1037 {
1038 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1039 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1040 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1041 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1042 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1043 }
1044
1045 if (defaultVertexExecutable)
1046 {
1047 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1048 }
1049
1050 if (defaultPixelExecutable)
1051 {
1052 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1053 }
1054#endif
1055
Geoff Langb543aff2014-09-30 14:52:54 -04001056 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001057 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001058}
1059
Geoff Lang7dd2e102014-11-10 15:19:26 -05001060LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1061 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1062 const std::vector<std::string> &transformFeedbackVaryings,
1063 GLenum transformFeedbackBufferMode,
1064 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1065 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001066{
Brandon Joneseb994362014-09-24 10:27:28 -07001067 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1068 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1069
Jamie Madillde8892b2014-11-11 13:00:22 -05001070 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1071 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001072
Brandon Joneseb994362014-09-24 10:27:28 -07001073 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001074
1075 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
1076 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1077
1078 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1079 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001080 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001081
1082 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001083 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001084 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1085
Geoff Langbdee2d52014-09-17 11:02:51 -04001086 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001087 {
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
Geoff Lang7dd2e102014-11-10 15:19:26 -05001091 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001092 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001093 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001094 }
1095
Jamie Madillde8892b2014-11-11 13:00:22 -05001096 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001097 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1098 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1099 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001100 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001101 }
1102
Brandon Jones44151a92014-09-10 11:32:25 -07001103 mUsesPointSize = vertexShaderD3D->usesPointSize();
1104
Jamie Madill437d2662014-12-05 14:23:35 -05001105 initAttributesByLayout();
1106
Geoff Lang7dd2e102014-11-10 15:19:26 -05001107 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001108}
1109
Brandon Jones44151a92014-09-10 11:32:25 -07001110void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1111{
1112 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1113}
1114
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001115void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001116{
1117 // Compute total default block size
1118 unsigned int vertexRegisters = 0;
1119 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001120 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001121 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001122 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001123
Geoff Lang2ec386b2014-12-03 14:44:38 -05001124 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001125 {
1126 if (uniform.isReferencedByVertexShader())
1127 {
1128 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1129 }
1130 if (uniform.isReferencedByFragmentShader())
1131 {
1132 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1133 }
1134 }
1135 }
1136
1137 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1138 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1139}
1140
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001141gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001142{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001143 updateSamplerMapping();
1144
1145 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1146 if (error.isError())
1147 {
1148 return error;
1149 }
1150
1151 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1152 {
1153 mUniforms[uniformIndex]->dirty = false;
1154 }
1155
1156 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001157}
1158
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001159gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001160{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001161 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1162
Brandon Jones18bd4102014-09-22 14:21:44 -07001163 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1164 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1165
1166 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1167 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1168
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001169 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001170 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001171 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001172 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1173
1174 ASSERT(uniformBlock && uniformBuffer);
1175
1176 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1177 {
1178 // undefined behaviour
1179 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1180 }
1181
1182 // Unnecessary to apply an unreferenced standard or shared UBO
1183 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1184 {
1185 continue;
1186 }
1187
1188 if (uniformBlock->isReferencedByVertexShader())
1189 {
1190 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1191 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1192 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1193 vertexUniformBuffers[registerIndex] = uniformBuffer;
1194 }
1195
1196 if (uniformBlock->isReferencedByFragmentShader())
1197 {
1198 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1199 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1200 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1201 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1202 }
1203 }
1204
1205 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1206}
1207
1208bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1209 unsigned int registerIndex, const gl::Caps &caps)
1210{
1211 if (shader == GL_VERTEX_SHADER)
1212 {
1213 uniformBlock->vsRegisterIndex = registerIndex;
1214 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1215 {
1216 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1217 return false;
1218 }
1219 }
1220 else if (shader == GL_FRAGMENT_SHADER)
1221 {
1222 uniformBlock->psRegisterIndex = registerIndex;
1223 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1224 {
1225 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1226 return false;
1227 }
1228 }
1229 else UNREACHABLE();
1230
1231 return true;
1232}
1233
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001234void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001235{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001236 unsigned int numUniforms = mUniforms.size();
1237 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001238 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001239 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001240 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001241}
1242
1243void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1244{
1245 setUniform(location, count, v, GL_FLOAT);
1246}
1247
1248void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1249{
1250 setUniform(location, count, v, GL_FLOAT_VEC2);
1251}
1252
1253void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1254{
1255 setUniform(location, count, v, GL_FLOAT_VEC3);
1256}
1257
1258void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1259{
1260 setUniform(location, count, v, GL_FLOAT_VEC4);
1261}
1262
1263void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1264{
1265 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1266}
1267
1268void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1269{
1270 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1271}
1272
1273void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1274{
1275 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1276}
1277
1278void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1279{
1280 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1281}
1282
1283void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1284{
1285 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1286}
1287
1288void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1289{
1290 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1291}
1292
1293void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1294{
1295 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1296}
1297
1298void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1299{
1300 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1301}
1302
1303void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1304{
1305 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1306}
1307
1308void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1309{
1310 setUniform(location, count, v, GL_INT);
1311}
1312
1313void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1314{
1315 setUniform(location, count, v, GL_INT_VEC2);
1316}
1317
1318void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1319{
1320 setUniform(location, count, v, GL_INT_VEC3);
1321}
1322
1323void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1324{
1325 setUniform(location, count, v, GL_INT_VEC4);
1326}
1327
1328void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1329{
1330 setUniform(location, count, v, GL_UNSIGNED_INT);
1331}
1332
1333void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1334{
1335 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1336}
1337
1338void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1339{
1340 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1341}
1342
1343void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1344{
1345 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1346}
1347
1348void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1349{
1350 getUniformv(location, params, GL_FLOAT);
1351}
1352
1353void ProgramD3D::getUniformiv(GLint location, GLint *params)
1354{
1355 getUniformv(location, params, GL_INT);
1356}
1357
1358void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1359{
1360 getUniformv(location, params, GL_UNSIGNED_INT);
1361}
1362
1363bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1364 const gl::Caps &caps)
1365{
Jamie Madill30d6c252014-11-13 10:03:33 -05001366 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1367 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001368
1369 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1370 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1371
1372 // Check that uniforms defined in the vertex and fragment shaders are identical
1373 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1374 UniformMap linkedUniforms;
1375
1376 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001377 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001378 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1379 linkedUniforms[vertexUniform.name] = &vertexUniform;
1380 }
1381
1382 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1383 {
1384 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1385 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1386 if (entry != linkedUniforms.end())
1387 {
1388 const sh::Uniform &vertexUniform = *entry->second;
1389 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001391 {
1392 return false;
1393 }
1394 }
1395 }
1396
1397 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1398 {
1399 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1400
1401 if (uniform.staticUse)
1402 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001403 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001404 }
1405 }
1406
1407 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1408 {
1409 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1410
1411 if (uniform.staticUse)
1412 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001413 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001414 }
1415 }
1416
1417 if (!indexUniforms(infoLog, caps))
1418 {
1419 return false;
1420 }
1421
1422 initializeUniformStorage();
1423
1424 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1425 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1426 {
1427 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1428
1429 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1430 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1431 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1432 }
1433
1434 return true;
1435}
1436
Geoff Lang492a7e42014-11-05 13:27:06 -05001437void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001438{
Geoff Lang492a7e42014-11-05 13:27:06 -05001439 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001440 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1441 encoder.skipRegisters(uniformRegister);
1442
1443 defineUniform(shader, uniform, uniform.name, &encoder);
1444}
1445
Geoff Lang492a7e42014-11-05 13:27:06 -05001446void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1447 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001448{
1449 if (uniform.isStruct())
1450 {
1451 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1452 {
1453 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1454
1455 encoder->enterAggregateType();
1456
1457 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1458 {
1459 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1460 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1461
1462 defineUniform(shader, field, fieldFullName, encoder);
1463 }
1464
1465 encoder->exitAggregateType();
1466 }
1467 }
1468 else // Not a struct
1469 {
1470 // Arrays are treated as aggregate types
1471 if (uniform.isArray())
1472 {
1473 encoder->enterAggregateType();
1474 }
1475
1476 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1477
1478 if (!linkedUniform)
1479 {
1480 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
1481 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
1482 ASSERT(linkedUniform);
1483 linkedUniform->registerElement = encoder->getCurrentElement();
1484 mUniforms.push_back(linkedUniform);
1485 }
1486
1487 ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
1488
Geoff Lang492a7e42014-11-05 13:27:06 -05001489 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001490 {
1491 linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
1492 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001493 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001494 {
1495 linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
1496 }
1497 else UNREACHABLE();
1498
1499 // Advance the uniform offset, to track registers allocation for structs
1500 encoder->encodeType(uniform.type, uniform.arraySize, false);
1501
1502 // Arrays are treated as aggregate types
1503 if (uniform.isArray())
1504 {
1505 encoder->exitAggregateType();
1506 }
1507 }
1508}
1509
1510template <typename T>
1511static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1512{
1513 ASSERT(dest != NULL);
1514 ASSERT(dirtyFlag != NULL);
1515
1516 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1517 *dest = source;
1518}
1519
1520template <typename T>
1521void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1522{
1523 const int components = gl::VariableComponentCount(targetUniformType);
1524 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1525
1526 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1527
1528 int elementCount = targetUniform->elementCount();
1529
1530 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1531
1532 if (targetUniform->type == targetUniformType)
1533 {
1534 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1535
1536 for (int i = 0; i < count; i++)
1537 {
1538 T *dest = target + (i * 4);
1539 const T *source = v + (i * components);
1540
1541 for (int c = 0; c < components; c++)
1542 {
1543 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1544 }
1545 for (int c = components; c < 4; c++)
1546 {
1547 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1548 }
1549 }
1550 }
1551 else if (targetUniform->type == targetBoolType)
1552 {
1553 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1554
1555 for (int i = 0; i < count; i++)
1556 {
1557 GLint *dest = boolParams + (i * 4);
1558 const T *source = v + (i * components);
1559
1560 for (int c = 0; c < components; c++)
1561 {
1562 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1563 }
1564 for (int c = components; c < 4; c++)
1565 {
1566 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1567 }
1568 }
1569 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001570 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001571 {
1572 ASSERT(targetUniformType == GL_INT);
1573
1574 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1575
1576 bool wasDirty = targetUniform->dirty;
1577
1578 for (int i = 0; i < count; i++)
1579 {
1580 GLint *dest = target + (i * 4);
1581 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1582
1583 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1584 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1585 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1586 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1587 }
1588
1589 if (!wasDirty && targetUniform->dirty)
1590 {
1591 mDirtySamplerMapping = true;
1592 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001593 }
1594 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001595}
Brandon Jones18bd4102014-09-22 14:21:44 -07001596
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001597template<typename T>
1598bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1599{
1600 bool dirty = false;
1601 int copyWidth = std::min(targetHeight, srcWidth);
1602 int copyHeight = std::min(targetWidth, srcHeight);
1603
1604 for (int x = 0; x < copyWidth; x++)
1605 {
1606 for (int y = 0; y < copyHeight; y++)
1607 {
1608 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1609 }
1610 }
1611 // clear unfilled right side
1612 for (int y = 0; y < copyWidth; y++)
1613 {
1614 for (int x = copyHeight; x < targetWidth; x++)
1615 {
1616 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1617 }
1618 }
1619 // clear unfilled bottom.
1620 for (int y = copyWidth; y < targetHeight; y++)
1621 {
1622 for (int x = 0; x < targetWidth; x++)
1623 {
1624 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1625 }
1626 }
1627
1628 return dirty;
1629}
1630
1631template<typename T>
1632bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1633{
1634 bool dirty = false;
1635 int copyWidth = std::min(targetWidth, srcWidth);
1636 int copyHeight = std::min(targetHeight, srcHeight);
1637
1638 for (int y = 0; y < copyHeight; y++)
1639 {
1640 for (int x = 0; x < copyWidth; x++)
1641 {
1642 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1643 }
1644 }
1645 // clear unfilled right side
1646 for (int y = 0; y < copyHeight; y++)
1647 {
1648 for (int x = copyWidth; x < targetWidth; x++)
1649 {
1650 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1651 }
1652 }
1653 // clear unfilled bottom.
1654 for (int y = copyHeight; y < targetHeight; y++)
1655 {
1656 for (int x = 0; x < targetWidth; x++)
1657 {
1658 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1659 }
1660 }
1661
1662 return dirty;
1663}
1664
1665template <int cols, int rows>
1666void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1667{
1668 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1669
1670 int elementCount = targetUniform->elementCount();
1671
1672 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1673 const unsigned int targetMatrixStride = (4 * rows);
1674 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1675
1676 for (int i = 0; i < count; i++)
1677 {
1678 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1679 if (transpose == GL_FALSE)
1680 {
1681 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1682 }
1683 else
1684 {
1685 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1686 }
1687 target += targetMatrixStride;
1688 value += cols * rows;
1689 }
1690}
1691
1692template <typename T>
1693void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1694{
1695 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1696
1697 if (gl::IsMatrixType(targetUniform->type))
1698 {
1699 const int rows = gl::VariableRowCount(targetUniform->type);
1700 const int cols = gl::VariableColumnCount(targetUniform->type);
1701 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1702 }
1703 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1704 {
1705 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1706 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1707 size * sizeof(T));
1708 }
1709 else
1710 {
1711 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1712 switch (gl::VariableComponentType(targetUniform->type))
1713 {
1714 case GL_BOOL:
1715 {
1716 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1717
1718 for (unsigned int i = 0; i < size; i++)
1719 {
1720 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1721 }
1722 }
1723 break;
1724
1725 case GL_FLOAT:
1726 {
1727 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1728
1729 for (unsigned int i = 0; i < size; i++)
1730 {
1731 params[i] = static_cast<T>(floatParams[i]);
1732 }
1733 }
1734 break;
1735
1736 case GL_INT:
1737 {
1738 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1739
1740 for (unsigned int i = 0; i < size; i++)
1741 {
1742 params[i] = static_cast<T>(intParams[i]);
1743 }
1744 }
1745 break;
1746
1747 case GL_UNSIGNED_INT:
1748 {
1749 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1750
1751 for (unsigned int i = 0; i < size; i++)
1752 {
1753 params[i] = static_cast<T>(uintParams[i]);
1754 }
1755 }
1756 break;
1757
1758 default: UNREACHABLE();
1759 }
1760 }
1761}
1762
1763template <typename VarT>
1764void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1765 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1766 bool inRowMajorLayout)
1767{
1768 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1769 {
1770 const VarT &field = fields[uniformIndex];
1771 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1772
1773 if (field.isStruct())
1774 {
1775 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1776
1777 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1778 {
1779 encoder->enterAggregateType();
1780
1781 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1782 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1783
1784 encoder->exitAggregateType();
1785 }
1786 }
1787 else
1788 {
1789 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1790
1791 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1792
1793 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1794 blockIndex, memberInfo);
1795
1796 // add to uniform list, but not index, since uniform block uniforms have no location
1797 blockUniformIndexes->push_back(mUniforms.size());
1798 mUniforms.push_back(newUniform);
1799 }
1800 }
1801}
1802
1803bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1804 const gl::Caps &caps)
1805{
Jamie Madill30d6c252014-11-13 10:03:33 -05001806 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001807
1808 // create uniform block entries if they do not exist
1809 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1810 {
1811 std::vector<unsigned int> blockUniformIndexes;
1812 const unsigned int blockIndex = mUniformBlocks.size();
1813
1814 // define member uniforms
1815 sh::BlockLayoutEncoder *encoder = NULL;
1816
1817 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1818 {
1819 encoder = new sh::Std140BlockEncoder;
1820 }
1821 else
1822 {
1823 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1824 }
1825 ASSERT(encoder);
1826
1827 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1828
1829 size_t dataSize = encoder->getBlockSize();
1830
1831 // create all the uniform blocks
1832 if (interfaceBlock.arraySize > 0)
1833 {
1834 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1835 {
1836 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1837 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1838 mUniformBlocks.push_back(newUniformBlock);
1839 }
1840 }
1841 else
1842 {
1843 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1844 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1845 mUniformBlocks.push_back(newUniformBlock);
1846 }
1847 }
1848
1849 if (interfaceBlock.staticUse)
1850 {
1851 // Assign registers to the uniform blocks
1852 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1853 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1854 ASSERT(blockIndex != GL_INVALID_INDEX);
1855 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1856
1857 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1858
1859 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1860 {
1861 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1862 ASSERT(uniformBlock->name == interfaceBlock.name);
1863
1864 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1865 interfaceBlockRegister + uniformBlockElement, caps))
1866 {
1867 return false;
1868 }
1869 }
1870 }
1871
1872 return true;
1873}
1874
1875bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1876 GLenum samplerType,
1877 unsigned int samplerCount,
1878 std::vector<Sampler> &outSamplers,
1879 GLuint *outUsedRange)
1880{
1881 unsigned int samplerIndex = startSamplerIndex;
1882
1883 do
1884 {
1885 if (samplerIndex < outSamplers.size())
1886 {
1887 Sampler& sampler = outSamplers[samplerIndex];
1888 sampler.active = true;
1889 sampler.textureType = GetTextureType(samplerType);
1890 sampler.logicalTextureUnit = 0;
1891 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1892 }
1893 else
1894 {
1895 return false;
1896 }
1897
1898 samplerIndex++;
1899 } while (samplerIndex < startSamplerIndex + samplerCount);
1900
1901 return true;
1902}
1903
1904bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1905{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001906 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001907 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1908
1909 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1910 {
1911 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1912 &mUsedVertexSamplerRange))
1913 {
1914 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1915 mSamplersVS.size());
1916 return false;
1917 }
1918
1919 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1920 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1921 {
1922 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1923 caps.maxVertexUniformVectors);
1924 return false;
1925 }
1926 }
1927
1928 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1929 {
1930 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1931 &mUsedPixelSamplerRange))
1932 {
1933 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1934 mSamplersPS.size());
1935 return false;
1936 }
1937
1938 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1939 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1940 {
1941 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1942 caps.maxFragmentUniformVectors);
1943 return false;
1944 }
1945 }
1946
1947 return true;
1948}
1949
1950bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1951{
1952 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1953 {
1954 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1955
Geoff Lang2ec386b2014-12-03 14:44:38 -05001956 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001957 {
1958 if (!indexSamplerUniform(uniform, infoLog, caps))
1959 {
1960 return false;
1961 }
1962 }
1963
1964 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1965 {
1966 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1967 }
1968 }
1969
1970 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001971}
1972
Brandon Jonesc9610c52014-08-25 17:02:59 -07001973void ProgramD3D::reset()
1974{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001975 ProgramImpl::reset();
1976
Brandon Joneseb994362014-09-24 10:27:28 -07001977 SafeDeleteContainer(mVertexExecutables);
1978 SafeDeleteContainer(mPixelExecutables);
1979 SafeDelete(mGeometryExecutable);
1980
1981 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001982
Brandon Jones22502d52014-08-29 16:58:36 -07001983 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001984 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001985 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001986
1987 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001988 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001989 mUsesFragDepth = false;
1990 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001991 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001992
Brandon Jonesc9610c52014-08-25 17:02:59 -07001993 SafeDelete(mVertexUniformStorage);
1994 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001995
1996 mSamplersPS.clear();
1997 mSamplersVS.clear();
1998
1999 mUsedVertexSamplerRange = 0;
2000 mUsedPixelSamplerRange = 0;
2001 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002002
2003 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002004}
2005
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006unsigned int ProgramD3D::getSerial() const
2007{
2008 return mSerial;
2009}
2010
2011unsigned int ProgramD3D::issueSerial()
2012{
2013 return mCurrentSerial++;
2014}
2015
Jamie Madill437d2662014-12-05 14:23:35 -05002016void ProgramD3D::initAttributesByLayout()
2017{
2018 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2019 {
2020 mAttributesByLayout[i] = i;
2021 }
2022
2023 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2024}
2025
2026void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
2027 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
2028{
2029 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
2030
2031 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2032 {
2033 oldTranslatedAttributes[i] = attributes[i];
2034 }
2035
2036 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2037 {
2038 int oldIndex = mAttributesByLayout[i];
2039 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2040 attributes[i] = oldTranslatedAttributes[oldIndex];
2041 }
2042}
2043
Brandon Jonesc9610c52014-08-25 17:02:59 -07002044}