blob: 15cd8fbad296f59552c25c3084a4f5d9a772b777 [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{
Jamie Madill13776892015-04-28 12:39:06 -0400339 // Skip cache if we're using an infolog, so we get the full error.
340 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
341 if (!mDirtySamplerMapping && infoLog == nullptr && mCachedValidateSamplersResult.valid())
342 {
343 return mCachedValidateSamplersResult.value();
344 }
345
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700346 // 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
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400351 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700352
353 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
354 {
355 if (mSamplersPS[i].active)
356 {
357 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
358
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400359 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700360 {
361 if (infoLog)
362 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400363 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, caps.maxCombinedTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700364 }
365
Jamie Madill13776892015-04-28 12:39:06 -0400366 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700367 return false;
368 }
369
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400370 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700371 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400372 if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700373 {
374 if (infoLog)
375 {
376 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
377 }
378
Jamie Madill13776892015-04-28 12:39:06 -0400379 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700380 return false;
381 }
382 }
383 else
384 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400385 mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700386 }
387 }
388 }
389
390 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
391 {
392 if (mSamplersVS[i].active)
393 {
394 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
395
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400396 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700397 {
398 if (infoLog)
399 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400400 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, caps.maxCombinedTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700401 }
402
Jamie Madill13776892015-04-28 12:39:06 -0400403 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700404 return false;
405 }
406
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400407 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700408 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400409 if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700410 {
411 if (infoLog)
412 {
413 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
414 }
415
Jamie Madill13776892015-04-28 12:39:06 -0400416 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700417 return false;
418 }
419 }
420 else
421 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400422 mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700423 }
424 }
425 }
426
Jamie Madill13776892015-04-28 12:39:06 -0400427 mCachedValidateSamplersResult = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700428 return true;
429}
430
Geoff Lang7dd2e102014-11-10 15:19:26 -0500431LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700432{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500433 int compileFlags = stream->readInt<int>();
434 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
435 {
436 infoLog.append("Mismatched compilation flags.");
437 return LinkResult(false, gl::Error(GL_NO_ERROR));
438 }
439
Brandon Jones44151a92014-09-10 11:32:25 -0700440 stream->readInt(&mShaderVersion);
441
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700442 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
443 for (unsigned int i = 0; i < psSamplerCount; ++i)
444 {
445 Sampler sampler;
446 stream->readBool(&sampler.active);
447 stream->readInt(&sampler.logicalTextureUnit);
448 stream->readInt(&sampler.textureType);
449 mSamplersPS.push_back(sampler);
450 }
451 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
452 for (unsigned int i = 0; i < vsSamplerCount; ++i)
453 {
454 Sampler sampler;
455 stream->readBool(&sampler.active);
456 stream->readInt(&sampler.logicalTextureUnit);
457 stream->readInt(&sampler.textureType);
458 mSamplersVS.push_back(sampler);
459 }
460
461 stream->readInt(&mUsedVertexSamplerRange);
462 stream->readInt(&mUsedPixelSamplerRange);
463
464 const unsigned int uniformCount = stream->readInt<unsigned int>();
465 if (stream->error())
466 {
467 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500468 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700469 }
470
471 mUniforms.resize(uniformCount);
472 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
473 {
474 GLenum type = stream->readInt<GLenum>();
475 GLenum precision = stream->readInt<GLenum>();
476 std::string name = stream->readString();
477 unsigned int arraySize = stream->readInt<unsigned int>();
478 int blockIndex = stream->readInt<int>();
479
480 int offset = stream->readInt<int>();
481 int arrayStride = stream->readInt<int>();
482 int matrixStride = stream->readInt<int>();
483 bool isRowMajorMatrix = stream->readBool();
484
485 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
486
487 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
488
489 stream->readInt(&uniform->psRegisterIndex);
490 stream->readInt(&uniform->vsRegisterIndex);
491 stream->readInt(&uniform->registerCount);
492 stream->readInt(&uniform->registerElement);
493
494 mUniforms[uniformIndex] = uniform;
495 }
496
497 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
498 if (stream->error())
499 {
500 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500501 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700502 }
503
504 mUniformIndex.resize(uniformIndexCount);
505 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
506 {
507 stream->readString(&mUniformIndex[uniformIndexIndex].name);
508 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
509 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
510 }
511
512 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
513 if (stream->error())
514 {
515 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500516 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700517 }
518
519 mUniformBlocks.resize(uniformBlockCount);
520 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
521 {
522 std::string name = stream->readString();
523 unsigned int elementIndex = stream->readInt<unsigned int>();
524 unsigned int dataSize = stream->readInt<unsigned int>();
525
526 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
527
528 stream->readInt(&uniformBlock->psRegisterIndex);
529 stream->readInt(&uniformBlock->vsRegisterIndex);
530
531 unsigned int numMembers = stream->readInt<unsigned int>();
532 uniformBlock->memberUniformIndexes.resize(numMembers);
533 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
534 {
535 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
536 }
537
538 mUniformBlocks[uniformBlockIndex] = uniformBlock;
539 }
540
Brandon Joneseb994362014-09-24 10:27:28 -0700541 stream->readInt(&mTransformFeedbackBufferMode);
542 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
543 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
544 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
545 {
546 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
547
548 stream->readString(&varying.name);
549 stream->readInt(&varying.type);
550 stream->readInt(&varying.size);
551 stream->readString(&varying.semanticName);
552 stream->readInt(&varying.semanticIndex);
553 stream->readInt(&varying.semanticIndexCount);
554 }
555
Brandon Jones22502d52014-08-29 16:58:36 -0700556 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530557 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700558 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530559 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700560 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700561 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700562
563 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
564 mPixelShaderKey.resize(pixelShaderKeySize);
565 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
566 {
567 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
568 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
569 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
570 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
571 }
572
Brandon Joneseb994362014-09-24 10:27:28 -0700573 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
574
575 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
576 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
577 {
578 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
579
580 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
581 {
582 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
583 stream->readInt(&vertexInput->mType);
584 stream->readInt(&vertexInput->mNormalized);
585 stream->readInt(&vertexInput->mComponents);
586 stream->readBool(&vertexInput->mPureInteger);
587 }
588
589 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
590 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400591
Geoff Lang359ef262015-01-05 14:42:29 -0500592 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400593 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
594 SHADER_VERTEX,
595 mTransformFeedbackLinkedVaryings,
596 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
597 &shaderExecutable);
598 if (error.isError())
599 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500600 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400601 }
602
Brandon Joneseb994362014-09-24 10:27:28 -0700603 if (!shaderExecutable)
604 {
605 infoLog.append("Could not create vertex shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500606 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700607 }
608
609 // generated converted input layout
610 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
611 getInputLayoutSignature(inputLayout, signature);
612
613 // add new binary
614 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
615
616 stream->skip(vertexShaderSize);
617 }
618
619 const size_t pixelShaderCount = stream->readInt<unsigned int>();
620 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
621 {
622 const size_t outputCount = stream->readInt<unsigned int>();
623 std::vector<GLenum> outputs(outputCount);
624 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
625 {
626 stream->readInt(&outputs[outputIndex]);
627 }
628
629 const size_t pixelShaderSize = stream->readInt<unsigned int>();
630 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500631 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400632 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
633 mTransformFeedbackLinkedVaryings,
634 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
635 &shaderExecutable);
636 if (error.isError())
637 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500638 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400639 }
Brandon Joneseb994362014-09-24 10:27:28 -0700640
641 if (!shaderExecutable)
642 {
643 infoLog.append("Could not create pixel shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500644 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700645 }
646
647 // add new binary
648 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
649
650 stream->skip(pixelShaderSize);
651 }
652
653 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
654
655 if (geometryShaderSize > 0)
656 {
657 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400658 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
659 mTransformFeedbackLinkedVaryings,
660 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
661 &mGeometryExecutable);
662 if (error.isError())
663 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500664 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400665 }
Brandon Joneseb994362014-09-24 10:27:28 -0700666
667 if (!mGeometryExecutable)
668 {
669 infoLog.append("Could not create geometry shader.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500670 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700671 }
672 stream->skip(geometryShaderSize);
673 }
674
Brandon Jones18bd4102014-09-22 14:21:44 -0700675 GUID binaryIdentifier = {0};
676 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
677
678 GUID identifier = mRenderer->getAdapterIdentifier();
679 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
680 {
681 infoLog.append("Invalid program binary.");
Geoff Lang7dd2e102014-11-10 15:19:26 -0500682 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700683 }
684
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500686 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700687
Geoff Lang7dd2e102014-11-10 15:19:26 -0500688 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700689}
690
Geoff Langb543aff2014-09-30 14:52:54 -0400691gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700692{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500693 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
694
Brandon Jones44151a92014-09-10 11:32:25 -0700695 stream->writeInt(mShaderVersion);
696
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700697 stream->writeInt(mSamplersPS.size());
698 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
699 {
700 stream->writeInt(mSamplersPS[i].active);
701 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
702 stream->writeInt(mSamplersPS[i].textureType);
703 }
704
705 stream->writeInt(mSamplersVS.size());
706 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
707 {
708 stream->writeInt(mSamplersVS[i].active);
709 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
710 stream->writeInt(mSamplersVS[i].textureType);
711 }
712
713 stream->writeInt(mUsedVertexSamplerRange);
714 stream->writeInt(mUsedPixelSamplerRange);
715
716 stream->writeInt(mUniforms.size());
717 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
718 {
719 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
720
721 stream->writeInt(uniform.type);
722 stream->writeInt(uniform.precision);
723 stream->writeString(uniform.name);
724 stream->writeInt(uniform.arraySize);
725 stream->writeInt(uniform.blockIndex);
726
727 stream->writeInt(uniform.blockInfo.offset);
728 stream->writeInt(uniform.blockInfo.arrayStride);
729 stream->writeInt(uniform.blockInfo.matrixStride);
730 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
731
732 stream->writeInt(uniform.psRegisterIndex);
733 stream->writeInt(uniform.vsRegisterIndex);
734 stream->writeInt(uniform.registerCount);
735 stream->writeInt(uniform.registerElement);
736 }
737
738 stream->writeInt(mUniformIndex.size());
739 for (size_t i = 0; i < mUniformIndex.size(); ++i)
740 {
741 stream->writeString(mUniformIndex[i].name);
742 stream->writeInt(mUniformIndex[i].element);
743 stream->writeInt(mUniformIndex[i].index);
744 }
745
746 stream->writeInt(mUniformBlocks.size());
747 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
748 {
749 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
750
751 stream->writeString(uniformBlock.name);
752 stream->writeInt(uniformBlock.elementIndex);
753 stream->writeInt(uniformBlock.dataSize);
754
755 stream->writeInt(uniformBlock.memberUniformIndexes.size());
756 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
757 {
758 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
759 }
760
761 stream->writeInt(uniformBlock.psRegisterIndex);
762 stream->writeInt(uniformBlock.vsRegisterIndex);
763 }
764
Brandon Joneseb994362014-09-24 10:27:28 -0700765 stream->writeInt(mTransformFeedbackBufferMode);
766 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
767 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
768 {
769 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
770
771 stream->writeString(varying.name);
772 stream->writeInt(varying.type);
773 stream->writeInt(varying.size);
774 stream->writeString(varying.semanticName);
775 stream->writeInt(varying.semanticIndex);
776 stream->writeInt(varying.semanticIndexCount);
777 }
778
Brandon Jones22502d52014-08-29 16:58:36 -0700779 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530780 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700781 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530782 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700783 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700784 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700785
Brandon Joneseb994362014-09-24 10:27:28 -0700786 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700787 stream->writeInt(pixelShaderKey.size());
788 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
789 {
Brandon Joneseb994362014-09-24 10:27:28 -0700790 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700791 stream->writeInt(variable.type);
792 stream->writeString(variable.name);
793 stream->writeString(variable.source);
794 stream->writeInt(variable.outputIndex);
795 }
796
Brandon Joneseb994362014-09-24 10:27:28 -0700797 stream->writeInt(mVertexExecutables.size());
798 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
799 {
800 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
801
802 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
803 {
804 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
805 stream->writeInt(vertexInput.mType);
806 stream->writeInt(vertexInput.mNormalized);
807 stream->writeInt(vertexInput.mComponents);
808 stream->writeInt(vertexInput.mPureInteger);
809 }
810
811 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
812 stream->writeInt(vertexShaderSize);
813
814 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
815 stream->writeBytes(vertexBlob, vertexShaderSize);
816 }
817
818 stream->writeInt(mPixelExecutables.size());
819 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
820 {
821 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
822
823 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
824 stream->writeInt(outputs.size());
825 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
826 {
827 stream->writeInt(outputs[outputIndex]);
828 }
829
830 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
831 stream->writeInt(pixelShaderSize);
832
833 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
834 stream->writeBytes(pixelBlob, pixelShaderSize);
835 }
836
837 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
838 stream->writeInt(geometryShaderSize);
839
840 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
841 {
842 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
843 stream->writeBytes(geometryBlob, geometryShaderSize);
844 }
845
Brandon Jones18bd4102014-09-22 14:21:44 -0700846 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400847 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700848
Geoff Langb543aff2014-09-30 14:52:54 -0400849 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700850}
851
Geoff Lang359ef262015-01-05 14:42:29 -0500852gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700853{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400854 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700855
Jamie Madill85a18042015-03-05 15:41:41 -0500856 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
857 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700858
859 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
860 {
861 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
862
863 if (colorbuffer)
864 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400865 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700866 }
867 else
868 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400869 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700870 }
871 }
872
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400873 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700874}
875
Jamie Madill97399232014-12-23 12:31:15 -0500876gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500877 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500878 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700879{
880 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
881 {
882 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
883 {
Geoff Langb543aff2014-09-30 14:52:54 -0400884 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
885 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700886 }
887 }
888
Brandon Jones22502d52014-08-29 16:58:36 -0700889 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
890 outputSignature);
891
892 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500893 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500894
895 gl::InfoLog tempInfoLog;
896 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
897
898 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400899 mTransformFeedbackLinkedVaryings,
900 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
901 mPixelWorkarounds, &pixelExecutable);
902 if (error.isError())
903 {
904 return error;
905 }
Brandon Joneseb994362014-09-24 10:27:28 -0700906
Jamie Madill97399232014-12-23 12:31:15 -0500907 if (pixelExecutable)
908 {
909 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
910 }
911 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700912 {
913 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
914 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
915 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
916 }
Brandon Jones22502d52014-08-29 16:58:36 -0700917
Geoff Langb543aff2014-09-30 14:52:54 -0400918 *outExectuable = pixelExecutable;
919 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700920}
921
Jamie Madill97399232014-12-23 12:31:15 -0500922gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500923 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500924 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700925{
Brandon Joneseb994362014-09-24 10:27:28 -0700926 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
927 getInputLayoutSignature(inputLayout, signature);
928
929 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
930 {
931 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
932 {
Geoff Langb543aff2014-09-30 14:52:54 -0400933 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
934 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700935 }
936 }
937
Brandon Jones22502d52014-08-29 16:58:36 -0700938 // Generate new dynamic layout with attribute conversions
Jamie Madill3da79b72015-04-27 11:09:17 -0400939 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, getShaderAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -0700940
941 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500942 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500943
944 gl::InfoLog tempInfoLog;
945 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
946
947 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400948 mTransformFeedbackLinkedVaryings,
949 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
950 mVertexWorkarounds, &vertexExecutable);
951 if (error.isError())
952 {
953 return error;
954 }
955
Jamie Madill97399232014-12-23 12:31:15 -0500956 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700957 {
958 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
959 }
Jamie Madill97399232014-12-23 12:31:15 -0500960 else if (!infoLog)
961 {
962 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
963 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
964 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
965 }
Brandon Jones22502d52014-08-29 16:58:36 -0700966
Geoff Langb543aff2014-09-30 14:52:54 -0400967 *outExectuable = vertexExecutable;
968 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700969}
970
Geoff Lang7dd2e102014-11-10 15:19:26 -0500971LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
972 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700973{
Jamie Madillf4bf3812015-04-01 16:15:32 -0400974 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
975 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones44151a92014-09-10 11:32:25 -0700976
Jamie Madille4ea2022015-03-26 20:35:05 +0000977 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
978 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
979 ShaderExecutableD3D *defaultVertexExecutable = NULL;
980 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
981 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -0800982 {
Jamie Madille4ea2022015-03-26 20:35:05 +0000983 return LinkResult(false, error);
984 }
Austin Kinross434953e2015-02-20 10:49:51 -0800985
Brandon Joneseb994362014-09-24 10:27:28 -0700986 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500987 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +0000988 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400989 if (error.isError())
990 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500991 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400992 }
Brandon Jones44151a92014-09-10 11:32:25 -0700993
Brandon Joneseb994362014-09-24 10:27:28 -0700994 if (usesGeometryShader())
995 {
996 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -0700997
Geoff Langb543aff2014-09-30 14:52:54 -0400998
999 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1000 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +05301001 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001002 if (error.isError())
1003 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001004 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001005 }
Brandon Joneseb994362014-09-24 10:27:28 -07001006 }
1007
Brandon Jones091540d2014-10-29 11:32:04 -07001008#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001009 if (usesGeometryShader() && mGeometryExecutable)
1010 {
1011 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1012 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1013 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1014 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1015 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1016 }
1017
1018 if (defaultVertexExecutable)
1019 {
1020 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1021 }
1022
1023 if (defaultPixelExecutable)
1024 {
1025 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1026 }
1027#endif
1028
Geoff Langb543aff2014-09-30 14:52:54 -04001029 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001030 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001031}
1032
Geoff Lang7dd2e102014-11-10 15:19:26 -05001033LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1034 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1035 const std::vector<std::string> &transformFeedbackVaryings,
1036 GLenum transformFeedbackBufferMode,
1037 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1038 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001039{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001040 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1041 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001042
Jamie Madillde8892b2014-11-11 13:00:22 -05001043 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1044 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001045
Brandon Joneseb994362014-09-24 10:27:28 -07001046 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001047
1048 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301049 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001050
1051 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301052 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001053 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001054
1055 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001056 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001057 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1058
Geoff Langbdee2d52014-09-17 11:02:51 -04001059 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001060 {
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
Geoff Lang7dd2e102014-11-10 15:19:26 -05001064 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001065 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001066 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001067 }
1068
Jamie Madillde8892b2014-11-11 13:00:22 -05001069 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001070 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1071 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1072 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001073 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001074 }
1075
Brandon Jones44151a92014-09-10 11:32:25 -07001076 mUsesPointSize = vertexShaderD3D->usesPointSize();
1077
Jamie Madill437d2662014-12-05 14:23:35 -05001078 initAttributesByLayout();
1079
Geoff Lang7dd2e102014-11-10 15:19:26 -05001080 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001081}
1082
Brandon Jones44151a92014-09-10 11:32:25 -07001083void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1084{
1085 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1086}
1087
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001088void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001089{
1090 // Compute total default block size
1091 unsigned int vertexRegisters = 0;
1092 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001093 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001094 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001095 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001096
Geoff Lang2ec386b2014-12-03 14:44:38 -05001097 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001098 {
1099 if (uniform.isReferencedByVertexShader())
1100 {
1101 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1102 }
1103 if (uniform.isReferencedByFragmentShader())
1104 {
1105 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1106 }
1107 }
1108 }
1109
1110 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1111 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1112}
1113
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001114gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001115{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001116 updateSamplerMapping();
1117
1118 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1119 if (error.isError())
1120 {
1121 return error;
1122 }
1123
1124 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1125 {
1126 mUniforms[uniformIndex]->dirty = false;
1127 }
1128
1129 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001130}
1131
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001132gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001133{
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001134 GLint vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
1135 GLint fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001136
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001137 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1138 {
1139 vertexUniformBuffers[registerIndex] = -1;
1140 }
1141
1142 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1143 {
1144 fragmentUniformBuffers[registerIndex] = -1;
1145 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001146
1147 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1148 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1149
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001150 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001151 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001152 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001153 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001154
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001155 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001156
1157 // Unnecessary to apply an unreferenced standard or shared UBO
1158 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1159 {
1160 continue;
1161 }
1162
1163 if (uniformBlock->isReferencedByVertexShader())
1164 {
1165 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001166 ASSERT(vertexUniformBuffers[registerIndex] == -1);
1167 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
1168 vertexUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001169 }
1170
1171 if (uniformBlock->isReferencedByFragmentShader())
1172 {
1173 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001174 ASSERT(fragmentUniformBuffers[registerIndex] == -1);
1175 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
1176 fragmentUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001177 }
1178 }
1179
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001180 return mRenderer->setUniformBuffers(data, vertexUniformBuffers, fragmentUniformBuffers);
Brandon Jones18bd4102014-09-22 14:21:44 -07001181}
1182
1183bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1184 unsigned int registerIndex, const gl::Caps &caps)
1185{
1186 if (shader == GL_VERTEX_SHADER)
1187 {
1188 uniformBlock->vsRegisterIndex = registerIndex;
1189 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1190 {
1191 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1192 return false;
1193 }
1194 }
1195 else if (shader == GL_FRAGMENT_SHADER)
1196 {
1197 uniformBlock->psRegisterIndex = registerIndex;
1198 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1199 {
1200 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1201 return false;
1202 }
1203 }
1204 else UNREACHABLE();
1205
1206 return true;
1207}
1208
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001209void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001210{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001211 unsigned int numUniforms = mUniforms.size();
1212 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001213 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001214 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001215 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001216}
1217
1218void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1219{
1220 setUniform(location, count, v, GL_FLOAT);
1221}
1222
1223void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1224{
1225 setUniform(location, count, v, GL_FLOAT_VEC2);
1226}
1227
1228void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1229{
1230 setUniform(location, count, v, GL_FLOAT_VEC3);
1231}
1232
1233void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1234{
1235 setUniform(location, count, v, GL_FLOAT_VEC4);
1236}
1237
1238void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1239{
1240 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1241}
1242
1243void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1244{
1245 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1246}
1247
1248void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1249{
1250 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1251}
1252
1253void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1254{
1255 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1256}
1257
1258void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1259{
1260 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1261}
1262
1263void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1264{
1265 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1266}
1267
1268void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1269{
1270 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1271}
1272
1273void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1274{
1275 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1276}
1277
1278void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1279{
1280 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1281}
1282
1283void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1284{
1285 setUniform(location, count, v, GL_INT);
1286}
1287
1288void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1289{
1290 setUniform(location, count, v, GL_INT_VEC2);
1291}
1292
1293void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1294{
1295 setUniform(location, count, v, GL_INT_VEC3);
1296}
1297
1298void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1299{
1300 setUniform(location, count, v, GL_INT_VEC4);
1301}
1302
1303void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1304{
1305 setUniform(location, count, v, GL_UNSIGNED_INT);
1306}
1307
1308void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1309{
1310 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1311}
1312
1313void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1314{
1315 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1316}
1317
1318void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1319{
1320 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1321}
1322
1323void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1324{
1325 getUniformv(location, params, GL_FLOAT);
1326}
1327
1328void ProgramD3D::getUniformiv(GLint location, GLint *params)
1329{
1330 getUniformv(location, params, GL_INT);
1331}
1332
1333void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1334{
1335 getUniformv(location, params, GL_UNSIGNED_INT);
1336}
1337
1338bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1339 const gl::Caps &caps)
1340{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001341 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1342 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001343
1344 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1345 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1346
1347 // Check that uniforms defined in the vertex and fragment shaders are identical
1348 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1349 UniformMap linkedUniforms;
1350
1351 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001352 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001353 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1354 linkedUniforms[vertexUniform.name] = &vertexUniform;
1355 }
1356
1357 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1358 {
1359 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1360 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1361 if (entry != linkedUniforms.end())
1362 {
1363 const sh::Uniform &vertexUniform = *entry->second;
1364 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001366 {
1367 return false;
1368 }
1369 }
1370 }
1371
1372 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1373 {
1374 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1375
1376 if (uniform.staticUse)
1377 {
Jamie Madill26e1a462015-05-01 19:19:19 +00001378 defineUniformBase(vertexShaderD3D, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001379 }
1380 }
1381
1382 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1383 {
1384 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1385
1386 if (uniform.staticUse)
1387 {
Jamie Madill26e1a462015-05-01 19:19:19 +00001388 defineUniformBase(fragmentShaderD3D, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001389 }
1390 }
1391
1392 if (!indexUniforms(infoLog, caps))
1393 {
1394 return false;
1395 }
1396
1397 initializeUniformStorage();
1398
Jamie Madill26e1a462015-05-01 19:19:19 +00001399 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1400 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1401 {
1402 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1403
1404 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1405 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1406 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1407 }
1408
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001409 return true;
1410}
1411
Geoff Lang492a7e42014-11-05 13:27:06 -05001412void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001413{
Geoff Lang492a7e42014-11-05 13:27:06 -05001414 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001415 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1416 encoder.skipRegisters(uniformRegister);
1417
1418 defineUniform(shader, uniform, uniform.name, &encoder);
1419}
1420
Geoff Lang492a7e42014-11-05 13:27:06 -05001421void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1422 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001423{
1424 if (uniform.isStruct())
1425 {
1426 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1427 {
1428 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1429
Jamie Madill26e1a462015-05-01 19:19:19 +00001430 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001431
1432 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1433 {
1434 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1435 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1436
1437 defineUniform(shader, field, fieldFullName, encoder);
1438 }
1439
Jamie Madill26e1a462015-05-01 19:19:19 +00001440 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001441 }
1442 }
1443 else // Not a struct
1444 {
1445 // Arrays are treated as aggregate types
Jamie Madill26e1a462015-05-01 19:19:19 +00001446 if (uniform.isArray())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001447 {
1448 encoder->enterAggregateType();
1449 }
1450
1451 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1452
Jamie Madill2857f482015-02-09 15:35:29 -05001453 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill26e1a462015-05-01 19:19:19 +00001454 sh::BlockMemberInfo blockInfo = encoder->encodeType(uniform.type, uniform.arraySize, false);
Jamie Madill2857f482015-02-09 15:35:29 -05001455
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001456 if (!linkedUniform)
1457 {
1458 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001459 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001460 ASSERT(linkedUniform);
Jamie Madill26e1a462015-05-01 19:19:19 +00001461 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001462 mUniforms.push_back(linkedUniform);
1463 }
1464
Jamie Madill26e1a462015-05-01 19:19:19 +00001465 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001466 {
Jamie Madill26e1a462015-05-01 19:19:19 +00001467 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001468 }
Jamie Madill26e1a462015-05-01 19:19:19 +00001469 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1470 {
1471 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1472 }
1473 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001474
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001475 // Arrays are treated as aggregate types
Jamie Madill26e1a462015-05-01 19:19:19 +00001476 if (uniform.isArray())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001477 {
1478 encoder->exitAggregateType();
1479 }
1480 }
1481}
1482
1483template <typename T>
1484static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1485{
1486 ASSERT(dest != NULL);
1487 ASSERT(dirtyFlag != NULL);
1488
1489 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1490 *dest = source;
1491}
1492
1493template <typename T>
1494void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1495{
1496 const int components = gl::VariableComponentCount(targetUniformType);
1497 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1498
1499 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1500
1501 int elementCount = targetUniform->elementCount();
1502
1503 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1504
1505 if (targetUniform->type == targetUniformType)
1506 {
1507 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1508
1509 for (int i = 0; i < count; i++)
1510 {
1511 T *dest = target + (i * 4);
1512 const T *source = v + (i * components);
1513
1514 for (int c = 0; c < components; c++)
1515 {
1516 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1517 }
1518 for (int c = components; c < 4; c++)
1519 {
1520 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1521 }
1522 }
1523 }
1524 else if (targetUniform->type == targetBoolType)
1525 {
1526 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1527
1528 for (int i = 0; i < count; i++)
1529 {
1530 GLint *dest = boolParams + (i * 4);
1531 const T *source = v + (i * components);
1532
1533 for (int c = 0; c < components; c++)
1534 {
1535 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1536 }
1537 for (int c = components; c < 4; c++)
1538 {
1539 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1540 }
1541 }
1542 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001543 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001544 {
1545 ASSERT(targetUniformType == GL_INT);
1546
1547 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1548
1549 bool wasDirty = targetUniform->dirty;
1550
1551 for (int i = 0; i < count; i++)
1552 {
1553 GLint *dest = target + (i * 4);
1554 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1555
1556 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1557 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1558 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1559 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1560 }
1561
1562 if (!wasDirty && targetUniform->dirty)
1563 {
1564 mDirtySamplerMapping = true;
1565 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001566 }
1567 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001568}
Brandon Jones18bd4102014-09-22 14:21:44 -07001569
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001570template<typename T>
1571bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1572{
1573 bool dirty = false;
1574 int copyWidth = std::min(targetHeight, srcWidth);
1575 int copyHeight = std::min(targetWidth, srcHeight);
1576
1577 for (int x = 0; x < copyWidth; x++)
1578 {
1579 for (int y = 0; y < copyHeight; y++)
1580 {
1581 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1582 }
1583 }
1584 // clear unfilled right side
1585 for (int y = 0; y < copyWidth; y++)
1586 {
1587 for (int x = copyHeight; x < targetWidth; x++)
1588 {
1589 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1590 }
1591 }
1592 // clear unfilled bottom.
1593 for (int y = copyWidth; y < targetHeight; y++)
1594 {
1595 for (int x = 0; x < targetWidth; x++)
1596 {
1597 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1598 }
1599 }
1600
1601 return dirty;
1602}
1603
1604template<typename T>
1605bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1606{
1607 bool dirty = false;
1608 int copyWidth = std::min(targetWidth, srcWidth);
1609 int copyHeight = std::min(targetHeight, srcHeight);
1610
1611 for (int y = 0; y < copyHeight; y++)
1612 {
1613 for (int x = 0; x < copyWidth; x++)
1614 {
1615 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1616 }
1617 }
1618 // clear unfilled right side
1619 for (int y = 0; y < copyHeight; y++)
1620 {
1621 for (int x = copyWidth; x < targetWidth; x++)
1622 {
1623 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1624 }
1625 }
1626 // clear unfilled bottom.
1627 for (int y = copyHeight; y < targetHeight; y++)
1628 {
1629 for (int x = 0; x < targetWidth; x++)
1630 {
1631 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1632 }
1633 }
1634
1635 return dirty;
1636}
1637
1638template <int cols, int rows>
1639void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1640{
1641 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1642
1643 int elementCount = targetUniform->elementCount();
1644
1645 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1646 const unsigned int targetMatrixStride = (4 * rows);
1647 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1648
1649 for (int i = 0; i < count; i++)
1650 {
1651 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1652 if (transpose == GL_FALSE)
1653 {
1654 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1655 }
1656 else
1657 {
1658 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1659 }
1660 target += targetMatrixStride;
1661 value += cols * rows;
1662 }
1663}
1664
1665template <typename T>
1666void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1667{
1668 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1669
1670 if (gl::IsMatrixType(targetUniform->type))
1671 {
1672 const int rows = gl::VariableRowCount(targetUniform->type);
1673 const int cols = gl::VariableColumnCount(targetUniform->type);
1674 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1675 }
1676 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1677 {
1678 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1679 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1680 size * sizeof(T));
1681 }
1682 else
1683 {
1684 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1685 switch (gl::VariableComponentType(targetUniform->type))
1686 {
1687 case GL_BOOL:
1688 {
1689 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1690
1691 for (unsigned int i = 0; i < size; i++)
1692 {
1693 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1694 }
1695 }
1696 break;
1697
1698 case GL_FLOAT:
1699 {
1700 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1701
1702 for (unsigned int i = 0; i < size; i++)
1703 {
1704 params[i] = static_cast<T>(floatParams[i]);
1705 }
1706 }
1707 break;
1708
1709 case GL_INT:
1710 {
1711 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1712
1713 for (unsigned int i = 0; i < size; i++)
1714 {
1715 params[i] = static_cast<T>(intParams[i]);
1716 }
1717 }
1718 break;
1719
1720 case GL_UNSIGNED_INT:
1721 {
1722 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1723
1724 for (unsigned int i = 0; i < size; i++)
1725 {
1726 params[i] = static_cast<T>(uintParams[i]);
1727 }
1728 }
1729 break;
1730
1731 default: UNREACHABLE();
1732 }
1733 }
1734}
1735
1736template <typename VarT>
1737void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1738 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1739 bool inRowMajorLayout)
1740{
1741 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1742 {
1743 const VarT &field = fields[uniformIndex];
1744 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1745
1746 if (field.isStruct())
1747 {
1748 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1749
1750 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1751 {
1752 encoder->enterAggregateType();
1753
1754 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1755 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1756
1757 encoder->exitAggregateType();
1758 }
1759 }
1760 else
1761 {
1762 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1763
1764 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1765
1766 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1767 blockIndex, memberInfo);
1768
1769 // add to uniform list, but not index, since uniform block uniforms have no location
1770 blockUniformIndexes->push_back(mUniforms.size());
1771 mUniforms.push_back(newUniform);
1772 }
1773 }
1774}
1775
1776bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1777 const gl::Caps &caps)
1778{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001779 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001780
1781 // create uniform block entries if they do not exist
1782 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1783 {
1784 std::vector<unsigned int> blockUniformIndexes;
1785 const unsigned int blockIndex = mUniformBlocks.size();
1786
1787 // define member uniforms
1788 sh::BlockLayoutEncoder *encoder = NULL;
1789
1790 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1791 {
1792 encoder = new sh::Std140BlockEncoder;
1793 }
1794 else
1795 {
1796 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1797 }
1798 ASSERT(encoder);
1799
1800 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1801
1802 size_t dataSize = encoder->getBlockSize();
1803
1804 // create all the uniform blocks
1805 if (interfaceBlock.arraySize > 0)
1806 {
1807 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1808 {
1809 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1810 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1811 mUniformBlocks.push_back(newUniformBlock);
1812 }
1813 }
1814 else
1815 {
1816 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1817 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1818 mUniformBlocks.push_back(newUniformBlock);
1819 }
1820 }
1821
1822 if (interfaceBlock.staticUse)
1823 {
1824 // Assign registers to the uniform blocks
1825 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1826 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1827 ASSERT(blockIndex != GL_INVALID_INDEX);
1828 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1829
1830 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1831
1832 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1833 {
1834 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1835 ASSERT(uniformBlock->name == interfaceBlock.name);
1836
1837 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1838 interfaceBlockRegister + uniformBlockElement, caps))
1839 {
1840 return false;
1841 }
1842 }
1843 }
1844
1845 return true;
1846}
1847
1848bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1849 GLenum samplerType,
1850 unsigned int samplerCount,
1851 std::vector<Sampler> &outSamplers,
1852 GLuint *outUsedRange)
1853{
1854 unsigned int samplerIndex = startSamplerIndex;
1855
1856 do
1857 {
1858 if (samplerIndex < outSamplers.size())
1859 {
1860 Sampler& sampler = outSamplers[samplerIndex];
1861 sampler.active = true;
1862 sampler.textureType = GetTextureType(samplerType);
1863 sampler.logicalTextureUnit = 0;
1864 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1865 }
1866 else
1867 {
1868 return false;
1869 }
1870
1871 samplerIndex++;
1872 } while (samplerIndex < startSamplerIndex + samplerCount);
1873
1874 return true;
1875}
1876
1877bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1878{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001879 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001880 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1881
1882 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1883 {
1884 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1885 &mUsedVertexSamplerRange))
1886 {
1887 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1888 mSamplersVS.size());
1889 return false;
1890 }
1891
1892 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1893 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1894 {
1895 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1896 caps.maxVertexUniformVectors);
1897 return false;
1898 }
1899 }
1900
1901 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1902 {
1903 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1904 &mUsedPixelSamplerRange))
1905 {
1906 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1907 mSamplersPS.size());
1908 return false;
1909 }
1910
1911 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1912 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1913 {
1914 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1915 caps.maxFragmentUniformVectors);
1916 return false;
1917 }
1918 }
1919
1920 return true;
1921}
1922
1923bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1924{
1925 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1926 {
1927 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1928
Geoff Lang2ec386b2014-12-03 14:44:38 -05001929 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001930 {
1931 if (!indexSamplerUniform(uniform, infoLog, caps))
1932 {
1933 return false;
1934 }
1935 }
1936
1937 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1938 {
1939 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1940 }
1941 }
1942
1943 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001944}
1945
Brandon Jonesc9610c52014-08-25 17:02:59 -07001946void ProgramD3D::reset()
1947{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001948 ProgramImpl::reset();
1949
Brandon Joneseb994362014-09-24 10:27:28 -07001950 SafeDeleteContainer(mVertexExecutables);
1951 SafeDeleteContainer(mPixelExecutables);
1952 SafeDelete(mGeometryExecutable);
1953
1954 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001955
Brandon Jones22502d52014-08-29 16:58:36 -07001956 mVertexHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301957 mVertexWorkarounds.reset();
Brandon Jones44151a92014-09-10 11:32:25 -07001958 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001959
1960 mPixelHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301961 mPixelWorkarounds.reset();
Brandon Jones22502d52014-08-29 16:58:36 -07001962 mUsesFragDepth = false;
1963 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001964 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001965
Brandon Jonesc9610c52014-08-25 17:02:59 -07001966 SafeDelete(mVertexUniformStorage);
1967 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001968
1969 mSamplersPS.clear();
1970 mSamplersVS.clear();
1971
1972 mUsedVertexSamplerRange = 0;
1973 mUsedPixelSamplerRange = 0;
1974 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001975
1976 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001977}
1978
Geoff Lang7dd2e102014-11-10 15:19:26 -05001979unsigned int ProgramD3D::getSerial() const
1980{
1981 return mSerial;
1982}
1983
1984unsigned int ProgramD3D::issueSerial()
1985{
1986 return mCurrentSerial++;
1987}
1988
Jamie Madill437d2662014-12-05 14:23:35 -05001989void ProgramD3D::initAttributesByLayout()
1990{
1991 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1992 {
1993 mAttributesByLayout[i] = i;
1994 }
1995
1996 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
1997}
1998
1999void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
2000 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
2001{
2002 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
2003
2004 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2005 {
2006 oldTranslatedAttributes[i] = attributes[i];
2007 }
2008
2009 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2010 {
2011 int oldIndex = mAttributesByLayout[i];
2012 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2013 attributes[i] = oldTranslatedAttributes[oldIndex];
2014 }
2015}
2016
Brandon Jonesc9610c52014-08-25 17:02:59 -07002017}