blob: 3cd4e14953bd074b9138342130e8fc67c0f390e6 [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
11#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050012#include "libANGLE/Framebuffer.h"
13#include "libANGLE/FramebufferAttachment.h"
14#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050015#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/renderer/d3d/DynamicHLSL.h"
17#include "libANGLE/renderer/d3d/RendererD3D.h"
18#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050019#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050020#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050021
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
197ProgramD3D *ProgramD3D::makeProgramD3D(ProgramImpl *impl)
198{
199 ASSERT(HAS_DYNAMIC_TYPE(ProgramD3D*, impl));
200 return static_cast<ProgramD3D*>(impl);
201}
202
203const ProgramD3D *ProgramD3D::makeProgramD3D(const ProgramImpl *impl)
204{
205 ASSERT(HAS_DYNAMIC_TYPE(const ProgramD3D*, impl));
206 return static_cast<const ProgramD3D*>(impl);
207}
208
Brandon Jones44151a92014-09-10 11:32:25 -0700209bool ProgramD3D::usesPointSpriteEmulation() const
210{
211 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
212}
213
214bool ProgramD3D::usesGeometryShader() const
215{
Cooper Partine6664f02015-01-09 16:22:24 -0800216 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
217}
218
219bool ProgramD3D::usesInstancedPointSpriteEmulation() const
220{
221 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700222}
223
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700224GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
225{
226 GLint logicalTextureUnit = -1;
227
228 switch (type)
229 {
230 case gl::SAMPLER_PIXEL:
231 ASSERT(samplerIndex < caps.maxTextureImageUnits);
232 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
233 {
234 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
235 }
236 break;
237 case gl::SAMPLER_VERTEX:
238 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
239 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
240 {
241 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
242 }
243 break;
244 default: UNREACHABLE();
245 }
246
247 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
248 {
249 return logicalTextureUnit;
250 }
251
252 return -1;
253}
254
255// Returns the texture type for a given Direct3D 9 sampler type and
256// index (0-15 for the pixel shader and 0-3 for the vertex shader).
257GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
258{
259 switch (type)
260 {
261 case gl::SAMPLER_PIXEL:
262 ASSERT(samplerIndex < mSamplersPS.size());
263 ASSERT(mSamplersPS[samplerIndex].active);
264 return mSamplersPS[samplerIndex].textureType;
265 case gl::SAMPLER_VERTEX:
266 ASSERT(samplerIndex < mSamplersVS.size());
267 ASSERT(mSamplersVS[samplerIndex].active);
268 return mSamplersVS[samplerIndex].textureType;
269 default: UNREACHABLE();
270 }
271
272 return GL_TEXTURE_2D;
273}
274
275GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
276{
277 switch (type)
278 {
279 case gl::SAMPLER_PIXEL:
280 return mUsedPixelSamplerRange;
281 case gl::SAMPLER_VERTEX:
282 return mUsedVertexSamplerRange;
283 default:
284 UNREACHABLE();
285 return 0;
286 }
287}
288
289void ProgramD3D::updateSamplerMapping()
290{
291 if (!mDirtySamplerMapping)
292 {
293 return;
294 }
295
296 mDirtySamplerMapping = false;
297
298 // Retrieve sampler uniform values
299 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
300 {
301 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
302
303 if (targetUniform->dirty)
304 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500305 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700306 {
307 int count = targetUniform->elementCount();
308 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
309
310 if (targetUniform->isReferencedByFragmentShader())
311 {
312 unsigned int firstIndex = targetUniform->psRegisterIndex;
313
314 for (int i = 0; i < count; i++)
315 {
316 unsigned int samplerIndex = firstIndex + i;
317
318 if (samplerIndex < mSamplersPS.size())
319 {
320 ASSERT(mSamplersPS[samplerIndex].active);
321 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
322 }
323 }
324 }
325
326 if (targetUniform->isReferencedByVertexShader())
327 {
328 unsigned int firstIndex = targetUniform->vsRegisterIndex;
329
330 for (int i = 0; i < count; i++)
331 {
332 unsigned int samplerIndex = firstIndex + i;
333
334 if (samplerIndex < mSamplersVS.size())
335 {
336 ASSERT(mSamplersVS[samplerIndex].active);
337 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
338 }
339 }
340 }
341 }
342 }
343 }
344}
345
346bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
347{
348 // if any two active samplers in a program are of different types, but refer to the same
349 // texture image unit, and this is the current program, then ValidateProgram will fail, and
350 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
351 updateSamplerMapping();
352
353 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
354
355 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
356 {
357 if (mSamplersPS[i].active)
358 {
359 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
360
361 if (unit >= textureUnitTypes.size())
362 {
363 if (infoLog)
364 {
365 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
366 }
367
368 return false;
369 }
370
371 if (textureUnitTypes[unit] != GL_NONE)
372 {
373 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
374 {
375 if (infoLog)
376 {
377 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
378 }
379
380 return false;
381 }
382 }
383 else
384 {
385 textureUnitTypes[unit] = mSamplersPS[i].textureType;
386 }
387 }
388 }
389
390 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
391 {
392 if (mSamplersVS[i].active)
393 {
394 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
395
396 if (unit >= textureUnitTypes.size())
397 {
398 if (infoLog)
399 {
400 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
401 }
402
403 return false;
404 }
405
406 if (textureUnitTypes[unit] != GL_NONE)
407 {
408 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
409 {
410 if (infoLog)
411 {
412 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
413 }
414
415 return false;
416 }
417 }
418 else
419 {
420 textureUnitTypes[unit] = mSamplersVS[i].textureType;
421 }
422 }
423 }
424
425 return true;
426}
427
Geoff Lang7dd2e102014-11-10 15:19:26 -0500428LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700429{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500430 int compileFlags = stream->readInt<int>();
431 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
432 {
433 infoLog.append("Mismatched compilation flags.");
434 return LinkResult(false, gl::Error(GL_NO_ERROR));
435 }
436
Brandon Jones44151a92014-09-10 11:32:25 -0700437 stream->readInt(&mShaderVersion);
438
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700439 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
440 for (unsigned int i = 0; i < psSamplerCount; ++i)
441 {
442 Sampler sampler;
443 stream->readBool(&sampler.active);
444 stream->readInt(&sampler.logicalTextureUnit);
445 stream->readInt(&sampler.textureType);
446 mSamplersPS.push_back(sampler);
447 }
448 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
449 for (unsigned int i = 0; i < vsSamplerCount; ++i)
450 {
451 Sampler sampler;
452 stream->readBool(&sampler.active);
453 stream->readInt(&sampler.logicalTextureUnit);
454 stream->readInt(&sampler.textureType);
455 mSamplersVS.push_back(sampler);
456 }
457
458 stream->readInt(&mUsedVertexSamplerRange);
459 stream->readInt(&mUsedPixelSamplerRange);
460
461 const unsigned int uniformCount = stream->readInt<unsigned int>();
462 if (stream->error())
463 {
464 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500465 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700466 }
467
468 mUniforms.resize(uniformCount);
469 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
470 {
471 GLenum type = stream->readInt<GLenum>();
472 GLenum precision = stream->readInt<GLenum>();
473 std::string name = stream->readString();
474 unsigned int arraySize = stream->readInt<unsigned int>();
475 int blockIndex = stream->readInt<int>();
476
477 int offset = stream->readInt<int>();
478 int arrayStride = stream->readInt<int>();
479 int matrixStride = stream->readInt<int>();
480 bool isRowMajorMatrix = stream->readBool();
481
482 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
483
484 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
485
486 stream->readInt(&uniform->psRegisterIndex);
487 stream->readInt(&uniform->vsRegisterIndex);
488 stream->readInt(&uniform->registerCount);
489 stream->readInt(&uniform->registerElement);
490
491 mUniforms[uniformIndex] = uniform;
492 }
493
494 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
495 if (stream->error())
496 {
497 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500498 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700499 }
500
501 mUniformIndex.resize(uniformIndexCount);
502 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
503 {
504 stream->readString(&mUniformIndex[uniformIndexIndex].name);
505 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
506 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
507 }
508
509 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
510 if (stream->error())
511 {
512 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500513 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700514 }
515
516 mUniformBlocks.resize(uniformBlockCount);
517 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
518 {
519 std::string name = stream->readString();
520 unsigned int elementIndex = stream->readInt<unsigned int>();
521 unsigned int dataSize = stream->readInt<unsigned int>();
522
523 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
524
525 stream->readInt(&uniformBlock->psRegisterIndex);
526 stream->readInt(&uniformBlock->vsRegisterIndex);
527
528 unsigned int numMembers = stream->readInt<unsigned int>();
529 uniformBlock->memberUniformIndexes.resize(numMembers);
530 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
531 {
532 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
533 }
534
535 mUniformBlocks[uniformBlockIndex] = uniformBlock;
536 }
537
Brandon Joneseb994362014-09-24 10:27:28 -0700538 stream->readInt(&mTransformFeedbackBufferMode);
539 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
540 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
541 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
542 {
543 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
544
545 stream->readString(&varying.name);
546 stream->readInt(&varying.type);
547 stream->readInt(&varying.size);
548 stream->readString(&varying.semanticName);
549 stream->readInt(&varying.semanticIndex);
550 stream->readInt(&varying.semanticIndexCount);
551 }
552
Brandon Jones22502d52014-08-29 16:58:36 -0700553 stream->readString(&mVertexHLSL);
554 stream->readInt(&mVertexWorkarounds);
555 stream->readString(&mPixelHLSL);
556 stream->readInt(&mPixelWorkarounds);
557 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700558 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700559
560 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
561 mPixelShaderKey.resize(pixelShaderKeySize);
562 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
563 {
564 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
565 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
566 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
567 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
568 }
569
Brandon Joneseb994362014-09-24 10:27:28 -0700570 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
571
572 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
573 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
574 {
575 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
576
577 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
578 {
579 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
580 stream->readInt(&vertexInput->mType);
581 stream->readInt(&vertexInput->mNormalized);
582 stream->readInt(&vertexInput->mComponents);
583 stream->readBool(&vertexInput->mPureInteger);
584 }
585
586 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
587 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400588
Geoff Lang359ef262015-01-05 14:42:29 -0500589 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400590 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
591 SHADER_VERTEX,
592 mTransformFeedbackLinkedVaryings,
593 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
594 &shaderExecutable);
595 if (error.isError())
596 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500597 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400598 }
599
Brandon Joneseb994362014-09-24 10:27:28 -0700600 if (!shaderExecutable)
601 {
602 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500603 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700604 }
605
606 // generated converted input layout
607 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
608 getInputLayoutSignature(inputLayout, signature);
609
610 // add new binary
611 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
612
613 stream->skip(vertexShaderSize);
614 }
615
616 const size_t pixelShaderCount = stream->readInt<unsigned int>();
617 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
618 {
619 const size_t outputCount = stream->readInt<unsigned int>();
620 std::vector<GLenum> outputs(outputCount);
621 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
622 {
623 stream->readInt(&outputs[outputIndex]);
624 }
625
626 const size_t pixelShaderSize = stream->readInt<unsigned int>();
627 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500628 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400629 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
630 mTransformFeedbackLinkedVaryings,
631 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
632 &shaderExecutable);
633 if (error.isError())
634 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500635 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400636 }
Brandon Joneseb994362014-09-24 10:27:28 -0700637
638 if (!shaderExecutable)
639 {
640 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500641 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700642 }
643
644 // add new binary
645 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
646
647 stream->skip(pixelShaderSize);
648 }
649
650 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
651
652 if (geometryShaderSize > 0)
653 {
654 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400655 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
656 mTransformFeedbackLinkedVaryings,
657 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
658 &mGeometryExecutable);
659 if (error.isError())
660 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500661 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400662 }
Brandon Joneseb994362014-09-24 10:27:28 -0700663
664 if (!mGeometryExecutable)
665 {
666 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500667 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700668 }
669 stream->skip(geometryShaderSize);
670 }
671
Brandon Jones18bd4102014-09-22 14:21:44 -0700672 GUID binaryIdentifier = {0};
673 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
674
675 GUID identifier = mRenderer->getAdapterIdentifier();
676 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
677 {
678 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500679 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700680 }
681
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700682 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500683 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700684
Geoff Lang7dd2e102014-11-10 15:19:26 -0500685 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700686}
687
Geoff Langb543aff2014-09-30 14:52:54 -0400688gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700689{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500690 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
691
Brandon Jones44151a92014-09-10 11:32:25 -0700692 stream->writeInt(mShaderVersion);
693
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700694 stream->writeInt(mSamplersPS.size());
695 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
696 {
697 stream->writeInt(mSamplersPS[i].active);
698 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
699 stream->writeInt(mSamplersPS[i].textureType);
700 }
701
702 stream->writeInt(mSamplersVS.size());
703 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
704 {
705 stream->writeInt(mSamplersVS[i].active);
706 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
707 stream->writeInt(mSamplersVS[i].textureType);
708 }
709
710 stream->writeInt(mUsedVertexSamplerRange);
711 stream->writeInt(mUsedPixelSamplerRange);
712
713 stream->writeInt(mUniforms.size());
714 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
715 {
716 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
717
718 stream->writeInt(uniform.type);
719 stream->writeInt(uniform.precision);
720 stream->writeString(uniform.name);
721 stream->writeInt(uniform.arraySize);
722 stream->writeInt(uniform.blockIndex);
723
724 stream->writeInt(uniform.blockInfo.offset);
725 stream->writeInt(uniform.blockInfo.arrayStride);
726 stream->writeInt(uniform.blockInfo.matrixStride);
727 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
728
729 stream->writeInt(uniform.psRegisterIndex);
730 stream->writeInt(uniform.vsRegisterIndex);
731 stream->writeInt(uniform.registerCount);
732 stream->writeInt(uniform.registerElement);
733 }
734
735 stream->writeInt(mUniformIndex.size());
736 for (size_t i = 0; i < mUniformIndex.size(); ++i)
737 {
738 stream->writeString(mUniformIndex[i].name);
739 stream->writeInt(mUniformIndex[i].element);
740 stream->writeInt(mUniformIndex[i].index);
741 }
742
743 stream->writeInt(mUniformBlocks.size());
744 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
745 {
746 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
747
748 stream->writeString(uniformBlock.name);
749 stream->writeInt(uniformBlock.elementIndex);
750 stream->writeInt(uniformBlock.dataSize);
751
752 stream->writeInt(uniformBlock.memberUniformIndexes.size());
753 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
754 {
755 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
756 }
757
758 stream->writeInt(uniformBlock.psRegisterIndex);
759 stream->writeInt(uniformBlock.vsRegisterIndex);
760 }
761
Brandon Joneseb994362014-09-24 10:27:28 -0700762 stream->writeInt(mTransformFeedbackBufferMode);
763 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
764 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
765 {
766 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
767
768 stream->writeString(varying.name);
769 stream->writeInt(varying.type);
770 stream->writeInt(varying.size);
771 stream->writeString(varying.semanticName);
772 stream->writeInt(varying.semanticIndex);
773 stream->writeInt(varying.semanticIndexCount);
774 }
775
Brandon Jones22502d52014-08-29 16:58:36 -0700776 stream->writeString(mVertexHLSL);
777 stream->writeInt(mVertexWorkarounds);
778 stream->writeString(mPixelHLSL);
779 stream->writeInt(mPixelWorkarounds);
780 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700781 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700782
Brandon Joneseb994362014-09-24 10:27:28 -0700783 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700784 stream->writeInt(pixelShaderKey.size());
785 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
786 {
Brandon Joneseb994362014-09-24 10:27:28 -0700787 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700788 stream->writeInt(variable.type);
789 stream->writeString(variable.name);
790 stream->writeString(variable.source);
791 stream->writeInt(variable.outputIndex);
792 }
793
Brandon Joneseb994362014-09-24 10:27:28 -0700794 stream->writeInt(mVertexExecutables.size());
795 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
796 {
797 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
798
799 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
800 {
801 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
802 stream->writeInt(vertexInput.mType);
803 stream->writeInt(vertexInput.mNormalized);
804 stream->writeInt(vertexInput.mComponents);
805 stream->writeInt(vertexInput.mPureInteger);
806 }
807
808 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
809 stream->writeInt(vertexShaderSize);
810
811 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
812 stream->writeBytes(vertexBlob, vertexShaderSize);
813 }
814
815 stream->writeInt(mPixelExecutables.size());
816 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
817 {
818 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
819
820 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
821 stream->writeInt(outputs.size());
822 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
823 {
824 stream->writeInt(outputs[outputIndex]);
825 }
826
827 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
828 stream->writeInt(pixelShaderSize);
829
830 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
831 stream->writeBytes(pixelBlob, pixelShaderSize);
832 }
833
834 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
835 stream->writeInt(geometryShaderSize);
836
837 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
838 {
839 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
840 stream->writeBytes(geometryBlob, geometryShaderSize);
841 }
842
Brandon Jones18bd4102014-09-22 14:21:44 -0700843 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400844 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700845
Geoff Langb543aff2014-09-30 14:52:54 -0400846 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700847}
848
Geoff Lang359ef262015-01-05 14:42:29 -0500849gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700850{
Brandon Joneseb994362014-09-24 10:27:28 -0700851 std::vector<GLenum> outputs;
852
Jamie Madill48faf802014-11-06 15:27:22 -0500853 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700854
855 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
856 {
857 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
858
859 if (colorbuffer)
860 {
861 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
862 }
863 else
864 {
865 outputs.push_back(GL_NONE);
866 }
867 }
868
Jamie Madill97399232014-12-23 12:31:15 -0500869 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700870}
871
Jamie Madill97399232014-12-23 12:31:15 -0500872gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500873 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500874 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700875{
876 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
877 {
878 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
879 {
Geoff Langb543aff2014-09-30 14:52:54 -0400880 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
881 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700882 }
883 }
884
Brandon Jones22502d52014-08-29 16:58:36 -0700885 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
886 outputSignature);
887
888 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500889 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500890
891 gl::InfoLog tempInfoLog;
892 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
893
894 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400895 mTransformFeedbackLinkedVaryings,
896 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
897 mPixelWorkarounds, &pixelExecutable);
898 if (error.isError())
899 {
900 return error;
901 }
Brandon Joneseb994362014-09-24 10:27:28 -0700902
Jamie Madill97399232014-12-23 12:31:15 -0500903 if (pixelExecutable)
904 {
905 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
906 }
907 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700908 {
909 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
910 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
911 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
912 }
Brandon Jones22502d52014-08-29 16:58:36 -0700913
Geoff Langb543aff2014-09-30 14:52:54 -0400914 *outExectuable = pixelExecutable;
915 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700916}
917
Jamie Madill97399232014-12-23 12:31:15 -0500918gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500919 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500920 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700921{
Brandon Joneseb994362014-09-24 10:27:28 -0700922 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
923 getInputLayoutSignature(inputLayout, signature);
924
925 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
926 {
927 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
928 {
Geoff Langb543aff2014-09-30 14:52:54 -0400929 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
930 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700931 }
932 }
933
Brandon Jones22502d52014-08-29 16:58:36 -0700934 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700935 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700936
937 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500938 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500939
940 gl::InfoLog tempInfoLog;
941 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
942
943 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400944 mTransformFeedbackLinkedVaryings,
945 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
946 mVertexWorkarounds, &vertexExecutable);
947 if (error.isError())
948 {
949 return error;
950 }
951
Jamie Madill97399232014-12-23 12:31:15 -0500952 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700953 {
954 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
955 }
Jamie Madill97399232014-12-23 12:31:15 -0500956 else if (!infoLog)
957 {
958 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
959 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
960 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
961 }
Brandon Jones22502d52014-08-29 16:58:36 -0700962
Geoff Langb543aff2014-09-30 14:52:54 -0400963 *outExectuable = vertexExecutable;
964 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700965}
966
Geoff Lang7dd2e102014-11-10 15:19:26 -0500967LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
968 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700969{
Brandon Jones18bd4102014-09-22 14:21:44 -0700970 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
971 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700972
Brandon Joneseb994362014-09-24 10:27:28 -0700973 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
974 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
Geoff Lang359ef262015-01-05 14:42:29 -0500975 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500976 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400977 if (error.isError())
978 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500979 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400980 }
Brandon Jones44151a92014-09-10 11:32:25 -0700981
Brandon Joneseb994362014-09-24 10:27:28 -0700982 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500983 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500984 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400985 if (error.isError())
986 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500987 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400988 }
Brandon Jones44151a92014-09-10 11:32:25 -0700989
Brandon Joneseb994362014-09-24 10:27:28 -0700990 if (usesGeometryShader())
991 {
992 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -0700993
Geoff Langb543aff2014-09-30 14:52:54 -0400994
995 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
996 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
997 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
998 if (error.isError())
999 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001000 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001001 }
Brandon Joneseb994362014-09-24 10:27:28 -07001002 }
1003
Brandon Jones091540d2014-10-29 11:32:04 -07001004#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001005 if (usesGeometryShader() && mGeometryExecutable)
1006 {
1007 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1008 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1009 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1010 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1011 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1012 }
1013
1014 if (defaultVertexExecutable)
1015 {
1016 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1017 }
1018
1019 if (defaultPixelExecutable)
1020 {
1021 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1022 }
1023#endif
1024
Geoff Langb543aff2014-09-30 14:52:54 -04001025 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001026 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001027}
1028
Geoff Lang7dd2e102014-11-10 15:19:26 -05001029LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1030 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1031 const std::vector<std::string> &transformFeedbackVaryings,
1032 GLenum transformFeedbackBufferMode,
1033 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1034 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001035{
Brandon Joneseb994362014-09-24 10:27:28 -07001036 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1037 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1038
Jamie Madillde8892b2014-11-11 13:00:22 -05001039 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1040 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001041
Brandon Joneseb994362014-09-24 10:27:28 -07001042 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001043
1044 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
1045 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1046
1047 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1048 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001049 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001050
1051 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001052 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001053 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1054
Geoff Langbdee2d52014-09-17 11:02:51 -04001055 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001056 {
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
Geoff Lang7dd2e102014-11-10 15:19:26 -05001060 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001061 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001062 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001063 }
1064
Jamie Madillde8892b2014-11-11 13:00:22 -05001065 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001066 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1067 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1068 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001069 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001070 }
1071
Brandon Jones44151a92014-09-10 11:32:25 -07001072 mUsesPointSize = vertexShaderD3D->usesPointSize();
1073
Jamie Madill437d2662014-12-05 14:23:35 -05001074 initAttributesByLayout();
1075
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001077}
1078
Brandon Jones44151a92014-09-10 11:32:25 -07001079void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1080{
1081 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1082}
1083
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001084void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001085{
1086 // Compute total default block size
1087 unsigned int vertexRegisters = 0;
1088 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001089 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001090 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001091 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001092
Geoff Lang2ec386b2014-12-03 14:44:38 -05001093 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001094 {
1095 if (uniform.isReferencedByVertexShader())
1096 {
1097 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1098 }
1099 if (uniform.isReferencedByFragmentShader())
1100 {
1101 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1102 }
1103 }
1104 }
1105
1106 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1107 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1108}
1109
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001110gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001111{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001112 updateSamplerMapping();
1113
1114 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1115 if (error.isError())
1116 {
1117 return error;
1118 }
1119
1120 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1121 {
1122 mUniforms[uniformIndex]->dirty = false;
1123 }
1124
1125 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001126}
1127
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001128gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001129{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001130 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1131
Brandon Jones18bd4102014-09-22 14:21:44 -07001132 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1133 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1134
1135 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1136 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1137
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001138 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001139 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001140 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001141 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1142
1143 ASSERT(uniformBlock && uniformBuffer);
1144
1145 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1146 {
1147 // undefined behaviour
1148 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1149 }
1150
1151 // Unnecessary to apply an unreferenced standard or shared UBO
1152 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1153 {
1154 continue;
1155 }
1156
1157 if (uniformBlock->isReferencedByVertexShader())
1158 {
1159 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1160 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1161 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1162 vertexUniformBuffers[registerIndex] = uniformBuffer;
1163 }
1164
1165 if (uniformBlock->isReferencedByFragmentShader())
1166 {
1167 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1168 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1169 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1170 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1171 }
1172 }
1173
1174 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1175}
1176
1177bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1178 unsigned int registerIndex, const gl::Caps &caps)
1179{
1180 if (shader == GL_VERTEX_SHADER)
1181 {
1182 uniformBlock->vsRegisterIndex = registerIndex;
1183 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1184 {
1185 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1186 return false;
1187 }
1188 }
1189 else if (shader == GL_FRAGMENT_SHADER)
1190 {
1191 uniformBlock->psRegisterIndex = registerIndex;
1192 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1193 {
1194 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1195 return false;
1196 }
1197 }
1198 else UNREACHABLE();
1199
1200 return true;
1201}
1202
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001203void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001204{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001205 unsigned int numUniforms = mUniforms.size();
1206 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001207 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001208 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001209 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001210}
1211
1212void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1213{
1214 setUniform(location, count, v, GL_FLOAT);
1215}
1216
1217void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1218{
1219 setUniform(location, count, v, GL_FLOAT_VEC2);
1220}
1221
1222void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1223{
1224 setUniform(location, count, v, GL_FLOAT_VEC3);
1225}
1226
1227void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1228{
1229 setUniform(location, count, v, GL_FLOAT_VEC4);
1230}
1231
1232void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1233{
1234 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1235}
1236
1237void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1238{
1239 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1240}
1241
1242void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1243{
1244 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1245}
1246
1247void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1248{
1249 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1250}
1251
1252void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1253{
1254 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1255}
1256
1257void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1258{
1259 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1260}
1261
1262void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1263{
1264 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1265}
1266
1267void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1268{
1269 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1270}
1271
1272void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1273{
1274 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1275}
1276
1277void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1278{
1279 setUniform(location, count, v, GL_INT);
1280}
1281
1282void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1283{
1284 setUniform(location, count, v, GL_INT_VEC2);
1285}
1286
1287void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1288{
1289 setUniform(location, count, v, GL_INT_VEC3);
1290}
1291
1292void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1293{
1294 setUniform(location, count, v, GL_INT_VEC4);
1295}
1296
1297void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1298{
1299 setUniform(location, count, v, GL_UNSIGNED_INT);
1300}
1301
1302void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1303{
1304 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1305}
1306
1307void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1308{
1309 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1310}
1311
1312void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1313{
1314 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1315}
1316
1317void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1318{
1319 getUniformv(location, params, GL_FLOAT);
1320}
1321
1322void ProgramD3D::getUniformiv(GLint location, GLint *params)
1323{
1324 getUniformv(location, params, GL_INT);
1325}
1326
1327void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1328{
1329 getUniformv(location, params, GL_UNSIGNED_INT);
1330}
1331
1332bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1333 const gl::Caps &caps)
1334{
Jamie Madill30d6c252014-11-13 10:03:33 -05001335 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1336 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001337
1338 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1339 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1340
1341 // Check that uniforms defined in the vertex and fragment shaders are identical
1342 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1343 UniformMap linkedUniforms;
1344
1345 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001346 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001347 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1348 linkedUniforms[vertexUniform.name] = &vertexUniform;
1349 }
1350
1351 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1352 {
1353 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1354 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1355 if (entry != linkedUniforms.end())
1356 {
1357 const sh::Uniform &vertexUniform = *entry->second;
1358 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001360 {
1361 return false;
1362 }
1363 }
1364 }
1365
1366 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1367 {
1368 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1369
1370 if (uniform.staticUse)
1371 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001372 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001373 }
1374 }
1375
1376 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1377 {
1378 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1379
1380 if (uniform.staticUse)
1381 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001382 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001383 }
1384 }
1385
1386 if (!indexUniforms(infoLog, caps))
1387 {
1388 return false;
1389 }
1390
1391 initializeUniformStorage();
1392
1393 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1394 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1395 {
1396 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1397
1398 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1399 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1400 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1401 }
1402
1403 return true;
1404}
1405
Geoff Lang492a7e42014-11-05 13:27:06 -05001406void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001407{
Geoff Lang492a7e42014-11-05 13:27:06 -05001408 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001409 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1410 encoder.skipRegisters(uniformRegister);
1411
1412 defineUniform(shader, uniform, uniform.name, &encoder);
1413}
1414
Geoff Lang492a7e42014-11-05 13:27:06 -05001415void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1416 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001417{
1418 if (uniform.isStruct())
1419 {
1420 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1421 {
1422 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1423
1424 encoder->enterAggregateType();
1425
1426 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1427 {
1428 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1429 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1430
1431 defineUniform(shader, field, fieldFullName, encoder);
1432 }
1433
1434 encoder->exitAggregateType();
1435 }
1436 }
1437 else // Not a struct
1438 {
1439 // Arrays are treated as aggregate types
1440 if (uniform.isArray())
1441 {
1442 encoder->enterAggregateType();
1443 }
1444
1445 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1446
1447 if (!linkedUniform)
1448 {
1449 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
1450 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
1451 ASSERT(linkedUniform);
1452 linkedUniform->registerElement = encoder->getCurrentElement();
1453 mUniforms.push_back(linkedUniform);
1454 }
1455
1456 ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
1457
Geoff Lang492a7e42014-11-05 13:27:06 -05001458 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001459 {
1460 linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
1461 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001462 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001463 {
1464 linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
1465 }
1466 else UNREACHABLE();
1467
1468 // Advance the uniform offset, to track registers allocation for structs
1469 encoder->encodeType(uniform.type, uniform.arraySize, false);
1470
1471 // Arrays are treated as aggregate types
1472 if (uniform.isArray())
1473 {
1474 encoder->exitAggregateType();
1475 }
1476 }
1477}
1478
1479template <typename T>
1480static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1481{
1482 ASSERT(dest != NULL);
1483 ASSERT(dirtyFlag != NULL);
1484
1485 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1486 *dest = source;
1487}
1488
1489template <typename T>
1490void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1491{
1492 const int components = gl::VariableComponentCount(targetUniformType);
1493 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1494
1495 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1496
1497 int elementCount = targetUniform->elementCount();
1498
1499 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1500
1501 if (targetUniform->type == targetUniformType)
1502 {
1503 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1504
1505 for (int i = 0; i < count; i++)
1506 {
1507 T *dest = target + (i * 4);
1508 const T *source = v + (i * components);
1509
1510 for (int c = 0; c < components; c++)
1511 {
1512 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1513 }
1514 for (int c = components; c < 4; c++)
1515 {
1516 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1517 }
1518 }
1519 }
1520 else if (targetUniform->type == targetBoolType)
1521 {
1522 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1523
1524 for (int i = 0; i < count; i++)
1525 {
1526 GLint *dest = boolParams + (i * 4);
1527 const T *source = v + (i * components);
1528
1529 for (int c = 0; c < components; c++)
1530 {
1531 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1532 }
1533 for (int c = components; c < 4; c++)
1534 {
1535 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1536 }
1537 }
1538 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001539 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001540 {
1541 ASSERT(targetUniformType == GL_INT);
1542
1543 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1544
1545 bool wasDirty = targetUniform->dirty;
1546
1547 for (int i = 0; i < count; i++)
1548 {
1549 GLint *dest = target + (i * 4);
1550 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1551
1552 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1553 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1554 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1555 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1556 }
1557
1558 if (!wasDirty && targetUniform->dirty)
1559 {
1560 mDirtySamplerMapping = true;
1561 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001562 }
1563 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001564}
Brandon Jones18bd4102014-09-22 14:21:44 -07001565
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001566template<typename T>
1567bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1568{
1569 bool dirty = false;
1570 int copyWidth = std::min(targetHeight, srcWidth);
1571 int copyHeight = std::min(targetWidth, srcHeight);
1572
1573 for (int x = 0; x < copyWidth; x++)
1574 {
1575 for (int y = 0; y < copyHeight; y++)
1576 {
1577 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1578 }
1579 }
1580 // clear unfilled right side
1581 for (int y = 0; y < copyWidth; y++)
1582 {
1583 for (int x = copyHeight; x < targetWidth; x++)
1584 {
1585 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1586 }
1587 }
1588 // clear unfilled bottom.
1589 for (int y = copyWidth; y < targetHeight; y++)
1590 {
1591 for (int x = 0; x < targetWidth; x++)
1592 {
1593 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1594 }
1595 }
1596
1597 return dirty;
1598}
1599
1600template<typename T>
1601bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1602{
1603 bool dirty = false;
1604 int copyWidth = std::min(targetWidth, srcWidth);
1605 int copyHeight = std::min(targetHeight, srcHeight);
1606
1607 for (int y = 0; y < copyHeight; y++)
1608 {
1609 for (int x = 0; x < copyWidth; x++)
1610 {
1611 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1612 }
1613 }
1614 // clear unfilled right side
1615 for (int y = 0; y < copyHeight; y++)
1616 {
1617 for (int x = copyWidth; x < targetWidth; x++)
1618 {
1619 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1620 }
1621 }
1622 // clear unfilled bottom.
1623 for (int y = copyHeight; y < targetHeight; y++)
1624 {
1625 for (int x = 0; x < targetWidth; x++)
1626 {
1627 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1628 }
1629 }
1630
1631 return dirty;
1632}
1633
1634template <int cols, int rows>
1635void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1636{
1637 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1638
1639 int elementCount = targetUniform->elementCount();
1640
1641 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1642 const unsigned int targetMatrixStride = (4 * rows);
1643 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1644
1645 for (int i = 0; i < count; i++)
1646 {
1647 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1648 if (transpose == GL_FALSE)
1649 {
1650 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1651 }
1652 else
1653 {
1654 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1655 }
1656 target += targetMatrixStride;
1657 value += cols * rows;
1658 }
1659}
1660
1661template <typename T>
1662void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1663{
1664 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1665
1666 if (gl::IsMatrixType(targetUniform->type))
1667 {
1668 const int rows = gl::VariableRowCount(targetUniform->type);
1669 const int cols = gl::VariableColumnCount(targetUniform->type);
1670 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1671 }
1672 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1673 {
1674 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1675 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1676 size * sizeof(T));
1677 }
1678 else
1679 {
1680 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1681 switch (gl::VariableComponentType(targetUniform->type))
1682 {
1683 case GL_BOOL:
1684 {
1685 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1686
1687 for (unsigned int i = 0; i < size; i++)
1688 {
1689 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1690 }
1691 }
1692 break;
1693
1694 case GL_FLOAT:
1695 {
1696 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1697
1698 for (unsigned int i = 0; i < size; i++)
1699 {
1700 params[i] = static_cast<T>(floatParams[i]);
1701 }
1702 }
1703 break;
1704
1705 case GL_INT:
1706 {
1707 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1708
1709 for (unsigned int i = 0; i < size; i++)
1710 {
1711 params[i] = static_cast<T>(intParams[i]);
1712 }
1713 }
1714 break;
1715
1716 case GL_UNSIGNED_INT:
1717 {
1718 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1719
1720 for (unsigned int i = 0; i < size; i++)
1721 {
1722 params[i] = static_cast<T>(uintParams[i]);
1723 }
1724 }
1725 break;
1726
1727 default: UNREACHABLE();
1728 }
1729 }
1730}
1731
1732template <typename VarT>
1733void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1734 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1735 bool inRowMajorLayout)
1736{
1737 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1738 {
1739 const VarT &field = fields[uniformIndex];
1740 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1741
1742 if (field.isStruct())
1743 {
1744 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1745
1746 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1747 {
1748 encoder->enterAggregateType();
1749
1750 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1751 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1752
1753 encoder->exitAggregateType();
1754 }
1755 }
1756 else
1757 {
1758 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1759
1760 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1761
1762 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1763 blockIndex, memberInfo);
1764
1765 // add to uniform list, but not index, since uniform block uniforms have no location
1766 blockUniformIndexes->push_back(mUniforms.size());
1767 mUniforms.push_back(newUniform);
1768 }
1769 }
1770}
1771
1772bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1773 const gl::Caps &caps)
1774{
Jamie Madill30d6c252014-11-13 10:03:33 -05001775 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001776
1777 // create uniform block entries if they do not exist
1778 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1779 {
1780 std::vector<unsigned int> blockUniformIndexes;
1781 const unsigned int blockIndex = mUniformBlocks.size();
1782
1783 // define member uniforms
1784 sh::BlockLayoutEncoder *encoder = NULL;
1785
1786 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1787 {
1788 encoder = new sh::Std140BlockEncoder;
1789 }
1790 else
1791 {
1792 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1793 }
1794 ASSERT(encoder);
1795
1796 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1797
1798 size_t dataSize = encoder->getBlockSize();
1799
1800 // create all the uniform blocks
1801 if (interfaceBlock.arraySize > 0)
1802 {
1803 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1804 {
1805 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1806 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1807 mUniformBlocks.push_back(newUniformBlock);
1808 }
1809 }
1810 else
1811 {
1812 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1813 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1814 mUniformBlocks.push_back(newUniformBlock);
1815 }
1816 }
1817
1818 if (interfaceBlock.staticUse)
1819 {
1820 // Assign registers to the uniform blocks
1821 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1822 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1823 ASSERT(blockIndex != GL_INVALID_INDEX);
1824 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1825
1826 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1827
1828 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1829 {
1830 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1831 ASSERT(uniformBlock->name == interfaceBlock.name);
1832
1833 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1834 interfaceBlockRegister + uniformBlockElement, caps))
1835 {
1836 return false;
1837 }
1838 }
1839 }
1840
1841 return true;
1842}
1843
1844bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1845 GLenum samplerType,
1846 unsigned int samplerCount,
1847 std::vector<Sampler> &outSamplers,
1848 GLuint *outUsedRange)
1849{
1850 unsigned int samplerIndex = startSamplerIndex;
1851
1852 do
1853 {
1854 if (samplerIndex < outSamplers.size())
1855 {
1856 Sampler& sampler = outSamplers[samplerIndex];
1857 sampler.active = true;
1858 sampler.textureType = GetTextureType(samplerType);
1859 sampler.logicalTextureUnit = 0;
1860 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1861 }
1862 else
1863 {
1864 return false;
1865 }
1866
1867 samplerIndex++;
1868 } while (samplerIndex < startSamplerIndex + samplerCount);
1869
1870 return true;
1871}
1872
1873bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1874{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001875 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001876 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1877
1878 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1879 {
1880 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1881 &mUsedVertexSamplerRange))
1882 {
1883 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1884 mSamplersVS.size());
1885 return false;
1886 }
1887
1888 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1889 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1890 {
1891 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1892 caps.maxVertexUniformVectors);
1893 return false;
1894 }
1895 }
1896
1897 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1898 {
1899 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1900 &mUsedPixelSamplerRange))
1901 {
1902 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1903 mSamplersPS.size());
1904 return false;
1905 }
1906
1907 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1908 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1909 {
1910 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1911 caps.maxFragmentUniformVectors);
1912 return false;
1913 }
1914 }
1915
1916 return true;
1917}
1918
1919bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1920{
1921 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1922 {
1923 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1924
Geoff Lang2ec386b2014-12-03 14:44:38 -05001925 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001926 {
1927 if (!indexSamplerUniform(uniform, infoLog, caps))
1928 {
1929 return false;
1930 }
1931 }
1932
1933 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1934 {
1935 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1936 }
1937 }
1938
1939 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001940}
1941
Brandon Jonesc9610c52014-08-25 17:02:59 -07001942void ProgramD3D::reset()
1943{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001944 ProgramImpl::reset();
1945
Brandon Joneseb994362014-09-24 10:27:28 -07001946 SafeDeleteContainer(mVertexExecutables);
1947 SafeDeleteContainer(mPixelExecutables);
1948 SafeDelete(mGeometryExecutable);
1949
1950 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001951
Brandon Jones22502d52014-08-29 16:58:36 -07001952 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001953 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001954 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001955
1956 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001957 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001958 mUsesFragDepth = false;
1959 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001960 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001961
Brandon Jonesc9610c52014-08-25 17:02:59 -07001962 SafeDelete(mVertexUniformStorage);
1963 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964
1965 mSamplersPS.clear();
1966 mSamplersVS.clear();
1967
1968 mUsedVertexSamplerRange = 0;
1969 mUsedPixelSamplerRange = 0;
1970 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001971
1972 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001973}
1974
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975unsigned int ProgramD3D::getSerial() const
1976{
1977 return mSerial;
1978}
1979
1980unsigned int ProgramD3D::issueSerial()
1981{
1982 return mCurrentSerial++;
1983}
1984
Jamie Madill437d2662014-12-05 14:23:35 -05001985void ProgramD3D::initAttributesByLayout()
1986{
1987 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1988 {
1989 mAttributesByLayout[i] = i;
1990 }
1991
1992 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
1993}
1994
1995void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
1996 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
1997{
1998 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
1999
2000 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2001 {
2002 oldTranslatedAttributes[i] = attributes[i];
2003 }
2004
2005 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2006 {
2007 int oldIndex = mAttributesByLayout[i];
2008 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2009 attributes[i] = oldTranslatedAttributes[oldIndex];
2010 }
2011}
2012
Brandon Jonesc9610c52014-08-25 17:02:59 -07002013}