blob: a368d29e598332b6ed02ff7147cf2df3e646268f [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
11#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050012#include "libANGLE/Framebuffer.h"
13#include "libANGLE/FramebufferAttachment.h"
14#include "libANGLE/Program.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000015#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/renderer/d3d/DynamicHLSL.h"
17#include "libANGLE/renderer/d3d/RendererD3D.h"
18#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050019#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050020#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050021
Brandon Jonesc9610c52014-08-25 17:02:59 -070022namespace rx
23{
24
Brandon Joneseb994362014-09-24 10:27:28 -070025namespace
26{
27
Brandon Jones1a8a7e32014-10-01 12:49:30 -070028GLenum GetTextureType(GLenum samplerType)
29{
30 switch (samplerType)
31 {
32 case GL_SAMPLER_2D:
33 case GL_INT_SAMPLER_2D:
34 case GL_UNSIGNED_INT_SAMPLER_2D:
35 case GL_SAMPLER_2D_SHADOW:
36 return GL_TEXTURE_2D;
37 case GL_SAMPLER_3D:
38 case GL_INT_SAMPLER_3D:
39 case GL_UNSIGNED_INT_SAMPLER_3D:
40 return GL_TEXTURE_3D;
41 case GL_SAMPLER_CUBE:
42 case GL_SAMPLER_CUBE_SHADOW:
43 return GL_TEXTURE_CUBE_MAP;
44 case GL_INT_SAMPLER_CUBE:
45 case GL_UNSIGNED_INT_SAMPLER_CUBE:
46 return GL_TEXTURE_CUBE_MAP;
47 case GL_SAMPLER_2D_ARRAY:
48 case GL_INT_SAMPLER_2D_ARRAY:
49 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
50 case GL_SAMPLER_2D_ARRAY_SHADOW:
51 return GL_TEXTURE_2D_ARRAY;
52 default: UNREACHABLE();
53 }
54
55 return GL_TEXTURE_2D;
56}
57
Brandon Joneseb994362014-09-24 10:27:28 -070058void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS])
59{
60 size_t layoutIndex = 0;
61 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
62 {
63 ASSERT(layoutIndex < gl::MAX_VERTEX_ATTRIBS);
64
65 const sh::Attribute &shaderAttr = shaderAttributes[attributeIndex];
66
67 if (shaderAttr.type != GL_NONE)
68 {
69 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
70
71 for (size_t rowIndex = 0; static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); rowIndex++, layoutIndex++)
72 {
73 gl::VertexFormat *defaultFormat = &inputLayout[layoutIndex];
74
75 defaultFormat->mType = gl::VariableComponentType(transposedType);
76 defaultFormat->mNormalized = false;
77 defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool
78 defaultFormat->mComponents = gl::VariableColumnCount(transposedType);
79 }
80 }
81 }
82}
83
84std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
85{
Jamie Madillb4463142014-12-19 14:56:54 -050086 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070087
Jamie Madillb4463142014-12-19 14:56:54 -050088 if (!shaderOutputVars.empty())
89 {
90 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
91 }
Brandon Joneseb994362014-09-24 10:27:28 -070092
93 return defaultPixelOutput;
94}
95
Brandon Jones1a8a7e32014-10-01 12:49:30 -070096bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
97{
98 return var.isRowMajorLayout;
99}
100
101bool IsRowMajorLayout(const sh::ShaderVariable &var)
102{
103 return false;
104}
105
Jamie Madill437d2662014-12-05 14:23:35 -0500106struct AttributeSorter
107{
108 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
109 : originalIndices(semanticIndices)
110 {
111 }
112
113 bool operator()(int a, int b)
114 {
115 if (originalIndices[a] == -1) return false;
116 if (originalIndices[b] == -1) return true;
117 return (originalIndices[a] < originalIndices[b]);
118 }
119
120 const ProgramImpl::SemanticIndexArray &originalIndices;
121};
122
Brandon Joneseb994362014-09-24 10:27:28 -0700123}
124
125ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
126 const GLenum signature[],
Geoff Lang359ef262015-01-05 14:42:29 -0500127 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700128 : mShaderExecutable(shaderExecutable)
129{
130 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
131 {
132 mInputs[attributeIndex] = inputLayout[attributeIndex];
133 mSignature[attributeIndex] = signature[attributeIndex];
134 }
135}
136
137ProgramD3D::VertexExecutable::~VertexExecutable()
138{
139 SafeDelete(mShaderExecutable);
140}
141
142bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
143{
144 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
145 {
146 if (mSignature[attributeIndex] != signature[attributeIndex])
147 {
148 return false;
149 }
150 }
151
152 return true;
153}
154
Geoff Lang359ef262015-01-05 14:42:29 -0500155ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700156 : mOutputSignature(outputSignature),
157 mShaderExecutable(shaderExecutable)
158{
159}
160
161ProgramD3D::PixelExecutable::~PixelExecutable()
162{
163 SafeDelete(mShaderExecutable);
164}
165
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700166ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
167{
168}
169
Geoff Lang7dd2e102014-11-10 15:19:26 -0500170unsigned int ProgramD3D::mCurrentSerial = 1;
171
Jamie Madill93e13fb2014-11-06 15:27:25 -0500172ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700173 : ProgramImpl(),
174 mRenderer(renderer),
175 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700176 mGeometryExecutable(NULL),
177 mVertexWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
178 mPixelWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
Brandon Jones44151a92014-09-10 11:32:25 -0700179 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700180 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700181 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700182 mUsedVertexSamplerRange(0),
183 mUsedPixelSamplerRange(0),
184 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500185 mShaderVersion(100),
186 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700187{
Brandon Joneseb994362014-09-24 10:27:28 -0700188 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700189}
190
191ProgramD3D::~ProgramD3D()
192{
193 reset();
194 SafeDelete(mDynamicHLSL);
195}
196
Brandon Jones44151a92014-09-10 11:32:25 -0700197bool ProgramD3D::usesPointSpriteEmulation() const
198{
199 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
200}
201
202bool ProgramD3D::usesGeometryShader() const
203{
Cooper Partine6664f02015-01-09 16:22:24 -0800204 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
205}
206
207bool ProgramD3D::usesInstancedPointSpriteEmulation() const
208{
209 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700210}
211
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700212GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
213{
214 GLint logicalTextureUnit = -1;
215
216 switch (type)
217 {
218 case gl::SAMPLER_PIXEL:
219 ASSERT(samplerIndex < caps.maxTextureImageUnits);
220 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
221 {
222 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
223 }
224 break;
225 case gl::SAMPLER_VERTEX:
226 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
227 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
228 {
229 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
230 }
231 break;
232 default: UNREACHABLE();
233 }
234
235 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
236 {
237 return logicalTextureUnit;
238 }
239
240 return -1;
241}
242
243// Returns the texture type for a given Direct3D 9 sampler type and
244// index (0-15 for the pixel shader and 0-3 for the vertex shader).
245GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
246{
247 switch (type)
248 {
249 case gl::SAMPLER_PIXEL:
250 ASSERT(samplerIndex < mSamplersPS.size());
251 ASSERT(mSamplersPS[samplerIndex].active);
252 return mSamplersPS[samplerIndex].textureType;
253 case gl::SAMPLER_VERTEX:
254 ASSERT(samplerIndex < mSamplersVS.size());
255 ASSERT(mSamplersVS[samplerIndex].active);
256 return mSamplersVS[samplerIndex].textureType;
257 default: UNREACHABLE();
258 }
259
260 return GL_TEXTURE_2D;
261}
262
263GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
264{
265 switch (type)
266 {
267 case gl::SAMPLER_PIXEL:
268 return mUsedPixelSamplerRange;
269 case gl::SAMPLER_VERTEX:
270 return mUsedVertexSamplerRange;
271 default:
272 UNREACHABLE();
273 return 0;
274 }
275}
276
277void ProgramD3D::updateSamplerMapping()
278{
279 if (!mDirtySamplerMapping)
280 {
281 return;
282 }
283
284 mDirtySamplerMapping = false;
285
286 // Retrieve sampler uniform values
287 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
288 {
289 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
290
291 if (targetUniform->dirty)
292 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500293 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700294 {
295 int count = targetUniform->elementCount();
296 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
297
298 if (targetUniform->isReferencedByFragmentShader())
299 {
300 unsigned int firstIndex = targetUniform->psRegisterIndex;
301
302 for (int i = 0; i < count; i++)
303 {
304 unsigned int samplerIndex = firstIndex + i;
305
306 if (samplerIndex < mSamplersPS.size())
307 {
308 ASSERT(mSamplersPS[samplerIndex].active);
309 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
310 }
311 }
312 }
313
314 if (targetUniform->isReferencedByVertexShader())
315 {
316 unsigned int firstIndex = targetUniform->vsRegisterIndex;
317
318 for (int i = 0; i < count; i++)
319 {
320 unsigned int samplerIndex = firstIndex + i;
321
322 if (samplerIndex < mSamplersVS.size())
323 {
324 ASSERT(mSamplersVS[samplerIndex].active);
325 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
326 }
327 }
328 }
329 }
330 }
331 }
332}
333
334bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
335{
336 // if any two active samplers in a program are of different types, but refer to the same
337 // texture image unit, and this is the current program, then ValidateProgram will fail, and
338 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
339 updateSamplerMapping();
340
341 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
342
343 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
344 {
345 if (mSamplersPS[i].active)
346 {
347 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
348
349 if (unit >= textureUnitTypes.size())
350 {
351 if (infoLog)
352 {
353 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
354 }
355
356 return false;
357 }
358
359 if (textureUnitTypes[unit] != GL_NONE)
360 {
361 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
362 {
363 if (infoLog)
364 {
365 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
366 }
367
368 return false;
369 }
370 }
371 else
372 {
373 textureUnitTypes[unit] = mSamplersPS[i].textureType;
374 }
375 }
376 }
377
378 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
379 {
380 if (mSamplersVS[i].active)
381 {
382 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
383
384 if (unit >= textureUnitTypes.size())
385 {
386 if (infoLog)
387 {
388 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
389 }
390
391 return false;
392 }
393
394 if (textureUnitTypes[unit] != GL_NONE)
395 {
396 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
397 {
398 if (infoLog)
399 {
400 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
401 }
402
403 return false;
404 }
405 }
406 else
407 {
408 textureUnitTypes[unit] = mSamplersVS[i].textureType;
409 }
410 }
411 }
412
413 return true;
414}
415
Geoff Lang7dd2e102014-11-10 15:19:26 -0500416LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700417{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500418 int compileFlags = stream->readInt<int>();
419 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
420 {
421 infoLog.append("Mismatched compilation flags.");
422 return LinkResult(false, gl::Error(GL_NO_ERROR));
423 }
424
Brandon Jones44151a92014-09-10 11:32:25 -0700425 stream->readInt(&mShaderVersion);
426
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700427 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
428 for (unsigned int i = 0; i < psSamplerCount; ++i)
429 {
430 Sampler sampler;
431 stream->readBool(&sampler.active);
432 stream->readInt(&sampler.logicalTextureUnit);
433 stream->readInt(&sampler.textureType);
434 mSamplersPS.push_back(sampler);
435 }
436 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
437 for (unsigned int i = 0; i < vsSamplerCount; ++i)
438 {
439 Sampler sampler;
440 stream->readBool(&sampler.active);
441 stream->readInt(&sampler.logicalTextureUnit);
442 stream->readInt(&sampler.textureType);
443 mSamplersVS.push_back(sampler);
444 }
445
446 stream->readInt(&mUsedVertexSamplerRange);
447 stream->readInt(&mUsedPixelSamplerRange);
448
449 const unsigned int uniformCount = stream->readInt<unsigned int>();
450 if (stream->error())
451 {
452 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500453 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700454 }
455
456 mUniforms.resize(uniformCount);
457 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
458 {
459 GLenum type = stream->readInt<GLenum>();
460 GLenum precision = stream->readInt<GLenum>();
461 std::string name = stream->readString();
462 unsigned int arraySize = stream->readInt<unsigned int>();
463 int blockIndex = stream->readInt<int>();
464
465 int offset = stream->readInt<int>();
466 int arrayStride = stream->readInt<int>();
467 int matrixStride = stream->readInt<int>();
468 bool isRowMajorMatrix = stream->readBool();
469
470 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
471
472 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
473
474 stream->readInt(&uniform->psRegisterIndex);
475 stream->readInt(&uniform->vsRegisterIndex);
476 stream->readInt(&uniform->registerCount);
477 stream->readInt(&uniform->registerElement);
478
479 mUniforms[uniformIndex] = uniform;
480 }
481
482 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
483 if (stream->error())
484 {
485 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500486 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700487 }
488
489 mUniformIndex.resize(uniformIndexCount);
490 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
491 {
492 stream->readString(&mUniformIndex[uniformIndexIndex].name);
493 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
494 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
495 }
496
497 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
498 if (stream->error())
499 {
500 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500501 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700502 }
503
504 mUniformBlocks.resize(uniformBlockCount);
505 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
506 {
507 std::string name = stream->readString();
508 unsigned int elementIndex = stream->readInt<unsigned int>();
509 unsigned int dataSize = stream->readInt<unsigned int>();
510
511 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
512
513 stream->readInt(&uniformBlock->psRegisterIndex);
514 stream->readInt(&uniformBlock->vsRegisterIndex);
515
516 unsigned int numMembers = stream->readInt<unsigned int>();
517 uniformBlock->memberUniformIndexes.resize(numMembers);
518 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
519 {
520 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
521 }
522
523 mUniformBlocks[uniformBlockIndex] = uniformBlock;
524 }
525
Brandon Joneseb994362014-09-24 10:27:28 -0700526 stream->readInt(&mTransformFeedbackBufferMode);
527 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
528 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
529 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
530 {
531 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
532
533 stream->readString(&varying.name);
534 stream->readInt(&varying.type);
535 stream->readInt(&varying.size);
536 stream->readString(&varying.semanticName);
537 stream->readInt(&varying.semanticIndex);
538 stream->readInt(&varying.semanticIndexCount);
539 }
540
Brandon Jones22502d52014-08-29 16:58:36 -0700541 stream->readString(&mVertexHLSL);
542 stream->readInt(&mVertexWorkarounds);
543 stream->readString(&mPixelHLSL);
544 stream->readInt(&mPixelWorkarounds);
545 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700546 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700547
548 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
549 mPixelShaderKey.resize(pixelShaderKeySize);
550 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
551 {
552 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
553 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
554 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
555 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
556 }
557
Brandon Joneseb994362014-09-24 10:27:28 -0700558 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
559
560 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
561 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
562 {
563 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
564
565 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
566 {
567 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
568 stream->readInt(&vertexInput->mType);
569 stream->readInt(&vertexInput->mNormalized);
570 stream->readInt(&vertexInput->mComponents);
571 stream->readBool(&vertexInput->mPureInteger);
572 }
573
574 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
575 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400576
Geoff Lang359ef262015-01-05 14:42:29 -0500577 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400578 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
579 SHADER_VERTEX,
580 mTransformFeedbackLinkedVaryings,
581 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
582 &shaderExecutable);
583 if (error.isError())
584 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500585 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400586 }
587
Brandon Joneseb994362014-09-24 10:27:28 -0700588 if (!shaderExecutable)
589 {
590 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500591 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700592 }
593
594 // generated converted input layout
595 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
596 getInputLayoutSignature(inputLayout, signature);
597
598 // add new binary
599 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
600
601 stream->skip(vertexShaderSize);
602 }
603
604 const size_t pixelShaderCount = stream->readInt<unsigned int>();
605 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
606 {
607 const size_t outputCount = stream->readInt<unsigned int>();
608 std::vector<GLenum> outputs(outputCount);
609 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
610 {
611 stream->readInt(&outputs[outputIndex]);
612 }
613
614 const size_t pixelShaderSize = stream->readInt<unsigned int>();
615 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500616 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400617 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
618 mTransformFeedbackLinkedVaryings,
619 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
620 &shaderExecutable);
621 if (error.isError())
622 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500623 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400624 }
Brandon Joneseb994362014-09-24 10:27:28 -0700625
626 if (!shaderExecutable)
627 {
628 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500629 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700630 }
631
632 // add new binary
633 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
634
635 stream->skip(pixelShaderSize);
636 }
637
638 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
639
640 if (geometryShaderSize > 0)
641 {
642 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400643 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
644 mTransformFeedbackLinkedVaryings,
645 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
646 &mGeometryExecutable);
647 if (error.isError())
648 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500649 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400650 }
Brandon Joneseb994362014-09-24 10:27:28 -0700651
652 if (!mGeometryExecutable)
653 {
654 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500655 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700656 }
657 stream->skip(geometryShaderSize);
658 }
659
Brandon Jones18bd4102014-09-22 14:21:44 -0700660 GUID binaryIdentifier = {0};
661 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
662
663 GUID identifier = mRenderer->getAdapterIdentifier();
664 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
665 {
666 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500667 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700668 }
669
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700670 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500671 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700672
Geoff Lang7dd2e102014-11-10 15:19:26 -0500673 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700674}
675
Geoff Langb543aff2014-09-30 14:52:54 -0400676gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700677{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500678 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
679
Brandon Jones44151a92014-09-10 11:32:25 -0700680 stream->writeInt(mShaderVersion);
681
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700682 stream->writeInt(mSamplersPS.size());
683 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
684 {
685 stream->writeInt(mSamplersPS[i].active);
686 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
687 stream->writeInt(mSamplersPS[i].textureType);
688 }
689
690 stream->writeInt(mSamplersVS.size());
691 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
692 {
693 stream->writeInt(mSamplersVS[i].active);
694 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
695 stream->writeInt(mSamplersVS[i].textureType);
696 }
697
698 stream->writeInt(mUsedVertexSamplerRange);
699 stream->writeInt(mUsedPixelSamplerRange);
700
701 stream->writeInt(mUniforms.size());
702 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
703 {
704 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
705
706 stream->writeInt(uniform.type);
707 stream->writeInt(uniform.precision);
708 stream->writeString(uniform.name);
709 stream->writeInt(uniform.arraySize);
710 stream->writeInt(uniform.blockIndex);
711
712 stream->writeInt(uniform.blockInfo.offset);
713 stream->writeInt(uniform.blockInfo.arrayStride);
714 stream->writeInt(uniform.blockInfo.matrixStride);
715 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
716
717 stream->writeInt(uniform.psRegisterIndex);
718 stream->writeInt(uniform.vsRegisterIndex);
719 stream->writeInt(uniform.registerCount);
720 stream->writeInt(uniform.registerElement);
721 }
722
723 stream->writeInt(mUniformIndex.size());
724 for (size_t i = 0; i < mUniformIndex.size(); ++i)
725 {
726 stream->writeString(mUniformIndex[i].name);
727 stream->writeInt(mUniformIndex[i].element);
728 stream->writeInt(mUniformIndex[i].index);
729 }
730
731 stream->writeInt(mUniformBlocks.size());
732 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
733 {
734 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
735
736 stream->writeString(uniformBlock.name);
737 stream->writeInt(uniformBlock.elementIndex);
738 stream->writeInt(uniformBlock.dataSize);
739
740 stream->writeInt(uniformBlock.memberUniformIndexes.size());
741 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
742 {
743 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
744 }
745
746 stream->writeInt(uniformBlock.psRegisterIndex);
747 stream->writeInt(uniformBlock.vsRegisterIndex);
748 }
749
Brandon Joneseb994362014-09-24 10:27:28 -0700750 stream->writeInt(mTransformFeedbackBufferMode);
751 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
752 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
753 {
754 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
755
756 stream->writeString(varying.name);
757 stream->writeInt(varying.type);
758 stream->writeInt(varying.size);
759 stream->writeString(varying.semanticName);
760 stream->writeInt(varying.semanticIndex);
761 stream->writeInt(varying.semanticIndexCount);
762 }
763
Brandon Jones22502d52014-08-29 16:58:36 -0700764 stream->writeString(mVertexHLSL);
765 stream->writeInt(mVertexWorkarounds);
766 stream->writeString(mPixelHLSL);
767 stream->writeInt(mPixelWorkarounds);
768 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700769 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700770
Brandon Joneseb994362014-09-24 10:27:28 -0700771 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700772 stream->writeInt(pixelShaderKey.size());
773 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
774 {
Brandon Joneseb994362014-09-24 10:27:28 -0700775 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700776 stream->writeInt(variable.type);
777 stream->writeString(variable.name);
778 stream->writeString(variable.source);
779 stream->writeInt(variable.outputIndex);
780 }
781
Brandon Joneseb994362014-09-24 10:27:28 -0700782 stream->writeInt(mVertexExecutables.size());
783 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
784 {
785 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
786
787 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
788 {
789 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
790 stream->writeInt(vertexInput.mType);
791 stream->writeInt(vertexInput.mNormalized);
792 stream->writeInt(vertexInput.mComponents);
793 stream->writeInt(vertexInput.mPureInteger);
794 }
795
796 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
797 stream->writeInt(vertexShaderSize);
798
799 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
800 stream->writeBytes(vertexBlob, vertexShaderSize);
801 }
802
803 stream->writeInt(mPixelExecutables.size());
804 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
805 {
806 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
807
808 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
809 stream->writeInt(outputs.size());
810 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
811 {
812 stream->writeInt(outputs[outputIndex]);
813 }
814
815 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
816 stream->writeInt(pixelShaderSize);
817
818 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
819 stream->writeBytes(pixelBlob, pixelShaderSize);
820 }
821
822 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
823 stream->writeInt(geometryShaderSize);
824
825 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
826 {
827 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
828 stream->writeBytes(geometryBlob, geometryShaderSize);
829 }
830
Brandon Jones18bd4102014-09-22 14:21:44 -0700831 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400832 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700833
Geoff Langb543aff2014-09-30 14:52:54 -0400834 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700835}
836
Geoff Lang359ef262015-01-05 14:42:29 -0500837gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700838{
Brandon Joneseb994362014-09-24 10:27:28 -0700839 std::vector<GLenum> outputs;
840
Jamie Madill48faf802014-11-06 15:27:22 -0500841 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700842
843 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
844 {
845 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
846
847 if (colorbuffer)
848 {
849 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
850 }
851 else
852 {
853 outputs.push_back(GL_NONE);
854 }
855 }
856
Jamie Madill97399232014-12-23 12:31:15 -0500857 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700858}
859
Jamie Madill97399232014-12-23 12:31:15 -0500860gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500861 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500862 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700863{
864 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
865 {
866 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
867 {
Geoff Langb543aff2014-09-30 14:52:54 -0400868 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
869 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700870 }
871 }
872
Brandon Jones22502d52014-08-29 16:58:36 -0700873 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
874 outputSignature);
875
876 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500877 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500878
879 gl::InfoLog tempInfoLog;
880 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
881
882 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400883 mTransformFeedbackLinkedVaryings,
884 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
885 mPixelWorkarounds, &pixelExecutable);
886 if (error.isError())
887 {
888 return error;
889 }
Brandon Joneseb994362014-09-24 10:27:28 -0700890
Jamie Madill97399232014-12-23 12:31:15 -0500891 if (pixelExecutable)
892 {
893 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
894 }
895 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700896 {
897 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
898 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
899 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
900 }
Brandon Jones22502d52014-08-29 16:58:36 -0700901
Geoff Langb543aff2014-09-30 14:52:54 -0400902 *outExectuable = pixelExecutable;
903 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700904}
905
Jamie Madill97399232014-12-23 12:31:15 -0500906gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500907 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500908 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700909{
Brandon Joneseb994362014-09-24 10:27:28 -0700910 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
911 getInputLayoutSignature(inputLayout, signature);
912
913 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
914 {
915 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
916 {
Geoff Langb543aff2014-09-30 14:52:54 -0400917 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
918 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700919 }
920 }
921
Brandon Jones22502d52014-08-29 16:58:36 -0700922 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700923 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700924
925 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500926 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500927
928 gl::InfoLog tempInfoLog;
929 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
930
931 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400932 mTransformFeedbackLinkedVaryings,
933 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
934 mVertexWorkarounds, &vertexExecutable);
935 if (error.isError())
936 {
937 return error;
938 }
939
Jamie Madill97399232014-12-23 12:31:15 -0500940 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700941 {
942 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
943 }
Jamie Madill97399232014-12-23 12:31:15 -0500944 else if (!infoLog)
945 {
946 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
947 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
948 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
949 }
Brandon Jones22502d52014-08-29 16:58:36 -0700950
Geoff Langb543aff2014-09-30 14:52:54 -0400951 *outExectuable = vertexExecutable;
952 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700953}
954
Geoff Lang7dd2e102014-11-10 15:19:26 -0500955LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
956 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700957{
Brandon Jones18bd4102014-09-22 14:21:44 -0700958 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
959 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700960
Jamie Madill6df9b372015-02-18 21:28:19 +0000961 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
962 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
963 ShaderExecutableD3D *defaultVertexExecutable = NULL;
964 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
965 if (error.isError())
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800966 {
Jamie Madill6df9b372015-02-18 21:28:19 +0000967 return LinkResult(false, error);
968 }
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800969
Brandon Joneseb994362014-09-24 10:27:28 -0700970 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500971 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill6df9b372015-02-18 21:28:19 +0000972 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400973 if (error.isError())
974 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500975 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400976 }
Brandon Jones44151a92014-09-10 11:32:25 -0700977
Brandon Joneseb994362014-09-24 10:27:28 -0700978 if (usesGeometryShader())
979 {
980 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -0700981
Geoff Langb543aff2014-09-30 14:52:54 -0400982
983 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
984 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
985 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
986 if (error.isError())
987 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500988 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400989 }
Brandon Joneseb994362014-09-24 10:27:28 -0700990 }
991
Brandon Jones091540d2014-10-29 11:32:04 -0700992#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +0200993 if (usesGeometryShader() && mGeometryExecutable)
994 {
995 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
996 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
997 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
998 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
999 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1000 }
1001
1002 if (defaultVertexExecutable)
1003 {
1004 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1005 }
1006
1007 if (defaultPixelExecutable)
1008 {
1009 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1010 }
1011#endif
1012
Geoff Langb543aff2014-09-30 14:52:54 -04001013 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001014 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001015}
1016
Geoff Lang7dd2e102014-11-10 15:19:26 -05001017LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1018 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1019 const std::vector<std::string> &transformFeedbackVaryings,
1020 GLenum transformFeedbackBufferMode,
1021 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1022 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001023{
Brandon Joneseb994362014-09-24 10:27:28 -07001024 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1025 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1026
Jamie Madillde8892b2014-11-11 13:00:22 -05001027 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1028 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001029
Brandon Joneseb994362014-09-24 10:27:28 -07001030 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001031
1032 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
1033 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1034
1035 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1036 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001037 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001038
1039 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001040 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001041 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1042
Geoff Langbdee2d52014-09-17 11:02:51 -04001043 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001044 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001045 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001046 }
1047
Geoff Lang7dd2e102014-11-10 15:19:26 -05001048 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001049 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001050 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001051 }
1052
Jamie Madillde8892b2014-11-11 13:00:22 -05001053 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001054 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1055 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1056 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001057 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001058 }
1059
Brandon Jones44151a92014-09-10 11:32:25 -07001060 mUsesPointSize = vertexShaderD3D->usesPointSize();
1061
Jamie Madill437d2662014-12-05 14:23:35 -05001062 initAttributesByLayout();
1063
Geoff Lang7dd2e102014-11-10 15:19:26 -05001064 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001065}
1066
Brandon Jones44151a92014-09-10 11:32:25 -07001067void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1068{
1069 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1070}
1071
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001072void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001073{
1074 // Compute total default block size
1075 unsigned int vertexRegisters = 0;
1076 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001077 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001078 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001079 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001080
Geoff Lang2ec386b2014-12-03 14:44:38 -05001081 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001082 {
1083 if (uniform.isReferencedByVertexShader())
1084 {
1085 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1086 }
1087 if (uniform.isReferencedByFragmentShader())
1088 {
1089 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1090 }
1091 }
1092 }
1093
1094 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1095 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1096}
1097
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001098gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001099{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001100 updateSamplerMapping();
1101
1102 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1103 if (error.isError())
1104 {
1105 return error;
1106 }
1107
1108 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1109 {
1110 mUniforms[uniformIndex]->dirty = false;
1111 }
1112
1113 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001114}
1115
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001116gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001117{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001118 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1119
Brandon Jones18bd4102014-09-22 14:21:44 -07001120 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1121 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1122
1123 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1124 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1125
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001126 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001127 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001128 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001129 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1130
1131 ASSERT(uniformBlock && uniformBuffer);
1132
1133 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1134 {
1135 // undefined behaviour
1136 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1137 }
1138
1139 // Unnecessary to apply an unreferenced standard or shared UBO
1140 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1141 {
1142 continue;
1143 }
1144
1145 if (uniformBlock->isReferencedByVertexShader())
1146 {
1147 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1148 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1149 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1150 vertexUniformBuffers[registerIndex] = uniformBuffer;
1151 }
1152
1153 if (uniformBlock->isReferencedByFragmentShader())
1154 {
1155 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1156 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1157 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1158 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1159 }
1160 }
1161
1162 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1163}
1164
1165bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1166 unsigned int registerIndex, const gl::Caps &caps)
1167{
1168 if (shader == GL_VERTEX_SHADER)
1169 {
1170 uniformBlock->vsRegisterIndex = registerIndex;
1171 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1172 {
1173 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1174 return false;
1175 }
1176 }
1177 else if (shader == GL_FRAGMENT_SHADER)
1178 {
1179 uniformBlock->psRegisterIndex = registerIndex;
1180 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1181 {
1182 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1183 return false;
1184 }
1185 }
1186 else UNREACHABLE();
1187
1188 return true;
1189}
1190
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001191void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001192{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001193 unsigned int numUniforms = mUniforms.size();
1194 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001195 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001196 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001197 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001198}
1199
1200void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1201{
1202 setUniform(location, count, v, GL_FLOAT);
1203}
1204
1205void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1206{
1207 setUniform(location, count, v, GL_FLOAT_VEC2);
1208}
1209
1210void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1211{
1212 setUniform(location, count, v, GL_FLOAT_VEC3);
1213}
1214
1215void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1216{
1217 setUniform(location, count, v, GL_FLOAT_VEC4);
1218}
1219
1220void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1221{
1222 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1223}
1224
1225void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1226{
1227 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1228}
1229
1230void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1231{
1232 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1233}
1234
1235void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1236{
1237 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1238}
1239
1240void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1241{
1242 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1243}
1244
1245void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1246{
1247 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1248}
1249
1250void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1251{
1252 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1253}
1254
1255void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1256{
1257 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1258}
1259
1260void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1261{
1262 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1263}
1264
1265void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1266{
1267 setUniform(location, count, v, GL_INT);
1268}
1269
1270void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1271{
1272 setUniform(location, count, v, GL_INT_VEC2);
1273}
1274
1275void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1276{
1277 setUniform(location, count, v, GL_INT_VEC3);
1278}
1279
1280void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1281{
1282 setUniform(location, count, v, GL_INT_VEC4);
1283}
1284
1285void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1286{
1287 setUniform(location, count, v, GL_UNSIGNED_INT);
1288}
1289
1290void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1291{
1292 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1293}
1294
1295void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1296{
1297 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1298}
1299
1300void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1301{
1302 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1303}
1304
1305void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1306{
1307 getUniformv(location, params, GL_FLOAT);
1308}
1309
1310void ProgramD3D::getUniformiv(GLint location, GLint *params)
1311{
1312 getUniformv(location, params, GL_INT);
1313}
1314
1315void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1316{
1317 getUniformv(location, params, GL_UNSIGNED_INT);
1318}
1319
1320bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1321 const gl::Caps &caps)
1322{
Jamie Madill30d6c252014-11-13 10:03:33 -05001323 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1324 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001325
1326 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1327 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1328
1329 // Check that uniforms defined in the vertex and fragment shaders are identical
1330 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1331 UniformMap linkedUniforms;
1332
1333 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001334 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001335 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1336 linkedUniforms[vertexUniform.name] = &vertexUniform;
1337 }
1338
1339 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1340 {
1341 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1342 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1343 if (entry != linkedUniforms.end())
1344 {
1345 const sh::Uniform &vertexUniform = *entry->second;
1346 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001348 {
1349 return false;
1350 }
1351 }
1352 }
1353
1354 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1355 {
1356 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1357
1358 if (uniform.staticUse)
1359 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001360 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001361 }
1362 }
1363
1364 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1365 {
1366 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1367
1368 if (uniform.staticUse)
1369 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001370 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001371 }
1372 }
1373
1374 if (!indexUniforms(infoLog, caps))
1375 {
1376 return false;
1377 }
1378
1379 initializeUniformStorage();
1380
1381 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1382 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1383 {
1384 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1385
1386 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1387 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1388 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1389 }
1390
1391 return true;
1392}
1393
Geoff Lang492a7e42014-11-05 13:27:06 -05001394void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001395{
Geoff Lang492a7e42014-11-05 13:27:06 -05001396 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001397 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1398 encoder.skipRegisters(uniformRegister);
1399
1400 defineUniform(shader, uniform, uniform.name, &encoder);
1401}
1402
Geoff Lang492a7e42014-11-05 13:27:06 -05001403void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1404 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001405{
1406 if (uniform.isStruct())
1407 {
1408 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1409 {
1410 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1411
1412 encoder->enterAggregateType();
1413
1414 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1415 {
1416 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1417 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1418
1419 defineUniform(shader, field, fieldFullName, encoder);
1420 }
1421
1422 encoder->exitAggregateType();
1423 }
1424 }
1425 else // Not a struct
1426 {
1427 // Arrays are treated as aggregate types
1428 if (uniform.isArray())
1429 {
1430 encoder->enterAggregateType();
1431 }
1432
1433 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1434
Jamie Madill2857f482015-02-09 15:35:29 -05001435 // Advance the uniform offset, to track registers allocation for structs
1436 sh::BlockMemberInfo blockInfo = encoder->encodeType(uniform.type, uniform.arraySize, false);
1437
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001438 if (!linkedUniform)
1439 {
1440 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001441 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001442 ASSERT(linkedUniform);
Jamie Madill2857f482015-02-09 15:35:29 -05001443 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001444 mUniforms.push_back(linkedUniform);
1445 }
1446
Geoff Lang492a7e42014-11-05 13:27:06 -05001447 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001448 {
Jamie Madill2857f482015-02-09 15:35:29 -05001449 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001450 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001451 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001452 {
Jamie Madill2857f482015-02-09 15:35:29 -05001453 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001454 }
1455 else UNREACHABLE();
1456
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001457 // Arrays are treated as aggregate types
1458 if (uniform.isArray())
1459 {
1460 encoder->exitAggregateType();
1461 }
1462 }
1463}
1464
1465template <typename T>
1466static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1467{
1468 ASSERT(dest != NULL);
1469 ASSERT(dirtyFlag != NULL);
1470
1471 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1472 *dest = source;
1473}
1474
1475template <typename T>
1476void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1477{
1478 const int components = gl::VariableComponentCount(targetUniformType);
1479 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1480
1481 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1482
1483 int elementCount = targetUniform->elementCount();
1484
1485 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1486
1487 if (targetUniform->type == targetUniformType)
1488 {
1489 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1490
1491 for (int i = 0; i < count; i++)
1492 {
1493 T *dest = target + (i * 4);
1494 const T *source = v + (i * components);
1495
1496 for (int c = 0; c < components; c++)
1497 {
1498 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1499 }
1500 for (int c = components; c < 4; c++)
1501 {
1502 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1503 }
1504 }
1505 }
1506 else if (targetUniform->type == targetBoolType)
1507 {
1508 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1509
1510 for (int i = 0; i < count; i++)
1511 {
1512 GLint *dest = boolParams + (i * 4);
1513 const T *source = v + (i * components);
1514
1515 for (int c = 0; c < components; c++)
1516 {
1517 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1518 }
1519 for (int c = components; c < 4; c++)
1520 {
1521 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1522 }
1523 }
1524 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001525 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001526 {
1527 ASSERT(targetUniformType == GL_INT);
1528
1529 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1530
1531 bool wasDirty = targetUniform->dirty;
1532
1533 for (int i = 0; i < count; i++)
1534 {
1535 GLint *dest = target + (i * 4);
1536 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1537
1538 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1539 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1540 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1541 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1542 }
1543
1544 if (!wasDirty && targetUniform->dirty)
1545 {
1546 mDirtySamplerMapping = true;
1547 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001548 }
1549 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001550}
Brandon Jones18bd4102014-09-22 14:21:44 -07001551
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001552template<typename T>
1553bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1554{
1555 bool dirty = false;
1556 int copyWidth = std::min(targetHeight, srcWidth);
1557 int copyHeight = std::min(targetWidth, srcHeight);
1558
1559 for (int x = 0; x < copyWidth; x++)
1560 {
1561 for (int y = 0; y < copyHeight; y++)
1562 {
1563 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1564 }
1565 }
1566 // clear unfilled right side
1567 for (int y = 0; y < copyWidth; y++)
1568 {
1569 for (int x = copyHeight; x < targetWidth; x++)
1570 {
1571 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1572 }
1573 }
1574 // clear unfilled bottom.
1575 for (int y = copyWidth; y < targetHeight; y++)
1576 {
1577 for (int x = 0; x < targetWidth; x++)
1578 {
1579 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1580 }
1581 }
1582
1583 return dirty;
1584}
1585
1586template<typename T>
1587bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1588{
1589 bool dirty = false;
1590 int copyWidth = std::min(targetWidth, srcWidth);
1591 int copyHeight = std::min(targetHeight, srcHeight);
1592
1593 for (int y = 0; y < copyHeight; y++)
1594 {
1595 for (int x = 0; x < copyWidth; x++)
1596 {
1597 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1598 }
1599 }
1600 // clear unfilled right side
1601 for (int y = 0; y < copyHeight; y++)
1602 {
1603 for (int x = copyWidth; x < targetWidth; x++)
1604 {
1605 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1606 }
1607 }
1608 // clear unfilled bottom.
1609 for (int y = copyHeight; y < targetHeight; y++)
1610 {
1611 for (int x = 0; x < targetWidth; x++)
1612 {
1613 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1614 }
1615 }
1616
1617 return dirty;
1618}
1619
1620template <int cols, int rows>
1621void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1622{
1623 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1624
1625 int elementCount = targetUniform->elementCount();
1626
1627 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1628 const unsigned int targetMatrixStride = (4 * rows);
1629 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1630
1631 for (int i = 0; i < count; i++)
1632 {
1633 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1634 if (transpose == GL_FALSE)
1635 {
1636 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1637 }
1638 else
1639 {
1640 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1641 }
1642 target += targetMatrixStride;
1643 value += cols * rows;
1644 }
1645}
1646
1647template <typename T>
1648void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1649{
1650 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1651
1652 if (gl::IsMatrixType(targetUniform->type))
1653 {
1654 const int rows = gl::VariableRowCount(targetUniform->type);
1655 const int cols = gl::VariableColumnCount(targetUniform->type);
1656 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1657 }
1658 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1659 {
1660 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1661 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1662 size * sizeof(T));
1663 }
1664 else
1665 {
1666 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1667 switch (gl::VariableComponentType(targetUniform->type))
1668 {
1669 case GL_BOOL:
1670 {
1671 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1672
1673 for (unsigned int i = 0; i < size; i++)
1674 {
1675 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1676 }
1677 }
1678 break;
1679
1680 case GL_FLOAT:
1681 {
1682 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1683
1684 for (unsigned int i = 0; i < size; i++)
1685 {
1686 params[i] = static_cast<T>(floatParams[i]);
1687 }
1688 }
1689 break;
1690
1691 case GL_INT:
1692 {
1693 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1694
1695 for (unsigned int i = 0; i < size; i++)
1696 {
1697 params[i] = static_cast<T>(intParams[i]);
1698 }
1699 }
1700 break;
1701
1702 case GL_UNSIGNED_INT:
1703 {
1704 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1705
1706 for (unsigned int i = 0; i < size; i++)
1707 {
1708 params[i] = static_cast<T>(uintParams[i]);
1709 }
1710 }
1711 break;
1712
1713 default: UNREACHABLE();
1714 }
1715 }
1716}
1717
1718template <typename VarT>
1719void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1720 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1721 bool inRowMajorLayout)
1722{
1723 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1724 {
1725 const VarT &field = fields[uniformIndex];
1726 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1727
1728 if (field.isStruct())
1729 {
1730 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1731
1732 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1733 {
1734 encoder->enterAggregateType();
1735
1736 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1737 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1738
1739 encoder->exitAggregateType();
1740 }
1741 }
1742 else
1743 {
1744 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1745
1746 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1747
1748 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1749 blockIndex, memberInfo);
1750
1751 // add to uniform list, but not index, since uniform block uniforms have no location
1752 blockUniformIndexes->push_back(mUniforms.size());
1753 mUniforms.push_back(newUniform);
1754 }
1755 }
1756}
1757
1758bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1759 const gl::Caps &caps)
1760{
Jamie Madill30d6c252014-11-13 10:03:33 -05001761 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001762
1763 // create uniform block entries if they do not exist
1764 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1765 {
1766 std::vector<unsigned int> blockUniformIndexes;
1767 const unsigned int blockIndex = mUniformBlocks.size();
1768
1769 // define member uniforms
1770 sh::BlockLayoutEncoder *encoder = NULL;
1771
1772 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1773 {
1774 encoder = new sh::Std140BlockEncoder;
1775 }
1776 else
1777 {
1778 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1779 }
1780 ASSERT(encoder);
1781
1782 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1783
1784 size_t dataSize = encoder->getBlockSize();
1785
1786 // create all the uniform blocks
1787 if (interfaceBlock.arraySize > 0)
1788 {
1789 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1790 {
1791 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1792 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1793 mUniformBlocks.push_back(newUniformBlock);
1794 }
1795 }
1796 else
1797 {
1798 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1799 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1800 mUniformBlocks.push_back(newUniformBlock);
1801 }
1802 }
1803
1804 if (interfaceBlock.staticUse)
1805 {
1806 // Assign registers to the uniform blocks
1807 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1808 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1809 ASSERT(blockIndex != GL_INVALID_INDEX);
1810 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1811
1812 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1813
1814 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1815 {
1816 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1817 ASSERT(uniformBlock->name == interfaceBlock.name);
1818
1819 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1820 interfaceBlockRegister + uniformBlockElement, caps))
1821 {
1822 return false;
1823 }
1824 }
1825 }
1826
1827 return true;
1828}
1829
1830bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1831 GLenum samplerType,
1832 unsigned int samplerCount,
1833 std::vector<Sampler> &outSamplers,
1834 GLuint *outUsedRange)
1835{
1836 unsigned int samplerIndex = startSamplerIndex;
1837
1838 do
1839 {
1840 if (samplerIndex < outSamplers.size())
1841 {
1842 Sampler& sampler = outSamplers[samplerIndex];
1843 sampler.active = true;
1844 sampler.textureType = GetTextureType(samplerType);
1845 sampler.logicalTextureUnit = 0;
1846 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1847 }
1848 else
1849 {
1850 return false;
1851 }
1852
1853 samplerIndex++;
1854 } while (samplerIndex < startSamplerIndex + samplerCount);
1855
1856 return true;
1857}
1858
1859bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1860{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001861 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001862 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1863
1864 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1865 {
1866 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1867 &mUsedVertexSamplerRange))
1868 {
1869 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1870 mSamplersVS.size());
1871 return false;
1872 }
1873
1874 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1875 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1876 {
1877 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1878 caps.maxVertexUniformVectors);
1879 return false;
1880 }
1881 }
1882
1883 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1884 {
1885 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1886 &mUsedPixelSamplerRange))
1887 {
1888 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1889 mSamplersPS.size());
1890 return false;
1891 }
1892
1893 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1894 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1895 {
1896 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1897 caps.maxFragmentUniformVectors);
1898 return false;
1899 }
1900 }
1901
1902 return true;
1903}
1904
1905bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1906{
1907 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1908 {
1909 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1910
Geoff Lang2ec386b2014-12-03 14:44:38 -05001911 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001912 {
1913 if (!indexSamplerUniform(uniform, infoLog, caps))
1914 {
1915 return false;
1916 }
1917 }
1918
1919 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1920 {
1921 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1922 }
1923 }
1924
1925 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001926}
1927
Brandon Jonesc9610c52014-08-25 17:02:59 -07001928void ProgramD3D::reset()
1929{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001930 ProgramImpl::reset();
1931
Brandon Joneseb994362014-09-24 10:27:28 -07001932 SafeDeleteContainer(mVertexExecutables);
1933 SafeDeleteContainer(mPixelExecutables);
1934 SafeDelete(mGeometryExecutable);
1935
1936 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001937
Brandon Jones22502d52014-08-29 16:58:36 -07001938 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001939 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001940 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001941
1942 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001943 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001944 mUsesFragDepth = false;
1945 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001946 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001947
Brandon Jonesc9610c52014-08-25 17:02:59 -07001948 SafeDelete(mVertexUniformStorage);
1949 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001950
1951 mSamplersPS.clear();
1952 mSamplersVS.clear();
1953
1954 mUsedVertexSamplerRange = 0;
1955 mUsedPixelSamplerRange = 0;
1956 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001957
1958 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001959}
1960
Geoff Lang7dd2e102014-11-10 15:19:26 -05001961unsigned int ProgramD3D::getSerial() const
1962{
1963 return mSerial;
1964}
1965
1966unsigned int ProgramD3D::issueSerial()
1967{
1968 return mCurrentSerial++;
1969}
1970
Jamie Madill437d2662014-12-05 14:23:35 -05001971void ProgramD3D::initAttributesByLayout()
1972{
1973 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1974 {
1975 mAttributesByLayout[i] = i;
1976 }
1977
1978 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
1979}
1980
1981void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
1982 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
1983{
1984 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
1985
1986 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1987 {
1988 oldTranslatedAttributes[i] = attributes[i];
1989 }
1990
1991 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1992 {
1993 int oldIndex = mAttributesByLayout[i];
1994 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
1995 attributes[i] = oldTranslatedAttributes[oldIndex];
1996 }
1997}
1998
Brandon Jonesc9610c52014-08-25 17:02:59 -07001999}