blob: 9ce9a27cd30dfe9b875013ac9d543b80af1d8a3f [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
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001120gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001121{
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001122 GLint vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
1123 GLint fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001124
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001125 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1126 {
1127 vertexUniformBuffers[registerIndex] = -1;
1128 }
1129
1130 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1131 {
1132 fragmentUniformBuffers[registerIndex] = -1;
1133 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001134
1135 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1136 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1137
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001138 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001139 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001140 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001141 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001142
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001143 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001144
1145 // Unnecessary to apply an unreferenced standard or shared UBO
1146 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1147 {
1148 continue;
1149 }
1150
1151 if (uniformBlock->isReferencedByVertexShader())
1152 {
1153 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001154 ASSERT(vertexUniformBuffers[registerIndex] == -1);
1155 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
1156 vertexUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001157 }
1158
1159 if (uniformBlock->isReferencedByFragmentShader())
1160 {
1161 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001162 ASSERT(fragmentUniformBuffers[registerIndex] == -1);
1163 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
1164 fragmentUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001165 }
1166 }
1167
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001168 return mRenderer->setUniformBuffers(data, vertexUniformBuffers, fragmentUniformBuffers);
Brandon Jones18bd4102014-09-22 14:21:44 -07001169}
1170
1171bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1172 unsigned int registerIndex, const gl::Caps &caps)
1173{
1174 if (shader == GL_VERTEX_SHADER)
1175 {
1176 uniformBlock->vsRegisterIndex = registerIndex;
1177 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1178 {
1179 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1180 return false;
1181 }
1182 }
1183 else if (shader == GL_FRAGMENT_SHADER)
1184 {
1185 uniformBlock->psRegisterIndex = registerIndex;
1186 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1187 {
1188 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1189 return false;
1190 }
1191 }
1192 else UNREACHABLE();
1193
1194 return true;
1195}
1196
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001197void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001198{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001199 unsigned int numUniforms = mUniforms.size();
1200 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001201 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001202 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001203 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001204}
1205
1206void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1207{
1208 setUniform(location, count, v, GL_FLOAT);
1209}
1210
1211void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1212{
1213 setUniform(location, count, v, GL_FLOAT_VEC2);
1214}
1215
1216void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1217{
1218 setUniform(location, count, v, GL_FLOAT_VEC3);
1219}
1220
1221void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1222{
1223 setUniform(location, count, v, GL_FLOAT_VEC4);
1224}
1225
1226void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1227{
1228 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1229}
1230
1231void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1232{
1233 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1234}
1235
1236void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1237{
1238 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1239}
1240
1241void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1242{
1243 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1244}
1245
1246void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1247{
1248 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1249}
1250
1251void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1252{
1253 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1254}
1255
1256void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1257{
1258 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1259}
1260
1261void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1262{
1263 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1264}
1265
1266void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1267{
1268 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1269}
1270
1271void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1272{
1273 setUniform(location, count, v, GL_INT);
1274}
1275
1276void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1277{
1278 setUniform(location, count, v, GL_INT_VEC2);
1279}
1280
1281void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1282{
1283 setUniform(location, count, v, GL_INT_VEC3);
1284}
1285
1286void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1287{
1288 setUniform(location, count, v, GL_INT_VEC4);
1289}
1290
1291void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1292{
1293 setUniform(location, count, v, GL_UNSIGNED_INT);
1294}
1295
1296void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1297{
1298 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1299}
1300
1301void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1302{
1303 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1304}
1305
1306void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1307{
1308 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1309}
1310
1311void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1312{
1313 getUniformv(location, params, GL_FLOAT);
1314}
1315
1316void ProgramD3D::getUniformiv(GLint location, GLint *params)
1317{
1318 getUniformv(location, params, GL_INT);
1319}
1320
1321void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1322{
1323 getUniformv(location, params, GL_UNSIGNED_INT);
1324}
1325
1326bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1327 const gl::Caps &caps)
1328{
Jamie Madill30d6c252014-11-13 10:03:33 -05001329 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1330 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001331
1332 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1333 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1334
1335 // Check that uniforms defined in the vertex and fragment shaders are identical
1336 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1337 UniformMap linkedUniforms;
1338
1339 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001340 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001341 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1342 linkedUniforms[vertexUniform.name] = &vertexUniform;
1343 }
1344
1345 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1346 {
1347 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1348 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1349 if (entry != linkedUniforms.end())
1350 {
1351 const sh::Uniform &vertexUniform = *entry->second;
1352 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001354 {
1355 return false;
1356 }
1357 }
1358 }
1359
1360 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1361 {
1362 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1363
1364 if (uniform.staticUse)
1365 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001366 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001367 }
1368 }
1369
1370 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1371 {
1372 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1373
1374 if (uniform.staticUse)
1375 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001376 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001377 }
1378 }
1379
1380 if (!indexUniforms(infoLog, caps))
1381 {
1382 return false;
1383 }
1384
1385 initializeUniformStorage();
1386
1387 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1388 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1389 {
1390 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1391
1392 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1393 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1394 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1395 }
1396
1397 return true;
1398}
1399
Geoff Lang492a7e42014-11-05 13:27:06 -05001400void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001401{
Geoff Lang492a7e42014-11-05 13:27:06 -05001402 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001403 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1404 encoder.skipRegisters(uniformRegister);
1405
1406 defineUniform(shader, uniform, uniform.name, &encoder);
1407}
1408
Geoff Lang492a7e42014-11-05 13:27:06 -05001409void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1410 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001411{
1412 if (uniform.isStruct())
1413 {
1414 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1415 {
1416 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1417
1418 encoder->enterAggregateType();
1419
1420 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1421 {
1422 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1423 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1424
1425 defineUniform(shader, field, fieldFullName, encoder);
1426 }
1427
1428 encoder->exitAggregateType();
1429 }
1430 }
1431 else // Not a struct
1432 {
1433 // Arrays are treated as aggregate types
1434 if (uniform.isArray())
1435 {
1436 encoder->enterAggregateType();
1437 }
1438
1439 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1440
Jamie Madill2857f482015-02-09 15:35:29 -05001441 // Advance the uniform offset, to track registers allocation for structs
1442 sh::BlockMemberInfo blockInfo = encoder->encodeType(uniform.type, uniform.arraySize, false);
1443
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001444 if (!linkedUniform)
1445 {
1446 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001447 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001448 ASSERT(linkedUniform);
Jamie Madill2857f482015-02-09 15:35:29 -05001449 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001450 mUniforms.push_back(linkedUniform);
1451 }
1452
Geoff Lang492a7e42014-11-05 13:27:06 -05001453 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001454 {
Jamie Madill2857f482015-02-09 15:35:29 -05001455 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001456 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001457 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001458 {
Jamie Madill2857f482015-02-09 15:35:29 -05001459 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001460 }
1461 else UNREACHABLE();
1462
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001463 // Arrays are treated as aggregate types
1464 if (uniform.isArray())
1465 {
1466 encoder->exitAggregateType();
1467 }
1468 }
1469}
1470
1471template <typename T>
1472static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1473{
1474 ASSERT(dest != NULL);
1475 ASSERT(dirtyFlag != NULL);
1476
1477 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1478 *dest = source;
1479}
1480
1481template <typename T>
1482void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1483{
1484 const int components = gl::VariableComponentCount(targetUniformType);
1485 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1486
1487 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1488
1489 int elementCount = targetUniform->elementCount();
1490
1491 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1492
1493 if (targetUniform->type == targetUniformType)
1494 {
1495 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1496
1497 for (int i = 0; i < count; i++)
1498 {
1499 T *dest = target + (i * 4);
1500 const T *source = v + (i * components);
1501
1502 for (int c = 0; c < components; c++)
1503 {
1504 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1505 }
1506 for (int c = components; c < 4; c++)
1507 {
1508 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1509 }
1510 }
1511 }
1512 else if (targetUniform->type == targetBoolType)
1513 {
1514 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1515
1516 for (int i = 0; i < count; i++)
1517 {
1518 GLint *dest = boolParams + (i * 4);
1519 const T *source = v + (i * components);
1520
1521 for (int c = 0; c < components; c++)
1522 {
1523 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1524 }
1525 for (int c = components; c < 4; c++)
1526 {
1527 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1528 }
1529 }
1530 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001531 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001532 {
1533 ASSERT(targetUniformType == GL_INT);
1534
1535 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1536
1537 bool wasDirty = targetUniform->dirty;
1538
1539 for (int i = 0; i < count; i++)
1540 {
1541 GLint *dest = target + (i * 4);
1542 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1543
1544 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1545 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1546 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1547 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1548 }
1549
1550 if (!wasDirty && targetUniform->dirty)
1551 {
1552 mDirtySamplerMapping = true;
1553 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001554 }
1555 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001556}
Brandon Jones18bd4102014-09-22 14:21:44 -07001557
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001558template<typename T>
1559bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1560{
1561 bool dirty = false;
1562 int copyWidth = std::min(targetHeight, srcWidth);
1563 int copyHeight = std::min(targetWidth, srcHeight);
1564
1565 for (int x = 0; x < copyWidth; x++)
1566 {
1567 for (int y = 0; y < copyHeight; y++)
1568 {
1569 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1570 }
1571 }
1572 // clear unfilled right side
1573 for (int y = 0; y < copyWidth; y++)
1574 {
1575 for (int x = copyHeight; x < targetWidth; x++)
1576 {
1577 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1578 }
1579 }
1580 // clear unfilled bottom.
1581 for (int y = copyWidth; y < targetHeight; y++)
1582 {
1583 for (int x = 0; x < targetWidth; x++)
1584 {
1585 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1586 }
1587 }
1588
1589 return dirty;
1590}
1591
1592template<typename T>
1593bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1594{
1595 bool dirty = false;
1596 int copyWidth = std::min(targetWidth, srcWidth);
1597 int copyHeight = std::min(targetHeight, srcHeight);
1598
1599 for (int y = 0; y < copyHeight; y++)
1600 {
1601 for (int x = 0; x < copyWidth; x++)
1602 {
1603 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1604 }
1605 }
1606 // clear unfilled right side
1607 for (int y = 0; y < copyHeight; y++)
1608 {
1609 for (int x = copyWidth; x < targetWidth; x++)
1610 {
1611 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1612 }
1613 }
1614 // clear unfilled bottom.
1615 for (int y = copyHeight; y < targetHeight; y++)
1616 {
1617 for (int x = 0; x < targetWidth; x++)
1618 {
1619 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1620 }
1621 }
1622
1623 return dirty;
1624}
1625
1626template <int cols, int rows>
1627void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1628{
1629 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1630
1631 int elementCount = targetUniform->elementCount();
1632
1633 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1634 const unsigned int targetMatrixStride = (4 * rows);
1635 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1636
1637 for (int i = 0; i < count; i++)
1638 {
1639 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1640 if (transpose == GL_FALSE)
1641 {
1642 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1643 }
1644 else
1645 {
1646 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1647 }
1648 target += targetMatrixStride;
1649 value += cols * rows;
1650 }
1651}
1652
1653template <typename T>
1654void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1655{
1656 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1657
1658 if (gl::IsMatrixType(targetUniform->type))
1659 {
1660 const int rows = gl::VariableRowCount(targetUniform->type);
1661 const int cols = gl::VariableColumnCount(targetUniform->type);
1662 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1663 }
1664 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1665 {
1666 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1667 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1668 size * sizeof(T));
1669 }
1670 else
1671 {
1672 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1673 switch (gl::VariableComponentType(targetUniform->type))
1674 {
1675 case GL_BOOL:
1676 {
1677 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1678
1679 for (unsigned int i = 0; i < size; i++)
1680 {
1681 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1682 }
1683 }
1684 break;
1685
1686 case GL_FLOAT:
1687 {
1688 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1689
1690 for (unsigned int i = 0; i < size; i++)
1691 {
1692 params[i] = static_cast<T>(floatParams[i]);
1693 }
1694 }
1695 break;
1696
1697 case GL_INT:
1698 {
1699 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1700
1701 for (unsigned int i = 0; i < size; i++)
1702 {
1703 params[i] = static_cast<T>(intParams[i]);
1704 }
1705 }
1706 break;
1707
1708 case GL_UNSIGNED_INT:
1709 {
1710 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1711
1712 for (unsigned int i = 0; i < size; i++)
1713 {
1714 params[i] = static_cast<T>(uintParams[i]);
1715 }
1716 }
1717 break;
1718
1719 default: UNREACHABLE();
1720 }
1721 }
1722}
1723
1724template <typename VarT>
1725void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1726 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1727 bool inRowMajorLayout)
1728{
1729 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1730 {
1731 const VarT &field = fields[uniformIndex];
1732 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1733
1734 if (field.isStruct())
1735 {
1736 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1737
1738 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1739 {
1740 encoder->enterAggregateType();
1741
1742 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1743 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1744
1745 encoder->exitAggregateType();
1746 }
1747 }
1748 else
1749 {
1750 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1751
1752 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1753
1754 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1755 blockIndex, memberInfo);
1756
1757 // add to uniform list, but not index, since uniform block uniforms have no location
1758 blockUniformIndexes->push_back(mUniforms.size());
1759 mUniforms.push_back(newUniform);
1760 }
1761 }
1762}
1763
1764bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1765 const gl::Caps &caps)
1766{
Jamie Madill30d6c252014-11-13 10:03:33 -05001767 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001768
1769 // create uniform block entries if they do not exist
1770 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1771 {
1772 std::vector<unsigned int> blockUniformIndexes;
1773 const unsigned int blockIndex = mUniformBlocks.size();
1774
1775 // define member uniforms
1776 sh::BlockLayoutEncoder *encoder = NULL;
1777
1778 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1779 {
1780 encoder = new sh::Std140BlockEncoder;
1781 }
1782 else
1783 {
1784 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1785 }
1786 ASSERT(encoder);
1787
1788 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1789
1790 size_t dataSize = encoder->getBlockSize();
1791
1792 // create all the uniform blocks
1793 if (interfaceBlock.arraySize > 0)
1794 {
1795 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1796 {
1797 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1798 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1799 mUniformBlocks.push_back(newUniformBlock);
1800 }
1801 }
1802 else
1803 {
1804 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1805 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1806 mUniformBlocks.push_back(newUniformBlock);
1807 }
1808 }
1809
1810 if (interfaceBlock.staticUse)
1811 {
1812 // Assign registers to the uniform blocks
1813 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1814 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1815 ASSERT(blockIndex != GL_INVALID_INDEX);
1816 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1817
1818 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1819
1820 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1821 {
1822 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1823 ASSERT(uniformBlock->name == interfaceBlock.name);
1824
1825 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1826 interfaceBlockRegister + uniformBlockElement, caps))
1827 {
1828 return false;
1829 }
1830 }
1831 }
1832
1833 return true;
1834}
1835
1836bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1837 GLenum samplerType,
1838 unsigned int samplerCount,
1839 std::vector<Sampler> &outSamplers,
1840 GLuint *outUsedRange)
1841{
1842 unsigned int samplerIndex = startSamplerIndex;
1843
1844 do
1845 {
1846 if (samplerIndex < outSamplers.size())
1847 {
1848 Sampler& sampler = outSamplers[samplerIndex];
1849 sampler.active = true;
1850 sampler.textureType = GetTextureType(samplerType);
1851 sampler.logicalTextureUnit = 0;
1852 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1853 }
1854 else
1855 {
1856 return false;
1857 }
1858
1859 samplerIndex++;
1860 } while (samplerIndex < startSamplerIndex + samplerCount);
1861
1862 return true;
1863}
1864
1865bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1866{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001867 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001868 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1869
1870 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1871 {
1872 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1873 &mUsedVertexSamplerRange))
1874 {
1875 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1876 mSamplersVS.size());
1877 return false;
1878 }
1879
1880 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1881 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1882 {
1883 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1884 caps.maxVertexUniformVectors);
1885 return false;
1886 }
1887 }
1888
1889 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1890 {
1891 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1892 &mUsedPixelSamplerRange))
1893 {
1894 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1895 mSamplersPS.size());
1896 return false;
1897 }
1898
1899 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1900 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1901 {
1902 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1903 caps.maxFragmentUniformVectors);
1904 return false;
1905 }
1906 }
1907
1908 return true;
1909}
1910
1911bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1912{
1913 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1914 {
1915 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1916
Geoff Lang2ec386b2014-12-03 14:44:38 -05001917 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001918 {
1919 if (!indexSamplerUniform(uniform, infoLog, caps))
1920 {
1921 return false;
1922 }
1923 }
1924
1925 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1926 {
1927 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1928 }
1929 }
1930
1931 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001932}
1933
Brandon Jonesc9610c52014-08-25 17:02:59 -07001934void ProgramD3D::reset()
1935{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001936 ProgramImpl::reset();
1937
Brandon Joneseb994362014-09-24 10:27:28 -07001938 SafeDeleteContainer(mVertexExecutables);
1939 SafeDeleteContainer(mPixelExecutables);
1940 SafeDelete(mGeometryExecutable);
1941
1942 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001943
Brandon Jones22502d52014-08-29 16:58:36 -07001944 mVertexHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301945 mVertexWorkarounds.reset();
Brandon Jones44151a92014-09-10 11:32:25 -07001946 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001947
1948 mPixelHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301949 mPixelWorkarounds.reset();
Brandon Jones22502d52014-08-29 16:58:36 -07001950 mUsesFragDepth = false;
1951 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001952 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001953
Brandon Jonesc9610c52014-08-25 17:02:59 -07001954 SafeDelete(mVertexUniformStorage);
1955 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001956
1957 mSamplersPS.clear();
1958 mSamplersVS.clear();
1959
1960 mUsedVertexSamplerRange = 0;
1961 mUsedPixelSamplerRange = 0;
1962 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001963
1964 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001965}
1966
Geoff Lang7dd2e102014-11-10 15:19:26 -05001967unsigned int ProgramD3D::getSerial() const
1968{
1969 return mSerial;
1970}
1971
1972unsigned int ProgramD3D::issueSerial()
1973{
1974 return mCurrentSerial++;
1975}
1976
Jamie Madill437d2662014-12-05 14:23:35 -05001977void ProgramD3D::initAttributesByLayout()
1978{
1979 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1980 {
1981 mAttributesByLayout[i] = i;
1982 }
1983
1984 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
1985}
1986
1987void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
1988 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
1989{
1990 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
1991
1992 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1993 {
1994 oldTranslatedAttributes[i] = attributes[i];
1995 }
1996
1997 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1998 {
1999 int oldIndex = mAttributesByLayout[i];
2000 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2001 attributes[i] = oldTranslatedAttributes[oldIndex];
2002 }
2003}
2004
Brandon Jonesc9610c52014-08-25 17:02:59 -07002005}