blob: 3ec61805cdc8f9b9ae658c894a9b943cd84becb8 [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
Austin Kinross434953e2015-02-20 10:49:51 -080023#if ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
24// We do not want to set _HAS_EXCEPTIONS=1 for Clang builds, nor turn on exception handlers.
25// We therefore must disable C4530 to allow <future> to compile.
26#pragma warning(disable: 4530) // C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
27#include <eh.h> // To allow <future> to compile when _HAS_EXCEPTIONS=0.
28#include <future> // For std::async
29#endif // ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
30
Brandon Jonesc9610c52014-08-25 17:02:59 -070031namespace rx
32{
33
Brandon Joneseb994362014-09-24 10:27:28 -070034namespace
35{
36
Brandon Jones1a8a7e32014-10-01 12:49:30 -070037GLenum GetTextureType(GLenum samplerType)
38{
39 switch (samplerType)
40 {
41 case GL_SAMPLER_2D:
42 case GL_INT_SAMPLER_2D:
43 case GL_UNSIGNED_INT_SAMPLER_2D:
44 case GL_SAMPLER_2D_SHADOW:
45 return GL_TEXTURE_2D;
46 case GL_SAMPLER_3D:
47 case GL_INT_SAMPLER_3D:
48 case GL_UNSIGNED_INT_SAMPLER_3D:
49 return GL_TEXTURE_3D;
50 case GL_SAMPLER_CUBE:
51 case GL_SAMPLER_CUBE_SHADOW:
52 return GL_TEXTURE_CUBE_MAP;
53 case GL_INT_SAMPLER_CUBE:
54 case GL_UNSIGNED_INT_SAMPLER_CUBE:
55 return GL_TEXTURE_CUBE_MAP;
56 case GL_SAMPLER_2D_ARRAY:
57 case GL_INT_SAMPLER_2D_ARRAY:
58 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
59 case GL_SAMPLER_2D_ARRAY_SHADOW:
60 return GL_TEXTURE_2D_ARRAY;
61 default: UNREACHABLE();
62 }
63
64 return GL_TEXTURE_2D;
65}
66
Brandon Joneseb994362014-09-24 10:27:28 -070067void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS])
68{
69 size_t layoutIndex = 0;
70 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
71 {
72 ASSERT(layoutIndex < gl::MAX_VERTEX_ATTRIBS);
73
74 const sh::Attribute &shaderAttr = shaderAttributes[attributeIndex];
75
76 if (shaderAttr.type != GL_NONE)
77 {
78 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
79
80 for (size_t rowIndex = 0; static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); rowIndex++, layoutIndex++)
81 {
82 gl::VertexFormat *defaultFormat = &inputLayout[layoutIndex];
83
84 defaultFormat->mType = gl::VariableComponentType(transposedType);
85 defaultFormat->mNormalized = false;
86 defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool
87 defaultFormat->mComponents = gl::VariableColumnCount(transposedType);
88 }
89 }
90 }
91}
92
93std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
94{
Jamie Madillb4463142014-12-19 14:56:54 -050095 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070096
Jamie Madillb4463142014-12-19 14:56:54 -050097 if (!shaderOutputVars.empty())
98 {
99 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
100 }
Brandon Joneseb994362014-09-24 10:27:28 -0700101
102 return defaultPixelOutput;
103}
104
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700105bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
106{
107 return var.isRowMajorLayout;
108}
109
110bool IsRowMajorLayout(const sh::ShaderVariable &var)
111{
112 return false;
113}
114
Jamie Madill437d2662014-12-05 14:23:35 -0500115struct AttributeSorter
116{
117 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -0500118 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -0500119 {
120 }
121
122 bool operator()(int a, int b)
123 {
Jamie Madill80d934b2015-02-19 10:16:12 -0500124 int indexA = (*originalIndices)[a];
125 int indexB = (*originalIndices)[b];
126
127 if (indexA == -1) return false;
128 if (indexB == -1) return true;
129 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -0500130 }
131
Jamie Madill80d934b2015-02-19 10:16:12 -0500132 const ProgramImpl::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500133};
134
Brandon Joneseb994362014-09-24 10:27:28 -0700135}
136
137ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
138 const GLenum signature[],
Geoff Lang359ef262015-01-05 14:42:29 -0500139 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700140 : mShaderExecutable(shaderExecutable)
141{
142 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
143 {
144 mInputs[attributeIndex] = inputLayout[attributeIndex];
145 mSignature[attributeIndex] = signature[attributeIndex];
146 }
147}
148
149ProgramD3D::VertexExecutable::~VertexExecutable()
150{
151 SafeDelete(mShaderExecutable);
152}
153
154bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
155{
156 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
157 {
158 if (mSignature[attributeIndex] != signature[attributeIndex])
159 {
160 return false;
161 }
162 }
163
164 return true;
165}
166
Geoff Lang359ef262015-01-05 14:42:29 -0500167ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700168 : mOutputSignature(outputSignature),
169 mShaderExecutable(shaderExecutable)
170{
171}
172
173ProgramD3D::PixelExecutable::~PixelExecutable()
174{
175 SafeDelete(mShaderExecutable);
176}
177
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700178ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
179{
180}
181
Geoff Lang7dd2e102014-11-10 15:19:26 -0500182unsigned int ProgramD3D::mCurrentSerial = 1;
183
Jamie Madill93e13fb2014-11-06 15:27:25 -0500184ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700185 : ProgramImpl(),
186 mRenderer(renderer),
187 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700188 mGeometryExecutable(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700189 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700190 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700191 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700192 mUsedVertexSamplerRange(0),
193 mUsedPixelSamplerRange(0),
194 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500195 mShaderVersion(100),
196 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700197{
Brandon Joneseb994362014-09-24 10:27:28 -0700198 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700199}
200
201ProgramD3D::~ProgramD3D()
202{
203 reset();
204 SafeDelete(mDynamicHLSL);
205}
206
Brandon Jones44151a92014-09-10 11:32:25 -0700207bool ProgramD3D::usesPointSpriteEmulation() const
208{
209 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
210}
211
212bool ProgramD3D::usesGeometryShader() const
213{
Cooper Partine6664f02015-01-09 16:22:24 -0800214 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
215}
216
217bool ProgramD3D::usesInstancedPointSpriteEmulation() const
218{
219 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700220}
221
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700222GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
223{
224 GLint logicalTextureUnit = -1;
225
226 switch (type)
227 {
228 case gl::SAMPLER_PIXEL:
229 ASSERT(samplerIndex < caps.maxTextureImageUnits);
230 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
231 {
232 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
233 }
234 break;
235 case gl::SAMPLER_VERTEX:
236 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
237 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
238 {
239 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
240 }
241 break;
242 default: UNREACHABLE();
243 }
244
245 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
246 {
247 return logicalTextureUnit;
248 }
249
250 return -1;
251}
252
253// Returns the texture type for a given Direct3D 9 sampler type and
254// index (0-15 for the pixel shader and 0-3 for the vertex shader).
255GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
256{
257 switch (type)
258 {
259 case gl::SAMPLER_PIXEL:
260 ASSERT(samplerIndex < mSamplersPS.size());
261 ASSERT(mSamplersPS[samplerIndex].active);
262 return mSamplersPS[samplerIndex].textureType;
263 case gl::SAMPLER_VERTEX:
264 ASSERT(samplerIndex < mSamplersVS.size());
265 ASSERT(mSamplersVS[samplerIndex].active);
266 return mSamplersVS[samplerIndex].textureType;
267 default: UNREACHABLE();
268 }
269
270 return GL_TEXTURE_2D;
271}
272
273GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
274{
275 switch (type)
276 {
277 case gl::SAMPLER_PIXEL:
278 return mUsedPixelSamplerRange;
279 case gl::SAMPLER_VERTEX:
280 return mUsedVertexSamplerRange;
281 default:
282 UNREACHABLE();
283 return 0;
284 }
285}
286
287void ProgramD3D::updateSamplerMapping()
288{
289 if (!mDirtySamplerMapping)
290 {
291 return;
292 }
293
294 mDirtySamplerMapping = false;
295
296 // Retrieve sampler uniform values
297 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
298 {
299 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
300
301 if (targetUniform->dirty)
302 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500303 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700304 {
305 int count = targetUniform->elementCount();
306 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
307
308 if (targetUniform->isReferencedByFragmentShader())
309 {
310 unsigned int firstIndex = targetUniform->psRegisterIndex;
311
312 for (int i = 0; i < count; i++)
313 {
314 unsigned int samplerIndex = firstIndex + i;
315
316 if (samplerIndex < mSamplersPS.size())
317 {
318 ASSERT(mSamplersPS[samplerIndex].active);
319 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
320 }
321 }
322 }
323
324 if (targetUniform->isReferencedByVertexShader())
325 {
326 unsigned int firstIndex = targetUniform->vsRegisterIndex;
327
328 for (int i = 0; i < count; i++)
329 {
330 unsigned int samplerIndex = firstIndex + i;
331
332 if (samplerIndex < mSamplersVS.size())
333 {
334 ASSERT(mSamplersVS[samplerIndex].active);
335 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
336 }
337 }
338 }
339 }
340 }
341 }
342}
343
344bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
345{
346 // if any two active samplers in a program are of different types, but refer to the same
347 // texture image unit, and this is the current program, then ValidateProgram will fail, and
348 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
349 updateSamplerMapping();
350
351 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
352
353 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
354 {
355 if (mSamplersPS[i].active)
356 {
357 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
358
359 if (unit >= textureUnitTypes.size())
360 {
361 if (infoLog)
362 {
363 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
364 }
365
366 return false;
367 }
368
369 if (textureUnitTypes[unit] != GL_NONE)
370 {
371 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
372 {
373 if (infoLog)
374 {
375 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
376 }
377
378 return false;
379 }
380 }
381 else
382 {
383 textureUnitTypes[unit] = mSamplersPS[i].textureType;
384 }
385 }
386 }
387
388 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
389 {
390 if (mSamplersVS[i].active)
391 {
392 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
393
394 if (unit >= textureUnitTypes.size())
395 {
396 if (infoLog)
397 {
398 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
399 }
400
401 return false;
402 }
403
404 if (textureUnitTypes[unit] != GL_NONE)
405 {
406 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
407 {
408 if (infoLog)
409 {
410 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
411 }
412
413 return false;
414 }
415 }
416 else
417 {
418 textureUnitTypes[unit] = mSamplersVS[i].textureType;
419 }
420 }
421 }
422
423 return true;
424}
425
Geoff Lang7dd2e102014-11-10 15:19:26 -0500426LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700427{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500428 int compileFlags = stream->readInt<int>();
429 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
430 {
431 infoLog.append("Mismatched compilation flags.");
432 return LinkResult(false, gl::Error(GL_NO_ERROR));
433 }
434
Brandon Jones44151a92014-09-10 11:32:25 -0700435 stream->readInt(&mShaderVersion);
436
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700437 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
438 for (unsigned int i = 0; i < psSamplerCount; ++i)
439 {
440 Sampler sampler;
441 stream->readBool(&sampler.active);
442 stream->readInt(&sampler.logicalTextureUnit);
443 stream->readInt(&sampler.textureType);
444 mSamplersPS.push_back(sampler);
445 }
446 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
447 for (unsigned int i = 0; i < vsSamplerCount; ++i)
448 {
449 Sampler sampler;
450 stream->readBool(&sampler.active);
451 stream->readInt(&sampler.logicalTextureUnit);
452 stream->readInt(&sampler.textureType);
453 mSamplersVS.push_back(sampler);
454 }
455
456 stream->readInt(&mUsedVertexSamplerRange);
457 stream->readInt(&mUsedPixelSamplerRange);
458
459 const unsigned int uniformCount = stream->readInt<unsigned int>();
460 if (stream->error())
461 {
462 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500463 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700464 }
465
466 mUniforms.resize(uniformCount);
467 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
468 {
469 GLenum type = stream->readInt<GLenum>();
470 GLenum precision = stream->readInt<GLenum>();
471 std::string name = stream->readString();
472 unsigned int arraySize = stream->readInt<unsigned int>();
473 int blockIndex = stream->readInt<int>();
474
475 int offset = stream->readInt<int>();
476 int arrayStride = stream->readInt<int>();
477 int matrixStride = stream->readInt<int>();
478 bool isRowMajorMatrix = stream->readBool();
479
480 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
481
482 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
483
484 stream->readInt(&uniform->psRegisterIndex);
485 stream->readInt(&uniform->vsRegisterIndex);
486 stream->readInt(&uniform->registerCount);
487 stream->readInt(&uniform->registerElement);
488
489 mUniforms[uniformIndex] = uniform;
490 }
491
492 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
493 if (stream->error())
494 {
495 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500496 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700497 }
498
499 mUniformIndex.resize(uniformIndexCount);
500 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
501 {
502 stream->readString(&mUniformIndex[uniformIndexIndex].name);
503 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
504 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
505 }
506
507 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
508 if (stream->error())
509 {
510 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500511 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700512 }
513
514 mUniformBlocks.resize(uniformBlockCount);
515 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
516 {
517 std::string name = stream->readString();
518 unsigned int elementIndex = stream->readInt<unsigned int>();
519 unsigned int dataSize = stream->readInt<unsigned int>();
520
521 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
522
523 stream->readInt(&uniformBlock->psRegisterIndex);
524 stream->readInt(&uniformBlock->vsRegisterIndex);
525
526 unsigned int numMembers = stream->readInt<unsigned int>();
527 uniformBlock->memberUniformIndexes.resize(numMembers);
528 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
529 {
530 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
531 }
532
533 mUniformBlocks[uniformBlockIndex] = uniformBlock;
534 }
535
Brandon Joneseb994362014-09-24 10:27:28 -0700536 stream->readInt(&mTransformFeedbackBufferMode);
537 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
538 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
539 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
540 {
541 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
542
543 stream->readString(&varying.name);
544 stream->readInt(&varying.type);
545 stream->readInt(&varying.size);
546 stream->readString(&varying.semanticName);
547 stream->readInt(&varying.semanticIndex);
548 stream->readInt(&varying.semanticIndexCount);
549 }
550
Brandon Jones22502d52014-08-29 16:58:36 -0700551 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530552 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700553 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530554 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700555 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700556 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700557
558 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
559 mPixelShaderKey.resize(pixelShaderKeySize);
560 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
561 {
562 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
563 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
564 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
565 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
566 }
567
Brandon Joneseb994362014-09-24 10:27:28 -0700568 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
569
570 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
571 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
572 {
573 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
574
575 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
576 {
577 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
578 stream->readInt(&vertexInput->mType);
579 stream->readInt(&vertexInput->mNormalized);
580 stream->readInt(&vertexInput->mComponents);
581 stream->readBool(&vertexInput->mPureInteger);
582 }
583
584 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
585 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400586
Geoff Lang359ef262015-01-05 14:42:29 -0500587 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400588 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
589 SHADER_VERTEX,
590 mTransformFeedbackLinkedVaryings,
591 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
592 &shaderExecutable);
593 if (error.isError())
594 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500595 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400596 }
597
Brandon Joneseb994362014-09-24 10:27:28 -0700598 if (!shaderExecutable)
599 {
600 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500601 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700602 }
603
604 // generated converted input layout
605 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
606 getInputLayoutSignature(inputLayout, signature);
607
608 // add new binary
609 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
610
611 stream->skip(vertexShaderSize);
612 }
613
614 const size_t pixelShaderCount = stream->readInt<unsigned int>();
615 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
616 {
617 const size_t outputCount = stream->readInt<unsigned int>();
618 std::vector<GLenum> outputs(outputCount);
619 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
620 {
621 stream->readInt(&outputs[outputIndex]);
622 }
623
624 const size_t pixelShaderSize = stream->readInt<unsigned int>();
625 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500626 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400627 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
628 mTransformFeedbackLinkedVaryings,
629 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
630 &shaderExecutable);
631 if (error.isError())
632 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500633 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400634 }
Brandon Joneseb994362014-09-24 10:27:28 -0700635
636 if (!shaderExecutable)
637 {
638 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500639 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700640 }
641
642 // add new binary
643 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
644
645 stream->skip(pixelShaderSize);
646 }
647
648 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
649
650 if (geometryShaderSize > 0)
651 {
652 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400653 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
654 mTransformFeedbackLinkedVaryings,
655 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
656 &mGeometryExecutable);
657 if (error.isError())
658 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500659 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400660 }
Brandon Joneseb994362014-09-24 10:27:28 -0700661
662 if (!mGeometryExecutable)
663 {
664 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500665 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700666 }
667 stream->skip(geometryShaderSize);
668 }
669
Brandon Jones18bd4102014-09-22 14:21:44 -0700670 GUID binaryIdentifier = {0};
671 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
672
673 GUID identifier = mRenderer->getAdapterIdentifier();
674 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
675 {
676 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500677 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700678 }
679
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700680 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500681 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700682
Geoff Lang7dd2e102014-11-10 15:19:26 -0500683 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700684}
685
Geoff Langb543aff2014-09-30 14:52:54 -0400686gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700687{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500688 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
689
Brandon Jones44151a92014-09-10 11:32:25 -0700690 stream->writeInt(mShaderVersion);
691
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700692 stream->writeInt(mSamplersPS.size());
693 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
694 {
695 stream->writeInt(mSamplersPS[i].active);
696 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
697 stream->writeInt(mSamplersPS[i].textureType);
698 }
699
700 stream->writeInt(mSamplersVS.size());
701 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
702 {
703 stream->writeInt(mSamplersVS[i].active);
704 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
705 stream->writeInt(mSamplersVS[i].textureType);
706 }
707
708 stream->writeInt(mUsedVertexSamplerRange);
709 stream->writeInt(mUsedPixelSamplerRange);
710
711 stream->writeInt(mUniforms.size());
712 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
713 {
714 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
715
716 stream->writeInt(uniform.type);
717 stream->writeInt(uniform.precision);
718 stream->writeString(uniform.name);
719 stream->writeInt(uniform.arraySize);
720 stream->writeInt(uniform.blockIndex);
721
722 stream->writeInt(uniform.blockInfo.offset);
723 stream->writeInt(uniform.blockInfo.arrayStride);
724 stream->writeInt(uniform.blockInfo.matrixStride);
725 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
726
727 stream->writeInt(uniform.psRegisterIndex);
728 stream->writeInt(uniform.vsRegisterIndex);
729 stream->writeInt(uniform.registerCount);
730 stream->writeInt(uniform.registerElement);
731 }
732
733 stream->writeInt(mUniformIndex.size());
734 for (size_t i = 0; i < mUniformIndex.size(); ++i)
735 {
736 stream->writeString(mUniformIndex[i].name);
737 stream->writeInt(mUniformIndex[i].element);
738 stream->writeInt(mUniformIndex[i].index);
739 }
740
741 stream->writeInt(mUniformBlocks.size());
742 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
743 {
744 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
745
746 stream->writeString(uniformBlock.name);
747 stream->writeInt(uniformBlock.elementIndex);
748 stream->writeInt(uniformBlock.dataSize);
749
750 stream->writeInt(uniformBlock.memberUniformIndexes.size());
751 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
752 {
753 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
754 }
755
756 stream->writeInt(uniformBlock.psRegisterIndex);
757 stream->writeInt(uniformBlock.vsRegisterIndex);
758 }
759
Brandon Joneseb994362014-09-24 10:27:28 -0700760 stream->writeInt(mTransformFeedbackBufferMode);
761 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
762 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
763 {
764 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
765
766 stream->writeString(varying.name);
767 stream->writeInt(varying.type);
768 stream->writeInt(varying.size);
769 stream->writeString(varying.semanticName);
770 stream->writeInt(varying.semanticIndex);
771 stream->writeInt(varying.semanticIndexCount);
772 }
773
Brandon Jones22502d52014-08-29 16:58:36 -0700774 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530775 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700776 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530777 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700778 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700779 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700780
Brandon Joneseb994362014-09-24 10:27:28 -0700781 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700782 stream->writeInt(pixelShaderKey.size());
783 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
784 {
Brandon Joneseb994362014-09-24 10:27:28 -0700785 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700786 stream->writeInt(variable.type);
787 stream->writeString(variable.name);
788 stream->writeString(variable.source);
789 stream->writeInt(variable.outputIndex);
790 }
791
Brandon Joneseb994362014-09-24 10:27:28 -0700792 stream->writeInt(mVertexExecutables.size());
793 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
794 {
795 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
796
797 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
798 {
799 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
800 stream->writeInt(vertexInput.mType);
801 stream->writeInt(vertexInput.mNormalized);
802 stream->writeInt(vertexInput.mComponents);
803 stream->writeInt(vertexInput.mPureInteger);
804 }
805
806 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
807 stream->writeInt(vertexShaderSize);
808
809 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
810 stream->writeBytes(vertexBlob, vertexShaderSize);
811 }
812
813 stream->writeInt(mPixelExecutables.size());
814 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
815 {
816 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
817
818 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
819 stream->writeInt(outputs.size());
820 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
821 {
822 stream->writeInt(outputs[outputIndex]);
823 }
824
825 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
826 stream->writeInt(pixelShaderSize);
827
828 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
829 stream->writeBytes(pixelBlob, pixelShaderSize);
830 }
831
832 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
833 stream->writeInt(geometryShaderSize);
834
835 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
836 {
837 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
838 stream->writeBytes(geometryBlob, geometryShaderSize);
839 }
840
Brandon Jones18bd4102014-09-22 14:21:44 -0700841 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400842 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700843
Geoff Langb543aff2014-09-30 14:52:54 -0400844 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700845}
846
Geoff Lang359ef262015-01-05 14:42:29 -0500847gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700848{
Brandon Joneseb994362014-09-24 10:27:28 -0700849 std::vector<GLenum> outputs;
850
Jamie Madill85a18042015-03-05 15:41:41 -0500851 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
852 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700853
854 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
855 {
856 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
857
858 if (colorbuffer)
859 {
860 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
861 }
862 else
863 {
864 outputs.push_back(GL_NONE);
865 }
866 }
867
Jamie Madill97399232014-12-23 12:31:15 -0500868 return getPixelExecutableForOutputLayout(outputs, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700869}
870
Jamie Madill97399232014-12-23 12:31:15 -0500871gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500872 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500873 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700874{
875 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
876 {
877 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
878 {
Geoff Langb543aff2014-09-30 14:52:54 -0400879 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
880 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700881 }
882 }
883
Brandon Jones22502d52014-08-29 16:58:36 -0700884 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
885 outputSignature);
886
887 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500888 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500889
890 gl::InfoLog tempInfoLog;
891 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
892
893 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400894 mTransformFeedbackLinkedVaryings,
895 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
896 mPixelWorkarounds, &pixelExecutable);
897 if (error.isError())
898 {
899 return error;
900 }
Brandon Joneseb994362014-09-24 10:27:28 -0700901
Jamie Madill97399232014-12-23 12:31:15 -0500902 if (pixelExecutable)
903 {
904 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
905 }
906 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700907 {
908 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
909 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
910 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
911 }
Brandon Jones22502d52014-08-29 16:58:36 -0700912
Geoff Langb543aff2014-09-30 14:52:54 -0400913 *outExectuable = pixelExecutable;
914 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700915}
916
Jamie Madill97399232014-12-23 12:31:15 -0500917gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500918 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500919 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700920{
Brandon Joneseb994362014-09-24 10:27:28 -0700921 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
922 getInputLayoutSignature(inputLayout, signature);
923
924 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
925 {
926 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
927 {
Geoff Langb543aff2014-09-30 14:52:54 -0400928 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
929 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700930 }
931 }
932
Brandon Jones22502d52014-08-29 16:58:36 -0700933 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700934 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700935
936 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500937 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500938
939 gl::InfoLog tempInfoLog;
940 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
941
942 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400943 mTransformFeedbackLinkedVaryings,
944 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
945 mVertexWorkarounds, &vertexExecutable);
946 if (error.isError())
947 {
948 return error;
949 }
950
Jamie Madill97399232014-12-23 12:31:15 -0500951 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700952 {
953 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
954 }
Jamie Madill97399232014-12-23 12:31:15 -0500955 else if (!infoLog)
956 {
Austin Kinross434953e2015-02-20 10:49:51 -0800957 // This isn't thread-safe, so we should ensure that we always pass in an infoLog if using multiple threads.
Jamie Madill97399232014-12-23 12:31:15 -0500958 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
959 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
960 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
961 }
Brandon Jones22502d52014-08-29 16:58:36 -0700962
Geoff Langb543aff2014-09-30 14:52:54 -0400963 *outExectuable = vertexExecutable;
964 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700965}
966
Geoff Lang7dd2e102014-11-10 15:19:26 -0500967LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
968 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700969{
Brandon Jones18bd4102014-09-22 14:21:44 -0700970 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
971 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700972
Austin Kinross434953e2015-02-20 10:49:51 -0800973 gl::Error vertexShaderResult(GL_NO_ERROR);
974 gl::InfoLog tempVertexShaderInfoLog;
Austin Kinrossaf1bdff2015-02-17 11:07:46 -0800975
Austin Kinross434953e2015-02-20 10:49:51 -0800976#if ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
977 // Use an async task to begin compiling the vertex shader asynchronously on its own task.
978 std::future<ShaderExecutableD3D*> vertexShaderTask = std::async([this, vertexShader, &tempVertexShaderInfoLog, &vertexShaderResult]()
979 {
980#endif // ANGLE_MULTITHREADED_D3D_SHADER_COMPILE
981
982 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
983 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
984 ShaderExecutableD3D *defaultVertexExecutable = NULL;
985 vertexShaderResult = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &tempVertexShaderInfoLog);
986
987#if ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
988 return defaultVertexExecutable;
989 });
990#endif // ANGLE_MULTITHREADED_D3D_SHADER_COMPILE
991
992 // Continue to compile the pixel shader on the main thread
Brandon Joneseb994362014-09-24 10:27:28 -0700993 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500994 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Austin Kinross434953e2015-02-20 10:49:51 -0800995 gl::Error error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
996
997#if ANGLE_MULTITHREADED_D3D_SHADER_COMPILE == ANGLE_ENABLED
998 // Call .get() on the vertex shader compilation. This waits until the task is complete before returning
999 ShaderExecutableD3D *defaultVertexExecutable = vertexShaderTask.get();
1000#endif // ANGLE_MULTITHREADED_D3D_SHADER_COMPILE
1001
1002 // Combine the temporary infoLog with the real one
1003 if (tempVertexShaderInfoLog.getLength() > 0)
1004 {
1005 std::vector<char> tempCharBuffer(tempVertexShaderInfoLog.getLength() + 3);
1006 tempVertexShaderInfoLog.getLog(tempVertexShaderInfoLog.getLength(), NULL, &tempCharBuffer[0]);
1007 infoLog.append(&tempCharBuffer[0]);
1008 }
1009
1010 if (vertexShaderResult.isError())
1011 {
1012 return LinkResult(false, vertexShaderResult);
1013 }
1014
1015 // If the pixel shader compilation failed, then return error
Geoff Langb543aff2014-09-30 14:52:54 -04001016 if (error.isError())
1017 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001018 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001019 }
Brandon Jones44151a92014-09-10 11:32:25 -07001020
Brandon Joneseb994362014-09-24 10:27:28 -07001021 if (usesGeometryShader())
1022 {
1023 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001024
Geoff Langb543aff2014-09-30 14:52:54 -04001025
1026 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1027 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +05301028 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001029 if (error.isError())
1030 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001031 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001032 }
Brandon Joneseb994362014-09-24 10:27:28 -07001033 }
1034
Brandon Jones091540d2014-10-29 11:32:04 -07001035#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001036 if (usesGeometryShader() && mGeometryExecutable)
1037 {
1038 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1039 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1040 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1041 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1042 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1043 }
1044
1045 if (defaultVertexExecutable)
1046 {
1047 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1048 }
1049
1050 if (defaultPixelExecutable)
1051 {
1052 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1053 }
1054#endif
1055
Geoff Langb543aff2014-09-30 14:52:54 -04001056 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001057 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001058}
1059
Geoff Lang7dd2e102014-11-10 15:19:26 -05001060LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1061 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1062 const std::vector<std::string> &transformFeedbackVaryings,
1063 GLenum transformFeedbackBufferMode,
1064 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1065 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001066{
Brandon Joneseb994362014-09-24 10:27:28 -07001067 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
1068 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
1069
Jamie Madillde8892b2014-11-11 13:00:22 -05001070 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1071 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001072
Brandon Joneseb994362014-09-24 10:27:28 -07001073 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001074
1075 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301076 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001077
1078 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301079 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001080 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001081
1082 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001083 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001084 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1085
Geoff Langbdee2d52014-09-17 11:02:51 -04001086 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001087 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001088 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001089 }
1090
Geoff Lang7dd2e102014-11-10 15:19:26 -05001091 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001092 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001093 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001094 }
1095
Jamie Madillde8892b2014-11-11 13:00:22 -05001096 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001097 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1098 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1099 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001100 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001101 }
1102
Brandon Jones44151a92014-09-10 11:32:25 -07001103 mUsesPointSize = vertexShaderD3D->usesPointSize();
1104
Jamie Madill437d2662014-12-05 14:23:35 -05001105 initAttributesByLayout();
1106
Geoff Lang7dd2e102014-11-10 15:19:26 -05001107 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001108}
1109
Brandon Jones44151a92014-09-10 11:32:25 -07001110void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1111{
1112 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1113}
1114
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001115void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001116{
1117 // Compute total default block size
1118 unsigned int vertexRegisters = 0;
1119 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001120 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001121 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001122 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001123
Geoff Lang2ec386b2014-12-03 14:44:38 -05001124 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001125 {
1126 if (uniform.isReferencedByVertexShader())
1127 {
1128 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1129 }
1130 if (uniform.isReferencedByFragmentShader())
1131 {
1132 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1133 }
1134 }
1135 }
1136
1137 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1138 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1139}
1140
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001141gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001142{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001143 updateSamplerMapping();
1144
1145 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1146 if (error.isError())
1147 {
1148 return error;
1149 }
1150
1151 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1152 {
1153 mUniforms[uniformIndex]->dirty = false;
1154 }
1155
1156 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001157}
1158
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001159gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001160{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001161 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1162
Brandon Jones18bd4102014-09-22 14:21:44 -07001163 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1164 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1165
1166 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1167 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1168
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001169 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001170 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001171 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001172 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1173
1174 ASSERT(uniformBlock && uniformBuffer);
1175
1176 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1177 {
1178 // undefined behaviour
1179 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1180 }
1181
1182 // Unnecessary to apply an unreferenced standard or shared UBO
1183 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1184 {
1185 continue;
1186 }
1187
1188 if (uniformBlock->isReferencedByVertexShader())
1189 {
1190 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1191 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1192 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1193 vertexUniformBuffers[registerIndex] = uniformBuffer;
1194 }
1195
1196 if (uniformBlock->isReferencedByFragmentShader())
1197 {
1198 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1199 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1200 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1201 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1202 }
1203 }
1204
1205 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1206}
1207
1208bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1209 unsigned int registerIndex, const gl::Caps &caps)
1210{
1211 if (shader == GL_VERTEX_SHADER)
1212 {
1213 uniformBlock->vsRegisterIndex = registerIndex;
1214 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1215 {
1216 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1217 return false;
1218 }
1219 }
1220 else if (shader == GL_FRAGMENT_SHADER)
1221 {
1222 uniformBlock->psRegisterIndex = registerIndex;
1223 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1224 {
1225 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1226 return false;
1227 }
1228 }
1229 else UNREACHABLE();
1230
1231 return true;
1232}
1233
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001234void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001235{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001236 unsigned int numUniforms = mUniforms.size();
1237 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001238 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001239 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001240 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001241}
1242
1243void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1244{
1245 setUniform(location, count, v, GL_FLOAT);
1246}
1247
1248void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1249{
1250 setUniform(location, count, v, GL_FLOAT_VEC2);
1251}
1252
1253void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1254{
1255 setUniform(location, count, v, GL_FLOAT_VEC3);
1256}
1257
1258void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1259{
1260 setUniform(location, count, v, GL_FLOAT_VEC4);
1261}
1262
1263void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1264{
1265 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1266}
1267
1268void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1269{
1270 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1271}
1272
1273void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1274{
1275 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1276}
1277
1278void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1279{
1280 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1281}
1282
1283void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1284{
1285 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1286}
1287
1288void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1289{
1290 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1291}
1292
1293void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1294{
1295 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1296}
1297
1298void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1299{
1300 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1301}
1302
1303void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1304{
1305 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1306}
1307
1308void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1309{
1310 setUniform(location, count, v, GL_INT);
1311}
1312
1313void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1314{
1315 setUniform(location, count, v, GL_INT_VEC2);
1316}
1317
1318void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1319{
1320 setUniform(location, count, v, GL_INT_VEC3);
1321}
1322
1323void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1324{
1325 setUniform(location, count, v, GL_INT_VEC4);
1326}
1327
1328void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1329{
1330 setUniform(location, count, v, GL_UNSIGNED_INT);
1331}
1332
1333void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1334{
1335 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1336}
1337
1338void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1339{
1340 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1341}
1342
1343void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1344{
1345 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1346}
1347
1348void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1349{
1350 getUniformv(location, params, GL_FLOAT);
1351}
1352
1353void ProgramD3D::getUniformiv(GLint location, GLint *params)
1354{
1355 getUniformv(location, params, GL_INT);
1356}
1357
1358void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1359{
1360 getUniformv(location, params, GL_UNSIGNED_INT);
1361}
1362
1363bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1364 const gl::Caps &caps)
1365{
Jamie Madill30d6c252014-11-13 10:03:33 -05001366 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1367 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001368
1369 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1370 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1371
1372 // Check that uniforms defined in the vertex and fragment shaders are identical
1373 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1374 UniformMap linkedUniforms;
1375
1376 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001377 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001378 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1379 linkedUniforms[vertexUniform.name] = &vertexUniform;
1380 }
1381
1382 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1383 {
1384 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1385 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1386 if (entry != linkedUniforms.end())
1387 {
1388 const sh::Uniform &vertexUniform = *entry->second;
1389 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001391 {
1392 return false;
1393 }
1394 }
1395 }
1396
1397 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1398 {
1399 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1400
1401 if (uniform.staticUse)
1402 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001403 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001404 }
1405 }
1406
1407 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1408 {
1409 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1410
1411 if (uniform.staticUse)
1412 {
Geoff Lang492a7e42014-11-05 13:27:06 -05001413 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001414 }
1415 }
1416
1417 if (!indexUniforms(infoLog, caps))
1418 {
1419 return false;
1420 }
1421
1422 initializeUniformStorage();
1423
1424 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1425 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1426 {
1427 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1428
1429 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1430 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1431 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1432 }
1433
1434 return true;
1435}
1436
Geoff Lang492a7e42014-11-05 13:27:06 -05001437void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001438{
Geoff Lang492a7e42014-11-05 13:27:06 -05001439 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001440 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1441 encoder.skipRegisters(uniformRegister);
1442
1443 defineUniform(shader, uniform, uniform.name, &encoder);
1444}
1445
Geoff Lang492a7e42014-11-05 13:27:06 -05001446void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1447 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001448{
1449 if (uniform.isStruct())
1450 {
1451 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1452 {
1453 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1454
1455 encoder->enterAggregateType();
1456
1457 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1458 {
1459 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1460 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1461
1462 defineUniform(shader, field, fieldFullName, encoder);
1463 }
1464
1465 encoder->exitAggregateType();
1466 }
1467 }
1468 else // Not a struct
1469 {
1470 // Arrays are treated as aggregate types
1471 if (uniform.isArray())
1472 {
1473 encoder->enterAggregateType();
1474 }
1475
1476 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1477
Jamie Madill2857f482015-02-09 15:35:29 -05001478 // Advance the uniform offset, to track registers allocation for structs
1479 sh::BlockMemberInfo blockInfo = encoder->encodeType(uniform.type, uniform.arraySize, false);
1480
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001481 if (!linkedUniform)
1482 {
1483 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001484 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001485 ASSERT(linkedUniform);
Jamie Madill2857f482015-02-09 15:35:29 -05001486 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001487 mUniforms.push_back(linkedUniform);
1488 }
1489
Geoff Lang492a7e42014-11-05 13:27:06 -05001490 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001491 {
Jamie Madill2857f482015-02-09 15:35:29 -05001492 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001493 }
Geoff Lang492a7e42014-11-05 13:27:06 -05001494 else if (shader->getShaderType() == GL_VERTEX_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001495 {
Jamie Madill2857f482015-02-09 15:35:29 -05001496 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001497 }
1498 else UNREACHABLE();
1499
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001500 // Arrays are treated as aggregate types
1501 if (uniform.isArray())
1502 {
1503 encoder->exitAggregateType();
1504 }
1505 }
1506}
1507
1508template <typename T>
1509static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1510{
1511 ASSERT(dest != NULL);
1512 ASSERT(dirtyFlag != NULL);
1513
1514 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1515 *dest = source;
1516}
1517
1518template <typename T>
1519void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1520{
1521 const int components = gl::VariableComponentCount(targetUniformType);
1522 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1523
1524 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1525
1526 int elementCount = targetUniform->elementCount();
1527
1528 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1529
1530 if (targetUniform->type == targetUniformType)
1531 {
1532 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1533
1534 for (int i = 0; i < count; i++)
1535 {
1536 T *dest = target + (i * 4);
1537 const T *source = v + (i * components);
1538
1539 for (int c = 0; c < components; c++)
1540 {
1541 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1542 }
1543 for (int c = components; c < 4; c++)
1544 {
1545 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1546 }
1547 }
1548 }
1549 else if (targetUniform->type == targetBoolType)
1550 {
1551 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1552
1553 for (int i = 0; i < count; i++)
1554 {
1555 GLint *dest = boolParams + (i * 4);
1556 const T *source = v + (i * components);
1557
1558 for (int c = 0; c < components; c++)
1559 {
1560 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1561 }
1562 for (int c = components; c < 4; c++)
1563 {
1564 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1565 }
1566 }
1567 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001568 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001569 {
1570 ASSERT(targetUniformType == GL_INT);
1571
1572 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1573
1574 bool wasDirty = targetUniform->dirty;
1575
1576 for (int i = 0; i < count; i++)
1577 {
1578 GLint *dest = target + (i * 4);
1579 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1580
1581 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1582 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1583 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1584 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1585 }
1586
1587 if (!wasDirty && targetUniform->dirty)
1588 {
1589 mDirtySamplerMapping = true;
1590 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001591 }
1592 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001593}
Brandon Jones18bd4102014-09-22 14:21:44 -07001594
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001595template<typename T>
1596bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1597{
1598 bool dirty = false;
1599 int copyWidth = std::min(targetHeight, srcWidth);
1600 int copyHeight = std::min(targetWidth, srcHeight);
1601
1602 for (int x = 0; x < copyWidth; x++)
1603 {
1604 for (int y = 0; y < copyHeight; y++)
1605 {
1606 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1607 }
1608 }
1609 // clear unfilled right side
1610 for (int y = 0; y < copyWidth; y++)
1611 {
1612 for (int x = copyHeight; x < targetWidth; x++)
1613 {
1614 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1615 }
1616 }
1617 // clear unfilled bottom.
1618 for (int y = copyWidth; y < targetHeight; y++)
1619 {
1620 for (int x = 0; x < targetWidth; x++)
1621 {
1622 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1623 }
1624 }
1625
1626 return dirty;
1627}
1628
1629template<typename T>
1630bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1631{
1632 bool dirty = false;
1633 int copyWidth = std::min(targetWidth, srcWidth);
1634 int copyHeight = std::min(targetHeight, srcHeight);
1635
1636 for (int y = 0; y < copyHeight; y++)
1637 {
1638 for (int x = 0; x < copyWidth; x++)
1639 {
1640 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1641 }
1642 }
1643 // clear unfilled right side
1644 for (int y = 0; y < copyHeight; y++)
1645 {
1646 for (int x = copyWidth; x < targetWidth; x++)
1647 {
1648 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1649 }
1650 }
1651 // clear unfilled bottom.
1652 for (int y = copyHeight; y < targetHeight; y++)
1653 {
1654 for (int x = 0; x < targetWidth; x++)
1655 {
1656 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1657 }
1658 }
1659
1660 return dirty;
1661}
1662
1663template <int cols, int rows>
1664void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1665{
1666 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1667
1668 int elementCount = targetUniform->elementCount();
1669
1670 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1671 const unsigned int targetMatrixStride = (4 * rows);
1672 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1673
1674 for (int i = 0; i < count; i++)
1675 {
1676 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1677 if (transpose == GL_FALSE)
1678 {
1679 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1680 }
1681 else
1682 {
1683 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1684 }
1685 target += targetMatrixStride;
1686 value += cols * rows;
1687 }
1688}
1689
1690template <typename T>
1691void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1692{
1693 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1694
1695 if (gl::IsMatrixType(targetUniform->type))
1696 {
1697 const int rows = gl::VariableRowCount(targetUniform->type);
1698 const int cols = gl::VariableColumnCount(targetUniform->type);
1699 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1700 }
1701 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1702 {
1703 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1704 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1705 size * sizeof(T));
1706 }
1707 else
1708 {
1709 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1710 switch (gl::VariableComponentType(targetUniform->type))
1711 {
1712 case GL_BOOL:
1713 {
1714 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1715
1716 for (unsigned int i = 0; i < size; i++)
1717 {
1718 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1719 }
1720 }
1721 break;
1722
1723 case GL_FLOAT:
1724 {
1725 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1726
1727 for (unsigned int i = 0; i < size; i++)
1728 {
1729 params[i] = static_cast<T>(floatParams[i]);
1730 }
1731 }
1732 break;
1733
1734 case GL_INT:
1735 {
1736 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1737
1738 for (unsigned int i = 0; i < size; i++)
1739 {
1740 params[i] = static_cast<T>(intParams[i]);
1741 }
1742 }
1743 break;
1744
1745 case GL_UNSIGNED_INT:
1746 {
1747 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1748
1749 for (unsigned int i = 0; i < size; i++)
1750 {
1751 params[i] = static_cast<T>(uintParams[i]);
1752 }
1753 }
1754 break;
1755
1756 default: UNREACHABLE();
1757 }
1758 }
1759}
1760
1761template <typename VarT>
1762void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1763 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1764 bool inRowMajorLayout)
1765{
1766 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1767 {
1768 const VarT &field = fields[uniformIndex];
1769 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1770
1771 if (field.isStruct())
1772 {
1773 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1774
1775 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1776 {
1777 encoder->enterAggregateType();
1778
1779 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1780 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1781
1782 encoder->exitAggregateType();
1783 }
1784 }
1785 else
1786 {
1787 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1788
1789 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1790
1791 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1792 blockIndex, memberInfo);
1793
1794 // add to uniform list, but not index, since uniform block uniforms have no location
1795 blockUniformIndexes->push_back(mUniforms.size());
1796 mUniforms.push_back(newUniform);
1797 }
1798 }
1799}
1800
1801bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1802 const gl::Caps &caps)
1803{
Jamie Madill30d6c252014-11-13 10:03:33 -05001804 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001805
1806 // create uniform block entries if they do not exist
1807 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1808 {
1809 std::vector<unsigned int> blockUniformIndexes;
1810 const unsigned int blockIndex = mUniformBlocks.size();
1811
1812 // define member uniforms
1813 sh::BlockLayoutEncoder *encoder = NULL;
1814
1815 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1816 {
1817 encoder = new sh::Std140BlockEncoder;
1818 }
1819 else
1820 {
1821 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1822 }
1823 ASSERT(encoder);
1824
1825 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1826
1827 size_t dataSize = encoder->getBlockSize();
1828
1829 // create all the uniform blocks
1830 if (interfaceBlock.arraySize > 0)
1831 {
1832 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1833 {
1834 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1835 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1836 mUniformBlocks.push_back(newUniformBlock);
1837 }
1838 }
1839 else
1840 {
1841 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1842 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1843 mUniformBlocks.push_back(newUniformBlock);
1844 }
1845 }
1846
1847 if (interfaceBlock.staticUse)
1848 {
1849 // Assign registers to the uniform blocks
1850 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1851 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1852 ASSERT(blockIndex != GL_INVALID_INDEX);
1853 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1854
1855 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1856
1857 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1858 {
1859 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1860 ASSERT(uniformBlock->name == interfaceBlock.name);
1861
1862 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1863 interfaceBlockRegister + uniformBlockElement, caps))
1864 {
1865 return false;
1866 }
1867 }
1868 }
1869
1870 return true;
1871}
1872
1873bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1874 GLenum samplerType,
1875 unsigned int samplerCount,
1876 std::vector<Sampler> &outSamplers,
1877 GLuint *outUsedRange)
1878{
1879 unsigned int samplerIndex = startSamplerIndex;
1880
1881 do
1882 {
1883 if (samplerIndex < outSamplers.size())
1884 {
1885 Sampler& sampler = outSamplers[samplerIndex];
1886 sampler.active = true;
1887 sampler.textureType = GetTextureType(samplerType);
1888 sampler.logicalTextureUnit = 0;
1889 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1890 }
1891 else
1892 {
1893 return false;
1894 }
1895
1896 samplerIndex++;
1897 } while (samplerIndex < startSamplerIndex + samplerCount);
1898
1899 return true;
1900}
1901
1902bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1903{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001904 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001905 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1906
1907 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1908 {
1909 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1910 &mUsedVertexSamplerRange))
1911 {
1912 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1913 mSamplersVS.size());
1914 return false;
1915 }
1916
1917 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1918 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1919 {
1920 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1921 caps.maxVertexUniformVectors);
1922 return false;
1923 }
1924 }
1925
1926 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1927 {
1928 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1929 &mUsedPixelSamplerRange))
1930 {
1931 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1932 mSamplersPS.size());
1933 return false;
1934 }
1935
1936 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1937 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1938 {
1939 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1940 caps.maxFragmentUniformVectors);
1941 return false;
1942 }
1943 }
1944
1945 return true;
1946}
1947
1948bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1949{
1950 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1951 {
1952 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1953
Geoff Lang2ec386b2014-12-03 14:44:38 -05001954 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001955 {
1956 if (!indexSamplerUniform(uniform, infoLog, caps))
1957 {
1958 return false;
1959 }
1960 }
1961
1962 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1963 {
1964 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1965 }
1966 }
1967
1968 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001969}
1970
Brandon Jonesc9610c52014-08-25 17:02:59 -07001971void ProgramD3D::reset()
1972{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001973 ProgramImpl::reset();
1974
Brandon Joneseb994362014-09-24 10:27:28 -07001975 SafeDeleteContainer(mVertexExecutables);
1976 SafeDeleteContainer(mPixelExecutables);
1977 SafeDelete(mGeometryExecutable);
1978
1979 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001980
Brandon Jones22502d52014-08-29 16:58:36 -07001981 mVertexHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301982 mVertexWorkarounds.reset();
Brandon Jones44151a92014-09-10 11:32:25 -07001983 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001984
1985 mPixelHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301986 mPixelWorkarounds.reset();
Brandon Jones22502d52014-08-29 16:58:36 -07001987 mUsesFragDepth = false;
1988 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001989 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001990
Brandon Jonesc9610c52014-08-25 17:02:59 -07001991 SafeDelete(mVertexUniformStorage);
1992 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001993
1994 mSamplersPS.clear();
1995 mSamplersVS.clear();
1996
1997 mUsedVertexSamplerRange = 0;
1998 mUsedPixelSamplerRange = 0;
1999 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002000
2001 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002002}
2003
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004unsigned int ProgramD3D::getSerial() const
2005{
2006 return mSerial;
2007}
2008
2009unsigned int ProgramD3D::issueSerial()
2010{
2011 return mCurrentSerial++;
2012}
2013
Jamie Madill437d2662014-12-05 14:23:35 -05002014void ProgramD3D::initAttributesByLayout()
2015{
2016 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2017 {
2018 mAttributesByLayout[i] = i;
2019 }
2020
2021 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2022}
2023
2024void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
2025 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
2026{
2027 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
2028
2029 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2030 {
2031 oldTranslatedAttributes[i] = attributes[i];
2032 }
2033
2034 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2035 {
2036 int oldIndex = mAttributesByLayout[i];
2037 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2038 attributes[i] = oldTranslatedAttributes[oldIndex];
2039 }
2040}
2041
Brandon Jonesc9610c52014-08-25 17:02:59 -07002042}