blob: 9b3f9630fa482a7042cfb4f1926949fd0316fd9b [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 Madilld3dfda22015-07-06 08:28:49 -040015#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000016#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050018#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/renderer/d3d/RendererD3D.h"
20#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050021#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050022#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050023
Brandon Jonesc9610c52014-08-25 17:02:59 -070024namespace rx
25{
26
Brandon Joneseb994362014-09-24 10:27:28 -070027namespace
28{
29
Brandon Jones1a8a7e32014-10-01 12:49:30 -070030GLenum GetTextureType(GLenum samplerType)
31{
32 switch (samplerType)
33 {
34 case GL_SAMPLER_2D:
35 case GL_INT_SAMPLER_2D:
36 case GL_UNSIGNED_INT_SAMPLER_2D:
37 case GL_SAMPLER_2D_SHADOW:
38 return GL_TEXTURE_2D;
39 case GL_SAMPLER_3D:
40 case GL_INT_SAMPLER_3D:
41 case GL_UNSIGNED_INT_SAMPLER_3D:
42 return GL_TEXTURE_3D;
43 case GL_SAMPLER_CUBE:
44 case GL_SAMPLER_CUBE_SHADOW:
45 return GL_TEXTURE_CUBE_MAP;
46 case GL_INT_SAMPLER_CUBE:
47 case GL_UNSIGNED_INT_SAMPLER_CUBE:
48 return GL_TEXTURE_CUBE_MAP;
49 case GL_SAMPLER_2D_ARRAY:
50 case GL_INT_SAMPLER_2D_ARRAY:
51 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
52 case GL_SAMPLER_2D_ARRAY_SHADOW:
53 return GL_TEXTURE_2D_ARRAY;
54 default: UNREACHABLE();
55 }
56
57 return GL_TEXTURE_2D;
58}
59
Jamie Madillf8dd7b12015-08-05 13:50:08 -040060gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070061{
Jamie Madillbd136f92015-08-10 14:51:37 -040062 gl::InputLayout defaultLayout;
63 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070064 {
Brandon Joneseb994362014-09-24 10:27:28 -070065 if (shaderAttr.type != GL_NONE)
66 {
67 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
68
Jamie Madilld3dfda22015-07-06 08:28:49 -040069 for (size_t rowIndex = 0;
70 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType);
71 ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070072 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040073 GLenum componentType = gl::VariableComponentType(transposedType);
74 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
75 bool pureInt = (componentType != GL_FLOAT);
76 gl::VertexFormatType defaultType = gl::GetVertexFormatType(
77 componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070078
Jamie Madillbd136f92015-08-10 14:51:37 -040079 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070080 }
81 }
82 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040083
84 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070085}
86
87std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
88{
Jamie Madillb4463142014-12-19 14:56:54 -050089 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070090
Jamie Madillb4463142014-12-19 14:56:54 -050091 if (!shaderOutputVars.empty())
92 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070093 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
94 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050095 }
Brandon Joneseb994362014-09-24 10:27:28 -070096
97 return defaultPixelOutput;
98}
99
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700100bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
101{
102 return var.isRowMajorLayout;
103}
104
105bool IsRowMajorLayout(const sh::ShaderVariable &var)
106{
107 return false;
108}
109
Jamie Madill437d2662014-12-05 14:23:35 -0500110struct AttributeSorter
111{
Jamie Madill63805b42015-08-25 13:17:39 -0400112 AttributeSorter(const ProgramD3D::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -0500113 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -0500114 {
115 }
116
117 bool operator()(int a, int b)
118 {
Jamie Madill80d934b2015-02-19 10:16:12 -0500119 int indexA = (*originalIndices)[a];
120 int indexB = (*originalIndices)[b];
121
122 if (indexA == -1) return false;
123 if (indexB == -1) return true;
124 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -0500125 }
126
Jamie Madill63805b42015-08-25 13:17:39 -0400127 const ProgramD3D::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500128};
129
Jamie Madillada9ecc2015-08-17 12:53:37 -0400130bool LinkVaryingRegisters(gl::InfoLog &infoLog,
131 ShaderD3D *vertexShaderD3D,
132 ShaderD3D *fragmentShaderD3D)
133{
134 for (gl::PackedVarying &input : fragmentShaderD3D->getVaryings())
135 {
136 bool matched = false;
137
138 // Built-in varyings obey special rules
139 if (input.isBuiltIn())
140 {
141 continue;
142 }
143
144 for (gl::PackedVarying &output : vertexShaderD3D->getVaryings())
145 {
146 if (output.name == input.name)
147 {
148 output.registerIndex = input.registerIndex;
149 output.columnIndex = input.columnIndex;
150
151 matched = true;
152 break;
153 }
154 }
155
156 // We permit unmatched, unreferenced varyings
157 ASSERT(matched || !input.staticUse);
158 }
159
160 return true;
Brandon Joneseb994362014-09-24 10:27:28 -0700161}
162
Jamie Madillada9ecc2015-08-17 12:53:37 -0400163} // anonymous namespace
164
Jamie Madilld3dfda22015-07-06 08:28:49 -0400165ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
166 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500167 ShaderExecutableD3D *shaderExecutable)
Jamie Madilld3dfda22015-07-06 08:28:49 -0400168 : mInputs(inputLayout),
169 mSignature(signature),
170 mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700171{
Brandon Joneseb994362014-09-24 10:27:28 -0700172}
173
174ProgramD3D::VertexExecutable::~VertexExecutable()
175{
176 SafeDelete(mShaderExecutable);
177}
178
Jamie Madilld3dfda22015-07-06 08:28:49 -0400179// static
180void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
181 const gl::InputLayout &inputLayout,
182 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700183{
Jamie Madillbd136f92015-08-10 14:51:37 -0400184 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400185
186 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700187 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400188 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400189 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400190 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700191 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400192 VertexConversionType conversionType =
193 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400194 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700195 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400196
197 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700198 }
Brandon Joneseb994362014-09-24 10:27:28 -0700199}
200
Jamie Madilld3dfda22015-07-06 08:28:49 -0400201bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
202{
Jamie Madillbd136f92015-08-10 14:51:37 -0400203 size_t limit = std::max(mSignature.size(), signature.size());
204 for (size_t index = 0; index < limit; ++index)
205 {
206 // treat undefined indexes as 'not converted'
207 bool a = index < signature.size() ? signature[index] : false;
208 bool b = index < mSignature.size() ? mSignature[index] : false;
209 if (a != b)
210 return false;
211 }
212
213 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400214}
215
216ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
217 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700218 : mOutputSignature(outputSignature),
219 mShaderExecutable(shaderExecutable)
220{
221}
222
223ProgramD3D::PixelExecutable::~PixelExecutable()
224{
225 SafeDelete(mShaderExecutable);
226}
227
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700228ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
229{
230}
231
Geoff Lang7dd2e102014-11-10 15:19:26 -0500232unsigned int ProgramD3D::mCurrentSerial = 1;
233
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400234ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
235 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700236 mRenderer(renderer),
237 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700238 mGeometryExecutable(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700239 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700240 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700241 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700242 mUsedVertexSamplerRange(0),
243 mUsedPixelSamplerRange(0),
244 mDirtySamplerMapping(true),
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400245 mTextureUnitTypesCache(renderer->getRendererCaps().maxCombinedTextureImageUnits),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500246 mShaderVersion(100),
247 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700248{
Brandon Joneseb994362014-09-24 10:27:28 -0700249 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700250}
251
252ProgramD3D::~ProgramD3D()
253{
254 reset();
255 SafeDelete(mDynamicHLSL);
256}
257
Brandon Jones44151a92014-09-10 11:32:25 -0700258bool ProgramD3D::usesPointSpriteEmulation() const
259{
260 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
261}
262
263bool ProgramD3D::usesGeometryShader() const
264{
Cooper Partine6664f02015-01-09 16:22:24 -0800265 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
266}
267
268bool ProgramD3D::usesInstancedPointSpriteEmulation() const
269{
270 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700271}
272
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700273GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
274{
275 GLint logicalTextureUnit = -1;
276
277 switch (type)
278 {
279 case gl::SAMPLER_PIXEL:
280 ASSERT(samplerIndex < caps.maxTextureImageUnits);
281 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
282 {
283 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
284 }
285 break;
286 case gl::SAMPLER_VERTEX:
287 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
288 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
289 {
290 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
291 }
292 break;
293 default: UNREACHABLE();
294 }
295
296 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
297 {
298 return logicalTextureUnit;
299 }
300
301 return -1;
302}
303
304// Returns the texture type for a given Direct3D 9 sampler type and
305// index (0-15 for the pixel shader and 0-3 for the vertex shader).
306GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
307{
308 switch (type)
309 {
310 case gl::SAMPLER_PIXEL:
311 ASSERT(samplerIndex < mSamplersPS.size());
312 ASSERT(mSamplersPS[samplerIndex].active);
313 return mSamplersPS[samplerIndex].textureType;
314 case gl::SAMPLER_VERTEX:
315 ASSERT(samplerIndex < mSamplersVS.size());
316 ASSERT(mSamplersVS[samplerIndex].active);
317 return mSamplersVS[samplerIndex].textureType;
318 default: UNREACHABLE();
319 }
320
321 return GL_TEXTURE_2D;
322}
323
324GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
325{
326 switch (type)
327 {
328 case gl::SAMPLER_PIXEL:
329 return mUsedPixelSamplerRange;
330 case gl::SAMPLER_VERTEX:
331 return mUsedVertexSamplerRange;
332 default:
333 UNREACHABLE();
334 return 0;
335 }
336}
337
338void ProgramD3D::updateSamplerMapping()
339{
340 if (!mDirtySamplerMapping)
341 {
342 return;
343 }
344
345 mDirtySamplerMapping = false;
346
347 // Retrieve sampler uniform values
348 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
349 {
350 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
351
352 if (targetUniform->dirty)
353 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500354 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700355 {
356 int count = targetUniform->elementCount();
357 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
358
359 if (targetUniform->isReferencedByFragmentShader())
360 {
361 unsigned int firstIndex = targetUniform->psRegisterIndex;
362
363 for (int i = 0; i < count; i++)
364 {
365 unsigned int samplerIndex = firstIndex + i;
366
367 if (samplerIndex < mSamplersPS.size())
368 {
369 ASSERT(mSamplersPS[samplerIndex].active);
370 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
371 }
372 }
373 }
374
375 if (targetUniform->isReferencedByVertexShader())
376 {
377 unsigned int firstIndex = targetUniform->vsRegisterIndex;
378
379 for (int i = 0; i < count; i++)
380 {
381 unsigned int samplerIndex = firstIndex + i;
382
383 if (samplerIndex < mSamplersVS.size())
384 {
385 ASSERT(mSamplersVS[samplerIndex].active);
386 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
387 }
388 }
389 }
390 }
391 }
392 }
393}
394
395bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
396{
Jamie Madill13776892015-04-28 12:39:06 -0400397 // Skip cache if we're using an infolog, so we get the full error.
398 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
399 if (!mDirtySamplerMapping && infoLog == nullptr && mCachedValidateSamplersResult.valid())
400 {
401 return mCachedValidateSamplersResult.value();
402 }
403
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700404 // if any two active samplers in a program are of different types, but refer to the same
405 // texture image unit, and this is the current program, then ValidateProgram will fail, and
406 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
407 updateSamplerMapping();
408
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400409 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700410
411 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
412 {
413 if (mSamplersPS[i].active)
414 {
415 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
416
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400417 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700418 {
419 if (infoLog)
420 {
Jamie Madillf6113162015-05-07 11:49:21 -0400421 (*infoLog) << "Sampler uniform (" << unit
422 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
423 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700424 }
425
Jamie Madill13776892015-04-28 12:39:06 -0400426 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700427 return false;
428 }
429
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400430 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700431 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400432 if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700433 {
434 if (infoLog)
435 {
Jamie Madillf6113162015-05-07 11:49:21 -0400436 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
437 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700438 }
439
Jamie Madill13776892015-04-28 12:39:06 -0400440 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700441 return false;
442 }
443 }
444 else
445 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400446 mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700447 }
448 }
449 }
450
451 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
452 {
453 if (mSamplersVS[i].active)
454 {
455 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
456
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400457 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700458 {
459 if (infoLog)
460 {
Jamie Madillf6113162015-05-07 11:49:21 -0400461 (*infoLog) << "Sampler uniform (" << unit
462 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
463 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700464 }
465
Jamie Madill13776892015-04-28 12:39:06 -0400466 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700467 return false;
468 }
469
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400470 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700471 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400472 if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700473 {
474 if (infoLog)
475 {
Jamie Madillf6113162015-05-07 11:49:21 -0400476 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
477 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700478 }
479
Jamie Madill13776892015-04-28 12:39:06 -0400480 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700481 return false;
482 }
483 }
484 else
485 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400486 mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700487 }
488 }
489 }
490
Jamie Madill13776892015-04-28 12:39:06 -0400491 mCachedValidateSamplersResult = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700492 return true;
493}
494
Geoff Lang7dd2e102014-11-10 15:19:26 -0500495LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700496{
Austin Kinross137b1512015-06-17 16:14:53 -0700497 DeviceIdentifier binaryDeviceIdentifier = { 0 };
498 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryDeviceIdentifier), sizeof(DeviceIdentifier));
499
500 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
501 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
502 {
503 infoLog << "Invalid program binary, device configuration has changed.";
504 return LinkResult(false, gl::Error(GL_NO_ERROR));
505 }
506
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500507 int compileFlags = stream->readInt<int>();
508 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
509 {
Jamie Madillf6113162015-05-07 11:49:21 -0400510 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500511 return LinkResult(false, gl::Error(GL_NO_ERROR));
512 }
513
Brandon Jones44151a92014-09-10 11:32:25 -0700514 stream->readInt(&mShaderVersion);
515
Jamie Madill63805b42015-08-25 13:17:39 -0400516 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
517 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
518 {
519 stream->readInt(&mSemanticIndexes[i]);
520 }
521
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700522 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
523 for (unsigned int i = 0; i < psSamplerCount; ++i)
524 {
525 Sampler sampler;
526 stream->readBool(&sampler.active);
527 stream->readInt(&sampler.logicalTextureUnit);
528 stream->readInt(&sampler.textureType);
529 mSamplersPS.push_back(sampler);
530 }
531 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
532 for (unsigned int i = 0; i < vsSamplerCount; ++i)
533 {
534 Sampler sampler;
535 stream->readBool(&sampler.active);
536 stream->readInt(&sampler.logicalTextureUnit);
537 stream->readInt(&sampler.textureType);
538 mSamplersVS.push_back(sampler);
539 }
540
541 stream->readInt(&mUsedVertexSamplerRange);
542 stream->readInt(&mUsedPixelSamplerRange);
543
544 const unsigned int uniformCount = stream->readInt<unsigned int>();
545 if (stream->error())
546 {
Jamie Madillf6113162015-05-07 11:49:21 -0400547 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500548 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700549 }
550
551 mUniforms.resize(uniformCount);
552 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
553 {
554 GLenum type = stream->readInt<GLenum>();
555 GLenum precision = stream->readInt<GLenum>();
556 std::string name = stream->readString();
557 unsigned int arraySize = stream->readInt<unsigned int>();
558 int blockIndex = stream->readInt<int>();
559
560 int offset = stream->readInt<int>();
561 int arrayStride = stream->readInt<int>();
562 int matrixStride = stream->readInt<int>();
563 bool isRowMajorMatrix = stream->readBool();
564
565 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
566
567 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
568
569 stream->readInt(&uniform->psRegisterIndex);
570 stream->readInt(&uniform->vsRegisterIndex);
571 stream->readInt(&uniform->registerCount);
572 stream->readInt(&uniform->registerElement);
573
574 mUniforms[uniformIndex] = uniform;
575 }
576
577 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
578 if (stream->error())
579 {
Jamie Madillf6113162015-05-07 11:49:21 -0400580 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500581 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700582 }
583
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700584 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
585 {
Geoff Lang95137842015-06-02 15:38:43 -0400586 GLuint location;
587 stream->readInt(&location);
588
589 gl::VariableLocation variable;
590 stream->readString(&variable.name);
591 stream->readInt(&variable.element);
592 stream->readInt(&variable.index);
593
594 mUniformIndex[location] = variable;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700595 }
596
597 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
598 if (stream->error())
599 {
Jamie Madillf6113162015-05-07 11:49:21 -0400600 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500601 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700602 }
603
604 mUniformBlocks.resize(uniformBlockCount);
605 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
606 {
607 std::string name = stream->readString();
608 unsigned int elementIndex = stream->readInt<unsigned int>();
609 unsigned int dataSize = stream->readInt<unsigned int>();
610
611 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
612
613 stream->readInt(&uniformBlock->psRegisterIndex);
614 stream->readInt(&uniformBlock->vsRegisterIndex);
615
616 unsigned int numMembers = stream->readInt<unsigned int>();
617 uniformBlock->memberUniformIndexes.resize(numMembers);
618 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
619 {
620 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
621 }
622
623 mUniformBlocks[uniformBlockIndex] = uniformBlock;
624 }
625
Brandon Joneseb994362014-09-24 10:27:28 -0700626 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
627 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
628 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
629 {
630 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
631
632 stream->readString(&varying.name);
633 stream->readInt(&varying.type);
634 stream->readInt(&varying.size);
635 stream->readString(&varying.semanticName);
636 stream->readInt(&varying.semanticIndex);
637 stream->readInt(&varying.semanticIndexCount);
638 }
639
Brandon Jones22502d52014-08-29 16:58:36 -0700640 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530641 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700642 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530643 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700644 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700645 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700646
647 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
648 mPixelShaderKey.resize(pixelShaderKeySize);
649 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
650 {
651 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
652 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
653 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
654 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
655 }
656
Brandon Joneseb994362014-09-24 10:27:28 -0700657 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
658
659 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
660 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
661 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400662 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400663 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700664
Jamie Madilld3dfda22015-07-06 08:28:49 -0400665 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700666 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400667 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700668 }
669
670 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
671 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400672
Jamie Madillada9ecc2015-08-17 12:53:37 -0400673 ShaderExecutableD3D *shaderExecutable = nullptr;
674
675 gl::Error error = mRenderer->loadExecutable(
676 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mTransformFeedbackLinkedVaryings,
677 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400678 if (error.isError())
679 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500680 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400681 }
682
Brandon Joneseb994362014-09-24 10:27:28 -0700683 if (!shaderExecutable)
684 {
Jamie Madillf6113162015-05-07 11:49:21 -0400685 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500686 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700687 }
688
689 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400690 VertexExecutable::Signature signature;
691 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700692
693 // add new binary
694 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
695
696 stream->skip(vertexShaderSize);
697 }
698
699 const size_t pixelShaderCount = stream->readInt<unsigned int>();
700 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
701 {
702 const size_t outputCount = stream->readInt<unsigned int>();
703 std::vector<GLenum> outputs(outputCount);
704 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
705 {
706 stream->readInt(&outputs[outputIndex]);
707 }
708
709 const size_t pixelShaderSize = stream->readInt<unsigned int>();
710 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400711 ShaderExecutableD3D *shaderExecutable = nullptr;
712
713 gl::Error error = mRenderer->loadExecutable(
714 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mTransformFeedbackLinkedVaryings,
715 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400716 if (error.isError())
717 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500718 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400719 }
Brandon Joneseb994362014-09-24 10:27:28 -0700720
721 if (!shaderExecutable)
722 {
Jamie Madillf6113162015-05-07 11:49:21 -0400723 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500724 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700725 }
726
727 // add new binary
728 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
729
730 stream->skip(pixelShaderSize);
731 }
732
733 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
734
735 if (geometryShaderSize > 0)
736 {
737 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400738 gl::Error error = mRenderer->loadExecutable(
739 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
740 mTransformFeedbackLinkedVaryings,
741 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400742 if (error.isError())
743 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500744 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400745 }
Brandon Joneseb994362014-09-24 10:27:28 -0700746
747 if (!mGeometryExecutable)
748 {
Jamie Madillf6113162015-05-07 11:49:21 -0400749 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500750 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700751 }
752 stream->skip(geometryShaderSize);
753 }
754
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700755 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500756 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700757
Geoff Lang7dd2e102014-11-10 15:19:26 -0500758 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700759}
760
Geoff Langb543aff2014-09-30 14:52:54 -0400761gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700762{
Austin Kinross137b1512015-06-17 16:14:53 -0700763 // Output the DeviceIdentifier before we output any shader code
764 // When we load the binary again later, we can validate the device identifier before trying to compile any HLSL
765 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
766 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(DeviceIdentifier));
767
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500768 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
769
Brandon Jones44151a92014-09-10 11:32:25 -0700770 stream->writeInt(mShaderVersion);
771
Jamie Madill63805b42015-08-25 13:17:39 -0400772 // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
773 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i)
774 {
775 stream->writeInt(mSemanticIndexes[i]);
776 }
777
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700778 stream->writeInt(mSamplersPS.size());
779 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
780 {
781 stream->writeInt(mSamplersPS[i].active);
782 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
783 stream->writeInt(mSamplersPS[i].textureType);
784 }
785
786 stream->writeInt(mSamplersVS.size());
787 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
788 {
789 stream->writeInt(mSamplersVS[i].active);
790 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
791 stream->writeInt(mSamplersVS[i].textureType);
792 }
793
794 stream->writeInt(mUsedVertexSamplerRange);
795 stream->writeInt(mUsedPixelSamplerRange);
796
797 stream->writeInt(mUniforms.size());
798 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
799 {
800 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
801
802 stream->writeInt(uniform.type);
803 stream->writeInt(uniform.precision);
804 stream->writeString(uniform.name);
805 stream->writeInt(uniform.arraySize);
806 stream->writeInt(uniform.blockIndex);
807
808 stream->writeInt(uniform.blockInfo.offset);
809 stream->writeInt(uniform.blockInfo.arrayStride);
810 stream->writeInt(uniform.blockInfo.matrixStride);
811 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
812
813 stream->writeInt(uniform.psRegisterIndex);
814 stream->writeInt(uniform.vsRegisterIndex);
815 stream->writeInt(uniform.registerCount);
816 stream->writeInt(uniform.registerElement);
817 }
818
819 stream->writeInt(mUniformIndex.size());
Geoff Lang95137842015-06-02 15:38:43 -0400820 for (const auto &uniform : mUniformIndex)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700821 {
Geoff Lang95137842015-06-02 15:38:43 -0400822 GLuint location = uniform.first;
823 stream->writeInt(location);
824
825 const gl::VariableLocation &variable = uniform.second;
826 stream->writeString(variable.name);
827 stream->writeInt(variable.element);
828 stream->writeInt(variable.index);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700829 }
830
831 stream->writeInt(mUniformBlocks.size());
832 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
833 {
834 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
835
836 stream->writeString(uniformBlock.name);
837 stream->writeInt(uniformBlock.elementIndex);
838 stream->writeInt(uniformBlock.dataSize);
839
840 stream->writeInt(uniformBlock.memberUniformIndexes.size());
841 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
842 {
843 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
844 }
845
846 stream->writeInt(uniformBlock.psRegisterIndex);
847 stream->writeInt(uniformBlock.vsRegisterIndex);
848 }
849
Brandon Joneseb994362014-09-24 10:27:28 -0700850 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
851 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
852 {
853 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
854
855 stream->writeString(varying.name);
856 stream->writeInt(varying.type);
857 stream->writeInt(varying.size);
858 stream->writeString(varying.semanticName);
859 stream->writeInt(varying.semanticIndex);
860 stream->writeInt(varying.semanticIndexCount);
861 }
862
Brandon Jones22502d52014-08-29 16:58:36 -0700863 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530864 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700865 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530866 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700867 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700868 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700869
Brandon Joneseb994362014-09-24 10:27:28 -0700870 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700871 stream->writeInt(pixelShaderKey.size());
872 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
873 {
Brandon Joneseb994362014-09-24 10:27:28 -0700874 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700875 stream->writeInt(variable.type);
876 stream->writeString(variable.name);
877 stream->writeString(variable.source);
878 stream->writeInt(variable.outputIndex);
879 }
880
Brandon Joneseb994362014-09-24 10:27:28 -0700881 stream->writeInt(mVertexExecutables.size());
882 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
883 {
884 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
885
Jamie Madilld3dfda22015-07-06 08:28:49 -0400886 const auto &inputLayout = vertexExecutable->inputs();
887 stream->writeInt(inputLayout.size());
888
889 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700890 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400891 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -0700892 }
893
894 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
895 stream->writeInt(vertexShaderSize);
896
897 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
898 stream->writeBytes(vertexBlob, vertexShaderSize);
899 }
900
901 stream->writeInt(mPixelExecutables.size());
902 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
903 {
904 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
905
906 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
907 stream->writeInt(outputs.size());
908 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
909 {
910 stream->writeInt(outputs[outputIndex]);
911 }
912
913 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
914 stream->writeInt(pixelShaderSize);
915
916 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
917 stream->writeBytes(pixelBlob, pixelShaderSize);
918 }
919
920 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
921 stream->writeInt(geometryShaderSize);
922
923 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
924 {
925 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
926 stream->writeBytes(geometryBlob, geometryShaderSize);
927 }
928
Geoff Langb543aff2014-09-30 14:52:54 -0400929 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700930}
931
Geoff Lang359ef262015-01-05 14:42:29 -0500932gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700933{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400934 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700935
Jamie Madill85a18042015-03-05 15:41:41 -0500936 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
937 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700938
939 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
940 {
941 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
942
943 if (colorbuffer)
944 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400945 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700946 }
947 else
948 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400949 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700950 }
951 }
952
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400953 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700954}
955
Jamie Madill97399232014-12-23 12:31:15 -0500956gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500957 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500958 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700959{
960 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
961 {
962 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
963 {
Geoff Langb543aff2014-09-30 14:52:54 -0400964 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
965 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700966 }
967 }
968
Brandon Jones22502d52014-08-29 16:58:36 -0700969 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
970 outputSignature);
971
972 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500973 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500974
975 gl::InfoLog tempInfoLog;
976 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
977
Jamie Madillada9ecc2015-08-17 12:53:37 -0400978 gl::Error error = mRenderer->compileToExecutable(
979 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mTransformFeedbackLinkedVaryings,
980 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
981 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400982 if (error.isError())
983 {
984 return error;
985 }
Brandon Joneseb994362014-09-24 10:27:28 -0700986
Jamie Madill97399232014-12-23 12:31:15 -0500987 if (pixelExecutable)
988 {
989 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
990 }
991 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700992 {
993 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700994 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -0700995 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
996 }
Brandon Jones22502d52014-08-29 16:58:36 -0700997
Geoff Langb543aff2014-09-30 14:52:54 -0400998 *outExectuable = pixelExecutable;
999 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001000}
1001
Jamie Madilld3dfda22015-07-06 08:28:49 -04001002gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001003 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001004 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001005{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001006 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001007
1008 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1009 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001010 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001011 {
Geoff Langb543aff2014-09-30 14:52:54 -04001012 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1013 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001014 }
1015 }
1016
Brandon Jones22502d52014-08-29 16:58:36 -07001017 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001018 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1019 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001020
1021 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001022 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001023
1024 gl::InfoLog tempInfoLog;
1025 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1026
Jamie Madillada9ecc2015-08-17 12:53:37 -04001027 gl::Error error = mRenderer->compileToExecutable(
1028 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mTransformFeedbackLinkedVaryings,
1029 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1030 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001031 if (error.isError())
1032 {
1033 return error;
1034 }
1035
Jamie Madill97399232014-12-23 12:31:15 -05001036 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001037 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001038 mVertexExecutables.push_back(new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001039 }
Jamie Madill97399232014-12-23 12:31:15 -05001040 else if (!infoLog)
1041 {
1042 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001043 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001044 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1045 }
Brandon Jones22502d52014-08-29 16:58:36 -07001046
Geoff Langb543aff2014-09-30 14:52:54 -04001047 *outExectuable = vertexExecutable;
1048 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001049}
1050
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001051LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, int registers)
Brandon Jones44151a92014-09-10 11:32:25 -07001052{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001053 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1054 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Brandon Jones44151a92014-09-10 11:32:25 -07001055
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001056 const gl::InputLayout &defaultInputLayout =
1057 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001058 ShaderExecutableD3D *defaultVertexExecutable = NULL;
1059 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
1060 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001061 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001062 return LinkResult(false, error);
1063 }
Austin Kinross434953e2015-02-20 10:49:51 -08001064
Brandon Joneseb994362014-09-24 10:27:28 -07001065 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001066 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +00001067 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001068 if (error.isError())
1069 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001070 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001071 }
Brandon Jones44151a92014-09-10 11:32:25 -07001072
Brandon Joneseb994362014-09-24 10:27:28 -07001073 if (usesGeometryShader())
1074 {
1075 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001076
Jamie Madillada9ecc2015-08-17 12:53:37 -04001077 error = mRenderer->compileToExecutable(
1078 infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1079 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
1080 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001081 if (error.isError())
1082 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001083 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001084 }
Brandon Joneseb994362014-09-24 10:27:28 -07001085 }
1086
Brandon Jones091540d2014-10-29 11:32:04 -07001087#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001088 if (usesGeometryShader() && mGeometryExecutable)
1089 {
1090 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1091 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1092 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1093 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1094 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1095 }
1096
1097 if (defaultVertexExecutable)
1098 {
1099 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1100 }
1101
1102 if (defaultPixelExecutable)
1103 {
1104 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1105 }
1106#endif
1107
Geoff Langb543aff2014-09-30 14:52:54 -04001108 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001109 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001110}
1111
Jamie Madillccdf74b2015-08-18 10:46:12 -04001112LinkResult ProgramD3D::link(const gl::Data &data,
1113 gl::InfoLog &infoLog,
1114 gl::Shader *fragmentShader,
1115 gl::Shader *vertexShader,
Geoff Lang7dd2e102014-11-10 15:19:26 -05001116 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001117{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001118 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1119 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001120
Jamie Madillde8892b2014-11-11 13:00:22 -05001121 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1122 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001123
Brandon Jones22502d52014-08-29 16:58:36 -07001124 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301125 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001126
1127 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301128 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001129 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001130
Austin Kinross02df7962015-07-01 10:03:42 -07001131 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1132 {
1133 if (fragmentShaderD3D->usesFrontFacing())
1134 {
1135 infoLog << "The current renderer doesn't support gl_FrontFacing";
1136 return LinkResult(false, gl::Error(GL_NO_ERROR));
1137 }
1138 }
1139
Brandon Jones22502d52014-08-29 16:58:36 -07001140 // Map the varyings to the register file
Daniel Chengf33ab832015-06-30 19:08:16 -07001141 VaryingPacking packing = {};
Jamie Madill31c8c562015-08-19 14:08:03 -04001142 int registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D,
1143 mData.getTransformFeedbackVaryingNames());
Brandon Jones22502d52014-08-29 16:58:36 -07001144
Jamie Madill31c8c562015-08-19 14:08:03 -04001145 if (registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001146 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001148 }
1149
Jamie Madillada9ecc2015-08-17 12:53:37 -04001150 LinkVaryingRegisters(infoLog, vertexShaderD3D, fragmentShaderD3D);
Brandon Jones22502d52014-08-29 16:58:36 -07001151
Jamie Madillccdf74b2015-08-18 10:46:12 -04001152 std::vector<gl::LinkedVarying> linkedVaryings;
1153 if (!mDynamicHLSL->generateShaderLinkHLSL(
Jamie Madill31c8c562015-08-19 14:08:03 -04001154 data, infoLog, registers, packing, mPixelHLSL, mVertexHLSL, fragmentShaderD3D,
Jamie Madillccdf74b2015-08-18 10:46:12 -04001155 vertexShaderD3D, mData.getTransformFeedbackVaryingNames(), &linkedVaryings,
1156 outputVariables, &mPixelShaderKey, &mUsesFragDepth))
Brandon Jones22502d52014-08-29 16:58:36 -07001157 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001158 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001159 }
1160
Brandon Jones44151a92014-09-10 11:32:25 -07001161 mUsesPointSize = vertexShaderD3D->usesPointSize();
1162
Jamie Madill63805b42015-08-25 13:17:39 -04001163 initSemanticIndex();
Jamie Madill437d2662014-12-05 14:23:35 -05001164
Jamie Madillea918db2015-08-18 14:48:59 -04001165 if (!defineUniforms(infoLog, *data.caps))
1166 {
1167 return LinkResult(false, gl::Error(GL_NO_ERROR));
1168 }
1169
Jamie Madille473dee2015-08-18 14:49:01 -04001170 defineUniformBlocks(*data.caps);
1171
Jamie Madillccdf74b2015-08-18 10:46:12 -04001172 gatherTransformFeedbackVaryings(linkedVaryings);
1173
Jamie Madill31c8c562015-08-19 14:08:03 -04001174 LinkResult result = compileProgramExecutables(infoLog, registers);
1175 if (result.error.isError() || !result.linkSuccess)
1176 {
1177 infoLog << "Failed to create D3D shaders.";
1178 return result;
1179 }
1180
Geoff Lang7dd2e102014-11-10 15:19:26 -05001181 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001182}
1183
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001184GLboolean ProgramD3D::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
1185{
1186 applyUniforms();
1187 return validateSamplers(infoLog, caps);
1188}
1189
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001190void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001191{
1192 // Compute total default block size
1193 unsigned int vertexRegisters = 0;
1194 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001195 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001196 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001197 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001198
Geoff Lang2ec386b2014-12-03 14:44:38 -05001199 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001200 {
1201 if (uniform.isReferencedByVertexShader())
1202 {
1203 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1204 }
1205 if (uniform.isReferencedByFragmentShader())
1206 {
1207 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1208 }
1209 }
1210 }
1211
1212 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1213 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1214}
1215
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001216gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001217{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001218 updateSamplerMapping();
1219
1220 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1221 if (error.isError())
1222 {
1223 return error;
1224 }
1225
1226 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1227 {
1228 mUniforms[uniformIndex]->dirty = false;
1229 }
1230
1231 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001232}
1233
Jamie Madilld1fe1642015-08-21 16:26:04 -04001234gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001235{
Jamie Madill03260fa2015-06-22 13:57:22 -04001236 mVertexUBOCache.clear();
1237 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001238
1239 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1240 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1241
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001242 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001243 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001244 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Jamie Madilld1fe1642015-08-21 16:26:04 -04001245 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001246
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001247 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001248
1249 // Unnecessary to apply an unreferenced standard or shared UBO
1250 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1251 {
1252 continue;
1253 }
1254
1255 if (uniformBlock->isReferencedByVertexShader())
1256 {
1257 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001258 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001259
Jamie Madill969194d2015-07-20 14:36:56 -04001260 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001261 {
1262 mVertexUBOCache.resize(registerIndex + 1, -1);
1263 }
1264
1265 ASSERT(mVertexUBOCache[registerIndex] == -1);
1266 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001267 }
1268
1269 if (uniformBlock->isReferencedByFragmentShader())
1270 {
1271 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001272 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001273
1274 if (mFragmentUBOCache.size() <= registerIndex)
1275 {
1276 mFragmentUBOCache.resize(registerIndex + 1, -1);
1277 }
1278
1279 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1280 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001281 }
1282 }
1283
Jamie Madill03260fa2015-06-22 13:57:22 -04001284 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001285}
1286
Jamie Madille473dee2015-08-18 14:49:01 -04001287void ProgramD3D::assignUniformBlockRegister(gl::UniformBlock *uniformBlock,
1288 GLenum shader,
1289 unsigned int registerIndex,
1290 const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001291{
Jamie Madille473dee2015-08-18 14:49:01 -04001292 // Validation done in the GL-level Program.
Brandon Jones18bd4102014-09-22 14:21:44 -07001293 if (shader == GL_VERTEX_SHADER)
1294 {
1295 uniformBlock->vsRegisterIndex = registerIndex;
Jamie Madille473dee2015-08-18 14:49:01 -04001296 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
Brandon Jones18bd4102014-09-22 14:21:44 -07001297 }
1298 else if (shader == GL_FRAGMENT_SHADER)
1299 {
1300 uniformBlock->psRegisterIndex = registerIndex;
Jamie Madille473dee2015-08-18 14:49:01 -04001301 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
Brandon Jones18bd4102014-09-22 14:21:44 -07001302 }
1303 else UNREACHABLE();
Brandon Jones18bd4102014-09-22 14:21:44 -07001304}
1305
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001306void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001307{
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001308 unsigned int numUniforms = static_cast<unsigned int>(mUniforms.size());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001309 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001310 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001311 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001312 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001313}
1314
1315void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1316{
1317 setUniform(location, count, v, GL_FLOAT);
1318}
1319
1320void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1321{
1322 setUniform(location, count, v, GL_FLOAT_VEC2);
1323}
1324
1325void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1326{
1327 setUniform(location, count, v, GL_FLOAT_VEC3);
1328}
1329
1330void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1331{
1332 setUniform(location, count, v, GL_FLOAT_VEC4);
1333}
1334
1335void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1336{
1337 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1338}
1339
1340void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1341{
1342 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1343}
1344
1345void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1346{
1347 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1348}
1349
1350void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1351{
1352 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1353}
1354
1355void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1356{
1357 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1358}
1359
1360void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1361{
1362 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1363}
1364
1365void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1366{
1367 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1368}
1369
1370void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1371{
1372 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1373}
1374
1375void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1376{
1377 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1378}
1379
1380void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1381{
1382 setUniform(location, count, v, GL_INT);
1383}
1384
1385void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1386{
1387 setUniform(location, count, v, GL_INT_VEC2);
1388}
1389
1390void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1391{
1392 setUniform(location, count, v, GL_INT_VEC3);
1393}
1394
1395void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1396{
1397 setUniform(location, count, v, GL_INT_VEC4);
1398}
1399
1400void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1401{
1402 setUniform(location, count, v, GL_UNSIGNED_INT);
1403}
1404
1405void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1406{
1407 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1408}
1409
1410void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1411{
1412 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1413}
1414
1415void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1416{
1417 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1418}
1419
1420void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1421{
1422 getUniformv(location, params, GL_FLOAT);
1423}
1424
1425void ProgramD3D::getUniformiv(GLint location, GLint *params)
1426{
1427 getUniformv(location, params, GL_INT);
1428}
1429
1430void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1431{
1432 getUniformv(location, params, GL_UNSIGNED_INT);
1433}
1434
Jamie Madillea918db2015-08-18 14:48:59 -04001435bool ProgramD3D::defineUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001436{
Jamie Madillea918db2015-08-18 14:48:59 -04001437 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1438 const std::vector<sh::Uniform> &vertexUniforms = vertexShader->getUniforms();
1439 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001440
Jamie Madillea918db2015-08-18 14:48:59 -04001441 for (const sh::Uniform &uniform : vertexUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001442 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001443 if (uniform.staticUse)
1444 {
Jamie Madill55def582015-05-04 11:24:57 -04001445 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1446 vertexShaderD3D->getUniformRegister(uniform.name);
1447 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001448 }
1449 }
1450
Jamie Madillea918db2015-08-18 14:48:59 -04001451 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1452 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader->getUniforms();
1453 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001454
Jamie Madillea918db2015-08-18 14:48:59 -04001455 for (const sh::Uniform &uniform : fragmentUniforms)
1456 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001457 if (uniform.staticUse)
1458 {
Jamie Madill55def582015-05-04 11:24:57 -04001459 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1460 fragmentShaderD3D->getUniformRegister(uniform.name);
1461 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001462 }
1463 }
1464
Jamie Madillea918db2015-08-18 14:48:59 -04001465 // TODO(jmadill): move the validation part to gl::Program
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001466 if (!indexUniforms(infoLog, caps))
1467 {
1468 return false;
1469 }
1470
1471 initializeUniformStorage();
1472
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001473 return true;
1474}
1475
Geoff Lang492a7e42014-11-05 13:27:06 -05001476void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001477{
Jamie Madill55def582015-05-04 11:24:57 -04001478 if (uniformRegister == GL_INVALID_INDEX)
1479 {
1480 defineUniform(shader, uniform, uniform.name, nullptr);
1481 return;
1482 }
1483
Geoff Lang492a7e42014-11-05 13:27:06 -05001484 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001485 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1486 encoder.skipRegisters(uniformRegister);
1487
1488 defineUniform(shader, uniform, uniform.name, &encoder);
1489}
1490
Geoff Lang492a7e42014-11-05 13:27:06 -05001491void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1492 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001493{
1494 if (uniform.isStruct())
1495 {
1496 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1497 {
1498 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1499
Jamie Madill55def582015-05-04 11:24:57 -04001500 if (encoder)
1501 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001502
1503 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1504 {
1505 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1506 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1507
1508 defineUniform(shader, field, fieldFullName, encoder);
1509 }
1510
Jamie Madill55def582015-05-04 11:24:57 -04001511 if (encoder)
1512 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001513 }
1514 }
1515 else // Not a struct
1516 {
1517 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001518 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001519 {
1520 encoder->enterAggregateType();
1521 }
1522
1523 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1524
Jamie Madill2857f482015-02-09 15:35:29 -05001525 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001526 sh::BlockMemberInfo blockInfo = encoder ?
1527 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1528 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001529
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001530 if (!linkedUniform)
1531 {
1532 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001533 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001534 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001535
1536 if (encoder)
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001537 linkedUniform->registerElement = static_cast<unsigned int>(
1538 sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001539 mUniforms.push_back(linkedUniform);
1540 }
1541
Jamie Madill55def582015-05-04 11:24:57 -04001542 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001543 {
Jamie Madill55def582015-05-04 11:24:57 -04001544 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1545 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001546 linkedUniform->psRegisterIndex =
1547 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill55def582015-05-04 11:24:57 -04001548 }
1549 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1550 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001551 linkedUniform->vsRegisterIndex =
1552 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill55def582015-05-04 11:24:57 -04001553 }
1554 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001555 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001556
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001557 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001558 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001559 {
1560 encoder->exitAggregateType();
1561 }
1562 }
1563}
1564
Jamie Madille473dee2015-08-18 14:49:01 -04001565void ProgramD3D::defineUniformBlocks(const gl::Caps &caps)
1566{
1567 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1568
1569 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1570 {
1571 if (vertexBlock.staticUse || vertexBlock.layout != sh::BLOCKLAYOUT_PACKED)
1572 {
1573 defineUniformBlock(*vertexShader, vertexBlock, caps);
1574 }
1575 }
1576
1577 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1578
1579 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1580 {
1581 if (fragmentBlock.staticUse || fragmentBlock.layout != sh::BLOCKLAYOUT_PACKED)
1582 {
1583 defineUniformBlock(*fragmentShader, fragmentBlock, caps);
1584 }
1585 }
1586}
1587
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001588template <typename T>
1589static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1590{
1591 ASSERT(dest != NULL);
1592 ASSERT(dirtyFlag != NULL);
1593
1594 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1595 *dest = source;
1596}
1597
1598template <typename T>
1599void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1600{
1601 const int components = gl::VariableComponentCount(targetUniformType);
1602 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1603
1604 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1605
1606 int elementCount = targetUniform->elementCount();
1607
1608 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1609
1610 if (targetUniform->type == targetUniformType)
1611 {
1612 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1613
1614 for (int i = 0; i < count; i++)
1615 {
1616 T *dest = target + (i * 4);
1617 const T *source = v + (i * components);
1618
1619 for (int c = 0; c < components; c++)
1620 {
1621 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1622 }
1623 for (int c = components; c < 4; c++)
1624 {
1625 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1626 }
1627 }
1628 }
1629 else if (targetUniform->type == targetBoolType)
1630 {
1631 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1632
1633 for (int i = 0; i < count; i++)
1634 {
1635 GLint *dest = boolParams + (i * 4);
1636 const T *source = v + (i * components);
1637
1638 for (int c = 0; c < components; c++)
1639 {
1640 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1641 }
1642 for (int c = components; c < 4; c++)
1643 {
1644 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1645 }
1646 }
1647 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001648 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001649 {
1650 ASSERT(targetUniformType == GL_INT);
1651
1652 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1653
1654 bool wasDirty = targetUniform->dirty;
1655
1656 for (int i = 0; i < count; i++)
1657 {
1658 GLint *dest = target + (i * 4);
1659 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1660
1661 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1662 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1663 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1664 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1665 }
1666
1667 if (!wasDirty && targetUniform->dirty)
1668 {
1669 mDirtySamplerMapping = true;
1670 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001671 }
1672 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001673}
Brandon Jones18bd4102014-09-22 14:21:44 -07001674
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001675template<typename T>
1676bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1677{
1678 bool dirty = false;
1679 int copyWidth = std::min(targetHeight, srcWidth);
1680 int copyHeight = std::min(targetWidth, srcHeight);
1681
1682 for (int x = 0; x < copyWidth; x++)
1683 {
1684 for (int y = 0; y < copyHeight; y++)
1685 {
1686 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1687 }
1688 }
1689 // clear unfilled right side
1690 for (int y = 0; y < copyWidth; y++)
1691 {
1692 for (int x = copyHeight; x < targetWidth; x++)
1693 {
1694 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1695 }
1696 }
1697 // clear unfilled bottom.
1698 for (int y = copyWidth; y < targetHeight; y++)
1699 {
1700 for (int x = 0; x < targetWidth; x++)
1701 {
1702 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1703 }
1704 }
1705
1706 return dirty;
1707}
1708
1709template<typename T>
1710bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1711{
1712 bool dirty = false;
1713 int copyWidth = std::min(targetWidth, srcWidth);
1714 int copyHeight = std::min(targetHeight, srcHeight);
1715
1716 for (int y = 0; y < copyHeight; y++)
1717 {
1718 for (int x = 0; x < copyWidth; x++)
1719 {
1720 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1721 }
1722 }
1723 // clear unfilled right side
1724 for (int y = 0; y < copyHeight; y++)
1725 {
1726 for (int x = copyWidth; x < targetWidth; x++)
1727 {
1728 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1729 }
1730 }
1731 // clear unfilled bottom.
1732 for (int y = copyHeight; y < targetHeight; y++)
1733 {
1734 for (int x = 0; x < targetWidth; x++)
1735 {
1736 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1737 }
1738 }
1739
1740 return dirty;
1741}
1742
1743template <int cols, int rows>
1744void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1745{
1746 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1747
1748 int elementCount = targetUniform->elementCount();
1749
1750 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1751 const unsigned int targetMatrixStride = (4 * rows);
1752 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1753
1754 for (int i = 0; i < count; i++)
1755 {
1756 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1757 if (transpose == GL_FALSE)
1758 {
1759 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1760 }
1761 else
1762 {
1763 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1764 }
1765 target += targetMatrixStride;
1766 value += cols * rows;
1767 }
1768}
1769
1770template <typename T>
1771void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1772{
1773 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1774
1775 if (gl::IsMatrixType(targetUniform->type))
1776 {
1777 const int rows = gl::VariableRowCount(targetUniform->type);
1778 const int cols = gl::VariableColumnCount(targetUniform->type);
1779 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1780 }
1781 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1782 {
1783 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1784 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1785 size * sizeof(T));
1786 }
1787 else
1788 {
1789 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1790 switch (gl::VariableComponentType(targetUniform->type))
1791 {
1792 case GL_BOOL:
1793 {
1794 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1795
1796 for (unsigned int i = 0; i < size; i++)
1797 {
1798 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1799 }
1800 }
1801 break;
1802
1803 case GL_FLOAT:
1804 {
1805 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1806
1807 for (unsigned int i = 0; i < size; i++)
1808 {
1809 params[i] = static_cast<T>(floatParams[i]);
1810 }
1811 }
1812 break;
1813
1814 case GL_INT:
1815 {
1816 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1817
1818 for (unsigned int i = 0; i < size; i++)
1819 {
1820 params[i] = static_cast<T>(intParams[i]);
1821 }
1822 }
1823 break;
1824
1825 case GL_UNSIGNED_INT:
1826 {
1827 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1828
1829 for (unsigned int i = 0; i < size; i++)
1830 {
1831 params[i] = static_cast<T>(uintParams[i]);
1832 }
1833 }
1834 break;
1835
1836 default: UNREACHABLE();
1837 }
1838 }
1839}
1840
1841template <typename VarT>
1842void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1843 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1844 bool inRowMajorLayout)
1845{
1846 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1847 {
1848 const VarT &field = fields[uniformIndex];
1849 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1850
1851 if (field.isStruct())
1852 {
1853 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1854
1855 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1856 {
1857 encoder->enterAggregateType();
1858
1859 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1860 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1861
1862 encoder->exitAggregateType();
1863 }
1864 }
1865 else
1866 {
1867 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1868
1869 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1870
1871 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1872 blockIndex, memberInfo);
1873
1874 // add to uniform list, but not index, since uniform block uniforms have no location
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001875 blockUniformIndexes->push_back(static_cast<GLenum>(mUniforms.size()));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001876 mUniforms.push_back(newUniform);
1877 }
1878 }
1879}
1880
Jamie Madille473dee2015-08-18 14:49:01 -04001881void ProgramD3D::defineUniformBlock(const gl::Shader &shader,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001882 const sh::InterfaceBlock &interfaceBlock,
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001883 const gl::Caps &caps)
1884{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001885 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001886
1887 // create uniform block entries if they do not exist
1888 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1889 {
1890 std::vector<unsigned int> blockUniformIndexes;
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001891 const unsigned int blockIndex = static_cast<unsigned int>(mUniformBlocks.size());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001892
1893 // define member uniforms
1894 sh::BlockLayoutEncoder *encoder = NULL;
1895
1896 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1897 {
1898 encoder = new sh::Std140BlockEncoder;
1899 }
1900 else
1901 {
1902 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1903 }
1904 ASSERT(encoder);
1905
1906 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1907
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001908 unsigned int dataSize = static_cast<unsigned int>(encoder->getBlockSize());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001909
1910 // create all the uniform blocks
1911 if (interfaceBlock.arraySize > 0)
1912 {
1913 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1914 {
1915 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1916 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1917 mUniformBlocks.push_back(newUniformBlock);
1918 }
1919 }
1920 else
1921 {
1922 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1923 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1924 mUniformBlocks.push_back(newUniformBlock);
1925 }
1926 }
1927
1928 if (interfaceBlock.staticUse)
1929 {
1930 // Assign registers to the uniform blocks
1931 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1932 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1933 ASSERT(blockIndex != GL_INVALID_INDEX);
1934 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1935
1936 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1937
1938 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1939 {
1940 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1941 ASSERT(uniformBlock->name == interfaceBlock.name);
1942
Jamie Madille473dee2015-08-18 14:49:01 -04001943 assignUniformBlockRegister(uniformBlock, shader.getType(),
1944 interfaceBlockRegister + uniformBlockElement, caps);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945 }
1946 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001947}
1948
1949bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001950 GLenum samplerType,
1951 unsigned int samplerCount,
1952 std::vector<Sampler> &outSamplers,
1953 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001954{
1955 unsigned int samplerIndex = startSamplerIndex;
1956
1957 do
1958 {
1959 if (samplerIndex < outSamplers.size())
1960 {
1961 Sampler& sampler = outSamplers[samplerIndex];
1962 sampler.active = true;
1963 sampler.textureType = GetTextureType(samplerType);
1964 sampler.logicalTextureUnit = 0;
1965 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1966 }
1967 else
1968 {
1969 return false;
1970 }
1971
1972 samplerIndex++;
1973 } while (samplerIndex < startSamplerIndex + samplerCount);
1974
1975 return true;
1976}
1977
1978bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1979{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001980 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001981 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1982
1983 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1984 {
1985 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1986 &mUsedVertexSamplerRange))
1987 {
Jamie Madillf6113162015-05-07 11:49:21 -04001988 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1989 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990 return false;
1991 }
1992
1993 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1994 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1995 {
Jamie Madillf6113162015-05-07 11:49:21 -04001996 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1997 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001998 return false;
1999 }
2000 }
2001
2002 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
2003 {
2004 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
2005 &mUsedPixelSamplerRange))
2006 {
Jamie Madillf6113162015-05-07 11:49:21 -04002007 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2008 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002009 return false;
2010 }
2011
2012 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
2013 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
2014 {
Jamie Madillf6113162015-05-07 11:49:21 -04002015 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
2016 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017 return false;
2018 }
2019 }
2020
2021 return true;
2022}
2023
2024bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
2025{
2026 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
2027 {
2028 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
2029
Geoff Lang2ec386b2014-12-03 14:44:38 -05002030 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031 {
2032 if (!indexSamplerUniform(uniform, infoLog, caps))
2033 {
2034 return false;
2035 }
2036 }
2037
Jamie Madill55def582015-05-04 11:24:57 -04002038 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002039 {
Jamie Madill55def582015-05-04 11:24:57 -04002040 if (!uniform.isBuiltIn())
2041 {
Geoff Lang95137842015-06-02 15:38:43 -04002042 // Assign in-order uniform locations
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002043 mUniformIndex[static_cast<GLuint>(mUniformIndex.size())] = gl::VariableLocation(
2044 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex));
Jamie Madill55def582015-05-04 11:24:57 -04002045 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002046 }
2047 }
2048
2049 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07002050}
2051
Brandon Jonesc9610c52014-08-25 17:02:59 -07002052void ProgramD3D::reset()
2053{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002054 ProgramImpl::reset();
2055
Brandon Joneseb994362014-09-24 10:27:28 -07002056 SafeDeleteContainer(mVertexExecutables);
2057 SafeDeleteContainer(mPixelExecutables);
2058 SafeDelete(mGeometryExecutable);
2059
Brandon Jones22502d52014-08-29 16:58:36 -07002060 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002061 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07002062 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07002063
2064 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002065 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002066 mUsesFragDepth = false;
2067 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002068 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002069
Brandon Jonesc9610c52014-08-25 17:02:59 -07002070 SafeDelete(mVertexUniformStorage);
2071 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002072
2073 mSamplersPS.clear();
2074 mSamplersVS.clear();
2075
2076 mUsedVertexSamplerRange = 0;
2077 mUsedPixelSamplerRange = 0;
2078 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002079
Jamie Madill63805b42015-08-25 13:17:39 -04002080 std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1);
Jamie Madill437d2662014-12-05 14:23:35 -05002081 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002082
2083 mTransformFeedbackLinkedVaryings.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002084}
2085
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086unsigned int ProgramD3D::getSerial() const
2087{
2088 return mSerial;
2089}
2090
2091unsigned int ProgramD3D::issueSerial()
2092{
2093 return mCurrentSerial++;
2094}
2095
Jamie Madill63805b42015-08-25 13:17:39 -04002096void ProgramD3D::initSemanticIndex()
2097{
2098 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2099 ASSERT(vertexShader != nullptr);
2100
2101 // Init semantic index
2102 for (const sh::Attribute &attribute : mData.getAttributes())
2103 {
2104 int attributeIndex = attribute.location;
2105 int index = vertexShader->getSemanticIndex(attribute.name);
2106 int regs = gl::VariableRegisterCount(attribute.type);
2107
2108 for (int reg = 0; reg < regs; ++reg)
2109 {
2110 mSemanticIndexes[attributeIndex + reg] = index + reg;
2111 }
2112 }
2113
2114 initAttributesByLayout();
2115}
2116
Jamie Madill437d2662014-12-05 14:23:35 -05002117void ProgramD3D::initAttributesByLayout()
2118{
2119 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2120 {
2121 mAttributesByLayout[i] = i;
2122 }
2123
Jamie Madill63805b42015-08-25 13:17:39 -04002124 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS],
2125 AttributeSorter(mSemanticIndexes));
Jamie Madill437d2662014-12-05 14:23:35 -05002126}
2127
Jamie Madill476682e2015-06-30 10:04:29 -04002128void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002129 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2130 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002131{
Jamie Madill476682e2015-06-30 10:04:29 -04002132 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002133 {
Jamie Madill476682e2015-06-30 10:04:29 -04002134 int oldIndex = mAttributesByLayout[attribIndex];
Jamie Madill63805b42015-08-25 13:17:39 -04002135 sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex];
Jamie Madill476682e2015-06-30 10:04:29 -04002136 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002137 }
2138}
2139
Jamie Madill63805b42015-08-25 13:17:39 -04002140void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002141{
Jamie Madillbd136f92015-08-10 14:51:37 -04002142 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002143 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002144
Jamie Madilld3dfda22015-07-06 08:28:49 -04002145 for (unsigned int attributeIndex = 0; attributeIndex < vertexAttributes.size(); attributeIndex++)
2146 {
Jamie Madill63805b42015-08-25 13:17:39 -04002147 int semanticIndex = mSemanticIndexes[attributeIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002148
2149 if (semanticIndex != -1)
2150 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002151 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2152 {
2153 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2154 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002155 mCachedInputLayout[semanticIndex] =
2156 GetVertexFormatType(vertexAttributes[attributeIndex],
2157 state.getVertexAttribCurrentValue(attributeIndex).Type);
2158 }
2159 }
2160}
2161
Jamie Madillccdf74b2015-08-18 10:46:12 -04002162void ProgramD3D::gatherTransformFeedbackVaryings(
2163 const std::vector<gl::LinkedVarying> &linkedVaryings)
2164{
2165 // Gather the linked varyings that are used for transform feedback, they should all exist.
2166 mTransformFeedbackLinkedVaryings.clear();
2167 for (const std::string &tfVaryingName : mData.getTransformFeedbackVaryingNames())
2168 {
2169 for (const gl::LinkedVarying &linkedVarying : linkedVaryings)
2170 {
2171 if (tfVaryingName == linkedVarying.name)
2172 {
2173 mTransformFeedbackLinkedVaryings.push_back(linkedVarying);
2174 break;
2175 }
2176 }
2177 }
2178}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002179}