blob: e4f7d5419b8806ce1bb8dac9747473eb00dac5a6 [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{
112 AttributeSorter(const ProgramImpl::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 Madill80d934b2015-02-19 10:16:12 -0500127 const ProgramImpl::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
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700516 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
517 for (unsigned int i = 0; i < psSamplerCount; ++i)
518 {
519 Sampler sampler;
520 stream->readBool(&sampler.active);
521 stream->readInt(&sampler.logicalTextureUnit);
522 stream->readInt(&sampler.textureType);
523 mSamplersPS.push_back(sampler);
524 }
525 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
526 for (unsigned int i = 0; i < vsSamplerCount; ++i)
527 {
528 Sampler sampler;
529 stream->readBool(&sampler.active);
530 stream->readInt(&sampler.logicalTextureUnit);
531 stream->readInt(&sampler.textureType);
532 mSamplersVS.push_back(sampler);
533 }
534
535 stream->readInt(&mUsedVertexSamplerRange);
536 stream->readInt(&mUsedPixelSamplerRange);
537
538 const unsigned int uniformCount = stream->readInt<unsigned int>();
539 if (stream->error())
540 {
Jamie Madillf6113162015-05-07 11:49:21 -0400541 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500542 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700543 }
544
545 mUniforms.resize(uniformCount);
546 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
547 {
548 GLenum type = stream->readInt<GLenum>();
549 GLenum precision = stream->readInt<GLenum>();
550 std::string name = stream->readString();
551 unsigned int arraySize = stream->readInt<unsigned int>();
552 int blockIndex = stream->readInt<int>();
553
554 int offset = stream->readInt<int>();
555 int arrayStride = stream->readInt<int>();
556 int matrixStride = stream->readInt<int>();
557 bool isRowMajorMatrix = stream->readBool();
558
559 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
560
561 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
562
563 stream->readInt(&uniform->psRegisterIndex);
564 stream->readInt(&uniform->vsRegisterIndex);
565 stream->readInt(&uniform->registerCount);
566 stream->readInt(&uniform->registerElement);
567
568 mUniforms[uniformIndex] = uniform;
569 }
570
571 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
572 if (stream->error())
573 {
Jamie Madillf6113162015-05-07 11:49:21 -0400574 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500575 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700576 }
577
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700578 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
579 {
Geoff Lang95137842015-06-02 15:38:43 -0400580 GLuint location;
581 stream->readInt(&location);
582
583 gl::VariableLocation variable;
584 stream->readString(&variable.name);
585 stream->readInt(&variable.element);
586 stream->readInt(&variable.index);
587
588 mUniformIndex[location] = variable;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700589 }
590
591 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
592 if (stream->error())
593 {
Jamie Madillf6113162015-05-07 11:49:21 -0400594 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500595 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700596 }
597
598 mUniformBlocks.resize(uniformBlockCount);
599 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
600 {
601 std::string name = stream->readString();
602 unsigned int elementIndex = stream->readInt<unsigned int>();
603 unsigned int dataSize = stream->readInt<unsigned int>();
604
605 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
606
607 stream->readInt(&uniformBlock->psRegisterIndex);
608 stream->readInt(&uniformBlock->vsRegisterIndex);
609
610 unsigned int numMembers = stream->readInt<unsigned int>();
611 uniformBlock->memberUniformIndexes.resize(numMembers);
612 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
613 {
614 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
615 }
616
617 mUniformBlocks[uniformBlockIndex] = uniformBlock;
618 }
619
Brandon Joneseb994362014-09-24 10:27:28 -0700620 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
621 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
622 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
623 {
624 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
625
626 stream->readString(&varying.name);
627 stream->readInt(&varying.type);
628 stream->readInt(&varying.size);
629 stream->readString(&varying.semanticName);
630 stream->readInt(&varying.semanticIndex);
631 stream->readInt(&varying.semanticIndexCount);
632 }
633
Brandon Jones22502d52014-08-29 16:58:36 -0700634 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530635 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700636 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530637 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700638 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700639 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700640
641 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
642 mPixelShaderKey.resize(pixelShaderKeySize);
643 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
644 {
645 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
646 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
647 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
648 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
649 }
650
Brandon Joneseb994362014-09-24 10:27:28 -0700651 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
652
653 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
654 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
655 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400656 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400657 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700658
Jamie Madilld3dfda22015-07-06 08:28:49 -0400659 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700660 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400661 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700662 }
663
664 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
665 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400666
Jamie Madillada9ecc2015-08-17 12:53:37 -0400667 ShaderExecutableD3D *shaderExecutable = nullptr;
668
669 gl::Error error = mRenderer->loadExecutable(
670 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mTransformFeedbackLinkedVaryings,
671 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400672 if (error.isError())
673 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500674 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400675 }
676
Brandon Joneseb994362014-09-24 10:27:28 -0700677 if (!shaderExecutable)
678 {
Jamie Madillf6113162015-05-07 11:49:21 -0400679 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500680 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700681 }
682
683 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400684 VertexExecutable::Signature signature;
685 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700686
687 // add new binary
688 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
689
690 stream->skip(vertexShaderSize);
691 }
692
693 const size_t pixelShaderCount = stream->readInt<unsigned int>();
694 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
695 {
696 const size_t outputCount = stream->readInt<unsigned int>();
697 std::vector<GLenum> outputs(outputCount);
698 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
699 {
700 stream->readInt(&outputs[outputIndex]);
701 }
702
703 const size_t pixelShaderSize = stream->readInt<unsigned int>();
704 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400705 ShaderExecutableD3D *shaderExecutable = nullptr;
706
707 gl::Error error = mRenderer->loadExecutable(
708 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mTransformFeedbackLinkedVaryings,
709 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400710 if (error.isError())
711 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500712 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400713 }
Brandon Joneseb994362014-09-24 10:27:28 -0700714
715 if (!shaderExecutable)
716 {
Jamie Madillf6113162015-05-07 11:49:21 -0400717 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500718 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700719 }
720
721 // add new binary
722 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
723
724 stream->skip(pixelShaderSize);
725 }
726
727 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
728
729 if (geometryShaderSize > 0)
730 {
731 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400732 gl::Error error = mRenderer->loadExecutable(
733 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
734 mTransformFeedbackLinkedVaryings,
735 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400736 if (error.isError())
737 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500738 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400739 }
Brandon Joneseb994362014-09-24 10:27:28 -0700740
741 if (!mGeometryExecutable)
742 {
Jamie Madillf6113162015-05-07 11:49:21 -0400743 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500744 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700745 }
746 stream->skip(geometryShaderSize);
747 }
748
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700749 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500750 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700751
Geoff Lang7dd2e102014-11-10 15:19:26 -0500752 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700753}
754
Geoff Langb543aff2014-09-30 14:52:54 -0400755gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700756{
Austin Kinross137b1512015-06-17 16:14:53 -0700757 // Output the DeviceIdentifier before we output any shader code
758 // When we load the binary again later, we can validate the device identifier before trying to compile any HLSL
759 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
760 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(DeviceIdentifier));
761
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500762 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
763
Brandon Jones44151a92014-09-10 11:32:25 -0700764 stream->writeInt(mShaderVersion);
765
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700766 stream->writeInt(mSamplersPS.size());
767 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
768 {
769 stream->writeInt(mSamplersPS[i].active);
770 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
771 stream->writeInt(mSamplersPS[i].textureType);
772 }
773
774 stream->writeInt(mSamplersVS.size());
775 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
776 {
777 stream->writeInt(mSamplersVS[i].active);
778 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
779 stream->writeInt(mSamplersVS[i].textureType);
780 }
781
782 stream->writeInt(mUsedVertexSamplerRange);
783 stream->writeInt(mUsedPixelSamplerRange);
784
785 stream->writeInt(mUniforms.size());
786 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
787 {
788 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
789
790 stream->writeInt(uniform.type);
791 stream->writeInt(uniform.precision);
792 stream->writeString(uniform.name);
793 stream->writeInt(uniform.arraySize);
794 stream->writeInt(uniform.blockIndex);
795
796 stream->writeInt(uniform.blockInfo.offset);
797 stream->writeInt(uniform.blockInfo.arrayStride);
798 stream->writeInt(uniform.blockInfo.matrixStride);
799 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
800
801 stream->writeInt(uniform.psRegisterIndex);
802 stream->writeInt(uniform.vsRegisterIndex);
803 stream->writeInt(uniform.registerCount);
804 stream->writeInt(uniform.registerElement);
805 }
806
807 stream->writeInt(mUniformIndex.size());
Geoff Lang95137842015-06-02 15:38:43 -0400808 for (const auto &uniform : mUniformIndex)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700809 {
Geoff Lang95137842015-06-02 15:38:43 -0400810 GLuint location = uniform.first;
811 stream->writeInt(location);
812
813 const gl::VariableLocation &variable = uniform.second;
814 stream->writeString(variable.name);
815 stream->writeInt(variable.element);
816 stream->writeInt(variable.index);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700817 }
818
819 stream->writeInt(mUniformBlocks.size());
820 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
821 {
822 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
823
824 stream->writeString(uniformBlock.name);
825 stream->writeInt(uniformBlock.elementIndex);
826 stream->writeInt(uniformBlock.dataSize);
827
828 stream->writeInt(uniformBlock.memberUniformIndexes.size());
829 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
830 {
831 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
832 }
833
834 stream->writeInt(uniformBlock.psRegisterIndex);
835 stream->writeInt(uniformBlock.vsRegisterIndex);
836 }
837
Brandon Joneseb994362014-09-24 10:27:28 -0700838 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
839 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
840 {
841 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
842
843 stream->writeString(varying.name);
844 stream->writeInt(varying.type);
845 stream->writeInt(varying.size);
846 stream->writeString(varying.semanticName);
847 stream->writeInt(varying.semanticIndex);
848 stream->writeInt(varying.semanticIndexCount);
849 }
850
Brandon Jones22502d52014-08-29 16:58:36 -0700851 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530852 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700853 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530854 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700855 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700856 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700857
Brandon Joneseb994362014-09-24 10:27:28 -0700858 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700859 stream->writeInt(pixelShaderKey.size());
860 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
861 {
Brandon Joneseb994362014-09-24 10:27:28 -0700862 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700863 stream->writeInt(variable.type);
864 stream->writeString(variable.name);
865 stream->writeString(variable.source);
866 stream->writeInt(variable.outputIndex);
867 }
868
Brandon Joneseb994362014-09-24 10:27:28 -0700869 stream->writeInt(mVertexExecutables.size());
870 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
871 {
872 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
873
Jamie Madilld3dfda22015-07-06 08:28:49 -0400874 const auto &inputLayout = vertexExecutable->inputs();
875 stream->writeInt(inputLayout.size());
876
877 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700878 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400879 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -0700880 }
881
882 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
883 stream->writeInt(vertexShaderSize);
884
885 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
886 stream->writeBytes(vertexBlob, vertexShaderSize);
887 }
888
889 stream->writeInt(mPixelExecutables.size());
890 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
891 {
892 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
893
894 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
895 stream->writeInt(outputs.size());
896 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
897 {
898 stream->writeInt(outputs[outputIndex]);
899 }
900
901 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
902 stream->writeInt(pixelShaderSize);
903
904 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
905 stream->writeBytes(pixelBlob, pixelShaderSize);
906 }
907
908 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
909 stream->writeInt(geometryShaderSize);
910
911 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
912 {
913 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
914 stream->writeBytes(geometryBlob, geometryShaderSize);
915 }
916
Geoff Langb543aff2014-09-30 14:52:54 -0400917 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700918}
919
Geoff Lang359ef262015-01-05 14:42:29 -0500920gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700921{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400922 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700923
Jamie Madill85a18042015-03-05 15:41:41 -0500924 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
925 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700926
927 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
928 {
929 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
930
931 if (colorbuffer)
932 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400933 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700934 }
935 else
936 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400937 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700938 }
939 }
940
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400941 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700942}
943
Jamie Madill97399232014-12-23 12:31:15 -0500944gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500945 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500946 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700947{
948 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
949 {
950 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
951 {
Geoff Langb543aff2014-09-30 14:52:54 -0400952 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
953 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700954 }
955 }
956
Brandon Jones22502d52014-08-29 16:58:36 -0700957 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
958 outputSignature);
959
960 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500961 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500962
963 gl::InfoLog tempInfoLog;
964 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
965
Jamie Madillada9ecc2015-08-17 12:53:37 -0400966 gl::Error error = mRenderer->compileToExecutable(
967 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mTransformFeedbackLinkedVaryings,
968 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
969 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400970 if (error.isError())
971 {
972 return error;
973 }
Brandon Joneseb994362014-09-24 10:27:28 -0700974
Jamie Madill97399232014-12-23 12:31:15 -0500975 if (pixelExecutable)
976 {
977 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
978 }
979 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700980 {
981 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700982 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -0700983 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
984 }
Brandon Jones22502d52014-08-29 16:58:36 -0700985
Geoff Langb543aff2014-09-30 14:52:54 -0400986 *outExectuable = pixelExecutable;
987 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700988}
989
Jamie Madilld3dfda22015-07-06 08:28:49 -0400990gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -0500991 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500992 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700993{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400994 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -0700995
996 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
997 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400998 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -0700999 {
Geoff Langb543aff2014-09-30 14:52:54 -04001000 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1001 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001002 }
1003 }
1004
Brandon Jones22502d52014-08-29 16:58:36 -07001005 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001006 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1007 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001008
1009 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001010 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001011
1012 gl::InfoLog tempInfoLog;
1013 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1014
Jamie Madillada9ecc2015-08-17 12:53:37 -04001015 gl::Error error = mRenderer->compileToExecutable(
1016 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mTransformFeedbackLinkedVaryings,
1017 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1018 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001019 if (error.isError())
1020 {
1021 return error;
1022 }
1023
Jamie Madill97399232014-12-23 12:31:15 -05001024 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001025 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001026 mVertexExecutables.push_back(new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001027 }
Jamie Madill97399232014-12-23 12:31:15 -05001028 else if (!infoLog)
1029 {
1030 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001031 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001032 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1033 }
Brandon Jones22502d52014-08-29 16:58:36 -07001034
Geoff Langb543aff2014-09-30 14:52:54 -04001035 *outExectuable = vertexExecutable;
1036 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001037}
1038
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001039LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, int registers)
Brandon Jones44151a92014-09-10 11:32:25 -07001040{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001041 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1042 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Brandon Jones44151a92014-09-10 11:32:25 -07001043
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001044 const gl::InputLayout &defaultInputLayout =
1045 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001046 ShaderExecutableD3D *defaultVertexExecutable = NULL;
1047 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
1048 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001049 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001050 return LinkResult(false, error);
1051 }
Austin Kinross434953e2015-02-20 10:49:51 -08001052
Brandon Joneseb994362014-09-24 10:27:28 -07001053 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001054 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +00001055 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001056 if (error.isError())
1057 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001058 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001059 }
Brandon Jones44151a92014-09-10 11:32:25 -07001060
Brandon Joneseb994362014-09-24 10:27:28 -07001061 if (usesGeometryShader())
1062 {
1063 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001064
Jamie Madillada9ecc2015-08-17 12:53:37 -04001065 error = mRenderer->compileToExecutable(
1066 infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1067 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
1068 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001069 if (error.isError())
1070 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001071 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001072 }
Brandon Joneseb994362014-09-24 10:27:28 -07001073 }
1074
Brandon Jones091540d2014-10-29 11:32:04 -07001075#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001076 if (usesGeometryShader() && mGeometryExecutable)
1077 {
1078 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1079 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1080 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1081 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1082 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1083 }
1084
1085 if (defaultVertexExecutable)
1086 {
1087 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1088 }
1089
1090 if (defaultPixelExecutable)
1091 {
1092 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1093 }
1094#endif
1095
Geoff Langb543aff2014-09-30 14:52:54 -04001096 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001098}
1099
Jamie Madillccdf74b2015-08-18 10:46:12 -04001100LinkResult ProgramD3D::link(const gl::Data &data,
1101 gl::InfoLog &infoLog,
1102 gl::Shader *fragmentShader,
1103 gl::Shader *vertexShader,
Geoff Lang7dd2e102014-11-10 15:19:26 -05001104 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001105{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001106 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1107 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001108
Jamie Madillde8892b2014-11-11 13:00:22 -05001109 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1110 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001111
Brandon Jones22502d52014-08-29 16:58:36 -07001112 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301113 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001114
1115 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301116 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001117 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001118
Austin Kinross02df7962015-07-01 10:03:42 -07001119 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1120 {
1121 if (fragmentShaderD3D->usesFrontFacing())
1122 {
1123 infoLog << "The current renderer doesn't support gl_FrontFacing";
1124 return LinkResult(false, gl::Error(GL_NO_ERROR));
1125 }
1126 }
1127
Brandon Jones22502d52014-08-29 16:58:36 -07001128 // Map the varyings to the register file
Daniel Chengf33ab832015-06-30 19:08:16 -07001129 VaryingPacking packing = {};
Jamie Madill31c8c562015-08-19 14:08:03 -04001130 int registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D,
1131 mData.getTransformFeedbackVaryingNames());
Brandon Jones22502d52014-08-29 16:58:36 -07001132
Jamie Madill31c8c562015-08-19 14:08:03 -04001133 if (registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001134 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001135 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001136 }
1137
Jamie Madillada9ecc2015-08-17 12:53:37 -04001138 LinkVaryingRegisters(infoLog, vertexShaderD3D, fragmentShaderD3D);
Brandon Jones22502d52014-08-29 16:58:36 -07001139
Jamie Madillccdf74b2015-08-18 10:46:12 -04001140 std::vector<gl::LinkedVarying> linkedVaryings;
1141 if (!mDynamicHLSL->generateShaderLinkHLSL(
Jamie Madill31c8c562015-08-19 14:08:03 -04001142 data, infoLog, registers, packing, mPixelHLSL, mVertexHLSL, fragmentShaderD3D,
Jamie Madillccdf74b2015-08-18 10:46:12 -04001143 vertexShaderD3D, mData.getTransformFeedbackVaryingNames(), &linkedVaryings,
1144 outputVariables, &mPixelShaderKey, &mUsesFragDepth))
Brandon Jones22502d52014-08-29 16:58:36 -07001145 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001146 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001147 }
1148
Brandon Jones44151a92014-09-10 11:32:25 -07001149 mUsesPointSize = vertexShaderD3D->usesPointSize();
1150
Jamie Madill437d2662014-12-05 14:23:35 -05001151 initAttributesByLayout();
1152
Jamie Madillea918db2015-08-18 14:48:59 -04001153 if (!defineUniforms(infoLog, *data.caps))
1154 {
1155 return LinkResult(false, gl::Error(GL_NO_ERROR));
1156 }
1157
Jamie Madille473dee2015-08-18 14:49:01 -04001158 defineUniformBlocks(*data.caps);
1159
Jamie Madillccdf74b2015-08-18 10:46:12 -04001160 gatherTransformFeedbackVaryings(linkedVaryings);
1161
Jamie Madill31c8c562015-08-19 14:08:03 -04001162 LinkResult result = compileProgramExecutables(infoLog, registers);
1163 if (result.error.isError() || !result.linkSuccess)
1164 {
1165 infoLog << "Failed to create D3D shaders.";
1166 return result;
1167 }
1168
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001170}
1171
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001172GLboolean ProgramD3D::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
1173{
1174 applyUniforms();
1175 return validateSamplers(infoLog, caps);
1176}
1177
Geoff Lang0ca53782015-05-07 13:49:39 -04001178void ProgramD3D::bindAttributeLocation(GLuint index, const std::string &name)
1179{
1180}
1181
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001182void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001183{
1184 // Compute total default block size
1185 unsigned int vertexRegisters = 0;
1186 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001187 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001188 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001189 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001190
Geoff Lang2ec386b2014-12-03 14:44:38 -05001191 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001192 {
1193 if (uniform.isReferencedByVertexShader())
1194 {
1195 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1196 }
1197 if (uniform.isReferencedByFragmentShader())
1198 {
1199 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1200 }
1201 }
1202 }
1203
1204 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1205 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1206}
1207
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001208gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001209{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001210 updateSamplerMapping();
1211
1212 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1213 if (error.isError())
1214 {
1215 return error;
1216 }
1217
1218 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1219 {
1220 mUniforms[uniformIndex]->dirty = false;
1221 }
1222
1223 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001224}
1225
Jamie Madilld1fe1642015-08-21 16:26:04 -04001226gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001227{
Jamie Madill03260fa2015-06-22 13:57:22 -04001228 mVertexUBOCache.clear();
1229 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001230
1231 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1232 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1233
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001234 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001235 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001236 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Jamie Madilld1fe1642015-08-21 16:26:04 -04001237 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001238
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001239 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001240
1241 // Unnecessary to apply an unreferenced standard or shared UBO
1242 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1243 {
1244 continue;
1245 }
1246
1247 if (uniformBlock->isReferencedByVertexShader())
1248 {
1249 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001250 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001251
Jamie Madill969194d2015-07-20 14:36:56 -04001252 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001253 {
1254 mVertexUBOCache.resize(registerIndex + 1, -1);
1255 }
1256
1257 ASSERT(mVertexUBOCache[registerIndex] == -1);
1258 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001259 }
1260
1261 if (uniformBlock->isReferencedByFragmentShader())
1262 {
1263 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001264 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001265
1266 if (mFragmentUBOCache.size() <= registerIndex)
1267 {
1268 mFragmentUBOCache.resize(registerIndex + 1, -1);
1269 }
1270
1271 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1272 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001273 }
1274 }
1275
Jamie Madill03260fa2015-06-22 13:57:22 -04001276 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001277}
1278
Jamie Madille473dee2015-08-18 14:49:01 -04001279void ProgramD3D::assignUniformBlockRegister(gl::UniformBlock *uniformBlock,
1280 GLenum shader,
1281 unsigned int registerIndex,
1282 const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001283{
Jamie Madille473dee2015-08-18 14:49:01 -04001284 // Validation done in the GL-level Program.
Brandon Jones18bd4102014-09-22 14:21:44 -07001285 if (shader == GL_VERTEX_SHADER)
1286 {
1287 uniformBlock->vsRegisterIndex = registerIndex;
Jamie Madille473dee2015-08-18 14:49:01 -04001288 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
Brandon Jones18bd4102014-09-22 14:21:44 -07001289 }
1290 else if (shader == GL_FRAGMENT_SHADER)
1291 {
1292 uniformBlock->psRegisterIndex = registerIndex;
Jamie Madille473dee2015-08-18 14:49:01 -04001293 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
Brandon Jones18bd4102014-09-22 14:21:44 -07001294 }
1295 else UNREACHABLE();
Brandon Jones18bd4102014-09-22 14:21:44 -07001296}
1297
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001298void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001299{
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001300 unsigned int numUniforms = static_cast<unsigned int>(mUniforms.size());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001301 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001302 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001303 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001304 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001305}
1306
1307void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1308{
1309 setUniform(location, count, v, GL_FLOAT);
1310}
1311
1312void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1313{
1314 setUniform(location, count, v, GL_FLOAT_VEC2);
1315}
1316
1317void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1318{
1319 setUniform(location, count, v, GL_FLOAT_VEC3);
1320}
1321
1322void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1323{
1324 setUniform(location, count, v, GL_FLOAT_VEC4);
1325}
1326
1327void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1328{
1329 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1330}
1331
1332void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1333{
1334 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1335}
1336
1337void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1338{
1339 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1340}
1341
1342void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1343{
1344 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1345}
1346
1347void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1348{
1349 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1350}
1351
1352void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1353{
1354 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1355}
1356
1357void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1358{
1359 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1360}
1361
1362void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1363{
1364 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1365}
1366
1367void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1368{
1369 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1370}
1371
1372void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1373{
1374 setUniform(location, count, v, GL_INT);
1375}
1376
1377void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1378{
1379 setUniform(location, count, v, GL_INT_VEC2);
1380}
1381
1382void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1383{
1384 setUniform(location, count, v, GL_INT_VEC3);
1385}
1386
1387void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1388{
1389 setUniform(location, count, v, GL_INT_VEC4);
1390}
1391
1392void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1393{
1394 setUniform(location, count, v, GL_UNSIGNED_INT);
1395}
1396
1397void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1398{
1399 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1400}
1401
1402void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1403{
1404 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1405}
1406
1407void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1408{
1409 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1410}
1411
1412void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1413{
1414 getUniformv(location, params, GL_FLOAT);
1415}
1416
1417void ProgramD3D::getUniformiv(GLint location, GLint *params)
1418{
1419 getUniformv(location, params, GL_INT);
1420}
1421
1422void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1423{
1424 getUniformv(location, params, GL_UNSIGNED_INT);
1425}
1426
Jamie Madillea918db2015-08-18 14:48:59 -04001427bool ProgramD3D::defineUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001428{
Jamie Madillea918db2015-08-18 14:48:59 -04001429 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1430 const std::vector<sh::Uniform> &vertexUniforms = vertexShader->getUniforms();
1431 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001432
Jamie Madillea918db2015-08-18 14:48:59 -04001433 for (const sh::Uniform &uniform : vertexUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001434 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001435 if (uniform.staticUse)
1436 {
Jamie Madill55def582015-05-04 11:24:57 -04001437 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1438 vertexShaderD3D->getUniformRegister(uniform.name);
1439 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001440 }
1441 }
1442
Jamie Madillea918db2015-08-18 14:48:59 -04001443 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1444 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader->getUniforms();
1445 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001446
Jamie Madillea918db2015-08-18 14:48:59 -04001447 for (const sh::Uniform &uniform : fragmentUniforms)
1448 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001449 if (uniform.staticUse)
1450 {
Jamie Madill55def582015-05-04 11:24:57 -04001451 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1452 fragmentShaderD3D->getUniformRegister(uniform.name);
1453 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001454 }
1455 }
1456
Jamie Madillea918db2015-08-18 14:48:59 -04001457 // TODO(jmadill): move the validation part to gl::Program
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001458 if (!indexUniforms(infoLog, caps))
1459 {
1460 return false;
1461 }
1462
1463 initializeUniformStorage();
1464
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001465 return true;
1466}
1467
Geoff Lang492a7e42014-11-05 13:27:06 -05001468void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001469{
Jamie Madill55def582015-05-04 11:24:57 -04001470 if (uniformRegister == GL_INVALID_INDEX)
1471 {
1472 defineUniform(shader, uniform, uniform.name, nullptr);
1473 return;
1474 }
1475
Geoff Lang492a7e42014-11-05 13:27:06 -05001476 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001477 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1478 encoder.skipRegisters(uniformRegister);
1479
1480 defineUniform(shader, uniform, uniform.name, &encoder);
1481}
1482
Geoff Lang492a7e42014-11-05 13:27:06 -05001483void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1484 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001485{
1486 if (uniform.isStruct())
1487 {
1488 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1489 {
1490 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1491
Jamie Madill55def582015-05-04 11:24:57 -04001492 if (encoder)
1493 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001494
1495 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1496 {
1497 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1498 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1499
1500 defineUniform(shader, field, fieldFullName, encoder);
1501 }
1502
Jamie Madill55def582015-05-04 11:24:57 -04001503 if (encoder)
1504 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001505 }
1506 }
1507 else // Not a struct
1508 {
1509 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001510 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001511 {
1512 encoder->enterAggregateType();
1513 }
1514
1515 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1516
Jamie Madill2857f482015-02-09 15:35:29 -05001517 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001518 sh::BlockMemberInfo blockInfo = encoder ?
1519 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1520 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001521
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001522 if (!linkedUniform)
1523 {
1524 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001525 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001526 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001527
1528 if (encoder)
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001529 linkedUniform->registerElement = static_cast<unsigned int>(
1530 sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001531 mUniforms.push_back(linkedUniform);
1532 }
1533
Jamie Madill55def582015-05-04 11:24:57 -04001534 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001535 {
Jamie Madill55def582015-05-04 11:24:57 -04001536 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1537 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001538 linkedUniform->psRegisterIndex =
1539 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill55def582015-05-04 11:24:57 -04001540 }
1541 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1542 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001543 linkedUniform->vsRegisterIndex =
1544 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill55def582015-05-04 11:24:57 -04001545 }
1546 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001547 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001548
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001549 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001550 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001551 {
1552 encoder->exitAggregateType();
1553 }
1554 }
1555}
1556
Jamie Madille473dee2015-08-18 14:49:01 -04001557void ProgramD3D::defineUniformBlocks(const gl::Caps &caps)
1558{
1559 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1560
1561 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1562 {
1563 if (vertexBlock.staticUse || vertexBlock.layout != sh::BLOCKLAYOUT_PACKED)
1564 {
1565 defineUniformBlock(*vertexShader, vertexBlock, caps);
1566 }
1567 }
1568
1569 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1570
1571 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1572 {
1573 if (fragmentBlock.staticUse || fragmentBlock.layout != sh::BLOCKLAYOUT_PACKED)
1574 {
1575 defineUniformBlock(*fragmentShader, fragmentBlock, caps);
1576 }
1577 }
1578}
1579
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001580template <typename T>
1581static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1582{
1583 ASSERT(dest != NULL);
1584 ASSERT(dirtyFlag != NULL);
1585
1586 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1587 *dest = source;
1588}
1589
1590template <typename T>
1591void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1592{
1593 const int components = gl::VariableComponentCount(targetUniformType);
1594 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1595
1596 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1597
1598 int elementCount = targetUniform->elementCount();
1599
1600 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1601
1602 if (targetUniform->type == targetUniformType)
1603 {
1604 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1605
1606 for (int i = 0; i < count; i++)
1607 {
1608 T *dest = target + (i * 4);
1609 const T *source = v + (i * components);
1610
1611 for (int c = 0; c < components; c++)
1612 {
1613 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1614 }
1615 for (int c = components; c < 4; c++)
1616 {
1617 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1618 }
1619 }
1620 }
1621 else if (targetUniform->type == targetBoolType)
1622 {
1623 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1624
1625 for (int i = 0; i < count; i++)
1626 {
1627 GLint *dest = boolParams + (i * 4);
1628 const T *source = v + (i * components);
1629
1630 for (int c = 0; c < components; c++)
1631 {
1632 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1633 }
1634 for (int c = components; c < 4; c++)
1635 {
1636 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1637 }
1638 }
1639 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001640 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001641 {
1642 ASSERT(targetUniformType == GL_INT);
1643
1644 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1645
1646 bool wasDirty = targetUniform->dirty;
1647
1648 for (int i = 0; i < count; i++)
1649 {
1650 GLint *dest = target + (i * 4);
1651 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1652
1653 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1654 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1655 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1656 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1657 }
1658
1659 if (!wasDirty && targetUniform->dirty)
1660 {
1661 mDirtySamplerMapping = true;
1662 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001663 }
1664 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001665}
Brandon Jones18bd4102014-09-22 14:21:44 -07001666
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001667template<typename T>
1668bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1669{
1670 bool dirty = false;
1671 int copyWidth = std::min(targetHeight, srcWidth);
1672 int copyHeight = std::min(targetWidth, srcHeight);
1673
1674 for (int x = 0; x < copyWidth; x++)
1675 {
1676 for (int y = 0; y < copyHeight; y++)
1677 {
1678 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1679 }
1680 }
1681 // clear unfilled right side
1682 for (int y = 0; y < copyWidth; y++)
1683 {
1684 for (int x = copyHeight; x < targetWidth; x++)
1685 {
1686 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1687 }
1688 }
1689 // clear unfilled bottom.
1690 for (int y = copyWidth; y < targetHeight; y++)
1691 {
1692 for (int x = 0; x < targetWidth; x++)
1693 {
1694 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1695 }
1696 }
1697
1698 return dirty;
1699}
1700
1701template<typename T>
1702bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1703{
1704 bool dirty = false;
1705 int copyWidth = std::min(targetWidth, srcWidth);
1706 int copyHeight = std::min(targetHeight, srcHeight);
1707
1708 for (int y = 0; y < copyHeight; y++)
1709 {
1710 for (int x = 0; x < copyWidth; x++)
1711 {
1712 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1713 }
1714 }
1715 // clear unfilled right side
1716 for (int y = 0; y < copyHeight; y++)
1717 {
1718 for (int x = copyWidth; x < targetWidth; x++)
1719 {
1720 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1721 }
1722 }
1723 // clear unfilled bottom.
1724 for (int y = copyHeight; y < targetHeight; y++)
1725 {
1726 for (int x = 0; x < targetWidth; x++)
1727 {
1728 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1729 }
1730 }
1731
1732 return dirty;
1733}
1734
1735template <int cols, int rows>
1736void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1737{
1738 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1739
1740 int elementCount = targetUniform->elementCount();
1741
1742 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1743 const unsigned int targetMatrixStride = (4 * rows);
1744 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1745
1746 for (int i = 0; i < count; i++)
1747 {
1748 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1749 if (transpose == GL_FALSE)
1750 {
1751 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1752 }
1753 else
1754 {
1755 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1756 }
1757 target += targetMatrixStride;
1758 value += cols * rows;
1759 }
1760}
1761
1762template <typename T>
1763void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1764{
1765 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1766
1767 if (gl::IsMatrixType(targetUniform->type))
1768 {
1769 const int rows = gl::VariableRowCount(targetUniform->type);
1770 const int cols = gl::VariableColumnCount(targetUniform->type);
1771 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1772 }
1773 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1774 {
1775 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1776 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1777 size * sizeof(T));
1778 }
1779 else
1780 {
1781 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1782 switch (gl::VariableComponentType(targetUniform->type))
1783 {
1784 case GL_BOOL:
1785 {
1786 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1787
1788 for (unsigned int i = 0; i < size; i++)
1789 {
1790 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1791 }
1792 }
1793 break;
1794
1795 case GL_FLOAT:
1796 {
1797 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1798
1799 for (unsigned int i = 0; i < size; i++)
1800 {
1801 params[i] = static_cast<T>(floatParams[i]);
1802 }
1803 }
1804 break;
1805
1806 case GL_INT:
1807 {
1808 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1809
1810 for (unsigned int i = 0; i < size; i++)
1811 {
1812 params[i] = static_cast<T>(intParams[i]);
1813 }
1814 }
1815 break;
1816
1817 case GL_UNSIGNED_INT:
1818 {
1819 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1820
1821 for (unsigned int i = 0; i < size; i++)
1822 {
1823 params[i] = static_cast<T>(uintParams[i]);
1824 }
1825 }
1826 break;
1827
1828 default: UNREACHABLE();
1829 }
1830 }
1831}
1832
1833template <typename VarT>
1834void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1835 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1836 bool inRowMajorLayout)
1837{
1838 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1839 {
1840 const VarT &field = fields[uniformIndex];
1841 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1842
1843 if (field.isStruct())
1844 {
1845 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1846
1847 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1848 {
1849 encoder->enterAggregateType();
1850
1851 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1852 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1853
1854 encoder->exitAggregateType();
1855 }
1856 }
1857 else
1858 {
1859 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1860
1861 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1862
1863 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1864 blockIndex, memberInfo);
1865
1866 // add to uniform list, but not index, since uniform block uniforms have no location
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001867 blockUniformIndexes->push_back(static_cast<GLenum>(mUniforms.size()));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001868 mUniforms.push_back(newUniform);
1869 }
1870 }
1871}
1872
Jamie Madille473dee2015-08-18 14:49:01 -04001873void ProgramD3D::defineUniformBlock(const gl::Shader &shader,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001874 const sh::InterfaceBlock &interfaceBlock,
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001875 const gl::Caps &caps)
1876{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001877 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001878
1879 // create uniform block entries if they do not exist
1880 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1881 {
1882 std::vector<unsigned int> blockUniformIndexes;
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001883 const unsigned int blockIndex = static_cast<unsigned int>(mUniformBlocks.size());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001884
1885 // define member uniforms
1886 sh::BlockLayoutEncoder *encoder = NULL;
1887
1888 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1889 {
1890 encoder = new sh::Std140BlockEncoder;
1891 }
1892 else
1893 {
1894 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1895 }
1896 ASSERT(encoder);
1897
1898 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1899
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001900 unsigned int dataSize = static_cast<unsigned int>(encoder->getBlockSize());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001901
1902 // create all the uniform blocks
1903 if (interfaceBlock.arraySize > 0)
1904 {
1905 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1906 {
1907 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1908 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1909 mUniformBlocks.push_back(newUniformBlock);
1910 }
1911 }
1912 else
1913 {
1914 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1915 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1916 mUniformBlocks.push_back(newUniformBlock);
1917 }
1918 }
1919
1920 if (interfaceBlock.staticUse)
1921 {
1922 // Assign registers to the uniform blocks
1923 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1924 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1925 ASSERT(blockIndex != GL_INVALID_INDEX);
1926 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1927
1928 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1929
1930 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1931 {
1932 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1933 ASSERT(uniformBlock->name == interfaceBlock.name);
1934
Jamie Madille473dee2015-08-18 14:49:01 -04001935 assignUniformBlockRegister(uniformBlock, shader.getType(),
1936 interfaceBlockRegister + uniformBlockElement, caps);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001937 }
1938 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001939}
1940
1941bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001942 GLenum samplerType,
1943 unsigned int samplerCount,
1944 std::vector<Sampler> &outSamplers,
1945 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001946{
1947 unsigned int samplerIndex = startSamplerIndex;
1948
1949 do
1950 {
1951 if (samplerIndex < outSamplers.size())
1952 {
1953 Sampler& sampler = outSamplers[samplerIndex];
1954 sampler.active = true;
1955 sampler.textureType = GetTextureType(samplerType);
1956 sampler.logicalTextureUnit = 0;
1957 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1958 }
1959 else
1960 {
1961 return false;
1962 }
1963
1964 samplerIndex++;
1965 } while (samplerIndex < startSamplerIndex + samplerCount);
1966
1967 return true;
1968}
1969
1970bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1971{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001972 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001973 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1974
1975 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1976 {
1977 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1978 &mUsedVertexSamplerRange))
1979 {
Jamie Madillf6113162015-05-07 11:49:21 -04001980 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1981 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001982 return false;
1983 }
1984
1985 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1986 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1987 {
Jamie Madillf6113162015-05-07 11:49:21 -04001988 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1989 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990 return false;
1991 }
1992 }
1993
1994 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1995 {
1996 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1997 &mUsedPixelSamplerRange))
1998 {
Jamie Madillf6113162015-05-07 11:49:21 -04001999 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2000 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002001 return false;
2002 }
2003
2004 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
2005 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
2006 {
Jamie Madillf6113162015-05-07 11:49:21 -04002007 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
2008 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002009 return false;
2010 }
2011 }
2012
2013 return true;
2014}
2015
2016bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
2017{
2018 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
2019 {
2020 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
2021
Geoff Lang2ec386b2014-12-03 14:44:38 -05002022 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002023 {
2024 if (!indexSamplerUniform(uniform, infoLog, caps))
2025 {
2026 return false;
2027 }
2028 }
2029
Jamie Madill55def582015-05-04 11:24:57 -04002030 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031 {
Jamie Madill55def582015-05-04 11:24:57 -04002032 if (!uniform.isBuiltIn())
2033 {
Geoff Lang95137842015-06-02 15:38:43 -04002034 // Assign in-order uniform locations
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002035 mUniformIndex[static_cast<GLuint>(mUniformIndex.size())] = gl::VariableLocation(
2036 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex));
Jamie Madill55def582015-05-04 11:24:57 -04002037 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002038 }
2039 }
2040
2041 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07002042}
2043
Brandon Jonesc9610c52014-08-25 17:02:59 -07002044void ProgramD3D::reset()
2045{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002046 ProgramImpl::reset();
2047
Brandon Joneseb994362014-09-24 10:27:28 -07002048 SafeDeleteContainer(mVertexExecutables);
2049 SafeDeleteContainer(mPixelExecutables);
2050 SafeDelete(mGeometryExecutable);
2051
Brandon Jones22502d52014-08-29 16:58:36 -07002052 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002053 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07002054 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07002055
2056 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002057 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002058 mUsesFragDepth = false;
2059 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002060 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002061
Brandon Jonesc9610c52014-08-25 17:02:59 -07002062 SafeDelete(mVertexUniformStorage);
2063 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002064
2065 mSamplersPS.clear();
2066 mSamplersVS.clear();
2067
2068 mUsedVertexSamplerRange = 0;
2069 mUsedPixelSamplerRange = 0;
2070 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002071
2072 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002073
2074 mTransformFeedbackLinkedVaryings.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002075}
2076
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077unsigned int ProgramD3D::getSerial() const
2078{
2079 return mSerial;
2080}
2081
2082unsigned int ProgramD3D::issueSerial()
2083{
2084 return mCurrentSerial++;
2085}
2086
Jamie Madill437d2662014-12-05 14:23:35 -05002087void ProgramD3D::initAttributesByLayout()
2088{
2089 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2090 {
2091 mAttributesByLayout[i] = i;
2092 }
2093
2094 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2095}
2096
Jamie Madill476682e2015-06-30 10:04:29 -04002097void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002098 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2099 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002100{
Jamie Madill476682e2015-06-30 10:04:29 -04002101 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002102 {
Jamie Madill476682e2015-06-30 10:04:29 -04002103 int oldIndex = mAttributesByLayout[attribIndex];
2104 sortedSemanticIndicesOut[attribIndex] = mSemanticIndex[oldIndex];
2105 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002106 }
2107}
2108
Jamie Madilld3dfda22015-07-06 08:28:49 -04002109void ProgramD3D::updateCachedInputLayout(const gl::Program *program, const gl::State &state)
2110{
Jamie Madillbd136f92015-08-10 14:51:37 -04002111 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002112 const int *semanticIndexes = program->getSemanticIndexes();
2113
2114 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002115
Jamie Madilld3dfda22015-07-06 08:28:49 -04002116 for (unsigned int attributeIndex = 0; attributeIndex < vertexAttributes.size(); attributeIndex++)
2117 {
2118 int semanticIndex = semanticIndexes[attributeIndex];
2119
2120 if (semanticIndex != -1)
2121 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002122 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2123 {
2124 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2125 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002126 mCachedInputLayout[semanticIndex] =
2127 GetVertexFormatType(vertexAttributes[attributeIndex],
2128 state.getVertexAttribCurrentValue(attributeIndex).Type);
2129 }
2130 }
2131}
2132
Jamie Madillccdf74b2015-08-18 10:46:12 -04002133void ProgramD3D::gatherTransformFeedbackVaryings(
2134 const std::vector<gl::LinkedVarying> &linkedVaryings)
2135{
2136 // Gather the linked varyings that are used for transform feedback, they should all exist.
2137 mTransformFeedbackLinkedVaryings.clear();
2138 for (const std::string &tfVaryingName : mData.getTransformFeedbackVaryingNames())
2139 {
2140 for (const gl::LinkedVarying &linkedVarying : linkedVaryings)
2141 {
2142 if (tfVaryingName == linkedVarying.name)
2143 {
2144 mTransformFeedbackLinkedVaryings.push_back(linkedVarying);
2145 break;
2146 }
2147 }
2148 }
2149}
Brandon Jonesc9610c52014-08-25 17:02:59 -07002150}