blob: 5bd7a9543aeb1f52852b2b2cf1acfec21883b5e8 [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
Brandon Jones44151a92014-09-10 11:32:25 -07001089void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1090{
1091 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1092}
1093
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001094void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001095{
1096 // Compute total default block size
1097 unsigned int vertexRegisters = 0;
1098 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001099 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001100 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001101 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001102
Geoff Lang2ec386b2014-12-03 14:44:38 -05001103 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001104 {
1105 if (uniform.isReferencedByVertexShader())
1106 {
1107 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1108 }
1109 if (uniform.isReferencedByFragmentShader())
1110 {
1111 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1112 }
1113 }
1114 }
1115
1116 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1117 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1118}
1119
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001120gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001121{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001122 updateSamplerMapping();
1123
1124 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1125 if (error.isError())
1126 {
1127 return error;
1128 }
1129
1130 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1131 {
1132 mUniforms[uniformIndex]->dirty = false;
1133 }
1134
1135 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001136}
1137
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001138gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001139{
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001140 GLint vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
1141 GLint fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001142
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001143 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1144 {
1145 vertexUniformBuffers[registerIndex] = -1;
1146 }
1147
1148 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1149 {
1150 fragmentUniformBuffers[registerIndex] = -1;
1151 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001152
1153 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1154 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1155
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001156 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001157 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001158 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001159 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001160
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001161 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001162
1163 // Unnecessary to apply an unreferenced standard or shared UBO
1164 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1165 {
1166 continue;
1167 }
1168
1169 if (uniformBlock->isReferencedByVertexShader())
1170 {
1171 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001172 ASSERT(vertexUniformBuffers[registerIndex] == -1);
1173 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
1174 vertexUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001175 }
1176
1177 if (uniformBlock->isReferencedByFragmentShader())
1178 {
1179 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001180 ASSERT(fragmentUniformBuffers[registerIndex] == -1);
1181 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
1182 fragmentUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001183 }
1184 }
1185
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001186 return mRenderer->setUniformBuffers(data, vertexUniformBuffers, fragmentUniformBuffers);
Brandon Jones18bd4102014-09-22 14:21:44 -07001187}
1188
1189bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1190 unsigned int registerIndex, const gl::Caps &caps)
1191{
1192 if (shader == GL_VERTEX_SHADER)
1193 {
1194 uniformBlock->vsRegisterIndex = registerIndex;
1195 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1196 {
Jamie Madillf6113162015-05-07 11:49:21 -04001197 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (" << caps.maxVertexUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001198 return false;
1199 }
1200 }
1201 else if (shader == GL_FRAGMENT_SHADER)
1202 {
1203 uniformBlock->psRegisterIndex = registerIndex;
1204 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1205 {
Jamie Madillf6113162015-05-07 11:49:21 -04001206 infoLog << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (" << caps.maxFragmentUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001207 return false;
1208 }
1209 }
1210 else UNREACHABLE();
1211
1212 return true;
1213}
1214
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001215void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001216{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001217 unsigned int numUniforms = mUniforms.size();
1218 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001219 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001220 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001221 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001222}
1223
1224void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1225{
1226 setUniform(location, count, v, GL_FLOAT);
1227}
1228
1229void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1230{
1231 setUniform(location, count, v, GL_FLOAT_VEC2);
1232}
1233
1234void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1235{
1236 setUniform(location, count, v, GL_FLOAT_VEC3);
1237}
1238
1239void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1240{
1241 setUniform(location, count, v, GL_FLOAT_VEC4);
1242}
1243
1244void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1245{
1246 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1247}
1248
1249void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1250{
1251 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1252}
1253
1254void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1255{
1256 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1257}
1258
1259void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1260{
1261 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1262}
1263
1264void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1265{
1266 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1267}
1268
1269void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1270{
1271 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1272}
1273
1274void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1275{
1276 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1277}
1278
1279void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1280{
1281 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1282}
1283
1284void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1285{
1286 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1287}
1288
1289void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1290{
1291 setUniform(location, count, v, GL_INT);
1292}
1293
1294void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1295{
1296 setUniform(location, count, v, GL_INT_VEC2);
1297}
1298
1299void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1300{
1301 setUniform(location, count, v, GL_INT_VEC3);
1302}
1303
1304void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1305{
1306 setUniform(location, count, v, GL_INT_VEC4);
1307}
1308
1309void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1310{
1311 setUniform(location, count, v, GL_UNSIGNED_INT);
1312}
1313
1314void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1315{
1316 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1317}
1318
1319void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1320{
1321 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1322}
1323
1324void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1325{
1326 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1327}
1328
1329void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1330{
1331 getUniformv(location, params, GL_FLOAT);
1332}
1333
1334void ProgramD3D::getUniformiv(GLint location, GLint *params)
1335{
1336 getUniformv(location, params, GL_INT);
1337}
1338
1339void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1340{
1341 getUniformv(location, params, GL_UNSIGNED_INT);
1342}
1343
1344bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1345 const gl::Caps &caps)
1346{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001347 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1348 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001349
1350 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1351 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1352
1353 // Check that uniforms defined in the vertex and fragment shaders are identical
1354 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1355 UniformMap linkedUniforms;
1356
1357 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001358 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001359 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1360 linkedUniforms[vertexUniform.name] = &vertexUniform;
1361 }
1362
1363 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1364 {
1365 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1366 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1367 if (entry != linkedUniforms.end())
1368 {
1369 const sh::Uniform &vertexUniform = *entry->second;
1370 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001372 {
1373 return false;
1374 }
1375 }
1376 }
1377
1378 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1379 {
1380 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1381
1382 if (uniform.staticUse)
1383 {
Jamie Madill55def582015-05-04 11:24:57 -04001384 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1385 vertexShaderD3D->getUniformRegister(uniform.name);
1386 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001387 }
1388 }
1389
1390 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1391 {
1392 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1393
1394 if (uniform.staticUse)
1395 {
Jamie Madill55def582015-05-04 11:24:57 -04001396 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1397 fragmentShaderD3D->getUniformRegister(uniform.name);
1398 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001399 }
1400 }
1401
1402 if (!indexUniforms(infoLog, caps))
1403 {
1404 return false;
1405 }
1406
1407 initializeUniformStorage();
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{
Jamie Madill55def582015-05-04 11:24:57 -04001414 if (uniformRegister == GL_INVALID_INDEX)
1415 {
1416 defineUniform(shader, uniform, uniform.name, nullptr);
1417 return;
1418 }
1419
Geoff Lang492a7e42014-11-05 13:27:06 -05001420 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001421 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1422 encoder.skipRegisters(uniformRegister);
1423
1424 defineUniform(shader, uniform, uniform.name, &encoder);
1425}
1426
Geoff Lang492a7e42014-11-05 13:27:06 -05001427void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1428 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001429{
1430 if (uniform.isStruct())
1431 {
1432 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1433 {
1434 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1435
Jamie Madill55def582015-05-04 11:24:57 -04001436 if (encoder)
1437 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001438
1439 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1440 {
1441 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1442 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1443
1444 defineUniform(shader, field, fieldFullName, encoder);
1445 }
1446
Jamie Madill55def582015-05-04 11:24:57 -04001447 if (encoder)
1448 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001449 }
1450 }
1451 else // Not a struct
1452 {
1453 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001454 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001455 {
1456 encoder->enterAggregateType();
1457 }
1458
1459 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1460
Jamie Madill2857f482015-02-09 15:35:29 -05001461 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001462 sh::BlockMemberInfo blockInfo = encoder ?
1463 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1464 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001465
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001466 if (!linkedUniform)
1467 {
1468 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001469 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001470 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001471
1472 if (encoder)
1473 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001474 mUniforms.push_back(linkedUniform);
1475 }
1476
Jamie Madill55def582015-05-04 11:24:57 -04001477 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001478 {
Jamie Madill55def582015-05-04 11:24:57 -04001479 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1480 {
1481 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1482 }
1483 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1484 {
1485 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1486 }
1487 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001488 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001489
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001490 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001491 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001492 {
1493 encoder->exitAggregateType();
1494 }
1495 }
1496}
1497
1498template <typename T>
1499static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1500{
1501 ASSERT(dest != NULL);
1502 ASSERT(dirtyFlag != NULL);
1503
1504 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1505 *dest = source;
1506}
1507
1508template <typename T>
1509void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1510{
1511 const int components = gl::VariableComponentCount(targetUniformType);
1512 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1513
1514 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1515
1516 int elementCount = targetUniform->elementCount();
1517
1518 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1519
1520 if (targetUniform->type == targetUniformType)
1521 {
1522 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1523
1524 for (int i = 0; i < count; i++)
1525 {
1526 T *dest = target + (i * 4);
1527 const T *source = v + (i * components);
1528
1529 for (int c = 0; c < components; c++)
1530 {
1531 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1532 }
1533 for (int c = components; c < 4; c++)
1534 {
1535 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1536 }
1537 }
1538 }
1539 else if (targetUniform->type == targetBoolType)
1540 {
1541 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1542
1543 for (int i = 0; i < count; i++)
1544 {
1545 GLint *dest = boolParams + (i * 4);
1546 const T *source = v + (i * components);
1547
1548 for (int c = 0; c < components; c++)
1549 {
1550 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1551 }
1552 for (int c = components; c < 4; c++)
1553 {
1554 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1555 }
1556 }
1557 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001558 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001559 {
1560 ASSERT(targetUniformType == GL_INT);
1561
1562 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1563
1564 bool wasDirty = targetUniform->dirty;
1565
1566 for (int i = 0; i < count; i++)
1567 {
1568 GLint *dest = target + (i * 4);
1569 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1570
1571 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1572 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1573 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1574 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1575 }
1576
1577 if (!wasDirty && targetUniform->dirty)
1578 {
1579 mDirtySamplerMapping = true;
1580 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001581 }
1582 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001583}
Brandon Jones18bd4102014-09-22 14:21:44 -07001584
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001585template<typename T>
1586bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1587{
1588 bool dirty = false;
1589 int copyWidth = std::min(targetHeight, srcWidth);
1590 int copyHeight = std::min(targetWidth, srcHeight);
1591
1592 for (int x = 0; x < copyWidth; x++)
1593 {
1594 for (int y = 0; y < copyHeight; y++)
1595 {
1596 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1597 }
1598 }
1599 // clear unfilled right side
1600 for (int y = 0; y < copyWidth; y++)
1601 {
1602 for (int x = copyHeight; x < targetWidth; x++)
1603 {
1604 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1605 }
1606 }
1607 // clear unfilled bottom.
1608 for (int y = copyWidth; y < targetHeight; y++)
1609 {
1610 for (int x = 0; x < targetWidth; x++)
1611 {
1612 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1613 }
1614 }
1615
1616 return dirty;
1617}
1618
1619template<typename T>
1620bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1621{
1622 bool dirty = false;
1623 int copyWidth = std::min(targetWidth, srcWidth);
1624 int copyHeight = std::min(targetHeight, srcHeight);
1625
1626 for (int y = 0; y < copyHeight; y++)
1627 {
1628 for (int x = 0; x < copyWidth; x++)
1629 {
1630 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1631 }
1632 }
1633 // clear unfilled right side
1634 for (int y = 0; y < copyHeight; y++)
1635 {
1636 for (int x = copyWidth; x < targetWidth; x++)
1637 {
1638 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1639 }
1640 }
1641 // clear unfilled bottom.
1642 for (int y = copyHeight; y < targetHeight; y++)
1643 {
1644 for (int x = 0; x < targetWidth; x++)
1645 {
1646 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1647 }
1648 }
1649
1650 return dirty;
1651}
1652
1653template <int cols, int rows>
1654void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1655{
1656 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1657
1658 int elementCount = targetUniform->elementCount();
1659
1660 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1661 const unsigned int targetMatrixStride = (4 * rows);
1662 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1663
1664 for (int i = 0; i < count; i++)
1665 {
1666 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1667 if (transpose == GL_FALSE)
1668 {
1669 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1670 }
1671 else
1672 {
1673 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1674 }
1675 target += targetMatrixStride;
1676 value += cols * rows;
1677 }
1678}
1679
1680template <typename T>
1681void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1682{
1683 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1684
1685 if (gl::IsMatrixType(targetUniform->type))
1686 {
1687 const int rows = gl::VariableRowCount(targetUniform->type);
1688 const int cols = gl::VariableColumnCount(targetUniform->type);
1689 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1690 }
1691 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1692 {
1693 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1694 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1695 size * sizeof(T));
1696 }
1697 else
1698 {
1699 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1700 switch (gl::VariableComponentType(targetUniform->type))
1701 {
1702 case GL_BOOL:
1703 {
1704 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1705
1706 for (unsigned int i = 0; i < size; i++)
1707 {
1708 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1709 }
1710 }
1711 break;
1712
1713 case GL_FLOAT:
1714 {
1715 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1716
1717 for (unsigned int i = 0; i < size; i++)
1718 {
1719 params[i] = static_cast<T>(floatParams[i]);
1720 }
1721 }
1722 break;
1723
1724 case GL_INT:
1725 {
1726 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1727
1728 for (unsigned int i = 0; i < size; i++)
1729 {
1730 params[i] = static_cast<T>(intParams[i]);
1731 }
1732 }
1733 break;
1734
1735 case GL_UNSIGNED_INT:
1736 {
1737 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1738
1739 for (unsigned int i = 0; i < size; i++)
1740 {
1741 params[i] = static_cast<T>(uintParams[i]);
1742 }
1743 }
1744 break;
1745
1746 default: UNREACHABLE();
1747 }
1748 }
1749}
1750
1751template <typename VarT>
1752void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1753 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1754 bool inRowMajorLayout)
1755{
1756 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1757 {
1758 const VarT &field = fields[uniformIndex];
1759 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1760
1761 if (field.isStruct())
1762 {
1763 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1764
1765 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1766 {
1767 encoder->enterAggregateType();
1768
1769 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1770 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1771
1772 encoder->exitAggregateType();
1773 }
1774 }
1775 else
1776 {
1777 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1778
1779 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1780
1781 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1782 blockIndex, memberInfo);
1783
1784 // add to uniform list, but not index, since uniform block uniforms have no location
1785 blockUniformIndexes->push_back(mUniforms.size());
1786 mUniforms.push_back(newUniform);
1787 }
1788 }
1789}
1790
1791bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1792 const gl::Caps &caps)
1793{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001794 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001795
1796 // create uniform block entries if they do not exist
1797 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1798 {
1799 std::vector<unsigned int> blockUniformIndexes;
1800 const unsigned int blockIndex = mUniformBlocks.size();
1801
1802 // define member uniforms
1803 sh::BlockLayoutEncoder *encoder = NULL;
1804
1805 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1806 {
1807 encoder = new sh::Std140BlockEncoder;
1808 }
1809 else
1810 {
1811 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1812 }
1813 ASSERT(encoder);
1814
1815 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1816
1817 size_t dataSize = encoder->getBlockSize();
1818
1819 // create all the uniform blocks
1820 if (interfaceBlock.arraySize > 0)
1821 {
1822 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1823 {
1824 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1825 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1826 mUniformBlocks.push_back(newUniformBlock);
1827 }
1828 }
1829 else
1830 {
1831 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1832 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1833 mUniformBlocks.push_back(newUniformBlock);
1834 }
1835 }
1836
1837 if (interfaceBlock.staticUse)
1838 {
1839 // Assign registers to the uniform blocks
1840 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1841 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1842 ASSERT(blockIndex != GL_INVALID_INDEX);
1843 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1844
1845 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1846
1847 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1848 {
1849 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1850 ASSERT(uniformBlock->name == interfaceBlock.name);
1851
1852 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1853 interfaceBlockRegister + uniformBlockElement, caps))
1854 {
1855 return false;
1856 }
1857 }
1858 }
1859
1860 return true;
1861}
1862
1863bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1864 GLenum samplerType,
1865 unsigned int samplerCount,
1866 std::vector<Sampler> &outSamplers,
1867 GLuint *outUsedRange)
1868{
1869 unsigned int samplerIndex = startSamplerIndex;
1870
1871 do
1872 {
1873 if (samplerIndex < outSamplers.size())
1874 {
1875 Sampler& sampler = outSamplers[samplerIndex];
1876 sampler.active = true;
1877 sampler.textureType = GetTextureType(samplerType);
1878 sampler.logicalTextureUnit = 0;
1879 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1880 }
1881 else
1882 {
1883 return false;
1884 }
1885
1886 samplerIndex++;
1887 } while (samplerIndex < startSamplerIndex + samplerCount);
1888
1889 return true;
1890}
1891
1892bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1893{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001894 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001895 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1896
1897 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1898 {
1899 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1900 &mUsedVertexSamplerRange))
1901 {
Jamie Madillf6113162015-05-07 11:49:21 -04001902 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1903 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001904 return false;
1905 }
1906
1907 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1908 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1909 {
Jamie Madillf6113162015-05-07 11:49:21 -04001910 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1911 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001912 return false;
1913 }
1914 }
1915
1916 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1917 {
1918 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1919 &mUsedPixelSamplerRange))
1920 {
Jamie Madillf6113162015-05-07 11:49:21 -04001921 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
1922 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001923 return false;
1924 }
1925
1926 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1927 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1928 {
Jamie Madillf6113162015-05-07 11:49:21 -04001929 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
1930 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001931 return false;
1932 }
1933 }
1934
1935 return true;
1936}
1937
1938bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1939{
1940 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1941 {
1942 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1943
Geoff Lang2ec386b2014-12-03 14:44:38 -05001944 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945 {
1946 if (!indexSamplerUniform(uniform, infoLog, caps))
1947 {
1948 return false;
1949 }
1950 }
1951
Jamie Madill55def582015-05-04 11:24:57 -04001952 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001953 {
Jamie Madill55def582015-05-04 11:24:57 -04001954 if (!uniform.isBuiltIn())
1955 {
1956 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayIndex, uniformIndex));
1957 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001958 }
1959 }
1960
1961 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001962}
1963
Brandon Jonesc9610c52014-08-25 17:02:59 -07001964void ProgramD3D::reset()
1965{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001966 ProgramImpl::reset();
1967
Brandon Joneseb994362014-09-24 10:27:28 -07001968 SafeDeleteContainer(mVertexExecutables);
1969 SafeDeleteContainer(mPixelExecutables);
1970 SafeDelete(mGeometryExecutable);
1971
1972 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001973
Brandon Jones22502d52014-08-29 16:58:36 -07001974 mVertexHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301975 mVertexWorkarounds.reset();
Brandon Jones44151a92014-09-10 11:32:25 -07001976 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001977
1978 mPixelHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301979 mPixelWorkarounds.reset();
Brandon Jones22502d52014-08-29 16:58:36 -07001980 mUsesFragDepth = false;
1981 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001982 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001983
Brandon Jonesc9610c52014-08-25 17:02:59 -07001984 SafeDelete(mVertexUniformStorage);
1985 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986
1987 mSamplersPS.clear();
1988 mSamplersVS.clear();
1989
1990 mUsedVertexSamplerRange = 0;
1991 mUsedPixelSamplerRange = 0;
1992 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001993
1994 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001995}
1996
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997unsigned int ProgramD3D::getSerial() const
1998{
1999 return mSerial;
2000}
2001
2002unsigned int ProgramD3D::issueSerial()
2003{
2004 return mCurrentSerial++;
2005}
2006
Jamie Madill437d2662014-12-05 14:23:35 -05002007void ProgramD3D::initAttributesByLayout()
2008{
2009 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2010 {
2011 mAttributesByLayout[i] = i;
2012 }
2013
2014 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2015}
2016
2017void ProgramD3D::sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
2018 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const
2019{
2020 rx::TranslatedAttribute oldTranslatedAttributes[gl::MAX_VERTEX_ATTRIBS];
2021
2022 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2023 {
2024 oldTranslatedAttributes[i] = attributes[i];
2025 }
2026
2027 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2028 {
2029 int oldIndex = mAttributesByLayout[i];
2030 sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
2031 attributes[i] = oldTranslatedAttributes[oldIndex];
2032 }
2033}
2034
Brandon Jonesc9610c52014-08-25 17:02:59 -07002035}