blob: 9d2dc48892297cafed82b0477893320f9d5aa550 [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/ShaderExecutable.h"
17#include "libANGLE/renderer/d3d/DynamicHLSL.h"
18#include "libANGLE/renderer/d3d/RendererD3D.h"
19#include "libANGLE/renderer/d3d/ShaderD3D.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[],
127 ShaderExecutable *shaderExecutable)
128 : 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
155ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutable *shaderExecutable)
156 : 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{
216 return usesPointSpriteEmulation();
217}
218
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700219GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
220{
221 GLint logicalTextureUnit = -1;
222
223 switch (type)
224 {
225 case gl::SAMPLER_PIXEL:
226 ASSERT(samplerIndex < caps.maxTextureImageUnits);
227 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
228 {
229 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
230 }
231 break;
232 case gl::SAMPLER_VERTEX:
233 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
234 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
235 {
236 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
237 }
238 break;
239 default: UNREACHABLE();
240 }
241
242 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
243 {
244 return logicalTextureUnit;
245 }
246
247 return -1;
248}
249
250// Returns the texture type for a given Direct3D 9 sampler type and
251// index (0-15 for the pixel shader and 0-3 for the vertex shader).
252GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
253{
254 switch (type)
255 {
256 case gl::SAMPLER_PIXEL:
257 ASSERT(samplerIndex < mSamplersPS.size());
258 ASSERT(mSamplersPS[samplerIndex].active);
259 return mSamplersPS[samplerIndex].textureType;
260 case gl::SAMPLER_VERTEX:
261 ASSERT(samplerIndex < mSamplersVS.size());
262 ASSERT(mSamplersVS[samplerIndex].active);
263 return mSamplersVS[samplerIndex].textureType;
264 default: UNREACHABLE();
265 }
266
267 return GL_TEXTURE_2D;
268}
269
270GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
271{
272 switch (type)
273 {
274 case gl::SAMPLER_PIXEL:
275 return mUsedPixelSamplerRange;
276 case gl::SAMPLER_VERTEX:
277 return mUsedVertexSamplerRange;
278 default:
279 UNREACHABLE();
280 return 0;
281 }
282}
283
284void ProgramD3D::updateSamplerMapping()
285{
286 if (!mDirtySamplerMapping)
287 {
288 return;
289 }
290
291 mDirtySamplerMapping = false;
292
293 // Retrieve sampler uniform values
294 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
295 {
296 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
297
298 if (targetUniform->dirty)
299 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500300 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700301 {
302 int count = targetUniform->elementCount();
303 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
304
305 if (targetUniform->isReferencedByFragmentShader())
306 {
307 unsigned int firstIndex = targetUniform->psRegisterIndex;
308
309 for (int i = 0; i < count; i++)
310 {
311 unsigned int samplerIndex = firstIndex + i;
312
313 if (samplerIndex < mSamplersPS.size())
314 {
315 ASSERT(mSamplersPS[samplerIndex].active);
316 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
317 }
318 }
319 }
320
321 if (targetUniform->isReferencedByVertexShader())
322 {
323 unsigned int firstIndex = targetUniform->vsRegisterIndex;
324
325 for (int i = 0; i < count; i++)
326 {
327 unsigned int samplerIndex = firstIndex + i;
328
329 if (samplerIndex < mSamplersVS.size())
330 {
331 ASSERT(mSamplersVS[samplerIndex].active);
332 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
333 }
334 }
335 }
336 }
337 }
338 }
339}
340
341bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
342{
343 // if any two active samplers in a program are of different types, but refer to the same
344 // texture image unit, and this is the current program, then ValidateProgram will fail, and
345 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
346 updateSamplerMapping();
347
348 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
349
350 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
351 {
352 if (mSamplersPS[i].active)
353 {
354 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
355
356 if (unit >= textureUnitTypes.size())
357 {
358 if (infoLog)
359 {
360 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
361 }
362
363 return false;
364 }
365
366 if (textureUnitTypes[unit] != GL_NONE)
367 {
368 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
369 {
370 if (infoLog)
371 {
372 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
373 }
374
375 return false;
376 }
377 }
378 else
379 {
380 textureUnitTypes[unit] = mSamplersPS[i].textureType;
381 }
382 }
383 }
384
385 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
386 {
387 if (mSamplersVS[i].active)
388 {
389 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
390
391 if (unit >= textureUnitTypes.size())
392 {
393 if (infoLog)
394 {
395 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
396 }
397
398 return false;
399 }
400
401 if (textureUnitTypes[unit] != GL_NONE)
402 {
403 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
404 {
405 if (infoLog)
406 {
407 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
408 }
409
410 return false;
411 }
412 }
413 else
414 {
415 textureUnitTypes[unit] = mSamplersVS[i].textureType;
416 }
417 }
418 }
419
420 return true;
421}
422
Geoff Lang7dd2e102014-11-10 15:19:26 -0500423LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700424{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500425 int compileFlags = stream->readInt<int>();
426 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
427 {
428 infoLog.append("Mismatched compilation flags.");
429 return LinkResult(false, gl::Error(GL_NO_ERROR));
430 }
431
Brandon Jones44151a92014-09-10 11:32:25 -0700432 stream->readInt(&mShaderVersion);
433
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700434 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
435 for (unsigned int i = 0; i < psSamplerCount; ++i)
436 {
437 Sampler sampler;
438 stream->readBool(&sampler.active);
439 stream->readInt(&sampler.logicalTextureUnit);
440 stream->readInt(&sampler.textureType);
441 mSamplersPS.push_back(sampler);
442 }
443 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
444 for (unsigned int i = 0; i < vsSamplerCount; ++i)
445 {
446 Sampler sampler;
447 stream->readBool(&sampler.active);
448 stream->readInt(&sampler.logicalTextureUnit);
449 stream->readInt(&sampler.textureType);
450 mSamplersVS.push_back(sampler);
451 }
452
453 stream->readInt(&mUsedVertexSamplerRange);
454 stream->readInt(&mUsedPixelSamplerRange);
455
456 const unsigned int uniformCount = stream->readInt<unsigned int>();
457 if (stream->error())
458 {
459 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500460 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700461 }
462
463 mUniforms.resize(uniformCount);
464 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
465 {
466 GLenum type = stream->readInt<GLenum>();
467 GLenum precision = stream->readInt<GLenum>();
468 std::string name = stream->readString();
469 unsigned int arraySize = stream->readInt<unsigned int>();
470 int blockIndex = stream->readInt<int>();
471
472 int offset = stream->readInt<int>();
473 int arrayStride = stream->readInt<int>();
474 int matrixStride = stream->readInt<int>();
475 bool isRowMajorMatrix = stream->readBool();
476
477 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
478
479 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
480
481 stream->readInt(&uniform->psRegisterIndex);
482 stream->readInt(&uniform->vsRegisterIndex);
483 stream->readInt(&uniform->registerCount);
484 stream->readInt(&uniform->registerElement);
485
486 mUniforms[uniformIndex] = uniform;
487 }
488
489 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
490 if (stream->error())
491 {
492 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500493 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700494 }
495
496 mUniformIndex.resize(uniformIndexCount);
497 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
498 {
499 stream->readString(&mUniformIndex[uniformIndexIndex].name);
500 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
501 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
502 }
503
504 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
505 if (stream->error())
506 {
507 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500508 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700509 }
510
511 mUniformBlocks.resize(uniformBlockCount);
512 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
513 {
514 std::string name = stream->readString();
515 unsigned int elementIndex = stream->readInt<unsigned int>();
516 unsigned int dataSize = stream->readInt<unsigned int>();
517
518 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
519
520 stream->readInt(&uniformBlock->psRegisterIndex);
521 stream->readInt(&uniformBlock->vsRegisterIndex);
522
523 unsigned int numMembers = stream->readInt<unsigned int>();
524 uniformBlock->memberUniformIndexes.resize(numMembers);
525 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
526 {
527 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
528 }
529
530 mUniformBlocks[uniformBlockIndex] = uniformBlock;
531 }
532
Brandon Joneseb994362014-09-24 10:27:28 -0700533 stream->readInt(&mTransformFeedbackBufferMode);
534 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
535 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
536 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
537 {
538 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
539
540 stream->readString(&varying.name);
541 stream->readInt(&varying.type);
542 stream->readInt(&varying.size);
543 stream->readString(&varying.semanticName);
544 stream->readInt(&varying.semanticIndex);
545 stream->readInt(&varying.semanticIndexCount);
546 }
547
Brandon Jones22502d52014-08-29 16:58:36 -0700548 stream->readString(&mVertexHLSL);
549 stream->readInt(&mVertexWorkarounds);
550 stream->readString(&mPixelHLSL);
551 stream->readInt(&mPixelWorkarounds);
552 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700553 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700554
555 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
556 mPixelShaderKey.resize(pixelShaderKeySize);
557 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
558 {
559 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
560 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
561 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
562 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
563 }
564
Brandon Joneseb994362014-09-24 10:27:28 -0700565 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
566
567 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
568 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
569 {
570 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
571
572 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
573 {
574 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
575 stream->readInt(&vertexInput->mType);
576 stream->readInt(&vertexInput->mNormalized);
577 stream->readInt(&vertexInput->mComponents);
578 stream->readBool(&vertexInput->mPureInteger);
579 }
580
581 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
582 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400583
584 ShaderExecutable *shaderExecutable = NULL;
585 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
586 SHADER_VERTEX,
587 mTransformFeedbackLinkedVaryings,
588 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
589 &shaderExecutable);
590 if (error.isError())
591 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500592 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400593 }
594
Brandon Joneseb994362014-09-24 10:27:28 -0700595 if (!shaderExecutable)
596 {
597 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500598 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700599 }
600
601 // generated converted input layout
602 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
603 getInputLayoutSignature(inputLayout, signature);
604
605 // add new binary
606 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
607
608 stream->skip(vertexShaderSize);
609 }
610
611 const size_t pixelShaderCount = stream->readInt<unsigned int>();
612 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
613 {
614 const size_t outputCount = stream->readInt<unsigned int>();
615 std::vector<GLenum> outputs(outputCount);
616 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
617 {
618 stream->readInt(&outputs[outputIndex]);
619 }
620
621 const size_t pixelShaderSize = stream->readInt<unsigned int>();
622 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400623 ShaderExecutable *shaderExecutable = NULL;
624 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
625 mTransformFeedbackLinkedVaryings,
626 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
627 &shaderExecutable);
628 if (error.isError())
629 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500630 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400631 }
Brandon Joneseb994362014-09-24 10:27:28 -0700632
633 if (!shaderExecutable)
634 {
635 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500636 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700637 }
638
639 // add new binary
640 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
641
642 stream->skip(pixelShaderSize);
643 }
644
645 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
646
647 if (geometryShaderSize > 0)
648 {
649 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400650 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
651 mTransformFeedbackLinkedVaryings,
652 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
653 &mGeometryExecutable);
654 if (error.isError())
655 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500656 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400657 }
Brandon Joneseb994362014-09-24 10:27:28 -0700658
659 if (!mGeometryExecutable)
660 {
661 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500662 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700663 }
664 stream->skip(geometryShaderSize);
665 }
666
Brandon Jones18bd4102014-09-22 14:21:44 -0700667 GUID binaryIdentifier = {0};
668 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
669
670 GUID identifier = mRenderer->getAdapterIdentifier();
671 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
672 {
673 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500674 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700675 }
676
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700677 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500678 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700679
Geoff Lang7dd2e102014-11-10 15:19:26 -0500680 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700681}
682
Geoff Langb543aff2014-09-30 14:52:54 -0400683gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700684{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500685 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
686
Brandon Jones44151a92014-09-10 11:32:25 -0700687 stream->writeInt(mShaderVersion);
688
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689 stream->writeInt(mSamplersPS.size());
690 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
691 {
692 stream->writeInt(mSamplersPS[i].active);
693 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
694 stream->writeInt(mSamplersPS[i].textureType);
695 }
696
697 stream->writeInt(mSamplersVS.size());
698 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
699 {
700 stream->writeInt(mSamplersVS[i].active);
701 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
702 stream->writeInt(mSamplersVS[i].textureType);
703 }
704
705 stream->writeInt(mUsedVertexSamplerRange);
706 stream->writeInt(mUsedPixelSamplerRange);
707
708 stream->writeInt(mUniforms.size());
709 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
710 {
711 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
712
713 stream->writeInt(uniform.type);
714 stream->writeInt(uniform.precision);
715 stream->writeString(uniform.name);
716 stream->writeInt(uniform.arraySize);
717 stream->writeInt(uniform.blockIndex);
718
719 stream->writeInt(uniform.blockInfo.offset);
720 stream->writeInt(uniform.blockInfo.arrayStride);
721 stream->writeInt(uniform.blockInfo.matrixStride);
722 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
723
724 stream->writeInt(uniform.psRegisterIndex);
725 stream->writeInt(uniform.vsRegisterIndex);
726 stream->writeInt(uniform.registerCount);
727 stream->writeInt(uniform.registerElement);
728 }
729
730 stream->writeInt(mUniformIndex.size());
731 for (size_t i = 0; i < mUniformIndex.size(); ++i)
732 {
733 stream->writeString(mUniformIndex[i].name);
734 stream->writeInt(mUniformIndex[i].element);
735 stream->writeInt(mUniformIndex[i].index);
736 }
737
738 stream->writeInt(mUniformBlocks.size());
739 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
740 {
741 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
742
743 stream->writeString(uniformBlock.name);
744 stream->writeInt(uniformBlock.elementIndex);
745 stream->writeInt(uniformBlock.dataSize);
746
747 stream->writeInt(uniformBlock.memberUniformIndexes.size());
748 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
749 {
750 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
751 }
752
753 stream->writeInt(uniformBlock.psRegisterIndex);
754 stream->writeInt(uniformBlock.vsRegisterIndex);
755 }
756
Brandon Joneseb994362014-09-24 10:27:28 -0700757 stream->writeInt(mTransformFeedbackBufferMode);
758 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
759 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
760 {
761 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
762
763 stream->writeString(varying.name);
764 stream->writeInt(varying.type);
765 stream->writeInt(varying.size);
766 stream->writeString(varying.semanticName);
767 stream->writeInt(varying.semanticIndex);
768 stream->writeInt(varying.semanticIndexCount);
769 }
770
Brandon Jones22502d52014-08-29 16:58:36 -0700771 stream->writeString(mVertexHLSL);
772 stream->writeInt(mVertexWorkarounds);
773 stream->writeString(mPixelHLSL);
774 stream->writeInt(mPixelWorkarounds);
775 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700776 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700777
Brandon Joneseb994362014-09-24 10:27:28 -0700778 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700779 stream->writeInt(pixelShaderKey.size());
780 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
781 {
Brandon Joneseb994362014-09-24 10:27:28 -0700782 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700783 stream->writeInt(variable.type);
784 stream->writeString(variable.name);
785 stream->writeString(variable.source);
786 stream->writeInt(variable.outputIndex);
787 }
788
Brandon Joneseb994362014-09-24 10:27:28 -0700789 stream->writeInt(mVertexExecutables.size());
790 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
791 {
792 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
793
794 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
795 {
796 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
797 stream->writeInt(vertexInput.mType);
798 stream->writeInt(vertexInput.mNormalized);
799 stream->writeInt(vertexInput.mComponents);
800 stream->writeInt(vertexInput.mPureInteger);
801 }
802
803 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
804 stream->writeInt(vertexShaderSize);
805
806 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
807 stream->writeBytes(vertexBlob, vertexShaderSize);
808 }
809
810 stream->writeInt(mPixelExecutables.size());
811 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
812 {
813 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
814
815 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
816 stream->writeInt(outputs.size());
817 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
818 {
819 stream->writeInt(outputs[outputIndex]);
820 }
821
822 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
823 stream->writeInt(pixelShaderSize);
824
825 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
826 stream->writeBytes(pixelBlob, pixelShaderSize);
827 }
828
829 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
830 stream->writeInt(geometryShaderSize);
831
832 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
833 {
834 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
835 stream->writeBytes(geometryBlob, geometryShaderSize);
836 }
837
Brandon Jones18bd4102014-09-22 14:21:44 -0700838 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400839 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700840
Geoff Langb543aff2014-09-30 14:52:54 -0400841 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700842}
843
Geoff Langb543aff2014-09-30 14:52:54 -0400844gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutable **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700845{
Brandon Joneseb994362014-09-24 10:27:28 -0700846 std::vector<GLenum> outputs;
847
Jamie Madill48faf802014-11-06 15:27:22 -0500848 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700849
850 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
851 {
852 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
853
854 if (colorbuffer)
855 {
856 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
857 }
858 else
859 {
860 outputs.push_back(GL_NONE);
861 }
862 }
863
Jamie Madill97399232014-12-23 12:31:15 -0500864 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700865}
866
Jamie Madill97399232014-12-23 12:31:15 -0500867gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
868 ShaderExecutable **outExectuable,
869 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700870{
871 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
872 {
873 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
874 {
Geoff Langb543aff2014-09-30 14:52:54 -0400875 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
876 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700877 }
878 }
879
Brandon Jones22502d52014-08-29 16:58:36 -0700880 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
881 outputSignature);
882
883 // Generate new pixel executable
Geoff Langb543aff2014-09-30 14:52:54 -0400884 ShaderExecutable *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500885
886 gl::InfoLog tempInfoLog;
887 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
888
889 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400890 mTransformFeedbackLinkedVaryings,
891 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
892 mPixelWorkarounds, &pixelExecutable);
893 if (error.isError())
894 {
895 return error;
896 }
Brandon Joneseb994362014-09-24 10:27:28 -0700897
Jamie Madill97399232014-12-23 12:31:15 -0500898 if (pixelExecutable)
899 {
900 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
901 }
902 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700903 {
904 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
905 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
906 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
907 }
Brandon Jones22502d52014-08-29 16:58:36 -0700908
Geoff Langb543aff2014-09-30 14:52:54 -0400909 *outExectuable = pixelExecutable;
910 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700911}
912
Jamie Madill97399232014-12-23 12:31:15 -0500913gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
914 ShaderExecutable **outExectuable,
915 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700916{
Brandon Joneseb994362014-09-24 10:27:28 -0700917 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
918 getInputLayoutSignature(inputLayout, signature);
919
920 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
921 {
922 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
923 {
Geoff Langb543aff2014-09-30 14:52:54 -0400924 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
925 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700926 }
927 }
928
Brandon Jones22502d52014-08-29 16:58:36 -0700929 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700930 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700931
932 // Generate new vertex executable
Geoff Langb543aff2014-09-30 14:52:54 -0400933 ShaderExecutable *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500934
935 gl::InfoLog tempInfoLog;
936 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
937
938 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400939 mTransformFeedbackLinkedVaryings,
940 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
941 mVertexWorkarounds, &vertexExecutable);
942 if (error.isError())
943 {
944 return error;
945 }
946
Jamie Madill97399232014-12-23 12:31:15 -0500947 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700948 {
949 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
950 }
Jamie Madill97399232014-12-23 12:31:15 -0500951 else if (!infoLog)
952 {
953 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
954 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
955 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
956 }
Brandon Jones22502d52014-08-29 16:58:36 -0700957
Geoff Langb543aff2014-09-30 14:52:54 -0400958 *outExectuable = vertexExecutable;
959 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700960}
961
Geoff Lang7dd2e102014-11-10 15:19:26 -0500962LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
963 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700964{
Brandon Jones18bd4102014-09-22 14:21:44 -0700965 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
966 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700967
Brandon Joneseb994362014-09-24 10:27:28 -0700968 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
969 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
Geoff Langb543aff2014-09-30 14:52:54 -0400970 ShaderExecutable *defaultVertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500971 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400972 if (error.isError())
973 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500974 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400975 }
Brandon Jones44151a92014-09-10 11:32:25 -0700976
Brandon Joneseb994362014-09-24 10:27:28 -0700977 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Langb543aff2014-09-30 14:52:54 -0400978 ShaderExecutable *defaultPixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500979 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400980 if (error.isError())
981 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500982 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400983 }
Brandon Jones44151a92014-09-10 11:32:25 -0700984
Brandon Joneseb994362014-09-24 10:27:28 -0700985 if (usesGeometryShader())
986 {
987 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -0700988
Geoff Langb543aff2014-09-30 14:52:54 -0400989
990 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
991 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
992 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
993 if (error.isError())
994 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500995 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400996 }
Brandon Joneseb994362014-09-24 10:27:28 -0700997 }
998
Brandon Jones091540d2014-10-29 11:32:04 -0700999#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001000 if (usesGeometryShader() && mGeometryExecutable)
1001 {
1002 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1003 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1004 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1005 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1006 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1007 }
1008
1009 if (defaultVertexExecutable)
1010 {
1011 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1012 }
1013
1014 if (defaultPixelExecutable)
1015 {
1016 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1017 }
1018#endif
1019
Geoff Langb543aff2014-09-30 14:52:54 -04001020 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001021 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001022}
1023
Geoff Lang7dd2e102014-11-10 15:19:26 -05001024LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1025 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1026 const std::vector<std::string> &transformFeedbackVaryings,
1027 GLenum transformFeedbackBufferMode,
1028 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1029 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001030{
Brandon Joneseb994362014-09-24 10:27:28 -07001031 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1032 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1033
Jamie Madillde8892b2014-11-11 13:00:22 -05001034 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1035 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001036
Brandon Joneseb994362014-09-24 10:27:28 -07001037 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001038
1039 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
1040 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1041
1042 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1043 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001044 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001045
1046 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001047 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001048 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1049
Geoff Langbdee2d52014-09-17 11:02:51 -04001050 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001051 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001052 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001053 }
1054
Geoff Lang7dd2e102014-11-10 15:19:26 -05001055 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
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
Jamie Madillde8892b2014-11-11 13:00:22 -05001060 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001061 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1062 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1063 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001064 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001065 }
1066
Brandon Jones44151a92014-09-10 11:32:25 -07001067 mUsesPointSize = vertexShaderD3D->usesPointSize();
1068
Jamie Madill437d2662014-12-05 14:23:35 -05001069 initAttributesByLayout();
1070
Geoff Lang7dd2e102014-11-10 15:19:26 -05001071 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001072}
1073
Brandon Jones44151a92014-09-10 11:32:25 -07001074void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1075{
1076 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1077}
1078
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001079void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001080{
1081 // Compute total default block size
1082 unsigned int vertexRegisters = 0;
1083 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001084 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001085 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001086 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001087
Geoff Lang2ec386b2014-12-03 14:44:38 -05001088 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001089 {
1090 if (uniform.isReferencedByVertexShader())
1091 {
1092 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1093 }
1094 if (uniform.isReferencedByFragmentShader())
1095 {
1096 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1097 }
1098 }
1099 }
1100
1101 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1102 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1103}
1104
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001105gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001106{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001107 updateSamplerMapping();
1108
1109 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1110 if (error.isError())
1111 {
1112 return error;
1113 }
1114
1115 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1116 {
1117 mUniforms[uniformIndex]->dirty = false;
1118 }
1119
1120 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001121}
1122
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001123gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001124{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001125 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1126
Brandon Jones18bd4102014-09-22 14:21:44 -07001127 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1128 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1129
1130 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1131 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1132
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001133 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001134 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001135 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001136 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1137
1138 ASSERT(uniformBlock && uniformBuffer);
1139
1140 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1141 {
1142 // undefined behaviour
1143 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1144 }
1145
1146 // Unnecessary to apply an unreferenced standard or shared UBO
1147 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1148 {
1149 continue;
1150 }
1151
1152 if (uniformBlock->isReferencedByVertexShader())
1153 {
1154 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1155 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1156 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1157 vertexUniformBuffers[registerIndex] = uniformBuffer;
1158 }
1159
1160 if (uniformBlock->isReferencedByFragmentShader())
1161 {
1162 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1163 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1164 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1165 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1166 }
1167 }
1168
1169 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1170}
1171
1172bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1173 unsigned int registerIndex, const gl::Caps &caps)
1174{
1175 if (shader == GL_VERTEX_SHADER)
1176 {
1177 uniformBlock->vsRegisterIndex = registerIndex;
1178 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1179 {
1180 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1181 return false;
1182 }
1183 }
1184 else if (shader == GL_FRAGMENT_SHADER)
1185 {
1186 uniformBlock->psRegisterIndex = registerIndex;
1187 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1188 {
1189 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1190 return false;
1191 }
1192 }
1193 else UNREACHABLE();
1194
1195 return true;
1196}
1197
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001198void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001199{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001200 unsigned int numUniforms = mUniforms.size();
1201 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001202 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001203 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001204 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001205}
1206
1207void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1208{
1209 setUniform(location, count, v, GL_FLOAT);
1210}
1211
1212void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1213{
1214 setUniform(location, count, v, GL_FLOAT_VEC2);
1215}
1216
1217void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1218{
1219 setUniform(location, count, v, GL_FLOAT_VEC3);
1220}
1221
1222void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1223{
1224 setUniform(location, count, v, GL_FLOAT_VEC4);
1225}
1226
1227void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1228{
1229 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1230}
1231
1232void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1233{
1234 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1235}
1236
1237void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1238{
1239 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1240}
1241
1242void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1243{
1244 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1245}
1246
1247void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1248{
1249 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1250}
1251
1252void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1253{
1254 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1255}
1256
1257void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1258{
1259 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1260}
1261
1262void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1263{
1264 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1265}
1266
1267void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1268{
1269 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1270}
1271
1272void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1273{
1274 setUniform(location, count, v, GL_INT);
1275}
1276
1277void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1278{
1279 setUniform(location, count, v, GL_INT_VEC2);
1280}
1281
1282void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1283{
1284 setUniform(location, count, v, GL_INT_VEC3);
1285}
1286
1287void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1288{
1289 setUniform(location, count, v, GL_INT_VEC4);
1290}
1291
1292void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1293{
1294 setUniform(location, count, v, GL_UNSIGNED_INT);
1295}
1296
1297void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1298{
1299 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1300}
1301
1302void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1303{
1304 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1305}
1306
1307void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1308{
1309 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1310}
1311
1312void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1313{
1314 getUniformv(location, params, GL_FLOAT);
1315}
1316
1317void ProgramD3D::getUniformiv(GLint location, GLint *params)
1318{
1319 getUniformv(location, params, GL_INT);
1320}
1321
1322void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1323{
1324 getUniformv(location, params, GL_UNSIGNED_INT);
1325}
1326
1327bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1328 const gl::Caps &caps)
1329{
Jamie Madill30d6c252014-11-13 10:03:33 -05001330 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1331 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001332
1333 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1334 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1335
1336 // Check that uniforms defined in the vertex and fragment shaders are identical
1337 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1338 UniformMap linkedUniforms;
1339
1340 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001341 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001342 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1343 linkedUniforms[vertexUniform.name] = &vertexUniform;
1344 }
1345
1346 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1347 {
1348 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1349 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1350 if (entry != linkedUniforms.end())
1351 {
1352 const sh::Uniform &vertexUniform = *entry->second;
1353 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001354 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001355 {
1356 return false;
1357 }
1358 }
1359 }
1360
1361 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1362 {
1363 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1364
1365 if (uniform.staticUse)
1366 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001367 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001368 }
1369 }
1370
1371 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1372 {
1373 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1374
1375 if (uniform.staticUse)
1376 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001377 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001378 }
1379 }
1380
1381 if (!indexUniforms(infoLog, caps))
1382 {
1383 return false;
1384 }
1385
1386 initializeUniformStorage();
1387
1388 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1389 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1390 {
1391 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1392
1393 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1394 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1395 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1396 }
1397
1398 return true;
1399}
1400
Geoff Lang492a7e42014-11-05 13:27:06 -05001401void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001402{
Geoff Lang492a7e42014-11-05 13:27:06 -05001403 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001404 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1405 encoder.skipRegisters(uniformRegister);
1406
1407 defineUniform(shader, uniform, uniform.name, &encoder);
1408}
1409
Geoff Lang492a7e42014-11-05 13:27:06 -05001410void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1411 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001412{
1413 if (uniform.isStruct())
1414 {
1415 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1416 {
1417 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1418
1419 encoder->enterAggregateType();
1420
1421 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1422 {
1423 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1424 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1425
1426 defineUniform(shader, field, fieldFullName, encoder);
1427 }
1428
1429 encoder->exitAggregateType();
1430 }
1431 }
1432 else // Not a struct
1433 {
1434 // Arrays are treated as aggregate types
1435 if (uniform.isArray())
1436 {
1437 encoder->enterAggregateType();
1438 }
1439
1440 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1441
1442 if (!linkedUniform)
1443 {
1444 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
1445 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
1446 ASSERT(linkedUniform);
1447 linkedUniform->registerElement = encoder->getCurrentElement();
1448 mUniforms.push_back(linkedUniform);
1449 }
1450
1451 ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
1452
Geoff Lang492a7e42014-11-05 13:27:06 -05001453 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001454 {
1455 linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
1456 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001457 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001458 {
1459 linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
1460 }
1461 else UNREACHABLE();
1462
1463 // Advance the uniform offset, to track registers allocation for structs
1464 encoder->encodeType(uniform.type, uniform.arraySize, false);
1465
1466 // Arrays are treated as aggregate types
1467 if (uniform.isArray())
1468 {
1469 encoder->exitAggregateType();
1470 }
1471 }
1472}
1473
1474template <typename T>
1475static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1476{
1477 ASSERT(dest != NULL);
1478 ASSERT(dirtyFlag != NULL);
1479
1480 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1481 *dest = source;
1482}
1483
1484template <typename T>
1485void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1486{
1487 const int components = gl::VariableComponentCount(targetUniformType);
1488 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1489
1490 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1491
1492 int elementCount = targetUniform->elementCount();
1493
1494 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1495
1496 if (targetUniform->type == targetUniformType)
1497 {
1498 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1499
1500 for (int i = 0; i < count; i++)
1501 {
1502 T *dest = target + (i * 4);
1503 const T *source = v + (i * components);
1504
1505 for (int c = 0; c < components; c++)
1506 {
1507 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1508 }
1509 for (int c = components; c < 4; c++)
1510 {
1511 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1512 }
1513 }
1514 }
1515 else if (targetUniform->type == targetBoolType)
1516 {
1517 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1518
1519 for (int i = 0; i < count; i++)
1520 {
1521 GLint *dest = boolParams + (i * 4);
1522 const T *source = v + (i * components);
1523
1524 for (int c = 0; c < components; c++)
1525 {
1526 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1527 }
1528 for (int c = components; c < 4; c++)
1529 {
1530 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1531 }
1532 }
1533 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001534 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001535 {
1536 ASSERT(targetUniformType == GL_INT);
1537
1538 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1539
1540 bool wasDirty = targetUniform->dirty;
1541
1542 for (int i = 0; i < count; i++)
1543 {
1544 GLint *dest = target + (i * 4);
1545 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1546
1547 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1548 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1549 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1550 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1551 }
1552
1553 if (!wasDirty && targetUniform->dirty)
1554 {
1555 mDirtySamplerMapping = true;
1556 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001557 }
1558 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001559}
Brandon Jones18bd4102014-09-22 14:21:44 -07001560
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001561template<typename T>
1562bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1563{
1564 bool dirty = false;
1565 int copyWidth = std::min(targetHeight, srcWidth);
1566 int copyHeight = std::min(targetWidth, srcHeight);
1567
1568 for (int x = 0; x < copyWidth; x++)
1569 {
1570 for (int y = 0; y < copyHeight; y++)
1571 {
1572 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1573 }
1574 }
1575 // clear unfilled right side
1576 for (int y = 0; y < copyWidth; y++)
1577 {
1578 for (int x = copyHeight; x < targetWidth; x++)
1579 {
1580 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1581 }
1582 }
1583 // clear unfilled bottom.
1584 for (int y = copyWidth; y < targetHeight; y++)
1585 {
1586 for (int x = 0; x < targetWidth; x++)
1587 {
1588 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1589 }
1590 }
1591
1592 return dirty;
1593}
1594
1595template<typename T>
1596bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1597{
1598 bool dirty = false;
1599 int copyWidth = std::min(targetWidth, srcWidth);
1600 int copyHeight = std::min(targetHeight, srcHeight);
1601
1602 for (int y = 0; y < copyHeight; y++)
1603 {
1604 for (int x = 0; x < copyWidth; x++)
1605 {
1606 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1607 }
1608 }
1609 // clear unfilled right side
1610 for (int y = 0; y < copyHeight; y++)
1611 {
1612 for (int x = copyWidth; x < targetWidth; x++)
1613 {
1614 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1615 }
1616 }
1617 // clear unfilled bottom.
1618 for (int y = copyHeight; y < targetHeight; y++)
1619 {
1620 for (int x = 0; x < targetWidth; x++)
1621 {
1622 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1623 }
1624 }
1625
1626 return dirty;
1627}
1628
1629template <int cols, int rows>
1630void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1631{
1632 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1633
1634 int elementCount = targetUniform->elementCount();
1635
1636 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1637 const unsigned int targetMatrixStride = (4 * rows);
1638 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1639
1640 for (int i = 0; i < count; i++)
1641 {
1642 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1643 if (transpose == GL_FALSE)
1644 {
1645 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1646 }
1647 else
1648 {
1649 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1650 }
1651 target += targetMatrixStride;
1652 value += cols * rows;
1653 }
1654}
1655
1656template <typename T>
1657void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1658{
1659 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1660
1661 if (gl::IsMatrixType(targetUniform->type))
1662 {
1663 const int rows = gl::VariableRowCount(targetUniform->type);
1664 const int cols = gl::VariableColumnCount(targetUniform->type);
1665 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1666 }
1667 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1668 {
1669 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1670 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1671 size * sizeof(T));
1672 }
1673 else
1674 {
1675 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1676 switch (gl::VariableComponentType(targetUniform->type))
1677 {
1678 case GL_BOOL:
1679 {
1680 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1681
1682 for (unsigned int i = 0; i < size; i++)
1683 {
1684 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1685 }
1686 }
1687 break;
1688
1689 case GL_FLOAT:
1690 {
1691 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1692
1693 for (unsigned int i = 0; i < size; i++)
1694 {
1695 params[i] = static_cast<T>(floatParams[i]);
1696 }
1697 }
1698 break;
1699
1700 case GL_INT:
1701 {
1702 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1703
1704 for (unsigned int i = 0; i < size; i++)
1705 {
1706 params[i] = static_cast<T>(intParams[i]);
1707 }
1708 }
1709 break;
1710
1711 case GL_UNSIGNED_INT:
1712 {
1713 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1714
1715 for (unsigned int i = 0; i < size; i++)
1716 {
1717 params[i] = static_cast<T>(uintParams[i]);
1718 }
1719 }
1720 break;
1721
1722 default: UNREACHABLE();
1723 }
1724 }
1725}
1726
1727template <typename VarT>
1728void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1729 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1730 bool inRowMajorLayout)
1731{
1732 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1733 {
1734 const VarT &field = fields[uniformIndex];
1735 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1736
1737 if (field.isStruct())
1738 {
1739 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1740
1741 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1742 {
1743 encoder->enterAggregateType();
1744
1745 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1746 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1747
1748 encoder->exitAggregateType();
1749 }
1750 }
1751 else
1752 {
1753 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1754
1755 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1756
1757 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1758 blockIndex, memberInfo);
1759
1760 // add to uniform list, but not index, since uniform block uniforms have no location
1761 blockUniformIndexes->push_back(mUniforms.size());
1762 mUniforms.push_back(newUniform);
1763 }
1764 }
1765}
1766
1767bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1768 const gl::Caps &caps)
1769{
Jamie Madill30d6c252014-11-13 10:03:33 -05001770 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001771
1772 // create uniform block entries if they do not exist
1773 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1774 {
1775 std::vector<unsigned int> blockUniformIndexes;
1776 const unsigned int blockIndex = mUniformBlocks.size();
1777
1778 // define member uniforms
1779 sh::BlockLayoutEncoder *encoder = NULL;
1780
1781 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1782 {
1783 encoder = new sh::Std140BlockEncoder;
1784 }
1785 else
1786 {
1787 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1788 }
1789 ASSERT(encoder);
1790
1791 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1792
1793 size_t dataSize = encoder->getBlockSize();
1794
1795 // create all the uniform blocks
1796 if (interfaceBlock.arraySize > 0)
1797 {
1798 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1799 {
1800 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1801 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1802 mUniformBlocks.push_back(newUniformBlock);
1803 }
1804 }
1805 else
1806 {
1807 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1808 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1809 mUniformBlocks.push_back(newUniformBlock);
1810 }
1811 }
1812
1813 if (interfaceBlock.staticUse)
1814 {
1815 // Assign registers to the uniform blocks
1816 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1817 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1818 ASSERT(blockIndex != GL_INVALID_INDEX);
1819 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1820
1821 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1822
1823 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1824 {
1825 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1826 ASSERT(uniformBlock->name == interfaceBlock.name);
1827
1828 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1829 interfaceBlockRegister + uniformBlockElement, caps))
1830 {
1831 return false;
1832 }
1833 }
1834 }
1835
1836 return true;
1837}
1838
1839bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1840 GLenum samplerType,
1841 unsigned int samplerCount,
1842 std::vector<Sampler> &outSamplers,
1843 GLuint *outUsedRange)
1844{
1845 unsigned int samplerIndex = startSamplerIndex;
1846
1847 do
1848 {
1849 if (samplerIndex < outSamplers.size())
1850 {
1851 Sampler& sampler = outSamplers[samplerIndex];
1852 sampler.active = true;
1853 sampler.textureType = GetTextureType(samplerType);
1854 sampler.logicalTextureUnit = 0;
1855 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1856 }
1857 else
1858 {
1859 return false;
1860 }
1861
1862 samplerIndex++;
1863 } while (samplerIndex < startSamplerIndex + samplerCount);
1864
1865 return true;
1866}
1867
1868bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1869{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001870 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001871 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1872
1873 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1874 {
1875 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1876 &mUsedVertexSamplerRange))
1877 {
1878 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1879 mSamplersVS.size());
1880 return false;
1881 }
1882
1883 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1884 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1885 {
1886 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1887 caps.maxVertexUniformVectors);
1888 return false;
1889 }
1890 }
1891
1892 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1893 {
1894 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1895 &mUsedPixelSamplerRange))
1896 {
1897 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1898 mSamplersPS.size());
1899 return false;
1900 }
1901
1902 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1903 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1904 {
1905 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1906 caps.maxFragmentUniformVectors);
1907 return false;
1908 }
1909 }
1910
1911 return true;
1912}
1913
1914bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1915{
1916 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1917 {
1918 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1919
Geoff Lang2ec386b2014-12-03 14:44:38 -05001920 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001921 {
1922 if (!indexSamplerUniform(uniform, infoLog, caps))
1923 {
1924 return false;
1925 }
1926 }
1927
1928 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1929 {
1930 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1931 }
1932 }
1933
1934 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001935}
1936
Brandon Jonesc9610c52014-08-25 17:02:59 -07001937void ProgramD3D::reset()
1938{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001939 ProgramImpl::reset();
1940
Brandon Joneseb994362014-09-24 10:27:28 -07001941 SafeDeleteContainer(mVertexExecutables);
1942 SafeDeleteContainer(mPixelExecutables);
1943 SafeDelete(mGeometryExecutable);
1944
1945 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001946
Brandon Jones22502d52014-08-29 16:58:36 -07001947 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001948 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001949 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001950
1951 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001952 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001953 mUsesFragDepth = false;
1954 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001955 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001956
Brandon Jonesc9610c52014-08-25 17:02:59 -07001957 SafeDelete(mVertexUniformStorage);
1958 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001959
1960 mSamplersPS.clear();
1961 mSamplersVS.clear();
1962
1963 mUsedVertexSamplerRange = 0;
1964 mUsedPixelSamplerRange = 0;
1965 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001966
1967 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001968}
1969
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970unsigned int ProgramD3D::getSerial() const
1971{
1972 return mSerial;
1973}
1974
1975unsigned int ProgramD3D::issueSerial()
1976{
1977 return mCurrentSerial++;
1978}
1979
Jamie Madill437d2662014-12-05 14:23:35 -05001980void ProgramD3D::initAttributesByLayout()
1981{
1982 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1983 {
1984 mAttributesByLayout[i] = i;
1985 }
1986
1987 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
1988}
1989
1990void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
1991 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
1992{
1993 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
1994
1995 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1996 {
1997 oldTranslatedAttributes[i] = attributes[i];
1998 }
1999
2000 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2001 {
2002 int oldIndex = mAttributesByLayout[i];
2003 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2004 attributes[i] = oldTranslatedAttributes[oldIndex];
2005 }
2006}
2007
Brandon Jonesc9610c52014-08-25 17:02:59 -07002008}