blob: e22db3b69b4d6d588d592f82d2d7fb600e0b06b8 [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),
Brandon Jones44151a92014-09-10 11:32:25 -0700181 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700182 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700183 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700184 mUsedVertexSamplerRange(0),
185 mUsedPixelSamplerRange(0),
186 mDirtySamplerMapping(true),
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400187 mTextureUnitTypesCache(renderer->getRendererCaps().maxCombinedTextureImageUnits),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500188 mShaderVersion(100),
189 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700190{
Brandon Joneseb994362014-09-24 10:27:28 -0700191 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700192}
193
194ProgramD3D::~ProgramD3D()
195{
196 reset();
197 SafeDelete(mDynamicHLSL);
198}
199
Brandon Jones44151a92014-09-10 11:32:25 -0700200bool ProgramD3D::usesPointSpriteEmulation() const
201{
202 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
203}
204
205bool ProgramD3D::usesGeometryShader() const
206{
Cooper Partine6664f02015-01-09 16:22:24 -0800207 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
208}
209
210bool ProgramD3D::usesInstancedPointSpriteEmulation() const
211{
212 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700213}
214
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700215GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
216{
217 GLint logicalTextureUnit = -1;
218
219 switch (type)
220 {
221 case gl::SAMPLER_PIXEL:
222 ASSERT(samplerIndex < caps.maxTextureImageUnits);
223 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
224 {
225 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
226 }
227 break;
228 case gl::SAMPLER_VERTEX:
229 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
230 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
231 {
232 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
233 }
234 break;
235 default: UNREACHABLE();
236 }
237
238 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
239 {
240 return logicalTextureUnit;
241 }
242
243 return -1;
244}
245
246// Returns the texture type for a given Direct3D 9 sampler type and
247// index (0-15 for the pixel shader and 0-3 for the vertex shader).
248GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
249{
250 switch (type)
251 {
252 case gl::SAMPLER_PIXEL:
253 ASSERT(samplerIndex < mSamplersPS.size());
254 ASSERT(mSamplersPS[samplerIndex].active);
255 return mSamplersPS[samplerIndex].textureType;
256 case gl::SAMPLER_VERTEX:
257 ASSERT(samplerIndex < mSamplersVS.size());
258 ASSERT(mSamplersVS[samplerIndex].active);
259 return mSamplersVS[samplerIndex].textureType;
260 default: UNREACHABLE();
261 }
262
263 return GL_TEXTURE_2D;
264}
265
266GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
267{
268 switch (type)
269 {
270 case gl::SAMPLER_PIXEL:
271 return mUsedPixelSamplerRange;
272 case gl::SAMPLER_VERTEX:
273 return mUsedVertexSamplerRange;
274 default:
275 UNREACHABLE();
276 return 0;
277 }
278}
279
280void ProgramD3D::updateSamplerMapping()
281{
282 if (!mDirtySamplerMapping)
283 {
284 return;
285 }
286
287 mDirtySamplerMapping = false;
288
289 // Retrieve sampler uniform values
290 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
291 {
292 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
293
294 if (targetUniform->dirty)
295 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500296 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700297 {
298 int count = targetUniform->elementCount();
299 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
300
301 if (targetUniform->isReferencedByFragmentShader())
302 {
303 unsigned int firstIndex = targetUniform->psRegisterIndex;
304
305 for (int i = 0; i < count; i++)
306 {
307 unsigned int samplerIndex = firstIndex + i;
308
309 if (samplerIndex < mSamplersPS.size())
310 {
311 ASSERT(mSamplersPS[samplerIndex].active);
312 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
313 }
314 }
315 }
316
317 if (targetUniform->isReferencedByVertexShader())
318 {
319 unsigned int firstIndex = targetUniform->vsRegisterIndex;
320
321 for (int i = 0; i < count; i++)
322 {
323 unsigned int samplerIndex = firstIndex + i;
324
325 if (samplerIndex < mSamplersVS.size())
326 {
327 ASSERT(mSamplersVS[samplerIndex].active);
328 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
329 }
330 }
331 }
332 }
333 }
334 }
335}
336
337bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
338{
339 // if any two active samplers in a program are of different types, but refer to the same
340 // texture image unit, and this is the current program, then ValidateProgram will fail, and
341 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
342 updateSamplerMapping();
343
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400344 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700345
346 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
347 {
348 if (mSamplersPS[i].active)
349 {
350 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
351
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400352 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700353 {
354 if (infoLog)
355 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400356 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, caps.maxCombinedTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700357 }
358
359 return false;
360 }
361
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400362 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700363 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400364 if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700365 {
366 if (infoLog)
367 {
368 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
369 }
370
371 return false;
372 }
373 }
374 else
375 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400376 mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700377 }
378 }
379 }
380
381 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
382 {
383 if (mSamplersVS[i].active)
384 {
385 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
386
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400387 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700388 {
389 if (infoLog)
390 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400391 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, caps.maxCombinedTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700392 }
393
394 return false;
395 }
396
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400397 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700398 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400399 if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700400 {
401 if (infoLog)
402 {
403 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
404 }
405
406 return false;
407 }
408 }
409 else
410 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400411 mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700412 }
413 }
414 }
415
416 return true;
417}
418
Geoff Lang7dd2e102014-11-10 15:19:26 -0500419LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700420{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500421 int compileFlags = stream->readInt<int>();
422 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
423 {
424 infoLog.append("Mismatched compilation flags.");
425 return LinkResult(false, gl::Error(GL_NO_ERROR));
426 }
427
Brandon Jones44151a92014-09-10 11:32:25 -0700428 stream->readInt(&mShaderVersion);
429
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700430 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
431 for (unsigned int i = 0; i < psSamplerCount; ++i)
432 {
433 Sampler sampler;
434 stream->readBool(&sampler.active);
435 stream->readInt(&sampler.logicalTextureUnit);
436 stream->readInt(&sampler.textureType);
437 mSamplersPS.push_back(sampler);
438 }
439 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
440 for (unsigned int i = 0; i < vsSamplerCount; ++i)
441 {
442 Sampler sampler;
443 stream->readBool(&sampler.active);
444 stream->readInt(&sampler.logicalTextureUnit);
445 stream->readInt(&sampler.textureType);
446 mSamplersVS.push_back(sampler);
447 }
448
449 stream->readInt(&mUsedVertexSamplerRange);
450 stream->readInt(&mUsedPixelSamplerRange);
451
452 const unsigned int uniformCount = stream->readInt<unsigned int>();
453 if (stream->error())
454 {
455 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500456 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700457 }
458
459 mUniforms.resize(uniformCount);
460 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
461 {
462 GLenum type = stream->readInt<GLenum>();
463 GLenum precision = stream->readInt<GLenum>();
464 std::string name = stream->readString();
465 unsigned int arraySize = stream->readInt<unsigned int>();
466 int blockIndex = stream->readInt<int>();
467
468 int offset = stream->readInt<int>();
469 int arrayStride = stream->readInt<int>();
470 int matrixStride = stream->readInt<int>();
471 bool isRowMajorMatrix = stream->readBool();
472
473 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
474
475 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
476
477 stream->readInt(&uniform->psRegisterIndex);
478 stream->readInt(&uniform->vsRegisterIndex);
479 stream->readInt(&uniform->registerCount);
480 stream->readInt(&uniform->registerElement);
481
482 mUniforms[uniformIndex] = uniform;
483 }
484
485 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
486 if (stream->error())
487 {
488 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500489 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700490 }
491
492 mUniformIndex.resize(uniformIndexCount);
493 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
494 {
495 stream->readString(&mUniformIndex[uniformIndexIndex].name);
496 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
497 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
498 }
499
500 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
501 if (stream->error())
502 {
503 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500504 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700505 }
506
507 mUniformBlocks.resize(uniformBlockCount);
508 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
509 {
510 std::string name = stream->readString();
511 unsigned int elementIndex = stream->readInt<unsigned int>();
512 unsigned int dataSize = stream->readInt<unsigned int>();
513
514 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
515
516 stream->readInt(&uniformBlock->psRegisterIndex);
517 stream->readInt(&uniformBlock->vsRegisterIndex);
518
519 unsigned int numMembers = stream->readInt<unsigned int>();
520 uniformBlock->memberUniformIndexes.resize(numMembers);
521 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
522 {
523 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
524 }
525
526 mUniformBlocks[uniformBlockIndex] = uniformBlock;
527 }
528
Brandon Joneseb994362014-09-24 10:27:28 -0700529 stream->readInt(&mTransformFeedbackBufferMode);
530 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
531 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
532 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
533 {
534 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
535
536 stream->readString(&varying.name);
537 stream->readInt(&varying.type);
538 stream->readInt(&varying.size);
539 stream->readString(&varying.semanticName);
540 stream->readInt(&varying.semanticIndex);
541 stream->readInt(&varying.semanticIndexCount);
542 }
543
Brandon Jones22502d52014-08-29 16:58:36 -0700544 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530545 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700546 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530547 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700548 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700549 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700550
551 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
552 mPixelShaderKey.resize(pixelShaderKeySize);
553 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
554 {
555 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
556 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
557 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
558 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
559 }
560
Brandon Joneseb994362014-09-24 10:27:28 -0700561 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
562
563 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
564 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
565 {
566 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
567
568 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
569 {
570 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
571 stream->readInt(&vertexInput->mType);
572 stream->readInt(&vertexInput->mNormalized);
573 stream->readInt(&vertexInput->mComponents);
574 stream->readBool(&vertexInput->mPureInteger);
575 }
576
577 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
578 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400579
Geoff Lang359ef262015-01-05 14:42:29 -0500580 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400581 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
582 SHADER_VERTEX,
583 mTransformFeedbackLinkedVaryings,
584 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
585 &shaderExecutable);
586 if (error.isError())
587 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500588 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400589 }
590
Brandon Joneseb994362014-09-24 10:27:28 -0700591 if (!shaderExecutable)
592 {
593 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500594 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700595 }
596
597 // generated converted input layout
598 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
599 getInputLayoutSignature(inputLayout, signature);
600
601 // add new binary
602 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
603
604 stream->skip(vertexShaderSize);
605 }
606
607 const size_t pixelShaderCount = stream->readInt<unsigned int>();
608 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
609 {
610 const size_t outputCount = stream->readInt<unsigned int>();
611 std::vector<GLenum> outputs(outputCount);
612 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
613 {
614 stream->readInt(&outputs[outputIndex]);
615 }
616
617 const size_t pixelShaderSize = stream->readInt<unsigned int>();
618 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500619 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400620 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
621 mTransformFeedbackLinkedVaryings,
622 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
623 &shaderExecutable);
624 if (error.isError())
625 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500626 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400627 }
Brandon Joneseb994362014-09-24 10:27:28 -0700628
629 if (!shaderExecutable)
630 {
631 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500632 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700633 }
634
635 // add new binary
636 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
637
638 stream->skip(pixelShaderSize);
639 }
640
641 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
642
643 if (geometryShaderSize > 0)
644 {
645 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400646 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
647 mTransformFeedbackLinkedVaryings,
648 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
649 &mGeometryExecutable);
650 if (error.isError())
651 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500652 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400653 }
Brandon Joneseb994362014-09-24 10:27:28 -0700654
655 if (!mGeometryExecutable)
656 {
657 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500658 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700659 }
660 stream->skip(geometryShaderSize);
661 }
662
Brandon Jones18bd4102014-09-22 14:21:44 -0700663 GUID binaryIdentifier = {0};
664 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
665
666 GUID identifier = mRenderer->getAdapterIdentifier();
667 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
668 {
669 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500670 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700671 }
672
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700673 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500674 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700675
Geoff Lang7dd2e102014-11-10 15:19:26 -0500676 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700677}
678
Geoff Langb543aff2014-09-30 14:52:54 -0400679gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700680{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500681 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
682
Brandon Jones44151a92014-09-10 11:32:25 -0700683 stream->writeInt(mShaderVersion);
684
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685 stream->writeInt(mSamplersPS.size());
686 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
687 {
688 stream->writeInt(mSamplersPS[i].active);
689 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
690 stream->writeInt(mSamplersPS[i].textureType);
691 }
692
693 stream->writeInt(mSamplersVS.size());
694 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
695 {
696 stream->writeInt(mSamplersVS[i].active);
697 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
698 stream->writeInt(mSamplersVS[i].textureType);
699 }
700
701 stream->writeInt(mUsedVertexSamplerRange);
702 stream->writeInt(mUsedPixelSamplerRange);
703
704 stream->writeInt(mUniforms.size());
705 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
706 {
707 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
708
709 stream->writeInt(uniform.type);
710 stream->writeInt(uniform.precision);
711 stream->writeString(uniform.name);
712 stream->writeInt(uniform.arraySize);
713 stream->writeInt(uniform.blockIndex);
714
715 stream->writeInt(uniform.blockInfo.offset);
716 stream->writeInt(uniform.blockInfo.arrayStride);
717 stream->writeInt(uniform.blockInfo.matrixStride);
718 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
719
720 stream->writeInt(uniform.psRegisterIndex);
721 stream->writeInt(uniform.vsRegisterIndex);
722 stream->writeInt(uniform.registerCount);
723 stream->writeInt(uniform.registerElement);
724 }
725
726 stream->writeInt(mUniformIndex.size());
727 for (size_t i = 0; i < mUniformIndex.size(); ++i)
728 {
729 stream->writeString(mUniformIndex[i].name);
730 stream->writeInt(mUniformIndex[i].element);
731 stream->writeInt(mUniformIndex[i].index);
732 }
733
734 stream->writeInt(mUniformBlocks.size());
735 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
736 {
737 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
738
739 stream->writeString(uniformBlock.name);
740 stream->writeInt(uniformBlock.elementIndex);
741 stream->writeInt(uniformBlock.dataSize);
742
743 stream->writeInt(uniformBlock.memberUniformIndexes.size());
744 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
745 {
746 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
747 }
748
749 stream->writeInt(uniformBlock.psRegisterIndex);
750 stream->writeInt(uniformBlock.vsRegisterIndex);
751 }
752
Brandon Joneseb994362014-09-24 10:27:28 -0700753 stream->writeInt(mTransformFeedbackBufferMode);
754 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
755 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
756 {
757 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
758
759 stream->writeString(varying.name);
760 stream->writeInt(varying.type);
761 stream->writeInt(varying.size);
762 stream->writeString(varying.semanticName);
763 stream->writeInt(varying.semanticIndex);
764 stream->writeInt(varying.semanticIndexCount);
765 }
766
Brandon Jones22502d52014-08-29 16:58:36 -0700767 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530768 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700769 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530770 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700771 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700772 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700773
Brandon Joneseb994362014-09-24 10:27:28 -0700774 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700775 stream->writeInt(pixelShaderKey.size());
776 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
777 {
Brandon Joneseb994362014-09-24 10:27:28 -0700778 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700779 stream->writeInt(variable.type);
780 stream->writeString(variable.name);
781 stream->writeString(variable.source);
782 stream->writeInt(variable.outputIndex);
783 }
784
Brandon Joneseb994362014-09-24 10:27:28 -0700785 stream->writeInt(mVertexExecutables.size());
786 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
787 {
788 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
789
790 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
791 {
792 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
793 stream->writeInt(vertexInput.mType);
794 stream->writeInt(vertexInput.mNormalized);
795 stream->writeInt(vertexInput.mComponents);
796 stream->writeInt(vertexInput.mPureInteger);
797 }
798
799 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
800 stream->writeInt(vertexShaderSize);
801
802 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
803 stream->writeBytes(vertexBlob, vertexShaderSize);
804 }
805
806 stream->writeInt(mPixelExecutables.size());
807 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
808 {
809 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
810
811 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
812 stream->writeInt(outputs.size());
813 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
814 {
815 stream->writeInt(outputs[outputIndex]);
816 }
817
818 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
819 stream->writeInt(pixelShaderSize);
820
821 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
822 stream->writeBytes(pixelBlob, pixelShaderSize);
823 }
824
825 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
826 stream->writeInt(geometryShaderSize);
827
828 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
829 {
830 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
831 stream->writeBytes(geometryBlob, geometryShaderSize);
832 }
833
Brandon Jones18bd4102014-09-22 14:21:44 -0700834 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400835 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700836
Geoff Langb543aff2014-09-30 14:52:54 -0400837 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700838}
839
Geoff Lang359ef262015-01-05 14:42:29 -0500840gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700841{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400842 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700843
Jamie Madill85a18042015-03-05 15:41:41 -0500844 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
845 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700846
847 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
848 {
849 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
850
851 if (colorbuffer)
852 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400853 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700854 }
855 else
856 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400857 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700858 }
859 }
860
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400861 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700862}
863
Jamie Madill97399232014-12-23 12:31:15 -0500864gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500865 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500866 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700867{
868 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
869 {
870 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
871 {
Geoff Langb543aff2014-09-30 14:52:54 -0400872 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
873 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700874 }
875 }
876
Brandon Jones22502d52014-08-29 16:58:36 -0700877 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
878 outputSignature);
879
880 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500881 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500882
883 gl::InfoLog tempInfoLog;
884 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
885
886 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400887 mTransformFeedbackLinkedVaryings,
888 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
889 mPixelWorkarounds, &pixelExecutable);
890 if (error.isError())
891 {
892 return error;
893 }
Brandon Joneseb994362014-09-24 10:27:28 -0700894
Jamie Madill97399232014-12-23 12:31:15 -0500895 if (pixelExecutable)
896 {
897 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
898 }
899 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700900 {
901 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
902 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
903 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
904 }
Brandon Jones22502d52014-08-29 16:58:36 -0700905
Geoff Langb543aff2014-09-30 14:52:54 -0400906 *outExectuable = pixelExecutable;
907 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700908}
909
Jamie Madill97399232014-12-23 12:31:15 -0500910gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500911 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500912 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700913{
Brandon Joneseb994362014-09-24 10:27:28 -0700914 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
915 getInputLayoutSignature(inputLayout, signature);
916
917 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
918 {
919 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
920 {
Geoff Langb543aff2014-09-30 14:52:54 -0400921 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
922 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700923 }
924 }
925
Brandon Jones22502d52014-08-29 16:58:36 -0700926 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700927 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700928
929 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500930 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500931
932 gl::InfoLog tempInfoLog;
933 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
934
935 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400936 mTransformFeedbackLinkedVaryings,
937 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
938 mVertexWorkarounds, &vertexExecutable);
939 if (error.isError())
940 {
941 return error;
942 }
943
Jamie Madill97399232014-12-23 12:31:15 -0500944 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700945 {
946 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
947 }
Jamie Madill97399232014-12-23 12:31:15 -0500948 else if (!infoLog)
949 {
950 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
951 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
952 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
953 }
Brandon Jones22502d52014-08-29 16:58:36 -0700954
Geoff Langb543aff2014-09-30 14:52:54 -0400955 *outExectuable = vertexExecutable;
956 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700957}
958
Geoff Lang7dd2e102014-11-10 15:19:26 -0500959LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
960 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700961{
Brandon Jones18bd4102014-09-22 14:21:44 -0700962 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
963 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700964
Jamie Madille4ea2022015-03-26 20:35:05 +0000965 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
966 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
967 ShaderExecutableD3D *defaultVertexExecutable = NULL;
968 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
969 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -0800970 {
Jamie Madille4ea2022015-03-26 20:35:05 +0000971 return LinkResult(false, error);
972 }
Austin Kinross434953e2015-02-20 10:49:51 -0800973
Brandon Joneseb994362014-09-24 10:27:28 -0700974 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500975 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +0000976 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400977 if (error.isError())
978 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500979 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400980 }
Brandon Jones44151a92014-09-10 11:32:25 -0700981
Brandon Joneseb994362014-09-24 10:27:28 -0700982 if (usesGeometryShader())
983 {
984 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -0700985
Geoff Langb543aff2014-09-30 14:52:54 -0400986
987 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
988 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +0530989 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400990 if (error.isError())
991 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500992 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400993 }
Brandon Joneseb994362014-09-24 10:27:28 -0700994 }
995
Brandon Jones091540d2014-10-29 11:32:04 -0700996#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +0200997 if (usesGeometryShader() && mGeometryExecutable)
998 {
999 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1000 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1001 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1002 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1003 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1004 }
1005
1006 if (defaultVertexExecutable)
1007 {
1008 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1009 }
1010
1011 if (defaultPixelExecutable)
1012 {
1013 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1014 }
1015#endif
1016
Geoff Langb543aff2014-09-30 14:52:54 -04001017 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001018 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001019}
1020
Geoff Lang7dd2e102014-11-10 15:19:26 -05001021LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1022 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1023 const std::vector<std::string> &transformFeedbackVaryings,
1024 GLenum transformFeedbackBufferMode,
1025 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1026 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001027{
Brandon Joneseb994362014-09-24 10:27:28 -07001028 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1029 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1030
Jamie Madillde8892b2014-11-11 13:00:22 -05001031 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1032 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001033
Brandon Joneseb994362014-09-24 10:27:28 -07001034 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001035
1036 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301037 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001038
1039 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301040 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001041 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001042
1043 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001044 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001045 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1046
Geoff Langbdee2d52014-09-17 11:02:51 -04001047 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001048 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001049 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001050 }
1051
Geoff Lang7dd2e102014-11-10 15:19:26 -05001052 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001053 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001054 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001055 }
1056
Jamie Madillde8892b2014-11-11 13:00:22 -05001057 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001058 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1059 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1060 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001061 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001062 }
1063
Brandon Jones44151a92014-09-10 11:32:25 -07001064 mUsesPointSize = vertexShaderD3D->usesPointSize();
1065
Jamie Madill437d2662014-12-05 14:23:35 -05001066 initAttributesByLayout();
1067
Geoff Lang7dd2e102014-11-10 15:19:26 -05001068 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001069}
1070
Brandon Jones44151a92014-09-10 11:32:25 -07001071void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1072{
1073 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1074}
1075
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001076void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001077{
1078 // Compute total default block size
1079 unsigned int vertexRegisters = 0;
1080 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001081 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001082 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001083 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001084
Geoff Lang2ec386b2014-12-03 14:44:38 -05001085 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001086 {
1087 if (uniform.isReferencedByVertexShader())
1088 {
1089 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1090 }
1091 if (uniform.isReferencedByFragmentShader())
1092 {
1093 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1094 }
1095 }
1096 }
1097
1098 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1099 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1100}
1101
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001102gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001103{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001104 updateSamplerMapping();
1105
1106 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1107 if (error.isError())
1108 {
1109 return error;
1110 }
1111
1112 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1113 {
1114 mUniforms[uniformIndex]->dirty = false;
1115 }
1116
1117 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001118}
1119
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001120gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001121{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001122 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1123
Brandon Jones18bd4102014-09-22 14:21:44 -07001124 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1125 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1126
1127 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1128 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1129
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001130 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001131 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001132 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001133 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1134
1135 ASSERT(uniformBlock && uniformBuffer);
1136
1137 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1138 {
1139 // undefined behaviour
1140 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1141 }
1142
1143 // Unnecessary to apply an unreferenced standard or shared UBO
1144 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1145 {
1146 continue;
1147 }
1148
1149 if (uniformBlock->isReferencedByVertexShader())
1150 {
1151 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1152 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1153 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1154 vertexUniformBuffers[registerIndex] = uniformBuffer;
1155 }
1156
1157 if (uniformBlock->isReferencedByFragmentShader())
1158 {
1159 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1160 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1161 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1162 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1163 }
1164 }
1165
1166 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1167}
1168
1169bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1170 unsigned int registerIndex, const gl::Caps &caps)
1171{
1172 if (shader == GL_VERTEX_SHADER)
1173 {
1174 uniformBlock->vsRegisterIndex = registerIndex;
1175 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1176 {
1177 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1178 return false;
1179 }
1180 }
1181 else if (shader == GL_FRAGMENT_SHADER)
1182 {
1183 uniformBlock->psRegisterIndex = registerIndex;
1184 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1185 {
1186 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1187 return false;
1188 }
1189 }
1190 else UNREACHABLE();
1191
1192 return true;
1193}
1194
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001195void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001196{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001197 unsigned int numUniforms = mUniforms.size();
1198 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001199 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001200 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001201 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001202}
1203
1204void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1205{
1206 setUniform(location, count, v, GL_FLOAT);
1207}
1208
1209void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1210{
1211 setUniform(location, count, v, GL_FLOAT_VEC2);
1212}
1213
1214void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1215{
1216 setUniform(location, count, v, GL_FLOAT_VEC3);
1217}
1218
1219void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1220{
1221 setUniform(location, count, v, GL_FLOAT_VEC4);
1222}
1223
1224void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1225{
1226 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1227}
1228
1229void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1230{
1231 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1232}
1233
1234void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1235{
1236 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1237}
1238
1239void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1240{
1241 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1242}
1243
1244void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1245{
1246 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1247}
1248
1249void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1250{
1251 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1252}
1253
1254void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1255{
1256 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1257}
1258
1259void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1260{
1261 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1262}
1263
1264void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1265{
1266 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1267}
1268
1269void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1270{
1271 setUniform(location, count, v, GL_INT);
1272}
1273
1274void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1275{
1276 setUniform(location, count, v, GL_INT_VEC2);
1277}
1278
1279void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1280{
1281 setUniform(location, count, v, GL_INT_VEC3);
1282}
1283
1284void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1285{
1286 setUniform(location, count, v, GL_INT_VEC4);
1287}
1288
1289void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1290{
1291 setUniform(location, count, v, GL_UNSIGNED_INT);
1292}
1293
1294void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1295{
1296 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1297}
1298
1299void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1300{
1301 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1302}
1303
1304void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1305{
1306 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1307}
1308
1309void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1310{
1311 getUniformv(location, params, GL_FLOAT);
1312}
1313
1314void ProgramD3D::getUniformiv(GLint location, GLint *params)
1315{
1316 getUniformv(location, params, GL_INT);
1317}
1318
1319void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1320{
1321 getUniformv(location, params, GL_UNSIGNED_INT);
1322}
1323
1324bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1325 const gl::Caps &caps)
1326{
Jamie Madill30d6c252014-11-13 10:03:33 -05001327 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1328 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001329
1330 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1331 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1332
1333 // Check that uniforms defined in the vertex and fragment shaders are identical
1334 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1335 UniformMap linkedUniforms;
1336
1337 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001338 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001339 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1340 linkedUniforms[vertexUniform.name] = &vertexUniform;
1341 }
1342
1343 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1344 {
1345 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1346 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1347 if (entry != linkedUniforms.end())
1348 {
1349 const sh::Uniform &vertexUniform = *entry->second;
1350 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001351 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001352 {
1353 return false;
1354 }
1355 }
1356 }
1357
1358 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1359 {
1360 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1361
1362 if (uniform.staticUse)
1363 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001364 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001365 }
1366 }
1367
1368 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1369 {
1370 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1371
1372 if (uniform.staticUse)
1373 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001374 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001375 }
1376 }
1377
1378 if (!indexUniforms(infoLog, caps))
1379 {
1380 return false;
1381 }
1382
1383 initializeUniformStorage();
1384
1385 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1386 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1387 {
1388 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1389
1390 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1391 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1392 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1393 }
1394
1395 return true;
1396}
1397
Geoff Lang492a7e42014-11-05 13:27:06 -05001398void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001399{
Geoff Lang492a7e42014-11-05 13:27:06 -05001400 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001401 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1402 encoder.skipRegisters(uniformRegister);
1403
1404 defineUniform(shader, uniform, uniform.name, &encoder);
1405}
1406
Geoff Lang492a7e42014-11-05 13:27:06 -05001407void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1408 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001409{
1410 if (uniform.isStruct())
1411 {
1412 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1413 {
1414 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1415
1416 encoder->enterAggregateType();
1417
1418 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1419 {
1420 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1421 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1422
1423 defineUniform(shader, field, fieldFullName, encoder);
1424 }
1425
1426 encoder->exitAggregateType();
1427 }
1428 }
1429 else // Not a struct
1430 {
1431 // Arrays are treated as aggregate types
1432 if (uniform.isArray())
1433 {
1434 encoder->enterAggregateType();
1435 }
1436
1437 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1438
Jamie Madill2857f482015-02-09 15:35:29 -05001439 // Advance the uniform offset, to track registers allocation for structs
1440 sh::BlockMemberInfo blockInfo = encoder->encodeType(uniform.type, uniform.arraySize, false);
1441
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001442 if (!linkedUniform)
1443 {
1444 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001445 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001446 ASSERT(linkedUniform);
Jamie Madill2857f482015-02-09 15:35:29 -05001447 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001448 mUniforms.push_back(linkedUniform);
1449 }
1450
Geoff Lang492a7e42014-11-05 13:27:06 -05001451 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001452 {
Jamie Madill2857f482015-02-09 15:35:29 -05001453 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001454 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001455 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001456 {
Jamie Madill2857f482015-02-09 15:35:29 -05001457 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001458 }
1459 else UNREACHABLE();
1460
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001461 // Arrays are treated as aggregate types
1462 if (uniform.isArray())
1463 {
1464 encoder->exitAggregateType();
1465 }
1466 }
1467}
1468
1469template <typename T>
1470static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1471{
1472 ASSERT(dest != NULL);
1473 ASSERT(dirtyFlag != NULL);
1474
1475 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1476 *dest = source;
1477}
1478
1479template <typename T>
1480void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1481{
1482 const int components = gl::VariableComponentCount(targetUniformType);
1483 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1484
1485 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1486
1487 int elementCount = targetUniform->elementCount();
1488
1489 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1490
1491 if (targetUniform->type == targetUniformType)
1492 {
1493 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1494
1495 for (int i = 0; i < count; i++)
1496 {
1497 T *dest = target + (i * 4);
1498 const T *source = v + (i * components);
1499
1500 for (int c = 0; c < components; c++)
1501 {
1502 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1503 }
1504 for (int c = components; c < 4; c++)
1505 {
1506 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1507 }
1508 }
1509 }
1510 else if (targetUniform->type == targetBoolType)
1511 {
1512 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1513
1514 for (int i = 0; i < count; i++)
1515 {
1516 GLint *dest = boolParams + (i * 4);
1517 const T *source = v + (i * components);
1518
1519 for (int c = 0; c < components; c++)
1520 {
1521 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1522 }
1523 for (int c = components; c < 4; c++)
1524 {
1525 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1526 }
1527 }
1528 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001529 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001530 {
1531 ASSERT(targetUniformType == GL_INT);
1532
1533 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1534
1535 bool wasDirty = targetUniform->dirty;
1536
1537 for (int i = 0; i < count; i++)
1538 {
1539 GLint *dest = target + (i * 4);
1540 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1541
1542 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1543 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1544 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1545 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1546 }
1547
1548 if (!wasDirty && targetUniform->dirty)
1549 {
1550 mDirtySamplerMapping = true;
1551 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001552 }
1553 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001554}
Brandon Jones18bd4102014-09-22 14:21:44 -07001555
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001556template<typename T>
1557bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1558{
1559 bool dirty = false;
1560 int copyWidth = std::min(targetHeight, srcWidth);
1561 int copyHeight = std::min(targetWidth, srcHeight);
1562
1563 for (int x = 0; x < copyWidth; x++)
1564 {
1565 for (int y = 0; y < copyHeight; y++)
1566 {
1567 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1568 }
1569 }
1570 // clear unfilled right side
1571 for (int y = 0; y < copyWidth; y++)
1572 {
1573 for (int x = copyHeight; x < targetWidth; x++)
1574 {
1575 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1576 }
1577 }
1578 // clear unfilled bottom.
1579 for (int y = copyWidth; y < targetHeight; y++)
1580 {
1581 for (int x = 0; x < targetWidth; x++)
1582 {
1583 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1584 }
1585 }
1586
1587 return dirty;
1588}
1589
1590template<typename T>
1591bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1592{
1593 bool dirty = false;
1594 int copyWidth = std::min(targetWidth, srcWidth);
1595 int copyHeight = std::min(targetHeight, srcHeight);
1596
1597 for (int y = 0; y < copyHeight; y++)
1598 {
1599 for (int x = 0; x < copyWidth; x++)
1600 {
1601 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1602 }
1603 }
1604 // clear unfilled right side
1605 for (int y = 0; y < copyHeight; y++)
1606 {
1607 for (int x = copyWidth; x < targetWidth; x++)
1608 {
1609 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1610 }
1611 }
1612 // clear unfilled bottom.
1613 for (int y = copyHeight; y < targetHeight; y++)
1614 {
1615 for (int x = 0; x < targetWidth; x++)
1616 {
1617 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1618 }
1619 }
1620
1621 return dirty;
1622}
1623
1624template <int cols, int rows>
1625void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1626{
1627 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1628
1629 int elementCount = targetUniform->elementCount();
1630
1631 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1632 const unsigned int targetMatrixStride = (4 * rows);
1633 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1634
1635 for (int i = 0; i < count; i++)
1636 {
1637 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1638 if (transpose == GL_FALSE)
1639 {
1640 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1641 }
1642 else
1643 {
1644 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1645 }
1646 target += targetMatrixStride;
1647 value += cols * rows;
1648 }
1649}
1650
1651template <typename T>
1652void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1653{
1654 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1655
1656 if (gl::IsMatrixType(targetUniform->type))
1657 {
1658 const int rows = gl::VariableRowCount(targetUniform->type);
1659 const int cols = gl::VariableColumnCount(targetUniform->type);
1660 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1661 }
1662 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1663 {
1664 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1665 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1666 size * sizeof(T));
1667 }
1668 else
1669 {
1670 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1671 switch (gl::VariableComponentType(targetUniform->type))
1672 {
1673 case GL_BOOL:
1674 {
1675 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1676
1677 for (unsigned int i = 0; i < size; i++)
1678 {
1679 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1680 }
1681 }
1682 break;
1683
1684 case GL_FLOAT:
1685 {
1686 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1687
1688 for (unsigned int i = 0; i < size; i++)
1689 {
1690 params[i] = static_cast<T>(floatParams[i]);
1691 }
1692 }
1693 break;
1694
1695 case GL_INT:
1696 {
1697 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1698
1699 for (unsigned int i = 0; i < size; i++)
1700 {
1701 params[i] = static_cast<T>(intParams[i]);
1702 }
1703 }
1704 break;
1705
1706 case GL_UNSIGNED_INT:
1707 {
1708 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1709
1710 for (unsigned int i = 0; i < size; i++)
1711 {
1712 params[i] = static_cast<T>(uintParams[i]);
1713 }
1714 }
1715 break;
1716
1717 default: UNREACHABLE();
1718 }
1719 }
1720}
1721
1722template <typename VarT>
1723void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1724 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1725 bool inRowMajorLayout)
1726{
1727 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1728 {
1729 const VarT &field = fields[uniformIndex];
1730 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1731
1732 if (field.isStruct())
1733 {
1734 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1735
1736 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1737 {
1738 encoder->enterAggregateType();
1739
1740 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1741 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1742
1743 encoder->exitAggregateType();
1744 }
1745 }
1746 else
1747 {
1748 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1749
1750 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1751
1752 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1753 blockIndex, memberInfo);
1754
1755 // add to uniform list, but not index, since uniform block uniforms have no location
1756 blockUniformIndexes->push_back(mUniforms.size());
1757 mUniforms.push_back(newUniform);
1758 }
1759 }
1760}
1761
1762bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1763 const gl::Caps &caps)
1764{
Jamie Madill30d6c252014-11-13 10:03:33 -05001765 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001766
1767 // create uniform block entries if they do not exist
1768 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1769 {
1770 std::vector<unsigned int> blockUniformIndexes;
1771 const unsigned int blockIndex = mUniformBlocks.size();
1772
1773 // define member uniforms
1774 sh::BlockLayoutEncoder *encoder = NULL;
1775
1776 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1777 {
1778 encoder = new sh::Std140BlockEncoder;
1779 }
1780 else
1781 {
1782 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1783 }
1784 ASSERT(encoder);
1785
1786 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1787
1788 size_t dataSize = encoder->getBlockSize();
1789
1790 // create all the uniform blocks
1791 if (interfaceBlock.arraySize > 0)
1792 {
1793 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1794 {
1795 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1796 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1797 mUniformBlocks.push_back(newUniformBlock);
1798 }
1799 }
1800 else
1801 {
1802 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1803 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1804 mUniformBlocks.push_back(newUniformBlock);
1805 }
1806 }
1807
1808 if (interfaceBlock.staticUse)
1809 {
1810 // Assign registers to the uniform blocks
1811 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1812 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1813 ASSERT(blockIndex != GL_INVALID_INDEX);
1814 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1815
1816 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1817
1818 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1819 {
1820 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1821 ASSERT(uniformBlock->name == interfaceBlock.name);
1822
1823 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1824 interfaceBlockRegister + uniformBlockElement, caps))
1825 {
1826 return false;
1827 }
1828 }
1829 }
1830
1831 return true;
1832}
1833
1834bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1835 GLenum samplerType,
1836 unsigned int samplerCount,
1837 std::vector<Sampler> &outSamplers,
1838 GLuint *outUsedRange)
1839{
1840 unsigned int samplerIndex = startSamplerIndex;
1841
1842 do
1843 {
1844 if (samplerIndex < outSamplers.size())
1845 {
1846 Sampler& sampler = outSamplers[samplerIndex];
1847 sampler.active = true;
1848 sampler.textureType = GetTextureType(samplerType);
1849 sampler.logicalTextureUnit = 0;
1850 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1851 }
1852 else
1853 {
1854 return false;
1855 }
1856
1857 samplerIndex++;
1858 } while (samplerIndex < startSamplerIndex + samplerCount);
1859
1860 return true;
1861}
1862
1863bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1864{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001865 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001866 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1867
1868 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1869 {
1870 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1871 &mUsedVertexSamplerRange))
1872 {
1873 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1874 mSamplersVS.size());
1875 return false;
1876 }
1877
1878 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1879 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1880 {
1881 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1882 caps.maxVertexUniformVectors);
1883 return false;
1884 }
1885 }
1886
1887 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1888 {
1889 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1890 &mUsedPixelSamplerRange))
1891 {
1892 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1893 mSamplersPS.size());
1894 return false;
1895 }
1896
1897 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1898 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1899 {
1900 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1901 caps.maxFragmentUniformVectors);
1902 return false;
1903 }
1904 }
1905
1906 return true;
1907}
1908
1909bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1910{
1911 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1912 {
1913 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1914
Geoff Lang2ec386b2014-12-03 14:44:38 -05001915 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001916 {
1917 if (!indexSamplerUniform(uniform, infoLog, caps))
1918 {
1919 return false;
1920 }
1921 }
1922
1923 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1924 {
1925 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1926 }
1927 }
1928
1929 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001930}
1931
Brandon Jonesc9610c52014-08-25 17:02:59 -07001932void ProgramD3D::reset()
1933{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001934 ProgramImpl::reset();
1935
Brandon Joneseb994362014-09-24 10:27:28 -07001936 SafeDeleteContainer(mVertexExecutables);
1937 SafeDeleteContainer(mPixelExecutables);
1938 SafeDelete(mGeometryExecutable);
1939
1940 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001941
Brandon Jones22502d52014-08-29 16:58:36 -07001942 mVertexHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301943 mVertexWorkarounds.reset();
Brandon Jones44151a92014-09-10 11:32:25 -07001944 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001945
1946 mPixelHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301947 mPixelWorkarounds.reset();
Brandon Jones22502d52014-08-29 16:58:36 -07001948 mUsesFragDepth = false;
1949 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001950 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001951
Brandon Jonesc9610c52014-08-25 17:02:59 -07001952 SafeDelete(mVertexUniformStorage);
1953 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001954
1955 mSamplersPS.clear();
1956 mSamplersVS.clear();
1957
1958 mUsedVertexSamplerRange = 0;
1959 mUsedPixelSamplerRange = 0;
1960 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001961
1962 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001963}
1964
Geoff Lang7dd2e102014-11-10 15:19:26 -05001965unsigned int ProgramD3D::getSerial() const
1966{
1967 return mSerial;
1968}
1969
1970unsigned int ProgramD3D::issueSerial()
1971{
1972 return mCurrentSerial++;
1973}
1974
Jamie Madill437d2662014-12-05 14:23:35 -05001975void ProgramD3D::initAttributesByLayout()
1976{
1977 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1978 {
1979 mAttributesByLayout[i] = i;
1980 }
1981
1982 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
1983}
1984
1985void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
1986 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
1987{
1988 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
1989
1990 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1991 {
1992 oldTranslatedAttributes[i] = attributes[i];
1993 }
1994
1995 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1996 {
1997 int oldIndex = mAttributesByLayout[i];
1998 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
1999 attributes[i] = oldTranslatedAttributes[oldIndex];
2000 }
2001}
2002
Brandon Jonesc9610c52014-08-25 17:02:59 -07002003}