blob: 1f67e8dd7707c5b6f77cd0e2579d615c42b27f0a [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{
86 std::vector<GLenum> defaultPixelOutput(1);
87
88 ASSERT(!shaderOutputVars.empty());
89 defaultPixelOutput[0] = GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex;
90
91 return defaultPixelOutput;
92}
93
Brandon Jones1a8a7e32014-10-01 12:49:30 -070094bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
95{
96 return var.isRowMajorLayout;
97}
98
99bool IsRowMajorLayout(const sh::ShaderVariable &var)
100{
101 return false;
102}
103
Jamie Madill437d2662014-12-05 14:23:35 -0500104struct AttributeSorter
105{
106 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
107 : originalIndices(semanticIndices)
108 {
109 }
110
111 bool operator()(int a, int b)
112 {
113 if (originalIndices[a] == -1) return false;
114 if (originalIndices[b] == -1) return true;
115 return (originalIndices[a] < originalIndices[b]);
116 }
117
118 const ProgramImpl::SemanticIndexArray &originalIndices;
119};
120
Brandon Joneseb994362014-09-24 10:27:28 -0700121}
122
123ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
124 const GLenum signature[],
125 ShaderExecutable *shaderExecutable)
126 : mShaderExecutable(shaderExecutable)
127{
128 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
129 {
130 mInputs[attributeIndex] = inputLayout[attributeIndex];
131 mSignature[attributeIndex] = signature[attributeIndex];
132 }
133}
134
135ProgramD3D::VertexExecutable::~VertexExecutable()
136{
137 SafeDelete(mShaderExecutable);
138}
139
140bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
141{
142 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
143 {
144 if (mSignature[attributeIndex] != signature[attributeIndex])
145 {
146 return false;
147 }
148 }
149
150 return true;
151}
152
153ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutable *shaderExecutable)
154 : mOutputSignature(outputSignature),
155 mShaderExecutable(shaderExecutable)
156{
157}
158
159ProgramD3D::PixelExecutable::~PixelExecutable()
160{
161 SafeDelete(mShaderExecutable);
162}
163
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700164ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
165{
166}
167
Geoff Lang7dd2e102014-11-10 15:19:26 -0500168unsigned int ProgramD3D::mCurrentSerial = 1;
169
Jamie Madill93e13fb2014-11-06 15:27:25 -0500170ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700171 : ProgramImpl(),
172 mRenderer(renderer),
173 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700174 mGeometryExecutable(NULL),
175 mVertexWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
176 mPixelWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
Brandon Jones44151a92014-09-10 11:32:25 -0700177 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700178 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700179 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700180 mUsedVertexSamplerRange(0),
181 mUsedPixelSamplerRange(0),
182 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500183 mShaderVersion(100),
184 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700185{
Brandon Joneseb994362014-09-24 10:27:28 -0700186 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700187}
188
189ProgramD3D::~ProgramD3D()
190{
191 reset();
192 SafeDelete(mDynamicHLSL);
193}
194
195ProgramD3D *ProgramD3D::makeProgramD3D(ProgramImpl *impl)
196{
197 ASSERT(HAS_DYNAMIC_TYPE(ProgramD3D*, impl));
198 return static_cast<ProgramD3D*>(impl);
199}
200
201const ProgramD3D *ProgramD3D::makeProgramD3D(const ProgramImpl *impl)
202{
203 ASSERT(HAS_DYNAMIC_TYPE(const ProgramD3D*, impl));
204 return static_cast<const ProgramD3D*>(impl);
205}
206
Brandon Jones44151a92014-09-10 11:32:25 -0700207bool ProgramD3D::usesPointSpriteEmulation() const
208{
209 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
210}
211
212bool ProgramD3D::usesGeometryShader() const
213{
214 return usesPointSpriteEmulation();
215}
216
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700217GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
218{
219 GLint logicalTextureUnit = -1;
220
221 switch (type)
222 {
223 case gl::SAMPLER_PIXEL:
224 ASSERT(samplerIndex < caps.maxTextureImageUnits);
225 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
226 {
227 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
228 }
229 break;
230 case gl::SAMPLER_VERTEX:
231 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
232 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
233 {
234 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
235 }
236 break;
237 default: UNREACHABLE();
238 }
239
240 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
241 {
242 return logicalTextureUnit;
243 }
244
245 return -1;
246}
247
248// Returns the texture type for a given Direct3D 9 sampler type and
249// index (0-15 for the pixel shader and 0-3 for the vertex shader).
250GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
251{
252 switch (type)
253 {
254 case gl::SAMPLER_PIXEL:
255 ASSERT(samplerIndex < mSamplersPS.size());
256 ASSERT(mSamplersPS[samplerIndex].active);
257 return mSamplersPS[samplerIndex].textureType;
258 case gl::SAMPLER_VERTEX:
259 ASSERT(samplerIndex < mSamplersVS.size());
260 ASSERT(mSamplersVS[samplerIndex].active);
261 return mSamplersVS[samplerIndex].textureType;
262 default: UNREACHABLE();
263 }
264
265 return GL_TEXTURE_2D;
266}
267
268GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
269{
270 switch (type)
271 {
272 case gl::SAMPLER_PIXEL:
273 return mUsedPixelSamplerRange;
274 case gl::SAMPLER_VERTEX:
275 return mUsedVertexSamplerRange;
276 default:
277 UNREACHABLE();
278 return 0;
279 }
280}
281
282void ProgramD3D::updateSamplerMapping()
283{
284 if (!mDirtySamplerMapping)
285 {
286 return;
287 }
288
289 mDirtySamplerMapping = false;
290
291 // Retrieve sampler uniform values
292 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
293 {
294 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
295
296 if (targetUniform->dirty)
297 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500298 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700299 {
300 int count = targetUniform->elementCount();
301 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
302
303 if (targetUniform->isReferencedByFragmentShader())
304 {
305 unsigned int firstIndex = targetUniform->psRegisterIndex;
306
307 for (int i = 0; i < count; i++)
308 {
309 unsigned int samplerIndex = firstIndex + i;
310
311 if (samplerIndex < mSamplersPS.size())
312 {
313 ASSERT(mSamplersPS[samplerIndex].active);
314 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
315 }
316 }
317 }
318
319 if (targetUniform->isReferencedByVertexShader())
320 {
321 unsigned int firstIndex = targetUniform->vsRegisterIndex;
322
323 for (int i = 0; i < count; i++)
324 {
325 unsigned int samplerIndex = firstIndex + i;
326
327 if (samplerIndex < mSamplersVS.size())
328 {
329 ASSERT(mSamplersVS[samplerIndex].active);
330 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
331 }
332 }
333 }
334 }
335 }
336 }
337}
338
339bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
340{
341 // if any two active samplers in a program are of different types, but refer to the same
342 // texture image unit, and this is the current program, then ValidateProgram will fail, and
343 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
344 updateSamplerMapping();
345
346 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
347
348 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
349 {
350 if (mSamplersPS[i].active)
351 {
352 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
353
354 if (unit >= textureUnitTypes.size())
355 {
356 if (infoLog)
357 {
358 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
359 }
360
361 return false;
362 }
363
364 if (textureUnitTypes[unit] != GL_NONE)
365 {
366 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
367 {
368 if (infoLog)
369 {
370 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
371 }
372
373 return false;
374 }
375 }
376 else
377 {
378 textureUnitTypes[unit] = mSamplersPS[i].textureType;
379 }
380 }
381 }
382
383 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
384 {
385 if (mSamplersVS[i].active)
386 {
387 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
388
389 if (unit >= textureUnitTypes.size())
390 {
391 if (infoLog)
392 {
393 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
394 }
395
396 return false;
397 }
398
399 if (textureUnitTypes[unit] != GL_NONE)
400 {
401 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
402 {
403 if (infoLog)
404 {
405 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
406 }
407
408 return false;
409 }
410 }
411 else
412 {
413 textureUnitTypes[unit] = mSamplersVS[i].textureType;
414 }
415 }
416 }
417
418 return true;
419}
420
Geoff Lang7dd2e102014-11-10 15:19:26 -0500421LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700422{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500423 int compileFlags = stream->readInt<int>();
424 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
425 {
426 infoLog.append("Mismatched compilation flags.");
427 return LinkResult(false, gl::Error(GL_NO_ERROR));
428 }
429
Brandon Jones44151a92014-09-10 11:32:25 -0700430 stream->readInt(&mShaderVersion);
431
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700432 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
433 for (unsigned int i = 0; i < psSamplerCount; ++i)
434 {
435 Sampler sampler;
436 stream->readBool(&sampler.active);
437 stream->readInt(&sampler.logicalTextureUnit);
438 stream->readInt(&sampler.textureType);
439 mSamplersPS.push_back(sampler);
440 }
441 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
442 for (unsigned int i = 0; i < vsSamplerCount; ++i)
443 {
444 Sampler sampler;
445 stream->readBool(&sampler.active);
446 stream->readInt(&sampler.logicalTextureUnit);
447 stream->readInt(&sampler.textureType);
448 mSamplersVS.push_back(sampler);
449 }
450
451 stream->readInt(&mUsedVertexSamplerRange);
452 stream->readInt(&mUsedPixelSamplerRange);
453
454 const unsigned int uniformCount = stream->readInt<unsigned int>();
455 if (stream->error())
456 {
457 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500458 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700459 }
460
461 mUniforms.resize(uniformCount);
462 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
463 {
464 GLenum type = stream->readInt<GLenum>();
465 GLenum precision = stream->readInt<GLenum>();
466 std::string name = stream->readString();
467 unsigned int arraySize = stream->readInt<unsigned int>();
468 int blockIndex = stream->readInt<int>();
469
470 int offset = stream->readInt<int>();
471 int arrayStride = stream->readInt<int>();
472 int matrixStride = stream->readInt<int>();
473 bool isRowMajorMatrix = stream->readBool();
474
475 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
476
477 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
478
479 stream->readInt(&uniform->psRegisterIndex);
480 stream->readInt(&uniform->vsRegisterIndex);
481 stream->readInt(&uniform->registerCount);
482 stream->readInt(&uniform->registerElement);
483
484 mUniforms[uniformIndex] = uniform;
485 }
486
487 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
488 if (stream->error())
489 {
490 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500491 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700492 }
493
494 mUniformIndex.resize(uniformIndexCount);
495 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
496 {
497 stream->readString(&mUniformIndex[uniformIndexIndex].name);
498 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
499 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
500 }
501
502 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
503 if (stream->error())
504 {
505 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500506 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700507 }
508
509 mUniformBlocks.resize(uniformBlockCount);
510 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
511 {
512 std::string name = stream->readString();
513 unsigned int elementIndex = stream->readInt<unsigned int>();
514 unsigned int dataSize = stream->readInt<unsigned int>();
515
516 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
517
518 stream->readInt(&uniformBlock->psRegisterIndex);
519 stream->readInt(&uniformBlock->vsRegisterIndex);
520
521 unsigned int numMembers = stream->readInt<unsigned int>();
522 uniformBlock->memberUniformIndexes.resize(numMembers);
523 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
524 {
525 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
526 }
527
528 mUniformBlocks[uniformBlockIndex] = uniformBlock;
529 }
530
Brandon Joneseb994362014-09-24 10:27:28 -0700531 stream->readInt(&mTransformFeedbackBufferMode);
532 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
533 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
534 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
535 {
536 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
537
538 stream->readString(&varying.name);
539 stream->readInt(&varying.type);
540 stream->readInt(&varying.size);
541 stream->readString(&varying.semanticName);
542 stream->readInt(&varying.semanticIndex);
543 stream->readInt(&varying.semanticIndexCount);
544 }
545
Brandon Jones22502d52014-08-29 16:58:36 -0700546 stream->readString(&mVertexHLSL);
547 stream->readInt(&mVertexWorkarounds);
548 stream->readString(&mPixelHLSL);
549 stream->readInt(&mPixelWorkarounds);
550 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700551 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700552
553 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
554 mPixelShaderKey.resize(pixelShaderKeySize);
555 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
556 {
557 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
558 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
559 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
560 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
561 }
562
Brandon Joneseb994362014-09-24 10:27:28 -0700563 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
564
565 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
566 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
567 {
568 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
569
570 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
571 {
572 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
573 stream->readInt(&vertexInput->mType);
574 stream->readInt(&vertexInput->mNormalized);
575 stream->readInt(&vertexInput->mComponents);
576 stream->readBool(&vertexInput->mPureInteger);
577 }
578
579 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
580 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400581
582 ShaderExecutable *shaderExecutable = NULL;
583 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
584 SHADER_VERTEX,
585 mTransformFeedbackLinkedVaryings,
586 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
587 &shaderExecutable);
588 if (error.isError())
589 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500590 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400591 }
592
Brandon Joneseb994362014-09-24 10:27:28 -0700593 if (!shaderExecutable)
594 {
595 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500596 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700597 }
598
599 // generated converted input layout
600 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
601 getInputLayoutSignature(inputLayout, signature);
602
603 // add new binary
604 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
605
606 stream->skip(vertexShaderSize);
607 }
608
609 const size_t pixelShaderCount = stream->readInt<unsigned int>();
610 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
611 {
612 const size_t outputCount = stream->readInt<unsigned int>();
613 std::vector<GLenum> outputs(outputCount);
614 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
615 {
616 stream->readInt(&outputs[outputIndex]);
617 }
618
619 const size_t pixelShaderSize = stream->readInt<unsigned int>();
620 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400621 ShaderExecutable *shaderExecutable = NULL;
622 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
623 mTransformFeedbackLinkedVaryings,
624 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
625 &shaderExecutable);
626 if (error.isError())
627 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500628 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400629 }
Brandon Joneseb994362014-09-24 10:27:28 -0700630
631 if (!shaderExecutable)
632 {
633 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500634 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700635 }
636
637 // add new binary
638 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
639
640 stream->skip(pixelShaderSize);
641 }
642
643 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
644
645 if (geometryShaderSize > 0)
646 {
647 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400648 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
649 mTransformFeedbackLinkedVaryings,
650 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
651 &mGeometryExecutable);
652 if (error.isError())
653 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500654 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400655 }
Brandon Joneseb994362014-09-24 10:27:28 -0700656
657 if (!mGeometryExecutable)
658 {
659 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500660 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700661 }
662 stream->skip(geometryShaderSize);
663 }
664
Brandon Jones18bd4102014-09-22 14:21:44 -0700665 GUID binaryIdentifier = {0};
666 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
667
668 GUID identifier = mRenderer->getAdapterIdentifier();
669 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
670 {
671 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500672 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700673 }
674
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700675 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500676 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700677
Geoff Lang7dd2e102014-11-10 15:19:26 -0500678 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700679}
680
Geoff Langb543aff2014-09-30 14:52:54 -0400681gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700682{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500683 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
684
Brandon Jones44151a92014-09-10 11:32:25 -0700685 stream->writeInt(mShaderVersion);
686
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700687 stream->writeInt(mSamplersPS.size());
688 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
689 {
690 stream->writeInt(mSamplersPS[i].active);
691 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
692 stream->writeInt(mSamplersPS[i].textureType);
693 }
694
695 stream->writeInt(mSamplersVS.size());
696 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
697 {
698 stream->writeInt(mSamplersVS[i].active);
699 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
700 stream->writeInt(mSamplersVS[i].textureType);
701 }
702
703 stream->writeInt(mUsedVertexSamplerRange);
704 stream->writeInt(mUsedPixelSamplerRange);
705
706 stream->writeInt(mUniforms.size());
707 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
708 {
709 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
710
711 stream->writeInt(uniform.type);
712 stream->writeInt(uniform.precision);
713 stream->writeString(uniform.name);
714 stream->writeInt(uniform.arraySize);
715 stream->writeInt(uniform.blockIndex);
716
717 stream->writeInt(uniform.blockInfo.offset);
718 stream->writeInt(uniform.blockInfo.arrayStride);
719 stream->writeInt(uniform.blockInfo.matrixStride);
720 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
721
722 stream->writeInt(uniform.psRegisterIndex);
723 stream->writeInt(uniform.vsRegisterIndex);
724 stream->writeInt(uniform.registerCount);
725 stream->writeInt(uniform.registerElement);
726 }
727
728 stream->writeInt(mUniformIndex.size());
729 for (size_t i = 0; i < mUniformIndex.size(); ++i)
730 {
731 stream->writeString(mUniformIndex[i].name);
732 stream->writeInt(mUniformIndex[i].element);
733 stream->writeInt(mUniformIndex[i].index);
734 }
735
736 stream->writeInt(mUniformBlocks.size());
737 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
738 {
739 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
740
741 stream->writeString(uniformBlock.name);
742 stream->writeInt(uniformBlock.elementIndex);
743 stream->writeInt(uniformBlock.dataSize);
744
745 stream->writeInt(uniformBlock.memberUniformIndexes.size());
746 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
747 {
748 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
749 }
750
751 stream->writeInt(uniformBlock.psRegisterIndex);
752 stream->writeInt(uniformBlock.vsRegisterIndex);
753 }
754
Brandon Joneseb994362014-09-24 10:27:28 -0700755 stream->writeInt(mTransformFeedbackBufferMode);
756 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
757 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
758 {
759 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
760
761 stream->writeString(varying.name);
762 stream->writeInt(varying.type);
763 stream->writeInt(varying.size);
764 stream->writeString(varying.semanticName);
765 stream->writeInt(varying.semanticIndex);
766 stream->writeInt(varying.semanticIndexCount);
767 }
768
Brandon Jones22502d52014-08-29 16:58:36 -0700769 stream->writeString(mVertexHLSL);
770 stream->writeInt(mVertexWorkarounds);
771 stream->writeString(mPixelHLSL);
772 stream->writeInt(mPixelWorkarounds);
773 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700774 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700775
Brandon Joneseb994362014-09-24 10:27:28 -0700776 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700777 stream->writeInt(pixelShaderKey.size());
778 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
779 {
Brandon Joneseb994362014-09-24 10:27:28 -0700780 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700781 stream->writeInt(variable.type);
782 stream->writeString(variable.name);
783 stream->writeString(variable.source);
784 stream->writeInt(variable.outputIndex);
785 }
786
Brandon Joneseb994362014-09-24 10:27:28 -0700787 stream->writeInt(mVertexExecutables.size());
788 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
789 {
790 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
791
792 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
793 {
794 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
795 stream->writeInt(vertexInput.mType);
796 stream->writeInt(vertexInput.mNormalized);
797 stream->writeInt(vertexInput.mComponents);
798 stream->writeInt(vertexInput.mPureInteger);
799 }
800
801 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
802 stream->writeInt(vertexShaderSize);
803
804 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
805 stream->writeBytes(vertexBlob, vertexShaderSize);
806 }
807
808 stream->writeInt(mPixelExecutables.size());
809 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
810 {
811 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
812
813 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
814 stream->writeInt(outputs.size());
815 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
816 {
817 stream->writeInt(outputs[outputIndex]);
818 }
819
820 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
821 stream->writeInt(pixelShaderSize);
822
823 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
824 stream->writeBytes(pixelBlob, pixelShaderSize);
825 }
826
827 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
828 stream->writeInt(geometryShaderSize);
829
830 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
831 {
832 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
833 stream->writeBytes(geometryBlob, geometryShaderSize);
834 }
835
Brandon Jones18bd4102014-09-22 14:21:44 -0700836 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400837 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700838
Geoff Langb543aff2014-09-30 14:52:54 -0400839 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700840}
841
Geoff Langb543aff2014-09-30 14:52:54 -0400842gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutable **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700843{
Brandon Joneseb994362014-09-24 10:27:28 -0700844 std::vector<GLenum> outputs;
845
Jamie Madill48faf802014-11-06 15:27:22 -0500846 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700847
848 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
849 {
850 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
851
852 if (colorbuffer)
853 {
854 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
855 }
856 else
857 {
858 outputs.push_back(GL_NONE);
859 }
860 }
861
Jamie Madill97399232014-12-23 12:31:15 -0500862 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700863}
864
Jamie Madill97399232014-12-23 12:31:15 -0500865gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
866 ShaderExecutable **outExectuable,
867 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700868{
869 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
870 {
871 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
872 {
Geoff Langb543aff2014-09-30 14:52:54 -0400873 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
874 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700875 }
876 }
877
Brandon Jones22502d52014-08-29 16:58:36 -0700878 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
879 outputSignature);
880
881 // Generate new pixel executable
Geoff Langb543aff2014-09-30 14:52:54 -0400882 ShaderExecutable *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500883
884 gl::InfoLog tempInfoLog;
885 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
886
887 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400888 mTransformFeedbackLinkedVaryings,
889 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
890 mPixelWorkarounds, &pixelExecutable);
891 if (error.isError())
892 {
893 return error;
894 }
Brandon Joneseb994362014-09-24 10:27:28 -0700895
Jamie Madill97399232014-12-23 12:31:15 -0500896 if (pixelExecutable)
897 {
898 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
899 }
900 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700901 {
902 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
903 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
904 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
905 }
Brandon Jones22502d52014-08-29 16:58:36 -0700906
Geoff Langb543aff2014-09-30 14:52:54 -0400907 *outExectuable = pixelExecutable;
908 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700909}
910
Jamie Madill97399232014-12-23 12:31:15 -0500911gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
912 ShaderExecutable **outExectuable,
913 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700914{
Brandon Joneseb994362014-09-24 10:27:28 -0700915 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
916 getInputLayoutSignature(inputLayout, signature);
917
918 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
919 {
920 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
921 {
Geoff Langb543aff2014-09-30 14:52:54 -0400922 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
923 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700924 }
925 }
926
Brandon Jones22502d52014-08-29 16:58:36 -0700927 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700928 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700929
930 // Generate new vertex executable
Geoff Langb543aff2014-09-30 14:52:54 -0400931 ShaderExecutable *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500932
933 gl::InfoLog tempInfoLog;
934 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
935
936 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400937 mTransformFeedbackLinkedVaryings,
938 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
939 mVertexWorkarounds, &vertexExecutable);
940 if (error.isError())
941 {
942 return error;
943 }
944
Jamie Madill97399232014-12-23 12:31:15 -0500945 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700946 {
947 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
948 }
Jamie Madill97399232014-12-23 12:31:15 -0500949 else if (!infoLog)
950 {
951 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
952 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
953 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
954 }
Brandon Jones22502d52014-08-29 16:58:36 -0700955
Geoff Langb543aff2014-09-30 14:52:54 -0400956 *outExectuable = vertexExecutable;
957 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700958}
959
Geoff Lang7dd2e102014-11-10 15:19:26 -0500960LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
961 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700962{
Brandon Jones18bd4102014-09-22 14:21:44 -0700963 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
964 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700965
Brandon Joneseb994362014-09-24 10:27:28 -0700966 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
967 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
Geoff Langb543aff2014-09-30 14:52:54 -0400968 ShaderExecutable *defaultVertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500969 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400970 if (error.isError())
971 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500972 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400973 }
Brandon Jones44151a92014-09-10 11:32:25 -0700974
Brandon Joneseb994362014-09-24 10:27:28 -0700975 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Langb543aff2014-09-30 14:52:54 -0400976 ShaderExecutable *defaultPixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500977 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400978 if (error.isError())
979 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500980 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400981 }
Brandon Jones44151a92014-09-10 11:32:25 -0700982
Brandon Joneseb994362014-09-24 10:27:28 -0700983 if (usesGeometryShader())
984 {
985 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -0700986
Geoff Langb543aff2014-09-30 14:52:54 -0400987
988 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
989 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
990 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
991 if (error.isError())
992 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500993 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400994 }
Brandon Joneseb994362014-09-24 10:27:28 -0700995 }
996
Brandon Jones091540d2014-10-29 11:32:04 -0700997#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +0200998 if (usesGeometryShader() && mGeometryExecutable)
999 {
1000 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1001 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1002 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1003 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1004 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1005 }
1006
1007 if (defaultVertexExecutable)
1008 {
1009 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1010 }
1011
1012 if (defaultPixelExecutable)
1013 {
1014 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1015 }
1016#endif
1017
Geoff Langb543aff2014-09-30 14:52:54 -04001018 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001019 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001020}
1021
Geoff Lang7dd2e102014-11-10 15:19:26 -05001022LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1023 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1024 const std::vector<std::string> &transformFeedbackVaryings,
1025 GLenum transformFeedbackBufferMode,
1026 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1027 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001028{
Brandon Joneseb994362014-09-24 10:27:28 -07001029 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1030 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1031
Jamie Madillde8892b2014-11-11 13:00:22 -05001032 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1033 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001034
Brandon Joneseb994362014-09-24 10:27:28 -07001035 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001036
1037 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
1038 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1039
1040 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1041 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001042 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001043
1044 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001045 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001046 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1047
Geoff Langbdee2d52014-09-17 11:02:51 -04001048 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001049 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001050 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001051 }
1052
Geoff Lang7dd2e102014-11-10 15:19:26 -05001053 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001054 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001055 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001056 }
1057
Jamie Madillde8892b2014-11-11 13:00:22 -05001058 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001059 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1060 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1061 {
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
Brandon Jones44151a92014-09-10 11:32:25 -07001065 mUsesPointSize = vertexShaderD3D->usesPointSize();
1066
Jamie Madill437d2662014-12-05 14:23:35 -05001067 initAttributesByLayout();
1068
Geoff Lang7dd2e102014-11-10 15:19:26 -05001069 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001070}
1071
Brandon Jones44151a92014-09-10 11:32:25 -07001072void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1073{
1074 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1075}
1076
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001077void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001078{
1079 // Compute total default block size
1080 unsigned int vertexRegisters = 0;
1081 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001082 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001083 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001084 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001085
Geoff Lang2ec386b2014-12-03 14:44:38 -05001086 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001087 {
1088 if (uniform.isReferencedByVertexShader())
1089 {
1090 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1091 }
1092 if (uniform.isReferencedByFragmentShader())
1093 {
1094 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1095 }
1096 }
1097 }
1098
1099 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1100 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1101}
1102
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001103gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001104{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001105 updateSamplerMapping();
1106
1107 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1108 if (error.isError())
1109 {
1110 return error;
1111 }
1112
1113 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1114 {
1115 mUniforms[uniformIndex]->dirty = false;
1116 }
1117
1118 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001119}
1120
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001121gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001122{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001123 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1124
Brandon Jones18bd4102014-09-22 14:21:44 -07001125 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1126 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1127
1128 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1129 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1130
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001131 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001132 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001133 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001134 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1135
1136 ASSERT(uniformBlock && uniformBuffer);
1137
1138 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1139 {
1140 // undefined behaviour
1141 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1142 }
1143
1144 // Unnecessary to apply an unreferenced standard or shared UBO
1145 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1146 {
1147 continue;
1148 }
1149
1150 if (uniformBlock->isReferencedByVertexShader())
1151 {
1152 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1153 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1154 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1155 vertexUniformBuffers[registerIndex] = uniformBuffer;
1156 }
1157
1158 if (uniformBlock->isReferencedByFragmentShader())
1159 {
1160 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1161 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1162 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1163 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1164 }
1165 }
1166
1167 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1168}
1169
1170bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1171 unsigned int registerIndex, const gl::Caps &caps)
1172{
1173 if (shader == GL_VERTEX_SHADER)
1174 {
1175 uniformBlock->vsRegisterIndex = registerIndex;
1176 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1177 {
1178 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1179 return false;
1180 }
1181 }
1182 else if (shader == GL_FRAGMENT_SHADER)
1183 {
1184 uniformBlock->psRegisterIndex = registerIndex;
1185 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1186 {
1187 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1188 return false;
1189 }
1190 }
1191 else UNREACHABLE();
1192
1193 return true;
1194}
1195
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001196void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001197{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001198 unsigned int numUniforms = mUniforms.size();
1199 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001200 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001201 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001202 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001203}
1204
1205void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1206{
1207 setUniform(location, count, v, GL_FLOAT);
1208}
1209
1210void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1211{
1212 setUniform(location, count, v, GL_FLOAT_VEC2);
1213}
1214
1215void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1216{
1217 setUniform(location, count, v, GL_FLOAT_VEC3);
1218}
1219
1220void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1221{
1222 setUniform(location, count, v, GL_FLOAT_VEC4);
1223}
1224
1225void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1226{
1227 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1228}
1229
1230void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1231{
1232 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1233}
1234
1235void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1236{
1237 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1238}
1239
1240void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1241{
1242 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1243}
1244
1245void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1246{
1247 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1248}
1249
1250void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1251{
1252 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1253}
1254
1255void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1256{
1257 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1258}
1259
1260void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1261{
1262 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1263}
1264
1265void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1266{
1267 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1268}
1269
1270void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1271{
1272 setUniform(location, count, v, GL_INT);
1273}
1274
1275void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1276{
1277 setUniform(location, count, v, GL_INT_VEC2);
1278}
1279
1280void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1281{
1282 setUniform(location, count, v, GL_INT_VEC3);
1283}
1284
1285void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1286{
1287 setUniform(location, count, v, GL_INT_VEC4);
1288}
1289
1290void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1291{
1292 setUniform(location, count, v, GL_UNSIGNED_INT);
1293}
1294
1295void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1296{
1297 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1298}
1299
1300void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1301{
1302 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1303}
1304
1305void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1306{
1307 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1308}
1309
1310void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1311{
1312 getUniformv(location, params, GL_FLOAT);
1313}
1314
1315void ProgramD3D::getUniformiv(GLint location, GLint *params)
1316{
1317 getUniformv(location, params, GL_INT);
1318}
1319
1320void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1321{
1322 getUniformv(location, params, GL_UNSIGNED_INT);
1323}
1324
1325bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1326 const gl::Caps &caps)
1327{
Jamie Madill30d6c252014-11-13 10:03:33 -05001328 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1329 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001330
1331 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1332 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1333
1334 // Check that uniforms defined in the vertex and fragment shaders are identical
1335 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1336 UniformMap linkedUniforms;
1337
1338 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001339 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001340 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1341 linkedUniforms[vertexUniform.name] = &vertexUniform;
1342 }
1343
1344 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1345 {
1346 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1347 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1348 if (entry != linkedUniforms.end())
1349 {
1350 const sh::Uniform &vertexUniform = *entry->second;
1351 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001352 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001353 {
1354 return false;
1355 }
1356 }
1357 }
1358
1359 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1360 {
1361 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1362
1363 if (uniform.staticUse)
1364 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001365 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001366 }
1367 }
1368
1369 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1370 {
1371 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1372
1373 if (uniform.staticUse)
1374 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001375 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001376 }
1377 }
1378
1379 if (!indexUniforms(infoLog, caps))
1380 {
1381 return false;
1382 }
1383
1384 initializeUniformStorage();
1385
1386 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1387 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1388 {
1389 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1390
1391 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1392 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1393 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1394 }
1395
1396 return true;
1397}
1398
Geoff Lang492a7e42014-11-05 13:27:06 -05001399void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001400{
Geoff Lang492a7e42014-11-05 13:27:06 -05001401 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001402 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1403 encoder.skipRegisters(uniformRegister);
1404
1405 defineUniform(shader, uniform, uniform.name, &encoder);
1406}
1407
Geoff Lang492a7e42014-11-05 13:27:06 -05001408void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1409 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001410{
1411 if (uniform.isStruct())
1412 {
1413 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1414 {
1415 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1416
1417 encoder->enterAggregateType();
1418
1419 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1420 {
1421 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1422 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1423
1424 defineUniform(shader, field, fieldFullName, encoder);
1425 }
1426
1427 encoder->exitAggregateType();
1428 }
1429 }
1430 else // Not a struct
1431 {
1432 // Arrays are treated as aggregate types
1433 if (uniform.isArray())
1434 {
1435 encoder->enterAggregateType();
1436 }
1437
1438 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1439
1440 if (!linkedUniform)
1441 {
1442 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
1443 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
1444 ASSERT(linkedUniform);
1445 linkedUniform->registerElement = encoder->getCurrentElement();
1446 mUniforms.push_back(linkedUniform);
1447 }
1448
1449 ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
1450
Geoff Lang492a7e42014-11-05 13:27:06 -05001451 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001452 {
1453 linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
1454 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001455 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001456 {
1457 linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
1458 }
1459 else UNREACHABLE();
1460
1461 // Advance the uniform offset, to track registers allocation for structs
1462 encoder->encodeType(uniform.type, uniform.arraySize, false);
1463
1464 // Arrays are treated as aggregate types
1465 if (uniform.isArray())
1466 {
1467 encoder->exitAggregateType();
1468 }
1469 }
1470}
1471
1472template <typename T>
1473static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1474{
1475 ASSERT(dest != NULL);
1476 ASSERT(dirtyFlag != NULL);
1477
1478 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1479 *dest = source;
1480}
1481
1482template <typename T>
1483void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1484{
1485 const int components = gl::VariableComponentCount(targetUniformType);
1486 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1487
1488 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1489
1490 int elementCount = targetUniform->elementCount();
1491
1492 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1493
1494 if (targetUniform->type == targetUniformType)
1495 {
1496 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1497
1498 for (int i = 0; i < count; i++)
1499 {
1500 T *dest = target + (i * 4);
1501 const T *source = v + (i * components);
1502
1503 for (int c = 0; c < components; c++)
1504 {
1505 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1506 }
1507 for (int c = components; c < 4; c++)
1508 {
1509 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1510 }
1511 }
1512 }
1513 else if (targetUniform->type == targetBoolType)
1514 {
1515 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1516
1517 for (int i = 0; i < count; i++)
1518 {
1519 GLint *dest = boolParams + (i * 4);
1520 const T *source = v + (i * components);
1521
1522 for (int c = 0; c < components; c++)
1523 {
1524 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1525 }
1526 for (int c = components; c < 4; c++)
1527 {
1528 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1529 }
1530 }
1531 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001532 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001533 {
1534 ASSERT(targetUniformType == GL_INT);
1535
1536 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1537
1538 bool wasDirty = targetUniform->dirty;
1539
1540 for (int i = 0; i < count; i++)
1541 {
1542 GLint *dest = target + (i * 4);
1543 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1544
1545 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1546 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1547 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1548 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1549 }
1550
1551 if (!wasDirty && targetUniform->dirty)
1552 {
1553 mDirtySamplerMapping = true;
1554 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001555 }
1556 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001557}
Brandon Jones18bd4102014-09-22 14:21:44 -07001558
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001559template<typename T>
1560bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1561{
1562 bool dirty = false;
1563 int copyWidth = std::min(targetHeight, srcWidth);
1564 int copyHeight = std::min(targetWidth, srcHeight);
1565
1566 for (int x = 0; x < copyWidth; x++)
1567 {
1568 for (int y = 0; y < copyHeight; y++)
1569 {
1570 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1571 }
1572 }
1573 // clear unfilled right side
1574 for (int y = 0; y < copyWidth; y++)
1575 {
1576 for (int x = copyHeight; x < targetWidth; x++)
1577 {
1578 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1579 }
1580 }
1581 // clear unfilled bottom.
1582 for (int y = copyWidth; y < targetHeight; y++)
1583 {
1584 for (int x = 0; x < targetWidth; x++)
1585 {
1586 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1587 }
1588 }
1589
1590 return dirty;
1591}
1592
1593template<typename T>
1594bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1595{
1596 bool dirty = false;
1597 int copyWidth = std::min(targetWidth, srcWidth);
1598 int copyHeight = std::min(targetHeight, srcHeight);
1599
1600 for (int y = 0; y < copyHeight; y++)
1601 {
1602 for (int x = 0; x < copyWidth; x++)
1603 {
1604 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1605 }
1606 }
1607 // clear unfilled right side
1608 for (int y = 0; y < copyHeight; y++)
1609 {
1610 for (int x = copyWidth; x < targetWidth; x++)
1611 {
1612 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1613 }
1614 }
1615 // clear unfilled bottom.
1616 for (int y = copyHeight; y < targetHeight; y++)
1617 {
1618 for (int x = 0; x < targetWidth; x++)
1619 {
1620 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1621 }
1622 }
1623
1624 return dirty;
1625}
1626
1627template <int cols, int rows>
1628void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1629{
1630 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1631
1632 int elementCount = targetUniform->elementCount();
1633
1634 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1635 const unsigned int targetMatrixStride = (4 * rows);
1636 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1637
1638 for (int i = 0; i < count; i++)
1639 {
1640 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1641 if (transpose == GL_FALSE)
1642 {
1643 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1644 }
1645 else
1646 {
1647 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1648 }
1649 target += targetMatrixStride;
1650 value += cols * rows;
1651 }
1652}
1653
1654template <typename T>
1655void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1656{
1657 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1658
1659 if (gl::IsMatrixType(targetUniform->type))
1660 {
1661 const int rows = gl::VariableRowCount(targetUniform->type);
1662 const int cols = gl::VariableColumnCount(targetUniform->type);
1663 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1664 }
1665 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1666 {
1667 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1668 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1669 size * sizeof(T));
1670 }
1671 else
1672 {
1673 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1674 switch (gl::VariableComponentType(targetUniform->type))
1675 {
1676 case GL_BOOL:
1677 {
1678 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1679
1680 for (unsigned int i = 0; i < size; i++)
1681 {
1682 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1683 }
1684 }
1685 break;
1686
1687 case GL_FLOAT:
1688 {
1689 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1690
1691 for (unsigned int i = 0; i < size; i++)
1692 {
1693 params[i] = static_cast<T>(floatParams[i]);
1694 }
1695 }
1696 break;
1697
1698 case GL_INT:
1699 {
1700 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1701
1702 for (unsigned int i = 0; i < size; i++)
1703 {
1704 params[i] = static_cast<T>(intParams[i]);
1705 }
1706 }
1707 break;
1708
1709 case GL_UNSIGNED_INT:
1710 {
1711 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1712
1713 for (unsigned int i = 0; i < size; i++)
1714 {
1715 params[i] = static_cast<T>(uintParams[i]);
1716 }
1717 }
1718 break;
1719
1720 default: UNREACHABLE();
1721 }
1722 }
1723}
1724
1725template <typename VarT>
1726void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1727 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1728 bool inRowMajorLayout)
1729{
1730 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1731 {
1732 const VarT &field = fields[uniformIndex];
1733 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1734
1735 if (field.isStruct())
1736 {
1737 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1738
1739 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1740 {
1741 encoder->enterAggregateType();
1742
1743 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1744 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1745
1746 encoder->exitAggregateType();
1747 }
1748 }
1749 else
1750 {
1751 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1752
1753 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1754
1755 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1756 blockIndex, memberInfo);
1757
1758 // add to uniform list, but not index, since uniform block uniforms have no location
1759 blockUniformIndexes->push_back(mUniforms.size());
1760 mUniforms.push_back(newUniform);
1761 }
1762 }
1763}
1764
1765bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1766 const gl::Caps &caps)
1767{
Jamie Madill30d6c252014-11-13 10:03:33 -05001768 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001769
1770 // create uniform block entries if they do not exist
1771 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1772 {
1773 std::vector<unsigned int> blockUniformIndexes;
1774 const unsigned int blockIndex = mUniformBlocks.size();
1775
1776 // define member uniforms
1777 sh::BlockLayoutEncoder *encoder = NULL;
1778
1779 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1780 {
1781 encoder = new sh::Std140BlockEncoder;
1782 }
1783 else
1784 {
1785 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1786 }
1787 ASSERT(encoder);
1788
1789 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1790
1791 size_t dataSize = encoder->getBlockSize();
1792
1793 // create all the uniform blocks
1794 if (interfaceBlock.arraySize > 0)
1795 {
1796 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1797 {
1798 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1799 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1800 mUniformBlocks.push_back(newUniformBlock);
1801 }
1802 }
1803 else
1804 {
1805 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1806 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1807 mUniformBlocks.push_back(newUniformBlock);
1808 }
1809 }
1810
1811 if (interfaceBlock.staticUse)
1812 {
1813 // Assign registers to the uniform blocks
1814 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1815 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1816 ASSERT(blockIndex != GL_INVALID_INDEX);
1817 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1818
1819 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1820
1821 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1822 {
1823 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1824 ASSERT(uniformBlock->name == interfaceBlock.name);
1825
1826 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1827 interfaceBlockRegister + uniformBlockElement, caps))
1828 {
1829 return false;
1830 }
1831 }
1832 }
1833
1834 return true;
1835}
1836
1837bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1838 GLenum samplerType,
1839 unsigned int samplerCount,
1840 std::vector<Sampler> &outSamplers,
1841 GLuint *outUsedRange)
1842{
1843 unsigned int samplerIndex = startSamplerIndex;
1844
1845 do
1846 {
1847 if (samplerIndex < outSamplers.size())
1848 {
1849 Sampler& sampler = outSamplers[samplerIndex];
1850 sampler.active = true;
1851 sampler.textureType = GetTextureType(samplerType);
1852 sampler.logicalTextureUnit = 0;
1853 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1854 }
1855 else
1856 {
1857 return false;
1858 }
1859
1860 samplerIndex++;
1861 } while (samplerIndex < startSamplerIndex + samplerCount);
1862
1863 return true;
1864}
1865
1866bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1867{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001868 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001869 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1870
1871 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1872 {
1873 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1874 &mUsedVertexSamplerRange))
1875 {
1876 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1877 mSamplersVS.size());
1878 return false;
1879 }
1880
1881 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1882 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1883 {
1884 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1885 caps.maxVertexUniformVectors);
1886 return false;
1887 }
1888 }
1889
1890 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1891 {
1892 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1893 &mUsedPixelSamplerRange))
1894 {
1895 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1896 mSamplersPS.size());
1897 return false;
1898 }
1899
1900 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1901 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1902 {
1903 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1904 caps.maxFragmentUniformVectors);
1905 return false;
1906 }
1907 }
1908
1909 return true;
1910}
1911
1912bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1913{
1914 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1915 {
1916 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1917
Geoff Lang2ec386b2014-12-03 14:44:38 -05001918 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001919 {
1920 if (!indexSamplerUniform(uniform, infoLog, caps))
1921 {
1922 return false;
1923 }
1924 }
1925
1926 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1927 {
1928 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1929 }
1930 }
1931
1932 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001933}
1934
Brandon Jonesc9610c52014-08-25 17:02:59 -07001935void ProgramD3D::reset()
1936{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001937 ProgramImpl::reset();
1938
Brandon Joneseb994362014-09-24 10:27:28 -07001939 SafeDeleteContainer(mVertexExecutables);
1940 SafeDeleteContainer(mPixelExecutables);
1941 SafeDelete(mGeometryExecutable);
1942
1943 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001944
Brandon Jones22502d52014-08-29 16:58:36 -07001945 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001946 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001947 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001948
1949 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001950 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001951 mUsesFragDepth = false;
1952 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001953 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001954
Brandon Jonesc9610c52014-08-25 17:02:59 -07001955 SafeDelete(mVertexUniformStorage);
1956 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001957
1958 mSamplersPS.clear();
1959 mSamplersVS.clear();
1960
1961 mUsedVertexSamplerRange = 0;
1962 mUsedPixelSamplerRange = 0;
1963 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001964
1965 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001966}
1967
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968unsigned int ProgramD3D::getSerial() const
1969{
1970 return mSerial;
1971}
1972
1973unsigned int ProgramD3D::issueSerial()
1974{
1975 return mCurrentSerial++;
1976}
1977
Jamie Madill437d2662014-12-05 14:23:35 -05001978void ProgramD3D::initAttributesByLayout()
1979{
1980 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1981 {
1982 mAttributesByLayout[i] = i;
1983 }
1984
1985 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
1986}
1987
1988void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
1989 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
1990{
1991 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
1992
1993 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1994 {
1995 oldTranslatedAttributes[i] = attributes[i];
1996 }
1997
1998 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1999 {
2000 int oldIndex = mAttributesByLayout[i];
2001 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2002 attributes[i] = oldTranslatedAttributes[oldIndex];
2003 }
2004}
2005
Brandon Jonesc9610c52014-08-25 17:02:59 -07002006}