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