blob: 0e6b653783afcc952fd99002af0e7346dd4a74e6 [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 Kinrossaf1bdff2015-02-17 11:07:46 -08009#include "libANGLE/features.h"
10
11#if ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
12// <future> requires _HAS_EXCEPTIONS to be defined
13#ifdef _HAS_EXCEPTIONS
14#undef _HAS_EXCEPTIONS
15#endif // _HAS_EXCEPTIONS
16#define _HAS_EXCEPTIONS 1
17#include <future> // For std::async
18#endif // ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
19
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021
22#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050023#include "libANGLE/Framebuffer.h"
24#include "libANGLE/FramebufferAttachment.h"
25#include "libANGLE/Program.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050026#include "libANGLE/renderer/d3d/DynamicHLSL.h"
27#include "libANGLE/renderer/d3d/RendererD3D.h"
28#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050029#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050030#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050031
Brandon Jonesc9610c52014-08-25 17:02:59 -070032namespace rx
33{
34
Brandon Joneseb994362014-09-24 10:27:28 -070035namespace
36{
37
Brandon Jones1a8a7e32014-10-01 12:49:30 -070038GLenum GetTextureType(GLenum samplerType)
39{
40 switch (samplerType)
41 {
42 case GL_SAMPLER_2D:
43 case GL_INT_SAMPLER_2D:
44 case GL_UNSIGNED_INT_SAMPLER_2D:
45 case GL_SAMPLER_2D_SHADOW:
46 return GL_TEXTURE_2D;
47 case GL_SAMPLER_3D:
48 case GL_INT_SAMPLER_3D:
49 case GL_UNSIGNED_INT_SAMPLER_3D:
50 return GL_TEXTURE_3D;
51 case GL_SAMPLER_CUBE:
52 case GL_SAMPLER_CUBE_SHADOW:
53 return GL_TEXTURE_CUBE_MAP;
54 case GL_INT_SAMPLER_CUBE:
55 case GL_UNSIGNED_INT_SAMPLER_CUBE:
56 return GL_TEXTURE_CUBE_MAP;
57 case GL_SAMPLER_2D_ARRAY:
58 case GL_INT_SAMPLER_2D_ARRAY:
59 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
60 case GL_SAMPLER_2D_ARRAY_SHADOW:
61 return GL_TEXTURE_2D_ARRAY;
62 default: UNREACHABLE();
63 }
64
65 return GL_TEXTURE_2D;
66}
67
Brandon Joneseb994362014-09-24 10:27:28 -070068void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS])
69{
70 size_t layoutIndex = 0;
71 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
72 {
73 ASSERT(layoutIndex < gl::MAX_VERTEX_ATTRIBS);
74
75 const sh::Attribute &shaderAttr = shaderAttributes[attributeIndex];
76
77 if (shaderAttr.type != GL_NONE)
78 {
79 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
80
81 for (size_t rowIndex = 0; static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); rowIndex++, layoutIndex++)
82 {
83 gl::VertexFormat *defaultFormat = &inputLayout[layoutIndex];
84
85 defaultFormat->mType = gl::VariableComponentType(transposedType);
86 defaultFormat->mNormalized = false;
87 defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool
88 defaultFormat->mComponents = gl::VariableColumnCount(transposedType);
89 }
90 }
91 }
92}
93
94std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
95{
Jamie Madillb4463142014-12-19 14:56:54 -050096 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070097
Jamie Madillb4463142014-12-19 14:56:54 -050098 if (!shaderOutputVars.empty())
99 {
100 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
101 }
Brandon Joneseb994362014-09-24 10:27:28 -0700102
103 return defaultPixelOutput;
104}
105
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700106bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
107{
108 return var.isRowMajorLayout;
109}
110
111bool IsRowMajorLayout(const sh::ShaderVariable &var)
112{
113 return false;
114}
115
Jamie Madill437d2662014-12-05 14:23:35 -0500116struct AttributeSorter
117{
118 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
119 : originalIndices(semanticIndices)
120 {
121 }
122
123 bool operator()(int a, int b)
124 {
125 if (originalIndices[a] == -1) return false;
126 if (originalIndices[b] == -1) return true;
127 return (originalIndices[a] < originalIndices[b]);
128 }
129
130 const ProgramImpl::SemanticIndexArray &originalIndices;
131};
132
Brandon Joneseb994362014-09-24 10:27:28 -0700133}
134
135ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
136 const GLenum signature[],
Geoff Lang359ef262015-01-05 14:42:29 -0500137 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700138 : mShaderExecutable(shaderExecutable)
139{
140 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
141 {
142 mInputs[attributeIndex] = inputLayout[attributeIndex];
143 mSignature[attributeIndex] = signature[attributeIndex];
144 }
145}
146
147ProgramD3D::VertexExecutable::~VertexExecutable()
148{
149 SafeDelete(mShaderExecutable);
150}
151
152bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
153{
154 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
155 {
156 if (mSignature[attributeIndex] != signature[attributeIndex])
157 {
158 return false;
159 }
160 }
161
162 return true;
163}
164
Geoff Lang359ef262015-01-05 14:42:29 -0500165ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700166 : mOutputSignature(outputSignature),
167 mShaderExecutable(shaderExecutable)
168{
169}
170
171ProgramD3D::PixelExecutable::~PixelExecutable()
172{
173 SafeDelete(mShaderExecutable);
174}
175
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700176ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
177{
178}
179
Geoff Lang7dd2e102014-11-10 15:19:26 -0500180unsigned int ProgramD3D::mCurrentSerial = 1;
181
Jamie Madill93e13fb2014-11-06 15:27:25 -0500182ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700183 : ProgramImpl(),
184 mRenderer(renderer),
185 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700186 mGeometryExecutable(NULL),
187 mVertexWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
188 mPixelWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
Brandon Jones44151a92014-09-10 11:32:25 -0700189 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700190 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700191 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700192 mUsedVertexSamplerRange(0),
193 mUsedPixelSamplerRange(0),
194 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500195 mShaderVersion(100),
196 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700197{
Brandon Joneseb994362014-09-24 10:27:28 -0700198 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700199}
200
201ProgramD3D::~ProgramD3D()
202{
203 reset();
204 SafeDelete(mDynamicHLSL);
205}
206
Brandon Jones44151a92014-09-10 11:32:25 -0700207bool ProgramD3D::usesPointSpriteEmulation() const
208{
209 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
210}
211
212bool ProgramD3D::usesGeometryShader() const
213{
Cooper Partine6664f02015-01-09 16:22:24 -0800214 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
215}
216
217bool ProgramD3D::usesInstancedPointSpriteEmulation() const
218{
219 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700220}
221
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700222GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
223{
224 GLint logicalTextureUnit = -1;
225
226 switch (type)
227 {
228 case gl::SAMPLER_PIXEL:
229 ASSERT(samplerIndex < caps.maxTextureImageUnits);
230 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
231 {
232 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
233 }
234 break;
235 case gl::SAMPLER_VERTEX:
236 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
237 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
238 {
239 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
240 }
241 break;
242 default: UNREACHABLE();
243 }
244
245 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
246 {
247 return logicalTextureUnit;
248 }
249
250 return -1;
251}
252
253// Returns the texture type for a given Direct3D 9 sampler type and
254// index (0-15 for the pixel shader and 0-3 for the vertex shader).
255GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
256{
257 switch (type)
258 {
259 case gl::SAMPLER_PIXEL:
260 ASSERT(samplerIndex < mSamplersPS.size());
261 ASSERT(mSamplersPS[samplerIndex].active);
262 return mSamplersPS[samplerIndex].textureType;
263 case gl::SAMPLER_VERTEX:
264 ASSERT(samplerIndex < mSamplersVS.size());
265 ASSERT(mSamplersVS[samplerIndex].active);
266 return mSamplersVS[samplerIndex].textureType;
267 default: UNREACHABLE();
268 }
269
270 return GL_TEXTURE_2D;
271}
272
273GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
274{
275 switch (type)
276 {
277 case gl::SAMPLER_PIXEL:
278 return mUsedPixelSamplerRange;
279 case gl::SAMPLER_VERTEX:
280 return mUsedVertexSamplerRange;
281 default:
282 UNREACHABLE();
283 return 0;
284 }
285}
286
287void ProgramD3D::updateSamplerMapping()
288{
289 if (!mDirtySamplerMapping)
290 {
291 return;
292 }
293
294 mDirtySamplerMapping = false;
295
296 // Retrieve sampler uniform values
297 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
298 {
299 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
300
301 if (targetUniform->dirty)
302 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500303 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700304 {
305 int count = targetUniform->elementCount();
306 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
307
308 if (targetUniform->isReferencedByFragmentShader())
309 {
310 unsigned int firstIndex = targetUniform->psRegisterIndex;
311
312 for (int i = 0; i < count; i++)
313 {
314 unsigned int samplerIndex = firstIndex + i;
315
316 if (samplerIndex < mSamplersPS.size())
317 {
318 ASSERT(mSamplersPS[samplerIndex].active);
319 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
320 }
321 }
322 }
323
324 if (targetUniform->isReferencedByVertexShader())
325 {
326 unsigned int firstIndex = targetUniform->vsRegisterIndex;
327
328 for (int i = 0; i < count; i++)
329 {
330 unsigned int samplerIndex = firstIndex + i;
331
332 if (samplerIndex < mSamplersVS.size())
333 {
334 ASSERT(mSamplersVS[samplerIndex].active);
335 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
336 }
337 }
338 }
339 }
340 }
341 }
342}
343
344bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
345{
346 // if any two active samplers in a program are of different types, but refer to the same
347 // texture image unit, and this is the current program, then ValidateProgram will fail, and
348 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
349 updateSamplerMapping();
350
351 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
352
353 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
354 {
355 if (mSamplersPS[i].active)
356 {
357 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
358
359 if (unit >= textureUnitTypes.size())
360 {
361 if (infoLog)
362 {
363 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
364 }
365
366 return false;
367 }
368
369 if (textureUnitTypes[unit] != GL_NONE)
370 {
371 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
372 {
373 if (infoLog)
374 {
375 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
376 }
377
378 return false;
379 }
380 }
381 else
382 {
383 textureUnitTypes[unit] = mSamplersPS[i].textureType;
384 }
385 }
386 }
387
388 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
389 {
390 if (mSamplersVS[i].active)
391 {
392 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
393
394 if (unit >= textureUnitTypes.size())
395 {
396 if (infoLog)
397 {
398 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
399 }
400
401 return false;
402 }
403
404 if (textureUnitTypes[unit] != GL_NONE)
405 {
406 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
407 {
408 if (infoLog)
409 {
410 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
411 }
412
413 return false;
414 }
415 }
416 else
417 {
418 textureUnitTypes[unit] = mSamplersVS[i].textureType;
419 }
420 }
421 }
422
423 return true;
424}
425
Geoff Lang7dd2e102014-11-10 15:19:26 -0500426LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700427{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500428 int compileFlags = stream->readInt<int>();
429 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
430 {
431 infoLog.append("Mismatched compilation flags.");
432 return LinkResult(false, gl::Error(GL_NO_ERROR));
433 }
434
Brandon Jones44151a92014-09-10 11:32:25 -0700435 stream->readInt(&mShaderVersion);
436
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700437 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
438 for (unsigned int i = 0; i < psSamplerCount; ++i)
439 {
440 Sampler sampler;
441 stream->readBool(&sampler.active);
442 stream->readInt(&sampler.logicalTextureUnit);
443 stream->readInt(&sampler.textureType);
444 mSamplersPS.push_back(sampler);
445 }
446 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
447 for (unsigned int i = 0; i < vsSamplerCount; ++i)
448 {
449 Sampler sampler;
450 stream->readBool(&sampler.active);
451 stream->readInt(&sampler.logicalTextureUnit);
452 stream->readInt(&sampler.textureType);
453 mSamplersVS.push_back(sampler);
454 }
455
456 stream->readInt(&mUsedVertexSamplerRange);
457 stream->readInt(&mUsedPixelSamplerRange);
458
459 const unsigned int uniformCount = stream->readInt<unsigned int>();
460 if (stream->error())
461 {
462 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500463 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700464 }
465
466 mUniforms.resize(uniformCount);
467 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
468 {
469 GLenum type = stream->readInt<GLenum>();
470 GLenum precision = stream->readInt<GLenum>();
471 std::string name = stream->readString();
472 unsigned int arraySize = stream->readInt<unsigned int>();
473 int blockIndex = stream->readInt<int>();
474
475 int offset = stream->readInt<int>();
476 int arrayStride = stream->readInt<int>();
477 int matrixStride = stream->readInt<int>();
478 bool isRowMajorMatrix = stream->readBool();
479
480 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
481
482 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
483
484 stream->readInt(&uniform->psRegisterIndex);
485 stream->readInt(&uniform->vsRegisterIndex);
486 stream->readInt(&uniform->registerCount);
487 stream->readInt(&uniform->registerElement);
488
489 mUniforms[uniformIndex] = uniform;
490 }
491
492 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
493 if (stream->error())
494 {
495 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500496 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700497 }
498
499 mUniformIndex.resize(uniformIndexCount);
500 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
501 {
502 stream->readString(&mUniformIndex[uniformIndexIndex].name);
503 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
504 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
505 }
506
507 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
508 if (stream->error())
509 {
510 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500511 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700512 }
513
514 mUniformBlocks.resize(uniformBlockCount);
515 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
516 {
517 std::string name = stream->readString();
518 unsigned int elementIndex = stream->readInt<unsigned int>();
519 unsigned int dataSize = stream->readInt<unsigned int>();
520
521 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
522
523 stream->readInt(&uniformBlock->psRegisterIndex);
524 stream->readInt(&uniformBlock->vsRegisterIndex);
525
526 unsigned int numMembers = stream->readInt<unsigned int>();
527 uniformBlock->memberUniformIndexes.resize(numMembers);
528 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
529 {
530 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
531 }
532
533 mUniformBlocks[uniformBlockIndex] = uniformBlock;
534 }
535
Brandon Joneseb994362014-09-24 10:27:28 -0700536 stream->readInt(&mTransformFeedbackBufferMode);
537 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
538 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
539 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
540 {
541 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
542
543 stream->readString(&varying.name);
544 stream->readInt(&varying.type);
545 stream->readInt(&varying.size);
546 stream->readString(&varying.semanticName);
547 stream->readInt(&varying.semanticIndex);
548 stream->readInt(&varying.semanticIndexCount);
549 }
550
Brandon Jones22502d52014-08-29 16:58:36 -0700551 stream->readString(&mVertexHLSL);
552 stream->readInt(&mVertexWorkarounds);
553 stream->readString(&mPixelHLSL);
554 stream->readInt(&mPixelWorkarounds);
555 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700556 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700557
558 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
559 mPixelShaderKey.resize(pixelShaderKeySize);
560 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
561 {
562 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
563 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
564 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
565 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
566 }
567
Brandon Joneseb994362014-09-24 10:27:28 -0700568 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
569
570 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
571 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
572 {
573 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
574
575 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
576 {
577 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
578 stream->readInt(&vertexInput->mType);
579 stream->readInt(&vertexInput->mNormalized);
580 stream->readInt(&vertexInput->mComponents);
581 stream->readBool(&vertexInput->mPureInteger);
582 }
583
584 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
585 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400586
Geoff Lang359ef262015-01-05 14:42:29 -0500587 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400588 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
589 SHADER_VERTEX,
590 mTransformFeedbackLinkedVaryings,
591 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
592 &shaderExecutable);
593 if (error.isError())
594 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500595 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400596 }
597
Brandon Joneseb994362014-09-24 10:27:28 -0700598 if (!shaderExecutable)
599 {
600 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500601 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700602 }
603
604 // generated converted input layout
605 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
606 getInputLayoutSignature(inputLayout, signature);
607
608 // add new binary
609 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
610
611 stream->skip(vertexShaderSize);
612 }
613
614 const size_t pixelShaderCount = stream->readInt<unsigned int>();
615 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
616 {
617 const size_t outputCount = stream->readInt<unsigned int>();
618 std::vector<GLenum> outputs(outputCount);
619 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
620 {
621 stream->readInt(&outputs[outputIndex]);
622 }
623
624 const size_t pixelShaderSize = stream->readInt<unsigned int>();
625 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500626 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400627 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
628 mTransformFeedbackLinkedVaryings,
629 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
630 &shaderExecutable);
631 if (error.isError())
632 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500633 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400634 }
Brandon Joneseb994362014-09-24 10:27:28 -0700635
636 if (!shaderExecutable)
637 {
638 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500639 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700640 }
641
642 // add new binary
643 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
644
645 stream->skip(pixelShaderSize);
646 }
647
648 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
649
650 if (geometryShaderSize > 0)
651 {
652 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400653 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
654 mTransformFeedbackLinkedVaryings,
655 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
656 &mGeometryExecutable);
657 if (error.isError())
658 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500659 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400660 }
Brandon Joneseb994362014-09-24 10:27:28 -0700661
662 if (!mGeometryExecutable)
663 {
664 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500665 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700666 }
667 stream->skip(geometryShaderSize);
668 }
669
Brandon Jones18bd4102014-09-22 14:21:44 -0700670 GUID binaryIdentifier = {0};
671 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
672
673 GUID identifier = mRenderer->getAdapterIdentifier();
674 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
675 {
676 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500677 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700678 }
679
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700680 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500681 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700682
Geoff Lang7dd2e102014-11-10 15:19:26 -0500683 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700684}
685
Geoff Langb543aff2014-09-30 14:52:54 -0400686gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700687{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500688 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
689
Brandon Jones44151a92014-09-10 11:32:25 -0700690 stream->writeInt(mShaderVersion);
691
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700692 stream->writeInt(mSamplersPS.size());
693 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
694 {
695 stream->writeInt(mSamplersPS[i].active);
696 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
697 stream->writeInt(mSamplersPS[i].textureType);
698 }
699
700 stream->writeInt(mSamplersVS.size());
701 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
702 {
703 stream->writeInt(mSamplersVS[i].active);
704 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
705 stream->writeInt(mSamplersVS[i].textureType);
706 }
707
708 stream->writeInt(mUsedVertexSamplerRange);
709 stream->writeInt(mUsedPixelSamplerRange);
710
711 stream->writeInt(mUniforms.size());
712 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
713 {
714 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
715
716 stream->writeInt(uniform.type);
717 stream->writeInt(uniform.precision);
718 stream->writeString(uniform.name);
719 stream->writeInt(uniform.arraySize);
720 stream->writeInt(uniform.blockIndex);
721
722 stream->writeInt(uniform.blockInfo.offset);
723 stream->writeInt(uniform.blockInfo.arrayStride);
724 stream->writeInt(uniform.blockInfo.matrixStride);
725 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
726
727 stream->writeInt(uniform.psRegisterIndex);
728 stream->writeInt(uniform.vsRegisterIndex);
729 stream->writeInt(uniform.registerCount);
730 stream->writeInt(uniform.registerElement);
731 }
732
733 stream->writeInt(mUniformIndex.size());
734 for (size_t i = 0; i < mUniformIndex.size(); ++i)
735 {
736 stream->writeString(mUniformIndex[i].name);
737 stream->writeInt(mUniformIndex[i].element);
738 stream->writeInt(mUniformIndex[i].index);
739 }
740
741 stream->writeInt(mUniformBlocks.size());
742 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
743 {
744 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
745
746 stream->writeString(uniformBlock.name);
747 stream->writeInt(uniformBlock.elementIndex);
748 stream->writeInt(uniformBlock.dataSize);
749
750 stream->writeInt(uniformBlock.memberUniformIndexes.size());
751 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
752 {
753 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
754 }
755
756 stream->writeInt(uniformBlock.psRegisterIndex);
757 stream->writeInt(uniformBlock.vsRegisterIndex);
758 }
759
Brandon Joneseb994362014-09-24 10:27:28 -0700760 stream->writeInt(mTransformFeedbackBufferMode);
761 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
762 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
763 {
764 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
765
766 stream->writeString(varying.name);
767 stream->writeInt(varying.type);
768 stream->writeInt(varying.size);
769 stream->writeString(varying.semanticName);
770 stream->writeInt(varying.semanticIndex);
771 stream->writeInt(varying.semanticIndexCount);
772 }
773
Brandon Jones22502d52014-08-29 16:58:36 -0700774 stream->writeString(mVertexHLSL);
775 stream->writeInt(mVertexWorkarounds);
776 stream->writeString(mPixelHLSL);
777 stream->writeInt(mPixelWorkarounds);
778 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700779 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700780
Brandon Joneseb994362014-09-24 10:27:28 -0700781 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700782 stream->writeInt(pixelShaderKey.size());
783 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
784 {
Brandon Joneseb994362014-09-24 10:27:28 -0700785 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700786 stream->writeInt(variable.type);
787 stream->writeString(variable.name);
788 stream->writeString(variable.source);
789 stream->writeInt(variable.outputIndex);
790 }
791
Brandon Joneseb994362014-09-24 10:27:28 -0700792 stream->writeInt(mVertexExecutables.size());
793 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
794 {
795 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
796
797 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
798 {
799 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
800 stream->writeInt(vertexInput.mType);
801 stream->writeInt(vertexInput.mNormalized);
802 stream->writeInt(vertexInput.mComponents);
803 stream->writeInt(vertexInput.mPureInteger);
804 }
805
806 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
807 stream->writeInt(vertexShaderSize);
808
809 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
810 stream->writeBytes(vertexBlob, vertexShaderSize);
811 }
812
813 stream->writeInt(mPixelExecutables.size());
814 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
815 {
816 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
817
818 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
819 stream->writeInt(outputs.size());
820 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
821 {
822 stream->writeInt(outputs[outputIndex]);
823 }
824
825 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
826 stream->writeInt(pixelShaderSize);
827
828 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
829 stream->writeBytes(pixelBlob, pixelShaderSize);
830 }
831
832 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
833 stream->writeInt(geometryShaderSize);
834
835 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
836 {
837 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
838 stream->writeBytes(geometryBlob, geometryShaderSize);
839 }
840
Brandon Jones18bd4102014-09-22 14:21:44 -0700841 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400842 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700843
Geoff Langb543aff2014-09-30 14:52:54 -0400844 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700845}
846
Geoff Lang359ef262015-01-05 14:42:29 -0500847gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700848{
Brandon Joneseb994362014-09-24 10:27:28 -0700849 std::vector<GLenum> outputs;
850
Jamie Madill48faf802014-11-06 15:27:22 -0500851 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700852
853 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
854 {
855 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
856
857 if (colorbuffer)
858 {
859 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
860 }
861 else
862 {
863 outputs.push_back(GL_NONE);
864 }
865 }
866
Jamie Madill97399232014-12-23 12:31:15 -0500867 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700868}
869
Jamie Madill97399232014-12-23 12:31:15 -0500870gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500871 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500872 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700873{
874 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
875 {
876 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
877 {
Geoff Langb543aff2014-09-30 14:52:54 -0400878 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
879 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700880 }
881 }
882
Brandon Jones22502d52014-08-29 16:58:36 -0700883 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
884 outputSignature);
885
886 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500887 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500888
889 gl::InfoLog tempInfoLog;
890 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
891
892 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400893 mTransformFeedbackLinkedVaryings,
894 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
895 mPixelWorkarounds, &pixelExecutable);
896 if (error.isError())
897 {
898 return error;
899 }
Brandon Joneseb994362014-09-24 10:27:28 -0700900
Jamie Madill97399232014-12-23 12:31:15 -0500901 if (pixelExecutable)
902 {
903 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
904 }
905 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700906 {
907 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
908 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
909 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
910 }
Brandon Jones22502d52014-08-29 16:58:36 -0700911
Geoff Langb543aff2014-09-30 14:52:54 -0400912 *outExectuable = pixelExecutable;
913 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700914}
915
Jamie Madill97399232014-12-23 12:31:15 -0500916gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500917 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500918 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700919{
Brandon Joneseb994362014-09-24 10:27:28 -0700920 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
921 getInputLayoutSignature(inputLayout, signature);
922
923 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
924 {
925 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
926 {
Geoff Langb543aff2014-09-30 14:52:54 -0400927 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
928 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700929 }
930 }
931
Brandon Jones22502d52014-08-29 16:58:36 -0700932 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700933 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700934
935 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500936 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500937
938 gl::InfoLog tempInfoLog;
939 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
940
941 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400942 mTransformFeedbackLinkedVaryings,
943 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
944 mVertexWorkarounds, &vertexExecutable);
945 if (error.isError())
946 {
947 return error;
948 }
949
Jamie Madill97399232014-12-23 12:31:15 -0500950 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700951 {
952 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
953 }
Jamie Madill97399232014-12-23 12:31:15 -0500954 else if (!infoLog)
955 {
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800956 // 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 -0500957 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
958 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
959 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
960 }
Brandon Jones22502d52014-08-29 16:58:36 -0700961
Geoff Langb543aff2014-09-30 14:52:54 -0400962 *outExectuable = vertexExecutable;
963 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700964}
965
Geoff Lang7dd2e102014-11-10 15:19:26 -0500966LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
967 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700968{
Brandon Jones18bd4102014-09-22 14:21:44 -0700969 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
970 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700971
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800972 gl::Error vertexShaderResult(GL_NO_ERROR);
973 gl::InfoLog tempVertexShaderInfoLog;
Austin Kinross31018482015-01-30 13:06:52 -0800974
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800975#if ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
976 // Use an async task to begin compiling the vertex shader asynchronously on its own task.
977 std::future<ShaderExecutableD3D*> vertexShaderTask = std::async([this, vertexShader, &tempVertexShaderInfoLog, &vertexShaderResult]()
978 {
979#endif // ANGLE_MULTITHREADED_D3D_SHADER_COMPILE
980
981 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
982 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
983 ShaderExecutableD3D *defaultVertexExecutable = NULL;
984 vertexShaderResult = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &tempVertexShaderInfoLog);
985
986#if ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
987 return defaultVertexExecutable;
988 });
989#endif // ANGLE_MULTITHREADED_D3D_SHADER_COMPILE
990
991 // Continue to compile the pixel shader on the main thread
Brandon Joneseb994362014-09-24 10:27:28 -0700992 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500993 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800994 gl::Error error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
995
996#if ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
997 // Call .get() on the vertex shader compilation. This waits until the task is complete before returning
998 ShaderExecutableD3D *defaultVertexExecutable = vertexShaderTask.get();
999#endif // ANGLE_MULTITHREADED_D3D_SHADER_COMPILE
1000
1001 // Combine the temporary infoLog with the real one
1002 if (tempVertexShaderInfoLog.getLength() > 0)
1003 {
1004 std::vector<char> tempCharBuffer(tempVertexShaderInfoLog.getLength() + 3);
1005 tempVertexShaderInfoLog.getLog(tempVertexShaderInfoLog.getLength(), NULL, &tempCharBuffer[0]);
1006 infoLog.append(&tempCharBuffer[0]);
1007 }
1008
1009 if (vertexShaderResult.isError())
1010 {
1011 return LinkResult(false, vertexShaderResult);
1012 }
1013
1014 // If the pixel shader compilation failed, then return error
Geoff Langb543aff2014-09-30 14:52:54 -04001015 if (error.isError())
1016 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001017 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001018 }
Brandon Jones44151a92014-09-10 11:32:25 -07001019
Brandon Joneseb994362014-09-24 10:27:28 -07001020 if (usesGeometryShader())
1021 {
1022 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001023
Geoff Langb543aff2014-09-30 14:52:54 -04001024
1025 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1026 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
1027 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
1028 if (error.isError())
1029 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001030 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001031 }
Brandon Joneseb994362014-09-24 10:27:28 -07001032 }
1033
Brandon Jones091540d2014-10-29 11:32:04 -07001034#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001035 if (usesGeometryShader() && mGeometryExecutable)
1036 {
1037 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1038 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1039 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1040 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1041 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1042 }
1043
1044 if (defaultVertexExecutable)
1045 {
1046 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1047 }
1048
1049 if (defaultPixelExecutable)
1050 {
1051 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1052 }
1053#endif
1054
Geoff Langb543aff2014-09-30 14:52:54 -04001055 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001056 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001057}
1058
Geoff Lang7dd2e102014-11-10 15:19:26 -05001059LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1060 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1061 const std::vector<std::string> &transformFeedbackVaryings,
1062 GLenum transformFeedbackBufferMode,
1063 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1064 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001065{
Brandon Joneseb994362014-09-24 10:27:28 -07001066 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1067 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1068
Jamie Madillde8892b2014-11-11 13:00:22 -05001069 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1070 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001071
Brandon Joneseb994362014-09-24 10:27:28 -07001072 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001073
1074 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
1075 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1076
1077 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1078 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001079 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001080
1081 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001082 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001083 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1084
Geoff Langbdee2d52014-09-17 11:02:51 -04001085 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001086 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001087 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001088 }
1089
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001091 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001092 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001093 }
1094
Jamie Madillde8892b2014-11-11 13:00:22 -05001095 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001096 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1097 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1098 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001100 }
1101
Brandon Jones44151a92014-09-10 11:32:25 -07001102 mUsesPointSize = vertexShaderD3D->usesPointSize();
1103
Jamie Madill437d2662014-12-05 14:23:35 -05001104 initAttributesByLayout();
1105
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001107}
1108
Brandon Jones44151a92014-09-10 11:32:25 -07001109void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1110{
1111 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1112}
1113
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001114void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001115{
1116 // Compute total default block size
1117 unsigned int vertexRegisters = 0;
1118 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001119 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001120 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001121 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001122
Geoff Lang2ec386b2014-12-03 14:44:38 -05001123 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001124 {
1125 if (uniform.isReferencedByVertexShader())
1126 {
1127 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1128 }
1129 if (uniform.isReferencedByFragmentShader())
1130 {
1131 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1132 }
1133 }
1134 }
1135
1136 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1137 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1138}
1139
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001140gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001141{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001142 updateSamplerMapping();
1143
1144 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1145 if (error.isError())
1146 {
1147 return error;
1148 }
1149
1150 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1151 {
1152 mUniforms[uniformIndex]->dirty = false;
1153 }
1154
1155 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001156}
1157
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001158gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001159{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001160 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1161
Brandon Jones18bd4102014-09-22 14:21:44 -07001162 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1163 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1164
1165 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1166 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1167
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001168 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001169 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001170 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001171 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1172
1173 ASSERT(uniformBlock && uniformBuffer);
1174
1175 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1176 {
1177 // undefined behaviour
1178 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1179 }
1180
1181 // Unnecessary to apply an unreferenced standard or shared UBO
1182 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1183 {
1184 continue;
1185 }
1186
1187 if (uniformBlock->isReferencedByVertexShader())
1188 {
1189 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1190 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1191 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1192 vertexUniformBuffers[registerIndex] = uniformBuffer;
1193 }
1194
1195 if (uniformBlock->isReferencedByFragmentShader())
1196 {
1197 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1198 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1199 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1200 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1201 }
1202 }
1203
1204 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1205}
1206
1207bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1208 unsigned int registerIndex, const gl::Caps &caps)
1209{
1210 if (shader == GL_VERTEX_SHADER)
1211 {
1212 uniformBlock->vsRegisterIndex = registerIndex;
1213 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1214 {
1215 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1216 return false;
1217 }
1218 }
1219 else if (shader == GL_FRAGMENT_SHADER)
1220 {
1221 uniformBlock->psRegisterIndex = registerIndex;
1222 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1223 {
1224 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1225 return false;
1226 }
1227 }
1228 else UNREACHABLE();
1229
1230 return true;
1231}
1232
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001233void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001234{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001235 unsigned int numUniforms = mUniforms.size();
1236 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001237 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001238 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001239 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001240}
1241
1242void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1243{
1244 setUniform(location, count, v, GL_FLOAT);
1245}
1246
1247void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1248{
1249 setUniform(location, count, v, GL_FLOAT_VEC2);
1250}
1251
1252void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1253{
1254 setUniform(location, count, v, GL_FLOAT_VEC3);
1255}
1256
1257void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1258{
1259 setUniform(location, count, v, GL_FLOAT_VEC4);
1260}
1261
1262void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1263{
1264 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1265}
1266
1267void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1268{
1269 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1270}
1271
1272void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1273{
1274 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1275}
1276
1277void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1278{
1279 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1280}
1281
1282void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1283{
1284 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1285}
1286
1287void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1288{
1289 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1290}
1291
1292void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1293{
1294 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1295}
1296
1297void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1298{
1299 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1300}
1301
1302void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1303{
1304 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1305}
1306
1307void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1308{
1309 setUniform(location, count, v, GL_INT);
1310}
1311
1312void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1313{
1314 setUniform(location, count, v, GL_INT_VEC2);
1315}
1316
1317void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1318{
1319 setUniform(location, count, v, GL_INT_VEC3);
1320}
1321
1322void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1323{
1324 setUniform(location, count, v, GL_INT_VEC4);
1325}
1326
1327void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1328{
1329 setUniform(location, count, v, GL_UNSIGNED_INT);
1330}
1331
1332void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1333{
1334 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1335}
1336
1337void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1338{
1339 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1340}
1341
1342void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1343{
1344 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1345}
1346
1347void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1348{
1349 getUniformv(location, params, GL_FLOAT);
1350}
1351
1352void ProgramD3D::getUniformiv(GLint location, GLint *params)
1353{
1354 getUniformv(location, params, GL_INT);
1355}
1356
1357void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1358{
1359 getUniformv(location, params, GL_UNSIGNED_INT);
1360}
1361
1362bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1363 const gl::Caps &caps)
1364{
Jamie Madill30d6c252014-11-13 10:03:33 -05001365 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1366 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001367
1368 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1369 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1370
1371 // Check that uniforms defined in the vertex and fragment shaders are identical
1372 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1373 UniformMap linkedUniforms;
1374
1375 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001376 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001377 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1378 linkedUniforms[vertexUniform.name] = &vertexUniform;
1379 }
1380
1381 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1382 {
1383 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1384 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1385 if (entry != linkedUniforms.end())
1386 {
1387 const sh::Uniform &vertexUniform = *entry->second;
1388 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001389 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001390 {
1391 return false;
1392 }
1393 }
1394 }
1395
1396 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1397 {
1398 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1399
1400 if (uniform.staticUse)
1401 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001402 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001403 }
1404 }
1405
1406 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1407 {
1408 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1409
1410 if (uniform.staticUse)
1411 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001412 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001413 }
1414 }
1415
1416 if (!indexUniforms(infoLog, caps))
1417 {
1418 return false;
1419 }
1420
1421 initializeUniformStorage();
1422
1423 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1424 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1425 {
1426 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1427
1428 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1429 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1430 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1431 }
1432
1433 return true;
1434}
1435
Geoff Lang492a7e42014-11-05 13:27:06 -05001436void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001437{
Geoff Lang492a7e42014-11-05 13:27:06 -05001438 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001439 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1440 encoder.skipRegisters(uniformRegister);
1441
1442 defineUniform(shader, uniform, uniform.name, &encoder);
1443}
1444
Geoff Lang492a7e42014-11-05 13:27:06 -05001445void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1446 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001447{
1448 if (uniform.isStruct())
1449 {
1450 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1451 {
1452 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1453
1454 encoder->enterAggregateType();
1455
1456 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1457 {
1458 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1459 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1460
1461 defineUniform(shader, field, fieldFullName, encoder);
1462 }
1463
1464 encoder->exitAggregateType();
1465 }
1466 }
1467 else // Not a struct
1468 {
1469 // Arrays are treated as aggregate types
1470 if (uniform.isArray())
1471 {
1472 encoder->enterAggregateType();
1473 }
1474
1475 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1476
Jamie Madill2857f482015-02-09 15:35:29 -05001477 // Advance the uniform offset, to track registers allocation for structs
1478 sh::BlockMemberInfo blockInfo = encoder->encodeType(uniform.type, uniform.arraySize, false);
1479
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001480 if (!linkedUniform)
1481 {
1482 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001483 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001484 ASSERT(linkedUniform);
Jamie Madill2857f482015-02-09 15:35:29 -05001485 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001486 mUniforms.push_back(linkedUniform);
1487 }
1488
Geoff Lang492a7e42014-11-05 13:27:06 -05001489 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001490 {
Jamie Madill2857f482015-02-09 15:35:29 -05001491 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001492 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001493 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001494 {
Jamie Madill2857f482015-02-09 15:35:29 -05001495 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001496 }
1497 else UNREACHABLE();
1498
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001499 // Arrays are treated as aggregate types
1500 if (uniform.isArray())
1501 {
1502 encoder->exitAggregateType();
1503 }
1504 }
1505}
1506
1507template <typename T>
1508static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1509{
1510 ASSERT(dest != NULL);
1511 ASSERT(dirtyFlag != NULL);
1512
1513 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1514 *dest = source;
1515}
1516
1517template <typename T>
1518void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1519{
1520 const int components = gl::VariableComponentCount(targetUniformType);
1521 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1522
1523 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1524
1525 int elementCount = targetUniform->elementCount();
1526
1527 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1528
1529 if (targetUniform->type == targetUniformType)
1530 {
1531 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1532
1533 for (int i = 0; i < count; i++)
1534 {
1535 T *dest = target + (i * 4);
1536 const T *source = v + (i * components);
1537
1538 for (int c = 0; c < components; c++)
1539 {
1540 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1541 }
1542 for (int c = components; c < 4; c++)
1543 {
1544 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1545 }
1546 }
1547 }
1548 else if (targetUniform->type == targetBoolType)
1549 {
1550 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1551
1552 for (int i = 0; i < count; i++)
1553 {
1554 GLint *dest = boolParams + (i * 4);
1555 const T *source = v + (i * components);
1556
1557 for (int c = 0; c < components; c++)
1558 {
1559 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1560 }
1561 for (int c = components; c < 4; c++)
1562 {
1563 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1564 }
1565 }
1566 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001567 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001568 {
1569 ASSERT(targetUniformType == GL_INT);
1570
1571 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1572
1573 bool wasDirty = targetUniform->dirty;
1574
1575 for (int i = 0; i < count; i++)
1576 {
1577 GLint *dest = target + (i * 4);
1578 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1579
1580 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1581 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1582 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1583 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1584 }
1585
1586 if (!wasDirty && targetUniform->dirty)
1587 {
1588 mDirtySamplerMapping = true;
1589 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001590 }
1591 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001592}
Brandon Jones18bd4102014-09-22 14:21:44 -07001593
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001594template<typename T>
1595bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1596{
1597 bool dirty = false;
1598 int copyWidth = std::min(targetHeight, srcWidth);
1599 int copyHeight = std::min(targetWidth, srcHeight);
1600
1601 for (int x = 0; x < copyWidth; x++)
1602 {
1603 for (int y = 0; y < copyHeight; y++)
1604 {
1605 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1606 }
1607 }
1608 // clear unfilled right side
1609 for (int y = 0; y < copyWidth; y++)
1610 {
1611 for (int x = copyHeight; x < targetWidth; x++)
1612 {
1613 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1614 }
1615 }
1616 // clear unfilled bottom.
1617 for (int y = copyWidth; y < targetHeight; y++)
1618 {
1619 for (int x = 0; x < targetWidth; x++)
1620 {
1621 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1622 }
1623 }
1624
1625 return dirty;
1626}
1627
1628template<typename T>
1629bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1630{
1631 bool dirty = false;
1632 int copyWidth = std::min(targetWidth, srcWidth);
1633 int copyHeight = std::min(targetHeight, srcHeight);
1634
1635 for (int y = 0; y < copyHeight; y++)
1636 {
1637 for (int x = 0; x < copyWidth; x++)
1638 {
1639 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1640 }
1641 }
1642 // clear unfilled right side
1643 for (int y = 0; y < copyHeight; y++)
1644 {
1645 for (int x = copyWidth; x < targetWidth; x++)
1646 {
1647 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1648 }
1649 }
1650 // clear unfilled bottom.
1651 for (int y = copyHeight; y < targetHeight; y++)
1652 {
1653 for (int x = 0; x < targetWidth; x++)
1654 {
1655 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1656 }
1657 }
1658
1659 return dirty;
1660}
1661
1662template <int cols, int rows>
1663void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1664{
1665 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1666
1667 int elementCount = targetUniform->elementCount();
1668
1669 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1670 const unsigned int targetMatrixStride = (4 * rows);
1671 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1672
1673 for (int i = 0; i < count; i++)
1674 {
1675 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1676 if (transpose == GL_FALSE)
1677 {
1678 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1679 }
1680 else
1681 {
1682 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1683 }
1684 target += targetMatrixStride;
1685 value += cols * rows;
1686 }
1687}
1688
1689template <typename T>
1690void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1691{
1692 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1693
1694 if (gl::IsMatrixType(targetUniform->type))
1695 {
1696 const int rows = gl::VariableRowCount(targetUniform->type);
1697 const int cols = gl::VariableColumnCount(targetUniform->type);
1698 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1699 }
1700 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1701 {
1702 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1703 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1704 size * sizeof(T));
1705 }
1706 else
1707 {
1708 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1709 switch (gl::VariableComponentType(targetUniform->type))
1710 {
1711 case GL_BOOL:
1712 {
1713 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1714
1715 for (unsigned int i = 0; i < size; i++)
1716 {
1717 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1718 }
1719 }
1720 break;
1721
1722 case GL_FLOAT:
1723 {
1724 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1725
1726 for (unsigned int i = 0; i < size; i++)
1727 {
1728 params[i] = static_cast<T>(floatParams[i]);
1729 }
1730 }
1731 break;
1732
1733 case GL_INT:
1734 {
1735 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1736
1737 for (unsigned int i = 0; i < size; i++)
1738 {
1739 params[i] = static_cast<T>(intParams[i]);
1740 }
1741 }
1742 break;
1743
1744 case GL_UNSIGNED_INT:
1745 {
1746 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1747
1748 for (unsigned int i = 0; i < size; i++)
1749 {
1750 params[i] = static_cast<T>(uintParams[i]);
1751 }
1752 }
1753 break;
1754
1755 default: UNREACHABLE();
1756 }
1757 }
1758}
1759
1760template <typename VarT>
1761void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1762 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1763 bool inRowMajorLayout)
1764{
1765 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1766 {
1767 const VarT &field = fields[uniformIndex];
1768 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1769
1770 if (field.isStruct())
1771 {
1772 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1773
1774 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1775 {
1776 encoder->enterAggregateType();
1777
1778 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1779 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1780
1781 encoder->exitAggregateType();
1782 }
1783 }
1784 else
1785 {
1786 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1787
1788 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1789
1790 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1791 blockIndex, memberInfo);
1792
1793 // add to uniform list, but not index, since uniform block uniforms have no location
1794 blockUniformIndexes->push_back(mUniforms.size());
1795 mUniforms.push_back(newUniform);
1796 }
1797 }
1798}
1799
1800bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1801 const gl::Caps &caps)
1802{
Jamie Madill30d6c252014-11-13 10:03:33 -05001803 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001804
1805 // create uniform block entries if they do not exist
1806 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1807 {
1808 std::vector<unsigned int> blockUniformIndexes;
1809 const unsigned int blockIndex = mUniformBlocks.size();
1810
1811 // define member uniforms
1812 sh::BlockLayoutEncoder *encoder = NULL;
1813
1814 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1815 {
1816 encoder = new sh::Std140BlockEncoder;
1817 }
1818 else
1819 {
1820 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1821 }
1822 ASSERT(encoder);
1823
1824 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1825
1826 size_t dataSize = encoder->getBlockSize();
1827
1828 // create all the uniform blocks
1829 if (interfaceBlock.arraySize > 0)
1830 {
1831 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1832 {
1833 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1834 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1835 mUniformBlocks.push_back(newUniformBlock);
1836 }
1837 }
1838 else
1839 {
1840 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1841 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1842 mUniformBlocks.push_back(newUniformBlock);
1843 }
1844 }
1845
1846 if (interfaceBlock.staticUse)
1847 {
1848 // Assign registers to the uniform blocks
1849 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1850 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1851 ASSERT(blockIndex != GL_INVALID_INDEX);
1852 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1853
1854 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1855
1856 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1857 {
1858 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1859 ASSERT(uniformBlock->name == interfaceBlock.name);
1860
1861 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1862 interfaceBlockRegister + uniformBlockElement, caps))
1863 {
1864 return false;
1865 }
1866 }
1867 }
1868
1869 return true;
1870}
1871
1872bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1873 GLenum samplerType,
1874 unsigned int samplerCount,
1875 std::vector<Sampler> &outSamplers,
1876 GLuint *outUsedRange)
1877{
1878 unsigned int samplerIndex = startSamplerIndex;
1879
1880 do
1881 {
1882 if (samplerIndex < outSamplers.size())
1883 {
1884 Sampler& sampler = outSamplers[samplerIndex];
1885 sampler.active = true;
1886 sampler.textureType = GetTextureType(samplerType);
1887 sampler.logicalTextureUnit = 0;
1888 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1889 }
1890 else
1891 {
1892 return false;
1893 }
1894
1895 samplerIndex++;
1896 } while (samplerIndex < startSamplerIndex + samplerCount);
1897
1898 return true;
1899}
1900
1901bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1902{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001903 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001904 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1905
1906 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1907 {
1908 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1909 &mUsedVertexSamplerRange))
1910 {
1911 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1912 mSamplersVS.size());
1913 return false;
1914 }
1915
1916 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1917 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1918 {
1919 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1920 caps.maxVertexUniformVectors);
1921 return false;
1922 }
1923 }
1924
1925 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1926 {
1927 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1928 &mUsedPixelSamplerRange))
1929 {
1930 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1931 mSamplersPS.size());
1932 return false;
1933 }
1934
1935 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1936 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1937 {
1938 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1939 caps.maxFragmentUniformVectors);
1940 return false;
1941 }
1942 }
1943
1944 return true;
1945}
1946
1947bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1948{
1949 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1950 {
1951 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1952
Geoff Lang2ec386b2014-12-03 14:44:38 -05001953 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001954 {
1955 if (!indexSamplerUniform(uniform, infoLog, caps))
1956 {
1957 return false;
1958 }
1959 }
1960
1961 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1962 {
1963 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1964 }
1965 }
1966
1967 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001968}
1969
Brandon Jonesc9610c52014-08-25 17:02:59 -07001970void ProgramD3D::reset()
1971{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001972 ProgramImpl::reset();
1973
Brandon Joneseb994362014-09-24 10:27:28 -07001974 SafeDeleteContainer(mVertexExecutables);
1975 SafeDeleteContainer(mPixelExecutables);
1976 SafeDelete(mGeometryExecutable);
1977
1978 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001979
Brandon Jones22502d52014-08-29 16:58:36 -07001980 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001981 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001982 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001983
1984 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001985 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001986 mUsesFragDepth = false;
1987 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001988 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001989
Brandon Jonesc9610c52014-08-25 17:02:59 -07001990 SafeDelete(mVertexUniformStorage);
1991 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001992
1993 mSamplersPS.clear();
1994 mSamplersVS.clear();
1995
1996 mUsedVertexSamplerRange = 0;
1997 mUsedPixelSamplerRange = 0;
1998 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001999
2000 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002001}
2002
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003unsigned int ProgramD3D::getSerial() const
2004{
2005 return mSerial;
2006}
2007
2008unsigned int ProgramD3D::issueSerial()
2009{
2010 return mCurrentSerial++;
2011}
2012
Jamie Madill437d2662014-12-05 14:23:35 -05002013void ProgramD3D::initAttributesByLayout()
2014{
2015 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2016 {
2017 mAttributesByLayout[i] = i;
2018 }
2019
2020 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2021}
2022
2023void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
2024 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
2025{
2026 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
2027
2028 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2029 {
2030 oldTranslatedAttributes[i] = attributes[i];
2031 }
2032
2033 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2034 {
2035 int oldIndex = mAttributesByLayout[i];
2036 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2037 attributes[i] = oldTranslatedAttributes[oldIndex];
2038 }
2039}
2040
Brandon Jonesc9610c52014-08-25 17:02:59 -07002041}