blob: afaec55f9bda95d630ad7619a22a9ebeb39185fe [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 {
Jamie Madillf6113162015-05-07 11:49:21 -0400363 (*infoLog) << "Sampler uniform (" << unit
364 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
365 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700366 }
367
Jamie Madill13776892015-04-28 12:39:06 -0400368 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700369 return false;
370 }
371
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400372 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700373 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400374 if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700375 {
376 if (infoLog)
377 {
Jamie Madillf6113162015-05-07 11:49:21 -0400378 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
379 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700380 }
381
Jamie Madill13776892015-04-28 12:39:06 -0400382 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700383 return false;
384 }
385 }
386 else
387 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400388 mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700389 }
390 }
391 }
392
393 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
394 {
395 if (mSamplersVS[i].active)
396 {
397 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
398
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400399 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700400 {
401 if (infoLog)
402 {
Jamie Madillf6113162015-05-07 11:49:21 -0400403 (*infoLog) << "Sampler uniform (" << unit
404 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
405 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700406 }
407
Jamie Madill13776892015-04-28 12:39:06 -0400408 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700409 return false;
410 }
411
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400412 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700413 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400414 if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700415 {
416 if (infoLog)
417 {
Jamie Madillf6113162015-05-07 11:49:21 -0400418 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
419 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700420 }
421
Jamie Madill13776892015-04-28 12:39:06 -0400422 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700423 return false;
424 }
425 }
426 else
427 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400428 mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700429 }
430 }
431 }
432
Jamie Madill13776892015-04-28 12:39:06 -0400433 mCachedValidateSamplersResult = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700434 return true;
435}
436
Geoff Lang7dd2e102014-11-10 15:19:26 -0500437LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700438{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500439 int compileFlags = stream->readInt<int>();
440 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
441 {
Jamie Madillf6113162015-05-07 11:49:21 -0400442 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500443 return LinkResult(false, gl::Error(GL_NO_ERROR));
444 }
445
Brandon Jones44151a92014-09-10 11:32:25 -0700446 stream->readInt(&mShaderVersion);
447
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700448 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
449 for (unsigned int i = 0; i < psSamplerCount; ++i)
450 {
451 Sampler sampler;
452 stream->readBool(&sampler.active);
453 stream->readInt(&sampler.logicalTextureUnit);
454 stream->readInt(&sampler.textureType);
455 mSamplersPS.push_back(sampler);
456 }
457 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
458 for (unsigned int i = 0; i < vsSamplerCount; ++i)
459 {
460 Sampler sampler;
461 stream->readBool(&sampler.active);
462 stream->readInt(&sampler.logicalTextureUnit);
463 stream->readInt(&sampler.textureType);
464 mSamplersVS.push_back(sampler);
465 }
466
467 stream->readInt(&mUsedVertexSamplerRange);
468 stream->readInt(&mUsedPixelSamplerRange);
469
470 const unsigned int uniformCount = stream->readInt<unsigned int>();
471 if (stream->error())
472 {
Jamie Madillf6113162015-05-07 11:49:21 -0400473 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500474 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700475 }
476
477 mUniforms.resize(uniformCount);
478 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
479 {
480 GLenum type = stream->readInt<GLenum>();
481 GLenum precision = stream->readInt<GLenum>();
482 std::string name = stream->readString();
483 unsigned int arraySize = stream->readInt<unsigned int>();
484 int blockIndex = stream->readInt<int>();
485
486 int offset = stream->readInt<int>();
487 int arrayStride = stream->readInt<int>();
488 int matrixStride = stream->readInt<int>();
489 bool isRowMajorMatrix = stream->readBool();
490
491 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
492
493 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
494
495 stream->readInt(&uniform->psRegisterIndex);
496 stream->readInt(&uniform->vsRegisterIndex);
497 stream->readInt(&uniform->registerCount);
498 stream->readInt(&uniform->registerElement);
499
500 mUniforms[uniformIndex] = uniform;
501 }
502
503 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
504 if (stream->error())
505 {
Jamie Madillf6113162015-05-07 11:49:21 -0400506 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500507 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700508 }
509
510 mUniformIndex.resize(uniformIndexCount);
511 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
512 {
513 stream->readString(&mUniformIndex[uniformIndexIndex].name);
514 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
515 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
516 }
517
518 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
519 if (stream->error())
520 {
Jamie Madillf6113162015-05-07 11:49:21 -0400521 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500522 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700523 }
524
525 mUniformBlocks.resize(uniformBlockCount);
526 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
527 {
528 std::string name = stream->readString();
529 unsigned int elementIndex = stream->readInt<unsigned int>();
530 unsigned int dataSize = stream->readInt<unsigned int>();
531
532 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
533
534 stream->readInt(&uniformBlock->psRegisterIndex);
535 stream->readInt(&uniformBlock->vsRegisterIndex);
536
537 unsigned int numMembers = stream->readInt<unsigned int>();
538 uniformBlock->memberUniformIndexes.resize(numMembers);
539 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
540 {
541 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
542 }
543
544 mUniformBlocks[uniformBlockIndex] = uniformBlock;
545 }
546
Brandon Joneseb994362014-09-24 10:27:28 -0700547 stream->readInt(&mTransformFeedbackBufferMode);
548 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
549 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
550 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
551 {
552 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
553
554 stream->readString(&varying.name);
555 stream->readInt(&varying.type);
556 stream->readInt(&varying.size);
557 stream->readString(&varying.semanticName);
558 stream->readInt(&varying.semanticIndex);
559 stream->readInt(&varying.semanticIndexCount);
560 }
561
Brandon Jones22502d52014-08-29 16:58:36 -0700562 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530563 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700564 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530565 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700566 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700567 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700568
569 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
570 mPixelShaderKey.resize(pixelShaderKeySize);
571 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
572 {
573 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
574 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
575 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
576 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
577 }
578
Brandon Joneseb994362014-09-24 10:27:28 -0700579 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
580
581 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
582 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
583 {
584 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
585
586 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
587 {
588 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
589 stream->readInt(&vertexInput->mType);
590 stream->readInt(&vertexInput->mNormalized);
591 stream->readInt(&vertexInput->mComponents);
592 stream->readBool(&vertexInput->mPureInteger);
593 }
594
595 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
596 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400597
Geoff Lang359ef262015-01-05 14:42:29 -0500598 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400599 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
600 SHADER_VERTEX,
601 mTransformFeedbackLinkedVaryings,
602 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
603 &shaderExecutable);
604 if (error.isError())
605 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500606 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400607 }
608
Brandon Joneseb994362014-09-24 10:27:28 -0700609 if (!shaderExecutable)
610 {
Jamie Madillf6113162015-05-07 11:49:21 -0400611 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500612 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700613 }
614
615 // generated converted input layout
616 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
617 getInputLayoutSignature(inputLayout, signature);
618
619 // add new binary
620 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
621
622 stream->skip(vertexShaderSize);
623 }
624
625 const size_t pixelShaderCount = stream->readInt<unsigned int>();
626 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
627 {
628 const size_t outputCount = stream->readInt<unsigned int>();
629 std::vector<GLenum> outputs(outputCount);
630 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
631 {
632 stream->readInt(&outputs[outputIndex]);
633 }
634
635 const size_t pixelShaderSize = stream->readInt<unsigned int>();
636 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500637 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400638 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
639 mTransformFeedbackLinkedVaryings,
640 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
641 &shaderExecutable);
642 if (error.isError())
643 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500644 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400645 }
Brandon Joneseb994362014-09-24 10:27:28 -0700646
647 if (!shaderExecutable)
648 {
Jamie Madillf6113162015-05-07 11:49:21 -0400649 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500650 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700651 }
652
653 // add new binary
654 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
655
656 stream->skip(pixelShaderSize);
657 }
658
659 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
660
661 if (geometryShaderSize > 0)
662 {
663 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400664 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
665 mTransformFeedbackLinkedVaryings,
666 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
667 &mGeometryExecutable);
668 if (error.isError())
669 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500670 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400671 }
Brandon Joneseb994362014-09-24 10:27:28 -0700672
673 if (!mGeometryExecutable)
674 {
Jamie Madillf6113162015-05-07 11:49:21 -0400675 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500676 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700677 }
678 stream->skip(geometryShaderSize);
679 }
680
Brandon Jones18bd4102014-09-22 14:21:44 -0700681 GUID binaryIdentifier = {0};
682 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
683
684 GUID identifier = mRenderer->getAdapterIdentifier();
685 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
686 {
Jamie Madillf6113162015-05-07 11:49:21 -0400687 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500688 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700689 }
690
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700691 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500692 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700693
Geoff Lang7dd2e102014-11-10 15:19:26 -0500694 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700695}
696
Geoff Langb543aff2014-09-30 14:52:54 -0400697gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700698{
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500699 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
700
Brandon Jones44151a92014-09-10 11:32:25 -0700701 stream->writeInt(mShaderVersion);
702
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700703 stream->writeInt(mSamplersPS.size());
704 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
705 {
706 stream->writeInt(mSamplersPS[i].active);
707 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
708 stream->writeInt(mSamplersPS[i].textureType);
709 }
710
711 stream->writeInt(mSamplersVS.size());
712 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
713 {
714 stream->writeInt(mSamplersVS[i].active);
715 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
716 stream->writeInt(mSamplersVS[i].textureType);
717 }
718
719 stream->writeInt(mUsedVertexSamplerRange);
720 stream->writeInt(mUsedPixelSamplerRange);
721
722 stream->writeInt(mUniforms.size());
723 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
724 {
725 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
726
727 stream->writeInt(uniform.type);
728 stream->writeInt(uniform.precision);
729 stream->writeString(uniform.name);
730 stream->writeInt(uniform.arraySize);
731 stream->writeInt(uniform.blockIndex);
732
733 stream->writeInt(uniform.blockInfo.offset);
734 stream->writeInt(uniform.blockInfo.arrayStride);
735 stream->writeInt(uniform.blockInfo.matrixStride);
736 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
737
738 stream->writeInt(uniform.psRegisterIndex);
739 stream->writeInt(uniform.vsRegisterIndex);
740 stream->writeInt(uniform.registerCount);
741 stream->writeInt(uniform.registerElement);
742 }
743
744 stream->writeInt(mUniformIndex.size());
745 for (size_t i = 0; i < mUniformIndex.size(); ++i)
746 {
747 stream->writeString(mUniformIndex[i].name);
748 stream->writeInt(mUniformIndex[i].element);
749 stream->writeInt(mUniformIndex[i].index);
750 }
751
752 stream->writeInt(mUniformBlocks.size());
753 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
754 {
755 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
756
757 stream->writeString(uniformBlock.name);
758 stream->writeInt(uniformBlock.elementIndex);
759 stream->writeInt(uniformBlock.dataSize);
760
761 stream->writeInt(uniformBlock.memberUniformIndexes.size());
762 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
763 {
764 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
765 }
766
767 stream->writeInt(uniformBlock.psRegisterIndex);
768 stream->writeInt(uniformBlock.vsRegisterIndex);
769 }
770
Brandon Joneseb994362014-09-24 10:27:28 -0700771 stream->writeInt(mTransformFeedbackBufferMode);
772 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
773 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
774 {
775 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
776
777 stream->writeString(varying.name);
778 stream->writeInt(varying.type);
779 stream->writeInt(varying.size);
780 stream->writeString(varying.semanticName);
781 stream->writeInt(varying.semanticIndex);
782 stream->writeInt(varying.semanticIndexCount);
783 }
784
Brandon Jones22502d52014-08-29 16:58:36 -0700785 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530786 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700787 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530788 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700789 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700790 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700791
Brandon Joneseb994362014-09-24 10:27:28 -0700792 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700793 stream->writeInt(pixelShaderKey.size());
794 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
795 {
Brandon Joneseb994362014-09-24 10:27:28 -0700796 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700797 stream->writeInt(variable.type);
798 stream->writeString(variable.name);
799 stream->writeString(variable.source);
800 stream->writeInt(variable.outputIndex);
801 }
802
Brandon Joneseb994362014-09-24 10:27:28 -0700803 stream->writeInt(mVertexExecutables.size());
804 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
805 {
806 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
807
808 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
809 {
810 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
811 stream->writeInt(vertexInput.mType);
812 stream->writeInt(vertexInput.mNormalized);
813 stream->writeInt(vertexInput.mComponents);
814 stream->writeInt(vertexInput.mPureInteger);
815 }
816
817 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
818 stream->writeInt(vertexShaderSize);
819
820 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
821 stream->writeBytes(vertexBlob, vertexShaderSize);
822 }
823
824 stream->writeInt(mPixelExecutables.size());
825 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
826 {
827 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
828
829 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
830 stream->writeInt(outputs.size());
831 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
832 {
833 stream->writeInt(outputs[outputIndex]);
834 }
835
836 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
837 stream->writeInt(pixelShaderSize);
838
839 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
840 stream->writeBytes(pixelBlob, pixelShaderSize);
841 }
842
843 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
844 stream->writeInt(geometryShaderSize);
845
846 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
847 {
848 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
849 stream->writeBytes(geometryBlob, geometryShaderSize);
850 }
851
Brandon Jones18bd4102014-09-22 14:21:44 -0700852 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400853 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700854
Geoff Langb543aff2014-09-30 14:52:54 -0400855 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700856}
857
Geoff Lang359ef262015-01-05 14:42:29 -0500858gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700859{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400860 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700861
Jamie Madill85a18042015-03-05 15:41:41 -0500862 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
863 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700864
865 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
866 {
867 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
868
869 if (colorbuffer)
870 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400871 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700872 }
873 else
874 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400875 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700876 }
877 }
878
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400879 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700880}
881
Jamie Madill97399232014-12-23 12:31:15 -0500882gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500883 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500884 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700885{
886 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
887 {
888 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
889 {
Geoff Langb543aff2014-09-30 14:52:54 -0400890 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
891 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700892 }
893 }
894
Brandon Jones22502d52014-08-29 16:58:36 -0700895 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
896 outputSignature);
897
898 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500899 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500900
901 gl::InfoLog tempInfoLog;
902 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
903
904 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400905 mTransformFeedbackLinkedVaryings,
906 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
907 mPixelWorkarounds, &pixelExecutable);
908 if (error.isError())
909 {
910 return error;
911 }
Brandon Joneseb994362014-09-24 10:27:28 -0700912
Jamie Madill97399232014-12-23 12:31:15 -0500913 if (pixelExecutable)
914 {
915 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
916 }
917 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700918 {
919 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
920 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
921 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
922 }
Brandon Jones22502d52014-08-29 16:58:36 -0700923
Geoff Langb543aff2014-09-30 14:52:54 -0400924 *outExectuable = pixelExecutable;
925 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700926}
927
Jamie Madill97399232014-12-23 12:31:15 -0500928gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500929 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500930 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700931{
Brandon Joneseb994362014-09-24 10:27:28 -0700932 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
933 getInputLayoutSignature(inputLayout, signature);
934
935 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
936 {
937 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
938 {
Geoff Langb543aff2014-09-30 14:52:54 -0400939 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
940 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700941 }
942 }
943
Brandon Jones22502d52014-08-29 16:58:36 -0700944 // Generate new dynamic layout with attribute conversions
Jamie Madill3da79b72015-04-27 11:09:17 -0400945 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, getShaderAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -0700946
947 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500948 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500949
950 gl::InfoLog tempInfoLog;
951 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
952
953 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400954 mTransformFeedbackLinkedVaryings,
955 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
956 mVertexWorkarounds, &vertexExecutable);
957 if (error.isError())
958 {
959 return error;
960 }
961
Jamie Madill97399232014-12-23 12:31:15 -0500962 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700963 {
964 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
965 }
Jamie Madill97399232014-12-23 12:31:15 -0500966 else if (!infoLog)
967 {
968 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
969 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
970 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
971 }
Brandon Jones22502d52014-08-29 16:58:36 -0700972
Geoff Langb543aff2014-09-30 14:52:54 -0400973 *outExectuable = vertexExecutable;
974 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700975}
976
Geoff Lang7dd2e102014-11-10 15:19:26 -0500977LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
978 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700979{
Jamie Madillf4bf3812015-04-01 16:15:32 -0400980 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
981 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones44151a92014-09-10 11:32:25 -0700982
Jamie Madille4ea2022015-03-26 20:35:05 +0000983 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
984 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
985 ShaderExecutableD3D *defaultVertexExecutable = NULL;
986 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
987 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -0800988 {
Jamie Madille4ea2022015-03-26 20:35:05 +0000989 return LinkResult(false, error);
990 }
Austin Kinross434953e2015-02-20 10:49:51 -0800991
Brandon Joneseb994362014-09-24 10:27:28 -0700992 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500993 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +0000994 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400995 if (error.isError())
996 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500997 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400998 }
Brandon Jones44151a92014-09-10 11:32:25 -0700999
Brandon Joneseb994362014-09-24 10:27:28 -07001000 if (usesGeometryShader())
1001 {
1002 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001003
Geoff Langb543aff2014-09-30 14:52:54 -04001004
1005 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1006 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +05301007 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001008 if (error.isError())
1009 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001010 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001011 }
Brandon Joneseb994362014-09-24 10:27:28 -07001012 }
1013
Brandon Jones091540d2014-10-29 11:32:04 -07001014#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001015 if (usesGeometryShader() && mGeometryExecutable)
1016 {
1017 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1018 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1019 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1020 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1021 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1022 }
1023
1024 if (defaultVertexExecutable)
1025 {
1026 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1027 }
1028
1029 if (defaultPixelExecutable)
1030 {
1031 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1032 }
1033#endif
1034
Geoff Langb543aff2014-09-30 14:52:54 -04001035 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001036 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001037}
1038
Geoff Lang7dd2e102014-11-10 15:19:26 -05001039LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1040 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1041 const std::vector<std::string> &transformFeedbackVaryings,
1042 GLenum transformFeedbackBufferMode,
1043 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1044 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001045{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001046 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1047 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001048
Jamie Madillde8892b2014-11-11 13:00:22 -05001049 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1050 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001051
Brandon Joneseb994362014-09-24 10:27:28 -07001052 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001053
1054 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301055 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001056
1057 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301058 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001059 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001060
1061 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001062 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001063 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1064
Geoff Langbdee2d52014-09-17 11:02:51 -04001065 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001066 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001067 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001068 }
1069
Geoff Lang7dd2e102014-11-10 15:19:26 -05001070 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001071 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001072 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001073 }
1074
Jamie Madillde8892b2014-11-11 13:00:22 -05001075 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001076 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1077 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1078 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001079 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001080 }
1081
Brandon Jones44151a92014-09-10 11:32:25 -07001082 mUsesPointSize = vertexShaderD3D->usesPointSize();
1083
Jamie Madill437d2662014-12-05 14:23:35 -05001084 initAttributesByLayout();
1085
Geoff Lang7dd2e102014-11-10 15:19:26 -05001086 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001087}
1088
Geoff Lang0ca53782015-05-07 13:49:39 -04001089void ProgramD3D::bindAttributeLocation(GLuint index, const std::string &name)
1090{
1091}
1092
Brandon Jones44151a92014-09-10 11:32:25 -07001093void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1094{
1095 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1096}
1097
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001098void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001099{
1100 // Compute total default block size
1101 unsigned int vertexRegisters = 0;
1102 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001103 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001104 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001105 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001106
Geoff Lang2ec386b2014-12-03 14:44:38 -05001107 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001108 {
1109 if (uniform.isReferencedByVertexShader())
1110 {
1111 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1112 }
1113 if (uniform.isReferencedByFragmentShader())
1114 {
1115 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1116 }
1117 }
1118 }
1119
1120 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1121 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1122}
1123
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001124gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001125{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001126 updateSamplerMapping();
1127
1128 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1129 if (error.isError())
1130 {
1131 return error;
1132 }
1133
1134 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1135 {
1136 mUniforms[uniformIndex]->dirty = false;
1137 }
1138
1139 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001140}
1141
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001142gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001143{
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001144 GLint vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
1145 GLint fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001146
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001147 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1148 {
1149 vertexUniformBuffers[registerIndex] = -1;
1150 }
1151
1152 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1153 {
1154 fragmentUniformBuffers[registerIndex] = -1;
1155 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001156
1157 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1158 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1159
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001160 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001161 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001162 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001163 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001164
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001165 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001166
1167 // Unnecessary to apply an unreferenced standard or shared UBO
1168 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1169 {
1170 continue;
1171 }
1172
1173 if (uniformBlock->isReferencedByVertexShader())
1174 {
1175 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001176 ASSERT(vertexUniformBuffers[registerIndex] == -1);
1177 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
1178 vertexUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001179 }
1180
1181 if (uniformBlock->isReferencedByFragmentShader())
1182 {
1183 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001184 ASSERT(fragmentUniformBuffers[registerIndex] == -1);
1185 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
1186 fragmentUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001187 }
1188 }
1189
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001190 return mRenderer->setUniformBuffers(data, vertexUniformBuffers, fragmentUniformBuffers);
Brandon Jones18bd4102014-09-22 14:21:44 -07001191}
1192
1193bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1194 unsigned int registerIndex, const gl::Caps &caps)
1195{
1196 if (shader == GL_VERTEX_SHADER)
1197 {
1198 uniformBlock->vsRegisterIndex = registerIndex;
1199 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1200 {
Jamie Madillf6113162015-05-07 11:49:21 -04001201 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (" << caps.maxVertexUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001202 return false;
1203 }
1204 }
1205 else if (shader == GL_FRAGMENT_SHADER)
1206 {
1207 uniformBlock->psRegisterIndex = registerIndex;
1208 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1209 {
Jamie Madillf6113162015-05-07 11:49:21 -04001210 infoLog << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (" << caps.maxFragmentUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001211 return false;
1212 }
1213 }
1214 else UNREACHABLE();
1215
1216 return true;
1217}
1218
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001219void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001220{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001221 unsigned int numUniforms = mUniforms.size();
1222 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001223 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001224 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001225 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001226}
1227
1228void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1229{
1230 setUniform(location, count, v, GL_FLOAT);
1231}
1232
1233void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1234{
1235 setUniform(location, count, v, GL_FLOAT_VEC2);
1236}
1237
1238void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1239{
1240 setUniform(location, count, v, GL_FLOAT_VEC3);
1241}
1242
1243void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1244{
1245 setUniform(location, count, v, GL_FLOAT_VEC4);
1246}
1247
1248void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1249{
1250 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1251}
1252
1253void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1254{
1255 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1256}
1257
1258void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1259{
1260 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1261}
1262
1263void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1264{
1265 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1266}
1267
1268void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1269{
1270 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1271}
1272
1273void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1274{
1275 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1276}
1277
1278void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1279{
1280 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1281}
1282
1283void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1284{
1285 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1286}
1287
1288void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1289{
1290 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1291}
1292
1293void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1294{
1295 setUniform(location, count, v, GL_INT);
1296}
1297
1298void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1299{
1300 setUniform(location, count, v, GL_INT_VEC2);
1301}
1302
1303void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1304{
1305 setUniform(location, count, v, GL_INT_VEC3);
1306}
1307
1308void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1309{
1310 setUniform(location, count, v, GL_INT_VEC4);
1311}
1312
1313void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1314{
1315 setUniform(location, count, v, GL_UNSIGNED_INT);
1316}
1317
1318void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1319{
1320 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1321}
1322
1323void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1324{
1325 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1326}
1327
1328void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1329{
1330 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1331}
1332
1333void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1334{
1335 getUniformv(location, params, GL_FLOAT);
1336}
1337
1338void ProgramD3D::getUniformiv(GLint location, GLint *params)
1339{
1340 getUniformv(location, params, GL_INT);
1341}
1342
1343void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1344{
1345 getUniformv(location, params, GL_UNSIGNED_INT);
1346}
1347
1348bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1349 const gl::Caps &caps)
1350{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001351 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1352 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001353
1354 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1355 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1356
1357 // Check that uniforms defined in the vertex and fragment shaders are identical
1358 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1359 UniformMap linkedUniforms;
1360
1361 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001362 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001363 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1364 linkedUniforms[vertexUniform.name] = &vertexUniform;
1365 }
1366
1367 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1368 {
1369 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1370 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1371 if (entry != linkedUniforms.end())
1372 {
1373 const sh::Uniform &vertexUniform = *entry->second;
1374 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001375 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001376 {
1377 return false;
1378 }
1379 }
1380 }
1381
1382 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1383 {
1384 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1385
1386 if (uniform.staticUse)
1387 {
Jamie Madill55def582015-05-04 11:24:57 -04001388 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1389 vertexShaderD3D->getUniformRegister(uniform.name);
1390 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001391 }
1392 }
1393
1394 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1395 {
1396 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1397
1398 if (uniform.staticUse)
1399 {
Jamie Madill55def582015-05-04 11:24:57 -04001400 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1401 fragmentShaderD3D->getUniformRegister(uniform.name);
1402 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001403 }
1404 }
1405
1406 if (!indexUniforms(infoLog, caps))
1407 {
1408 return false;
1409 }
1410
1411 initializeUniformStorage();
1412
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001413 return true;
1414}
1415
Geoff Lang492a7e42014-11-05 13:27:06 -05001416void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001417{
Jamie Madill55def582015-05-04 11:24:57 -04001418 if (uniformRegister == GL_INVALID_INDEX)
1419 {
1420 defineUniform(shader, uniform, uniform.name, nullptr);
1421 return;
1422 }
1423
Geoff Lang492a7e42014-11-05 13:27:06 -05001424 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001425 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1426 encoder.skipRegisters(uniformRegister);
1427
1428 defineUniform(shader, uniform, uniform.name, &encoder);
1429}
1430
Geoff Lang492a7e42014-11-05 13:27:06 -05001431void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1432 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001433{
1434 if (uniform.isStruct())
1435 {
1436 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1437 {
1438 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1439
Jamie Madill55def582015-05-04 11:24:57 -04001440 if (encoder)
1441 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001442
1443 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1444 {
1445 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1446 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1447
1448 defineUniform(shader, field, fieldFullName, encoder);
1449 }
1450
Jamie Madill55def582015-05-04 11:24:57 -04001451 if (encoder)
1452 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001453 }
1454 }
1455 else // Not a struct
1456 {
1457 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001458 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001459 {
1460 encoder->enterAggregateType();
1461 }
1462
1463 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1464
Jamie Madill2857f482015-02-09 15:35:29 -05001465 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001466 sh::BlockMemberInfo blockInfo = encoder ?
1467 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1468 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001469
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001470 if (!linkedUniform)
1471 {
1472 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001473 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001474 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001475
1476 if (encoder)
1477 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001478 mUniforms.push_back(linkedUniform);
1479 }
1480
Jamie Madill55def582015-05-04 11:24:57 -04001481 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001482 {
Jamie Madill55def582015-05-04 11:24:57 -04001483 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1484 {
1485 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1486 }
1487 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1488 {
1489 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1490 }
1491 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001492 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001493
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001494 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001495 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001496 {
1497 encoder->exitAggregateType();
1498 }
1499 }
1500}
1501
1502template <typename T>
1503static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1504{
1505 ASSERT(dest != NULL);
1506 ASSERT(dirtyFlag != NULL);
1507
1508 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1509 *dest = source;
1510}
1511
1512template <typename T>
1513void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1514{
1515 const int components = gl::VariableComponentCount(targetUniformType);
1516 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1517
1518 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1519
1520 int elementCount = targetUniform->elementCount();
1521
1522 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1523
1524 if (targetUniform->type == targetUniformType)
1525 {
1526 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1527
1528 for (int i = 0; i < count; i++)
1529 {
1530 T *dest = target + (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], &targetUniform->dirty);
1536 }
1537 for (int c = components; c < 4; c++)
1538 {
1539 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1540 }
1541 }
1542 }
1543 else if (targetUniform->type == targetBoolType)
1544 {
1545 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1546
1547 for (int i = 0; i < count; i++)
1548 {
1549 GLint *dest = boolParams + (i * 4);
1550 const T *source = v + (i * components);
1551
1552 for (int c = 0; c < components; c++)
1553 {
1554 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1555 }
1556 for (int c = components; c < 4; c++)
1557 {
1558 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1559 }
1560 }
1561 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001562 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001563 {
1564 ASSERT(targetUniformType == GL_INT);
1565
1566 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1567
1568 bool wasDirty = targetUniform->dirty;
1569
1570 for (int i = 0; i < count; i++)
1571 {
1572 GLint *dest = target + (i * 4);
1573 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1574
1575 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1576 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1577 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1578 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1579 }
1580
1581 if (!wasDirty && targetUniform->dirty)
1582 {
1583 mDirtySamplerMapping = true;
1584 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001585 }
1586 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001587}
Brandon Jones18bd4102014-09-22 14:21:44 -07001588
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001589template<typename T>
1590bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1591{
1592 bool dirty = false;
1593 int copyWidth = std::min(targetHeight, srcWidth);
1594 int copyHeight = std::min(targetWidth, srcHeight);
1595
1596 for (int x = 0; x < copyWidth; x++)
1597 {
1598 for (int y = 0; y < copyHeight; y++)
1599 {
1600 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1601 }
1602 }
1603 // clear unfilled right side
1604 for (int y = 0; y < copyWidth; y++)
1605 {
1606 for (int x = copyHeight; x < targetWidth; x++)
1607 {
1608 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1609 }
1610 }
1611 // clear unfilled bottom.
1612 for (int y = copyWidth; y < targetHeight; y++)
1613 {
1614 for (int x = 0; x < targetWidth; x++)
1615 {
1616 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1617 }
1618 }
1619
1620 return dirty;
1621}
1622
1623template<typename T>
1624bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1625{
1626 bool dirty = false;
1627 int copyWidth = std::min(targetWidth, srcWidth);
1628 int copyHeight = std::min(targetHeight, srcHeight);
1629
1630 for (int y = 0; y < copyHeight; y++)
1631 {
1632 for (int x = 0; x < copyWidth; x++)
1633 {
1634 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1635 }
1636 }
1637 // clear unfilled right side
1638 for (int y = 0; y < copyHeight; y++)
1639 {
1640 for (int x = copyWidth; x < targetWidth; x++)
1641 {
1642 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1643 }
1644 }
1645 // clear unfilled bottom.
1646 for (int y = copyHeight; y < targetHeight; y++)
1647 {
1648 for (int x = 0; x < targetWidth; x++)
1649 {
1650 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1651 }
1652 }
1653
1654 return dirty;
1655}
1656
1657template <int cols, int rows>
1658void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1659{
1660 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1661
1662 int elementCount = targetUniform->elementCount();
1663
1664 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1665 const unsigned int targetMatrixStride = (4 * rows);
1666 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1667
1668 for (int i = 0; i < count; i++)
1669 {
1670 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1671 if (transpose == GL_FALSE)
1672 {
1673 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1674 }
1675 else
1676 {
1677 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1678 }
1679 target += targetMatrixStride;
1680 value += cols * rows;
1681 }
1682}
1683
1684template <typename T>
1685void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1686{
1687 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1688
1689 if (gl::IsMatrixType(targetUniform->type))
1690 {
1691 const int rows = gl::VariableRowCount(targetUniform->type);
1692 const int cols = gl::VariableColumnCount(targetUniform->type);
1693 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1694 }
1695 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1696 {
1697 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1698 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1699 size * sizeof(T));
1700 }
1701 else
1702 {
1703 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1704 switch (gl::VariableComponentType(targetUniform->type))
1705 {
1706 case GL_BOOL:
1707 {
1708 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1709
1710 for (unsigned int i = 0; i < size; i++)
1711 {
1712 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1713 }
1714 }
1715 break;
1716
1717 case GL_FLOAT:
1718 {
1719 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1720
1721 for (unsigned int i = 0; i < size; i++)
1722 {
1723 params[i] = static_cast<T>(floatParams[i]);
1724 }
1725 }
1726 break;
1727
1728 case GL_INT:
1729 {
1730 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1731
1732 for (unsigned int i = 0; i < size; i++)
1733 {
1734 params[i] = static_cast<T>(intParams[i]);
1735 }
1736 }
1737 break;
1738
1739 case GL_UNSIGNED_INT:
1740 {
1741 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1742
1743 for (unsigned int i = 0; i < size; i++)
1744 {
1745 params[i] = static_cast<T>(uintParams[i]);
1746 }
1747 }
1748 break;
1749
1750 default: UNREACHABLE();
1751 }
1752 }
1753}
1754
1755template <typename VarT>
1756void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1757 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1758 bool inRowMajorLayout)
1759{
1760 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1761 {
1762 const VarT &field = fields[uniformIndex];
1763 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1764
1765 if (field.isStruct())
1766 {
1767 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1768
1769 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1770 {
1771 encoder->enterAggregateType();
1772
1773 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1774 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1775
1776 encoder->exitAggregateType();
1777 }
1778 }
1779 else
1780 {
1781 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1782
1783 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1784
1785 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1786 blockIndex, memberInfo);
1787
1788 // add to uniform list, but not index, since uniform block uniforms have no location
1789 blockUniformIndexes->push_back(mUniforms.size());
1790 mUniforms.push_back(newUniform);
1791 }
1792 }
1793}
1794
1795bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1796 const gl::Caps &caps)
1797{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001798 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001799
1800 // create uniform block entries if they do not exist
1801 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1802 {
1803 std::vector<unsigned int> blockUniformIndexes;
1804 const unsigned int blockIndex = mUniformBlocks.size();
1805
1806 // define member uniforms
1807 sh::BlockLayoutEncoder *encoder = NULL;
1808
1809 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1810 {
1811 encoder = new sh::Std140BlockEncoder;
1812 }
1813 else
1814 {
1815 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1816 }
1817 ASSERT(encoder);
1818
1819 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1820
1821 size_t dataSize = encoder->getBlockSize();
1822
1823 // create all the uniform blocks
1824 if (interfaceBlock.arraySize > 0)
1825 {
1826 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1827 {
1828 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1829 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1830 mUniformBlocks.push_back(newUniformBlock);
1831 }
1832 }
1833 else
1834 {
1835 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1836 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1837 mUniformBlocks.push_back(newUniformBlock);
1838 }
1839 }
1840
1841 if (interfaceBlock.staticUse)
1842 {
1843 // Assign registers to the uniform blocks
1844 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1845 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1846 ASSERT(blockIndex != GL_INVALID_INDEX);
1847 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1848
1849 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1850
1851 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1852 {
1853 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1854 ASSERT(uniformBlock->name == interfaceBlock.name);
1855
1856 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1857 interfaceBlockRegister + uniformBlockElement, caps))
1858 {
1859 return false;
1860 }
1861 }
1862 }
1863
1864 return true;
1865}
1866
1867bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1868 GLenum samplerType,
1869 unsigned int samplerCount,
1870 std::vector<Sampler> &outSamplers,
1871 GLuint *outUsedRange)
1872{
1873 unsigned int samplerIndex = startSamplerIndex;
1874
1875 do
1876 {
1877 if (samplerIndex < outSamplers.size())
1878 {
1879 Sampler& sampler = outSamplers[samplerIndex];
1880 sampler.active = true;
1881 sampler.textureType = GetTextureType(samplerType);
1882 sampler.logicalTextureUnit = 0;
1883 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1884 }
1885 else
1886 {
1887 return false;
1888 }
1889
1890 samplerIndex++;
1891 } while (samplerIndex < startSamplerIndex + samplerCount);
1892
1893 return true;
1894}
1895
1896bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1897{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001898 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001899 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1900
1901 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1902 {
1903 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1904 &mUsedVertexSamplerRange))
1905 {
Jamie Madillf6113162015-05-07 11:49:21 -04001906 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1907 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001908 return false;
1909 }
1910
1911 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1912 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1913 {
Jamie Madillf6113162015-05-07 11:49:21 -04001914 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1915 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001916 return false;
1917 }
1918 }
1919
1920 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1921 {
1922 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1923 &mUsedPixelSamplerRange))
1924 {
Jamie Madillf6113162015-05-07 11:49:21 -04001925 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
1926 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927 return false;
1928 }
1929
1930 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1931 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1932 {
Jamie Madillf6113162015-05-07 11:49:21 -04001933 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
1934 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001935 return false;
1936 }
1937 }
1938
1939 return true;
1940}
1941
1942bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1943{
1944 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1945 {
1946 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1947
Geoff Lang2ec386b2014-12-03 14:44:38 -05001948 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001949 {
1950 if (!indexSamplerUniform(uniform, infoLog, caps))
1951 {
1952 return false;
1953 }
1954 }
1955
Jamie Madill55def582015-05-04 11:24:57 -04001956 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001957 {
Jamie Madill55def582015-05-04 11:24:57 -04001958 if (!uniform.isBuiltIn())
1959 {
1960 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayIndex, uniformIndex));
1961 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001962 }
1963 }
1964
1965 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001966}
1967
Brandon Jonesc9610c52014-08-25 17:02:59 -07001968void ProgramD3D::reset()
1969{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001970 ProgramImpl::reset();
1971
Brandon Joneseb994362014-09-24 10:27:28 -07001972 SafeDeleteContainer(mVertexExecutables);
1973 SafeDeleteContainer(mPixelExecutables);
1974 SafeDelete(mGeometryExecutable);
1975
1976 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001977
Brandon Jones22502d52014-08-29 16:58:36 -07001978 mVertexHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301979 mVertexWorkarounds.reset();
Brandon Jones44151a92014-09-10 11:32:25 -07001980 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001981
1982 mPixelHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301983 mPixelWorkarounds.reset();
Brandon Jones22502d52014-08-29 16:58:36 -07001984 mUsesFragDepth = false;
1985 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001986 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001987
Brandon Jonesc9610c52014-08-25 17:02:59 -07001988 SafeDelete(mVertexUniformStorage);
1989 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990
1991 mSamplersPS.clear();
1992 mSamplersVS.clear();
1993
1994 mUsedVertexSamplerRange = 0;
1995 mUsedPixelSamplerRange = 0;
1996 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001997
1998 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001999}
2000
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001unsigned int ProgramD3D::getSerial() const
2002{
2003 return mSerial;
2004}
2005
2006unsigned int ProgramD3D::issueSerial()
2007{
2008 return mCurrentSerial++;
2009}
2010
Jamie Madill437d2662014-12-05 14:23:35 -05002011void ProgramD3D::initAttributesByLayout()
2012{
2013 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2014 {
2015 mAttributesByLayout[i] = i;
2016 }
2017
2018 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2019}
2020
Jamie Madill476682e2015-06-30 10:04:29 -04002021void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002022 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2023 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002024{
Jamie Madill476682e2015-06-30 10:04:29 -04002025 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002026 {
Jamie Madill476682e2015-06-30 10:04:29 -04002027 int oldIndex = mAttributesByLayout[attribIndex];
2028 sortedSemanticIndicesOut[attribIndex] = mSemanticIndex[oldIndex];
2029 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002030 }
2031}
2032
Brandon Jonesc9610c52014-08-25 17:02:59 -07002033}