blob: 50308061a23133b00a8bf610e9f7871067f26203 [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
11#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050012#include "libANGLE/Framebuffer.h"
13#include "libANGLE/FramebufferAttachment.h"
14#include "libANGLE/Program.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000015#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050017#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/renderer/d3d/RendererD3D.h"
19#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050020#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050022
Brandon Jonesc9610c52014-08-25 17:02:59 -070023namespace rx
24{
25
Brandon Joneseb994362014-09-24 10:27:28 -070026namespace
27{
28
Brandon Jones1a8a7e32014-10-01 12:49:30 -070029GLenum GetTextureType(GLenum samplerType)
30{
31 switch (samplerType)
32 {
33 case GL_SAMPLER_2D:
34 case GL_INT_SAMPLER_2D:
35 case GL_UNSIGNED_INT_SAMPLER_2D:
36 case GL_SAMPLER_2D_SHADOW:
37 return GL_TEXTURE_2D;
38 case GL_SAMPLER_3D:
39 case GL_INT_SAMPLER_3D:
40 case GL_UNSIGNED_INT_SAMPLER_3D:
41 return GL_TEXTURE_3D;
42 case GL_SAMPLER_CUBE:
43 case GL_SAMPLER_CUBE_SHADOW:
44 return GL_TEXTURE_CUBE_MAP;
45 case GL_INT_SAMPLER_CUBE:
46 case GL_UNSIGNED_INT_SAMPLER_CUBE:
47 return GL_TEXTURE_CUBE_MAP;
48 case GL_SAMPLER_2D_ARRAY:
49 case GL_INT_SAMPLER_2D_ARRAY:
50 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
51 case GL_SAMPLER_2D_ARRAY_SHADOW:
52 return GL_TEXTURE_2D_ARRAY;
53 default: UNREACHABLE();
54 }
55
56 return GL_TEXTURE_2D;
57}
58
Brandon Joneseb994362014-09-24 10:27:28 -070059void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS])
60{
61 size_t layoutIndex = 0;
62 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
63 {
64 ASSERT(layoutIndex < gl::MAX_VERTEX_ATTRIBS);
65
66 const sh::Attribute &shaderAttr = shaderAttributes[attributeIndex];
67
68 if (shaderAttr.type != GL_NONE)
69 {
70 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
71
72 for (size_t rowIndex = 0; static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); rowIndex++, layoutIndex++)
73 {
74 gl::VertexFormat *defaultFormat = &inputLayout[layoutIndex];
75
76 defaultFormat->mType = gl::VariableComponentType(transposedType);
77 defaultFormat->mNormalized = false;
78 defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool
79 defaultFormat->mComponents = gl::VariableColumnCount(transposedType);
80 }
81 }
82 }
83}
84
85std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
86{
Jamie Madillb4463142014-12-19 14:56:54 -050087 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070088
Jamie Madillb4463142014-12-19 14:56:54 -050089 if (!shaderOutputVars.empty())
90 {
91 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
92 }
Brandon Joneseb994362014-09-24 10:27:28 -070093
94 return defaultPixelOutput;
95}
96
Brandon Jones1a8a7e32014-10-01 12:49:30 -070097bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
98{
99 return var.isRowMajorLayout;
100}
101
102bool IsRowMajorLayout(const sh::ShaderVariable &var)
103{
104 return false;
105}
106
Jamie Madill437d2662014-12-05 14:23:35 -0500107struct AttributeSorter
108{
109 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -0500110 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -0500111 {
112 }
113
114 bool operator()(int a, int b)
115 {
Jamie Madill80d934b2015-02-19 10:16:12 -0500116 int indexA = (*originalIndices)[a];
117 int indexB = (*originalIndices)[b];
118
119 if (indexA == -1) return false;
120 if (indexB == -1) return true;
121 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -0500122 }
123
Jamie Madill80d934b2015-02-19 10:16:12 -0500124 const ProgramImpl::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500125};
126
Brandon Joneseb994362014-09-24 10:27:28 -0700127}
128
129ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
130 const GLenum signature[],
Geoff Lang359ef262015-01-05 14:42:29 -0500131 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700132 : mShaderExecutable(shaderExecutable)
133{
134 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
135 {
136 mInputs[attributeIndex] = inputLayout[attributeIndex];
137 mSignature[attributeIndex] = signature[attributeIndex];
138 }
139}
140
141ProgramD3D::VertexExecutable::~VertexExecutable()
142{
143 SafeDelete(mShaderExecutable);
144}
145
146bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
147{
148 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
149 {
150 if (mSignature[attributeIndex] != signature[attributeIndex])
151 {
152 return false;
153 }
154 }
155
156 return true;
157}
158
Geoff Lang359ef262015-01-05 14:42:29 -0500159ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700160 : mOutputSignature(outputSignature),
161 mShaderExecutable(shaderExecutable)
162{
163}
164
165ProgramD3D::PixelExecutable::~PixelExecutable()
166{
167 SafeDelete(mShaderExecutable);
168}
169
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700170ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
171{
172}
173
Geoff Lang7dd2e102014-11-10 15:19:26 -0500174unsigned int ProgramD3D::mCurrentSerial = 1;
175
Jamie Madill93e13fb2014-11-06 15:27:25 -0500176ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700177 : ProgramImpl(),
178 mRenderer(renderer),
179 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700180 mGeometryExecutable(NULL),
181 mVertexWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
182 mPixelWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
Brandon Jones44151a92014-09-10 11:32:25 -0700183 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700184 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700185 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700186 mUsedVertexSamplerRange(0),
187 mUsedPixelSamplerRange(0),
188 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500189 mShaderVersion(100),
190 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700191{
Brandon Joneseb994362014-09-24 10:27:28 -0700192 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700193}
194
195ProgramD3D::~ProgramD3D()
196{
197 reset();
198 SafeDelete(mDynamicHLSL);
199}
200
Brandon Jones44151a92014-09-10 11:32:25 -0700201bool ProgramD3D::usesPointSpriteEmulation() const
202{
203 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
204}
205
206bool ProgramD3D::usesGeometryShader() const
207{
Cooper Partine6664f02015-01-09 16:22:24 -0800208 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
209}
210
211bool ProgramD3D::usesInstancedPointSpriteEmulation() const
212{
213 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700214}
215
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700216GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
217{
218 GLint logicalTextureUnit = -1;
219
220 switch (type)
221 {
222 case gl::SAMPLER_PIXEL:
223 ASSERT(samplerIndex < caps.maxTextureImageUnits);
224 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
225 {
226 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
227 }
228 break;
229 case gl::SAMPLER_VERTEX:
230 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
231 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
232 {
233 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
234 }
235 break;
236 default: UNREACHABLE();
237 }
238
239 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
240 {
241 return logicalTextureUnit;
242 }
243
244 return -1;
245}
246
247// Returns the texture type for a given Direct3D 9 sampler type and
248// index (0-15 for the pixel shader and 0-3 for the vertex shader).
249GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
250{
251 switch (type)
252 {
253 case gl::SAMPLER_PIXEL:
254 ASSERT(samplerIndex < mSamplersPS.size());
255 ASSERT(mSamplersPS[samplerIndex].active);
256 return mSamplersPS[samplerIndex].textureType;
257 case gl::SAMPLER_VERTEX:
258 ASSERT(samplerIndex < mSamplersVS.size());
259 ASSERT(mSamplersVS[samplerIndex].active);
260 return mSamplersVS[samplerIndex].textureType;
261 default: UNREACHABLE();
262 }
263
264 return GL_TEXTURE_2D;
265}
266
267GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
268{
269 switch (type)
270 {
271 case gl::SAMPLER_PIXEL:
272 return mUsedPixelSamplerRange;
273 case gl::SAMPLER_VERTEX:
274 return mUsedVertexSamplerRange;
275 default:
276 UNREACHABLE();
277 return 0;
278 }
279}
280
281void ProgramD3D::updateSamplerMapping()
282{
283 if (!mDirtySamplerMapping)
284 {
285 return;
286 }
287
288 mDirtySamplerMapping = false;
289
290 // Retrieve sampler uniform values
291 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
292 {
293 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
294
295 if (targetUniform->dirty)
296 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500297 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700298 {
299 int count = targetUniform->elementCount();
300 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
301
302 if (targetUniform->isReferencedByFragmentShader())
303 {
304 unsigned int firstIndex = targetUniform->psRegisterIndex;
305
306 for (int i = 0; i < count; i++)
307 {
308 unsigned int samplerIndex = firstIndex + i;
309
310 if (samplerIndex < mSamplersPS.size())
311 {
312 ASSERT(mSamplersPS[samplerIndex].active);
313 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
314 }
315 }
316 }
317
318 if (targetUniform->isReferencedByVertexShader())
319 {
320 unsigned int firstIndex = targetUniform->vsRegisterIndex;
321
322 for (int i = 0; i < count; i++)
323 {
324 unsigned int samplerIndex = firstIndex + i;
325
326 if (samplerIndex < mSamplersVS.size())
327 {
328 ASSERT(mSamplersVS[samplerIndex].active);
329 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
330 }
331 }
332 }
333 }
334 }
335 }
336}
337
338bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
339{
340 // if any two active samplers in a program are of different types, but refer to the same
341 // texture image unit, and this is the current program, then ValidateProgram will fail, and
342 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
343 updateSamplerMapping();
344
345 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
346
347 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
348 {
349 if (mSamplersPS[i].active)
350 {
351 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
352
353 if (unit >= textureUnitTypes.size())
354 {
355 if (infoLog)
356 {
357 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
358 }
359
360 return false;
361 }
362
363 if (textureUnitTypes[unit] != GL_NONE)
364 {
365 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
366 {
367 if (infoLog)
368 {
369 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
370 }
371
372 return false;
373 }
374 }
375 else
376 {
377 textureUnitTypes[unit] = mSamplersPS[i].textureType;
378 }
379 }
380 }
381
382 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
383 {
384 if (mSamplersVS[i].active)
385 {
386 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
387
388 if (unit >= textureUnitTypes.size())
389 {
390 if (infoLog)
391 {
392 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
393 }
394
395 return false;
396 }
397
398 if (textureUnitTypes[unit] != GL_NONE)
399 {
400 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
401 {
402 if (infoLog)
403 {
404 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
405 }
406
407 return false;
408 }
409 }
410 else
411 {
412 textureUnitTypes[unit] = mSamplersVS[i].textureType;
413 }
414 }
415 }
416
417 return true;
418}
419
Geoff Lang7dd2e102014-11-10 15:19:26 -0500420LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700421{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500422 int compileFlags = stream->readInt<int>();
423 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
424 {
425 infoLog.append("Mismatched compilation flags.");
426 return LinkResult(false, gl::Error(GL_NO_ERROR));
427 }
428
Brandon Jones44151a92014-09-10 11:32:25 -0700429 stream->readInt(&mShaderVersion);
430
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700431 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
432 for (unsigned int i = 0; i < psSamplerCount; ++i)
433 {
434 Sampler sampler;
435 stream->readBool(&sampler.active);
436 stream->readInt(&sampler.logicalTextureUnit);
437 stream->readInt(&sampler.textureType);
438 mSamplersPS.push_back(sampler);
439 }
440 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
441 for (unsigned int i = 0; i < vsSamplerCount; ++i)
442 {
443 Sampler sampler;
444 stream->readBool(&sampler.active);
445 stream->readInt(&sampler.logicalTextureUnit);
446 stream->readInt(&sampler.textureType);
447 mSamplersVS.push_back(sampler);
448 }
449
450 stream->readInt(&mUsedVertexSamplerRange);
451 stream->readInt(&mUsedPixelSamplerRange);
452
453 const unsigned int uniformCount = stream->readInt<unsigned int>();
454 if (stream->error())
455 {
456 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500457 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700458 }
459
460 mUniforms.resize(uniformCount);
461 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
462 {
463 GLenum type = stream->readInt<GLenum>();
464 GLenum precision = stream->readInt<GLenum>();
465 std::string name = stream->readString();
466 unsigned int arraySize = stream->readInt<unsigned int>();
467 int blockIndex = stream->readInt<int>();
468
469 int offset = stream->readInt<int>();
470 int arrayStride = stream->readInt<int>();
471 int matrixStride = stream->readInt<int>();
472 bool isRowMajorMatrix = stream->readBool();
473
474 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
475
476 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
477
478 stream->readInt(&uniform->psRegisterIndex);
479 stream->readInt(&uniform->vsRegisterIndex);
480 stream->readInt(&uniform->registerCount);
481 stream->readInt(&uniform->registerElement);
482
483 mUniforms[uniformIndex] = uniform;
484 }
485
486 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
487 if (stream->error())
488 {
489 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500490 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700491 }
492
493 mUniformIndex.resize(uniformIndexCount);
494 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
495 {
496 stream->readString(&mUniformIndex[uniformIndexIndex].name);
497 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
498 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
499 }
500
501 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
502 if (stream->error())
503 {
504 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500505 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700506 }
507
508 mUniformBlocks.resize(uniformBlockCount);
509 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
510 {
511 std::string name = stream->readString();
512 unsigned int elementIndex = stream->readInt<unsigned int>();
513 unsigned int dataSize = stream->readInt<unsigned int>();
514
515 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
516
517 stream->readInt(&uniformBlock->psRegisterIndex);
518 stream->readInt(&uniformBlock->vsRegisterIndex);
519
520 unsigned int numMembers = stream->readInt<unsigned int>();
521 uniformBlock->memberUniformIndexes.resize(numMembers);
522 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
523 {
524 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
525 }
526
527 mUniformBlocks[uniformBlockIndex] = uniformBlock;
528 }
529
Brandon Joneseb994362014-09-24 10:27:28 -0700530 stream->readInt(&mTransformFeedbackBufferMode);
531 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
532 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
533 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
534 {
535 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
536
537 stream->readString(&varying.name);
538 stream->readInt(&varying.type);
539 stream->readInt(&varying.size);
540 stream->readString(&varying.semanticName);
541 stream->readInt(&varying.semanticIndex);
542 stream->readInt(&varying.semanticIndexCount);
543 }
544
Brandon Jones22502d52014-08-29 16:58:36 -0700545 stream->readString(&mVertexHLSL);
546 stream->readInt(&mVertexWorkarounds);
547 stream->readString(&mPixelHLSL);
548 stream->readInt(&mPixelWorkarounds);
549 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700550 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700551
552 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
553 mPixelShaderKey.resize(pixelShaderKeySize);
554 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
555 {
556 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
557 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
558 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
559 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
560 }
561
Brandon Joneseb994362014-09-24 10:27:28 -0700562 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
563
564 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
565 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
566 {
567 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
568
569 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
570 {
571 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
572 stream->readInt(&vertexInput->mType);
573 stream->readInt(&vertexInput->mNormalized);
574 stream->readInt(&vertexInput->mComponents);
575 stream->readBool(&vertexInput->mPureInteger);
576 }
577
578 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
579 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400580
Geoff Lang359ef262015-01-05 14:42:29 -0500581 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400582 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
583 SHADER_VERTEX,
584 mTransformFeedbackLinkedVaryings,
585 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
586 &shaderExecutable);
587 if (error.isError())
588 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500589 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400590 }
591
Brandon Joneseb994362014-09-24 10:27:28 -0700592 if (!shaderExecutable)
593 {
594 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500595 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700596 }
597
598 // generated converted input layout
599 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
600 getInputLayoutSignature(inputLayout, signature);
601
602 // add new binary
603 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
604
605 stream->skip(vertexShaderSize);
606 }
607
608 const size_t pixelShaderCount = stream->readInt<unsigned int>();
609 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
610 {
611 const size_t outputCount = stream->readInt<unsigned int>();
612 std::vector<GLenum> outputs(outputCount);
613 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
614 {
615 stream->readInt(&outputs[outputIndex]);
616 }
617
618 const size_t pixelShaderSize = stream->readInt<unsigned int>();
619 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500620 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400621 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
622 mTransformFeedbackLinkedVaryings,
623 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
624 &shaderExecutable);
625 if (error.isError())
626 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500627 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400628 }
Brandon Joneseb994362014-09-24 10:27:28 -0700629
630 if (!shaderExecutable)
631 {
632 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500633 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700634 }
635
636 // add new binary
637 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
638
639 stream->skip(pixelShaderSize);
640 }
641
642 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
643
644 if (geometryShaderSize > 0)
645 {
646 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400647 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
648 mTransformFeedbackLinkedVaryings,
649 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
650 &mGeometryExecutable);
651 if (error.isError())
652 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500653 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400654 }
Brandon Joneseb994362014-09-24 10:27:28 -0700655
656 if (!mGeometryExecutable)
657 {
658 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500659 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700660 }
661 stream->skip(geometryShaderSize);
662 }
663
Brandon Jones18bd4102014-09-22 14:21:44 -0700664 GUID binaryIdentifier = {0};
665 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
666
667 GUID identifier = mRenderer->getAdapterIdentifier();
668 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
669 {
670 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500671 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700672 }
673
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700674 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500675 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700676
Geoff Lang7dd2e102014-11-10 15:19:26 -0500677 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700678}
679
Geoff Langb543aff2014-09-30 14:52:54 -0400680gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700681{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500682 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
683
Brandon Jones44151a92014-09-10 11:32:25 -0700684 stream->writeInt(mShaderVersion);
685
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700686 stream->writeInt(mSamplersPS.size());
687 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
688 {
689 stream->writeInt(mSamplersPS[i].active);
690 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
691 stream->writeInt(mSamplersPS[i].textureType);
692 }
693
694 stream->writeInt(mSamplersVS.size());
695 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
696 {
697 stream->writeInt(mSamplersVS[i].active);
698 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
699 stream->writeInt(mSamplersVS[i].textureType);
700 }
701
702 stream->writeInt(mUsedVertexSamplerRange);
703 stream->writeInt(mUsedPixelSamplerRange);
704
705 stream->writeInt(mUniforms.size());
706 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
707 {
708 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
709
710 stream->writeInt(uniform.type);
711 stream->writeInt(uniform.precision);
712 stream->writeString(uniform.name);
713 stream->writeInt(uniform.arraySize);
714 stream->writeInt(uniform.blockIndex);
715
716 stream->writeInt(uniform.blockInfo.offset);
717 stream->writeInt(uniform.blockInfo.arrayStride);
718 stream->writeInt(uniform.blockInfo.matrixStride);
719 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
720
721 stream->writeInt(uniform.psRegisterIndex);
722 stream->writeInt(uniform.vsRegisterIndex);
723 stream->writeInt(uniform.registerCount);
724 stream->writeInt(uniform.registerElement);
725 }
726
727 stream->writeInt(mUniformIndex.size());
728 for (size_t i = 0; i < mUniformIndex.size(); ++i)
729 {
730 stream->writeString(mUniformIndex[i].name);
731 stream->writeInt(mUniformIndex[i].element);
732 stream->writeInt(mUniformIndex[i].index);
733 }
734
735 stream->writeInt(mUniformBlocks.size());
736 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
737 {
738 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
739
740 stream->writeString(uniformBlock.name);
741 stream->writeInt(uniformBlock.elementIndex);
742 stream->writeInt(uniformBlock.dataSize);
743
744 stream->writeInt(uniformBlock.memberUniformIndexes.size());
745 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
746 {
747 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
748 }
749
750 stream->writeInt(uniformBlock.psRegisterIndex);
751 stream->writeInt(uniformBlock.vsRegisterIndex);
752 }
753
Brandon Joneseb994362014-09-24 10:27:28 -0700754 stream->writeInt(mTransformFeedbackBufferMode);
755 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
756 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
757 {
758 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
759
760 stream->writeString(varying.name);
761 stream->writeInt(varying.type);
762 stream->writeInt(varying.size);
763 stream->writeString(varying.semanticName);
764 stream->writeInt(varying.semanticIndex);
765 stream->writeInt(varying.semanticIndexCount);
766 }
767
Brandon Jones22502d52014-08-29 16:58:36 -0700768 stream->writeString(mVertexHLSL);
769 stream->writeInt(mVertexWorkarounds);
770 stream->writeString(mPixelHLSL);
771 stream->writeInt(mPixelWorkarounds);
772 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700773 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700774
Brandon Joneseb994362014-09-24 10:27:28 -0700775 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700776 stream->writeInt(pixelShaderKey.size());
777 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
778 {
Brandon Joneseb994362014-09-24 10:27:28 -0700779 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700780 stream->writeInt(variable.type);
781 stream->writeString(variable.name);
782 stream->writeString(variable.source);
783 stream->writeInt(variable.outputIndex);
784 }
785
Brandon Joneseb994362014-09-24 10:27:28 -0700786 stream->writeInt(mVertexExecutables.size());
787 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
788 {
789 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
790
791 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
792 {
793 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
794 stream->writeInt(vertexInput.mType);
795 stream->writeInt(vertexInput.mNormalized);
796 stream->writeInt(vertexInput.mComponents);
797 stream->writeInt(vertexInput.mPureInteger);
798 }
799
800 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
801 stream->writeInt(vertexShaderSize);
802
803 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
804 stream->writeBytes(vertexBlob, vertexShaderSize);
805 }
806
807 stream->writeInt(mPixelExecutables.size());
808 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
809 {
810 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
811
812 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
813 stream->writeInt(outputs.size());
814 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
815 {
816 stream->writeInt(outputs[outputIndex]);
817 }
818
819 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
820 stream->writeInt(pixelShaderSize);
821
822 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
823 stream->writeBytes(pixelBlob, pixelShaderSize);
824 }
825
826 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
827 stream->writeInt(geometryShaderSize);
828
829 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
830 {
831 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
832 stream->writeBytes(geometryBlob, geometryShaderSize);
833 }
834
Brandon Jones18bd4102014-09-22 14:21:44 -0700835 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400836 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700837
Geoff Langb543aff2014-09-30 14:52:54 -0400838 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700839}
840
Geoff Lang359ef262015-01-05 14:42:29 -0500841gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700842{
Brandon Joneseb994362014-09-24 10:27:28 -0700843 std::vector<GLenum> outputs;
844
Jamie Madill85a18042015-03-05 15:41:41 -0500845 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
846 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(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,
Geoff Lang359ef262015-01-05 14:42:29 -0500866 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500867 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 Lang359ef262015-01-05 14:42:29 -0500882 ShaderExecutableD3D *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],
Geoff Lang359ef262015-01-05 14:42:29 -0500912 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500913 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 Lang359ef262015-01-05 14:42:29 -0500931 ShaderExecutableD3D *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
Jamie Madill6df9b372015-02-18 21:28:19 +0000966 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
967 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
968 ShaderExecutableD3D *defaultVertexExecutable = NULL;
969 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
970 if (error.isError())
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800971 {
Jamie Madill6df9b372015-02-18 21:28:19 +0000972 return LinkResult(false, error);
973 }
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800974
Brandon Joneseb994362014-09-24 10:27:28 -0700975 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500976 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill6df9b372015-02-18 21:28:19 +0000977 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
Jamie Madill2857f482015-02-09 15:35:29 -05001440 // Advance the uniform offset, to track registers allocation for structs
1441 sh::BlockMemberInfo blockInfo = encoder->encodeType(uniform.type, uniform.arraySize, false);
1442
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001443 if (!linkedUniform)
1444 {
1445 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001446 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001447 ASSERT(linkedUniform);
Jamie Madill2857f482015-02-09 15:35:29 -05001448 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001449 mUniforms.push_back(linkedUniform);
1450 }
1451
Geoff Lang492a7e42014-11-05 13:27:06 -05001452 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001453 {
Jamie Madill2857f482015-02-09 15:35:29 -05001454 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001455 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001456 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001457 {
Jamie Madill2857f482015-02-09 15:35:29 -05001458 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001459 }
1460 else UNREACHABLE();
1461
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001462 // Arrays are treated as aggregate types
1463 if (uniform.isArray())
1464 {
1465 encoder->exitAggregateType();
1466 }
1467 }
1468}
1469
1470template <typename T>
1471static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1472{
1473 ASSERT(dest != NULL);
1474 ASSERT(dirtyFlag != NULL);
1475
1476 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1477 *dest = source;
1478}
1479
1480template <typename T>
1481void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1482{
1483 const int components = gl::VariableComponentCount(targetUniformType);
1484 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1485
1486 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1487
1488 int elementCount = targetUniform->elementCount();
1489
1490 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1491
1492 if (targetUniform->type == targetUniformType)
1493 {
1494 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1495
1496 for (int i = 0; i < count; i++)
1497 {
1498 T *dest = target + (i * 4);
1499 const T *source = v + (i * components);
1500
1501 for (int c = 0; c < components; c++)
1502 {
1503 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1504 }
1505 for (int c = components; c < 4; c++)
1506 {
1507 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1508 }
1509 }
1510 }
1511 else if (targetUniform->type == targetBoolType)
1512 {
1513 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1514
1515 for (int i = 0; i < count; i++)
1516 {
1517 GLint *dest = boolParams + (i * 4);
1518 const T *source = v + (i * components);
1519
1520 for (int c = 0; c < components; c++)
1521 {
1522 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1523 }
1524 for (int c = components; c < 4; c++)
1525 {
1526 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1527 }
1528 }
1529 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001530 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001531 {
1532 ASSERT(targetUniformType == GL_INT);
1533
1534 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1535
1536 bool wasDirty = targetUniform->dirty;
1537
1538 for (int i = 0; i < count; i++)
1539 {
1540 GLint *dest = target + (i * 4);
1541 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1542
1543 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1544 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1545 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1546 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1547 }
1548
1549 if (!wasDirty && targetUniform->dirty)
1550 {
1551 mDirtySamplerMapping = true;
1552 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001553 }
1554 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001555}
Brandon Jones18bd4102014-09-22 14:21:44 -07001556
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001557template<typename T>
1558bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1559{
1560 bool dirty = false;
1561 int copyWidth = std::min(targetHeight, srcWidth);
1562 int copyHeight = std::min(targetWidth, srcHeight);
1563
1564 for (int x = 0; x < copyWidth; x++)
1565 {
1566 for (int y = 0; y < copyHeight; y++)
1567 {
1568 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1569 }
1570 }
1571 // clear unfilled right side
1572 for (int y = 0; y < copyWidth; y++)
1573 {
1574 for (int x = copyHeight; x < targetWidth; x++)
1575 {
1576 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1577 }
1578 }
1579 // clear unfilled bottom.
1580 for (int y = copyWidth; y < targetHeight; y++)
1581 {
1582 for (int x = 0; x < targetWidth; x++)
1583 {
1584 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1585 }
1586 }
1587
1588 return dirty;
1589}
1590
1591template<typename T>
1592bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1593{
1594 bool dirty = false;
1595 int copyWidth = std::min(targetWidth, srcWidth);
1596 int copyHeight = std::min(targetHeight, srcHeight);
1597
1598 for (int y = 0; y < copyHeight; y++)
1599 {
1600 for (int x = 0; x < copyWidth; x++)
1601 {
1602 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1603 }
1604 }
1605 // clear unfilled right side
1606 for (int y = 0; y < copyHeight; y++)
1607 {
1608 for (int x = copyWidth; x < targetWidth; x++)
1609 {
1610 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1611 }
1612 }
1613 // clear unfilled bottom.
1614 for (int y = copyHeight; y < targetHeight; y++)
1615 {
1616 for (int x = 0; x < targetWidth; x++)
1617 {
1618 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1619 }
1620 }
1621
1622 return dirty;
1623}
1624
1625template <int cols, int rows>
1626void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1627{
1628 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1629
1630 int elementCount = targetUniform->elementCount();
1631
1632 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1633 const unsigned int targetMatrixStride = (4 * rows);
1634 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1635
1636 for (int i = 0; i < count; i++)
1637 {
1638 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1639 if (transpose == GL_FALSE)
1640 {
1641 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1642 }
1643 else
1644 {
1645 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1646 }
1647 target += targetMatrixStride;
1648 value += cols * rows;
1649 }
1650}
1651
1652template <typename T>
1653void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1654{
1655 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1656
1657 if (gl::IsMatrixType(targetUniform->type))
1658 {
1659 const int rows = gl::VariableRowCount(targetUniform->type);
1660 const int cols = gl::VariableColumnCount(targetUniform->type);
1661 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1662 }
1663 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1664 {
1665 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1666 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1667 size * sizeof(T));
1668 }
1669 else
1670 {
1671 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1672 switch (gl::VariableComponentType(targetUniform->type))
1673 {
1674 case GL_BOOL:
1675 {
1676 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1677
1678 for (unsigned int i = 0; i < size; i++)
1679 {
1680 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1681 }
1682 }
1683 break;
1684
1685 case GL_FLOAT:
1686 {
1687 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1688
1689 for (unsigned int i = 0; i < size; i++)
1690 {
1691 params[i] = static_cast<T>(floatParams[i]);
1692 }
1693 }
1694 break;
1695
1696 case GL_INT:
1697 {
1698 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1699
1700 for (unsigned int i = 0; i < size; i++)
1701 {
1702 params[i] = static_cast<T>(intParams[i]);
1703 }
1704 }
1705 break;
1706
1707 case GL_UNSIGNED_INT:
1708 {
1709 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1710
1711 for (unsigned int i = 0; i < size; i++)
1712 {
1713 params[i] = static_cast<T>(uintParams[i]);
1714 }
1715 }
1716 break;
1717
1718 default: UNREACHABLE();
1719 }
1720 }
1721}
1722
1723template <typename VarT>
1724void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1725 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1726 bool inRowMajorLayout)
1727{
1728 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1729 {
1730 const VarT &field = fields[uniformIndex];
1731 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1732
1733 if (field.isStruct())
1734 {
1735 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1736
1737 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1738 {
1739 encoder->enterAggregateType();
1740
1741 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1742 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1743
1744 encoder->exitAggregateType();
1745 }
1746 }
1747 else
1748 {
1749 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1750
1751 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1752
1753 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1754 blockIndex, memberInfo);
1755
1756 // add to uniform list, but not index, since uniform block uniforms have no location
1757 blockUniformIndexes->push_back(mUniforms.size());
1758 mUniforms.push_back(newUniform);
1759 }
1760 }
1761}
1762
1763bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1764 const gl::Caps &caps)
1765{
Jamie Madill30d6c252014-11-13 10:03:33 -05001766 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001767
1768 // create uniform block entries if they do not exist
1769 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1770 {
1771 std::vector<unsigned int> blockUniformIndexes;
1772 const unsigned int blockIndex = mUniformBlocks.size();
1773
1774 // define member uniforms
1775 sh::BlockLayoutEncoder *encoder = NULL;
1776
1777 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1778 {
1779 encoder = new sh::Std140BlockEncoder;
1780 }
1781 else
1782 {
1783 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1784 }
1785 ASSERT(encoder);
1786
1787 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1788
1789 size_t dataSize = encoder->getBlockSize();
1790
1791 // create all the uniform blocks
1792 if (interfaceBlock.arraySize > 0)
1793 {
1794 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1795 {
1796 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1797 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1798 mUniformBlocks.push_back(newUniformBlock);
1799 }
1800 }
1801 else
1802 {
1803 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1804 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1805 mUniformBlocks.push_back(newUniformBlock);
1806 }
1807 }
1808
1809 if (interfaceBlock.staticUse)
1810 {
1811 // Assign registers to the uniform blocks
1812 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1813 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1814 ASSERT(blockIndex != GL_INVALID_INDEX);
1815 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1816
1817 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1818
1819 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1820 {
1821 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1822 ASSERT(uniformBlock->name == interfaceBlock.name);
1823
1824 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1825 interfaceBlockRegister + uniformBlockElement, caps))
1826 {
1827 return false;
1828 }
1829 }
1830 }
1831
1832 return true;
1833}
1834
1835bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1836 GLenum samplerType,
1837 unsigned int samplerCount,
1838 std::vector<Sampler> &outSamplers,
1839 GLuint *outUsedRange)
1840{
1841 unsigned int samplerIndex = startSamplerIndex;
1842
1843 do
1844 {
1845 if (samplerIndex < outSamplers.size())
1846 {
1847 Sampler& sampler = outSamplers[samplerIndex];
1848 sampler.active = true;
1849 sampler.textureType = GetTextureType(samplerType);
1850 sampler.logicalTextureUnit = 0;
1851 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1852 }
1853 else
1854 {
1855 return false;
1856 }
1857
1858 samplerIndex++;
1859 } while (samplerIndex < startSamplerIndex + samplerCount);
1860
1861 return true;
1862}
1863
1864bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1865{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001866 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001867 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1868
1869 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1870 {
1871 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1872 &mUsedVertexSamplerRange))
1873 {
1874 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1875 mSamplersVS.size());
1876 return false;
1877 }
1878
1879 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1880 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1881 {
1882 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1883 caps.maxVertexUniformVectors);
1884 return false;
1885 }
1886 }
1887
1888 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1889 {
1890 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1891 &mUsedPixelSamplerRange))
1892 {
1893 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1894 mSamplersPS.size());
1895 return false;
1896 }
1897
1898 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1899 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1900 {
1901 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1902 caps.maxFragmentUniformVectors);
1903 return false;
1904 }
1905 }
1906
1907 return true;
1908}
1909
1910bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1911{
1912 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1913 {
1914 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1915
Geoff Lang2ec386b2014-12-03 14:44:38 -05001916 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001917 {
1918 if (!indexSamplerUniform(uniform, infoLog, caps))
1919 {
1920 return false;
1921 }
1922 }
1923
1924 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1925 {
1926 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1927 }
1928 }
1929
1930 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001931}
1932
Brandon Jonesc9610c52014-08-25 17:02:59 -07001933void ProgramD3D::reset()
1934{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001935 ProgramImpl::reset();
1936
Brandon Joneseb994362014-09-24 10:27:28 -07001937 SafeDeleteContainer(mVertexExecutables);
1938 SafeDeleteContainer(mPixelExecutables);
1939 SafeDelete(mGeometryExecutable);
1940
1941 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001942
Brandon Jones22502d52014-08-29 16:58:36 -07001943 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001944 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001945 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001946
1947 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001948 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001949 mUsesFragDepth = false;
1950 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001951 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001952
Brandon Jonesc9610c52014-08-25 17:02:59 -07001953 SafeDelete(mVertexUniformStorage);
1954 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001955
1956 mSamplersPS.clear();
1957 mSamplersVS.clear();
1958
1959 mUsedVertexSamplerRange = 0;
1960 mUsedPixelSamplerRange = 0;
1961 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001962
1963 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001964}
1965
Geoff Lang7dd2e102014-11-10 15:19:26 -05001966unsigned int ProgramD3D::getSerial() const
1967{
1968 return mSerial;
1969}
1970
1971unsigned int ProgramD3D::issueSerial()
1972{
1973 return mCurrentSerial++;
1974}
1975
Jamie Madill437d2662014-12-05 14:23:35 -05001976void ProgramD3D::initAttributesByLayout()
1977{
1978 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1979 {
1980 mAttributesByLayout[i] = i;
1981 }
1982
1983 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
1984}
1985
1986void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
1987 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
1988{
1989 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
1990
1991 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1992 {
1993 oldTranslatedAttributes[i] = attributes[i];
1994 }
1995
1996 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1997 {
1998 int oldIndex = mAttributesByLayout[i];
1999 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2000 attributes[i] = oldTranslatedAttributes[oldIndex];
2001 }
2002}
2003
Brandon Jonesc9610c52014-08-25 17:02:59 -07002004}