blob: 35326340d2af91402de19af5d0ed25e07c4ddb55 [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 {
93 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
94 }
Brandon Joneseb994362014-09-24 10:27:28 -070095
96 return defaultPixelOutput;
97}
98
Brandon Jones1a8a7e32014-10-01 12:49:30 -070099bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
100{
101 return var.isRowMajorLayout;
102}
103
104bool IsRowMajorLayout(const sh::ShaderVariable &var)
105{
106 return false;
107}
108
Jamie Madill437d2662014-12-05 14:23:35 -0500109struct AttributeSorter
110{
111 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -0500112 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -0500113 {
114 }
115
116 bool operator()(int a, int b)
117 {
Jamie Madill80d934b2015-02-19 10:16:12 -0500118 int indexA = (*originalIndices)[a];
119 int indexB = (*originalIndices)[b];
120
121 if (indexA == -1) return false;
122 if (indexB == -1) return true;
123 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -0500124 }
125
Jamie Madill80d934b2015-02-19 10:16:12 -0500126 const ProgramImpl::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500127};
128
Brandon Joneseb994362014-09-24 10:27:28 -0700129}
130
Jamie Madilld3dfda22015-07-06 08:28:49 -0400131ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
132 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500133 ShaderExecutableD3D *shaderExecutable)
Jamie Madilld3dfda22015-07-06 08:28:49 -0400134 : mInputs(inputLayout),
135 mSignature(signature),
136 mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700137{
Brandon Joneseb994362014-09-24 10:27:28 -0700138}
139
140ProgramD3D::VertexExecutable::~VertexExecutable()
141{
142 SafeDelete(mShaderExecutable);
143}
144
Jamie Madilld3dfda22015-07-06 08:28:49 -0400145// static
146void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
147 const gl::InputLayout &inputLayout,
148 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700149{
Jamie Madillbd136f92015-08-10 14:51:37 -0400150 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400151
152 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700153 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400154 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400155 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400156 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700157 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400158 VertexConversionType conversionType =
159 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400160 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700161 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400162
163 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700164 }
Brandon Joneseb994362014-09-24 10:27:28 -0700165}
166
Jamie Madilld3dfda22015-07-06 08:28:49 -0400167bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
168{
Jamie Madillbd136f92015-08-10 14:51:37 -0400169 size_t limit = std::max(mSignature.size(), signature.size());
170 for (size_t index = 0; index < limit; ++index)
171 {
172 // treat undefined indexes as 'not converted'
173 bool a = index < signature.size() ? signature[index] : false;
174 bool b = index < mSignature.size() ? mSignature[index] : false;
175 if (a != b)
176 return false;
177 }
178
179 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400180}
181
182ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
183 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700184 : mOutputSignature(outputSignature),
185 mShaderExecutable(shaderExecutable)
186{
187}
188
189ProgramD3D::PixelExecutable::~PixelExecutable()
190{
191 SafeDelete(mShaderExecutable);
192}
193
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700194ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
195{
196}
197
Geoff Lang7dd2e102014-11-10 15:19:26 -0500198unsigned int ProgramD3D::mCurrentSerial = 1;
199
Jamie Madill93e13fb2014-11-06 15:27:25 -0500200ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700201 : ProgramImpl(),
202 mRenderer(renderer),
203 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700204 mGeometryExecutable(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700205 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700206 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700207 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700208 mUsedVertexSamplerRange(0),
209 mUsedPixelSamplerRange(0),
210 mDirtySamplerMapping(true),
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400211 mTextureUnitTypesCache(renderer->getRendererCaps().maxCombinedTextureImageUnits),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500212 mShaderVersion(100),
213 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700214{
Brandon Joneseb994362014-09-24 10:27:28 -0700215 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700216}
217
218ProgramD3D::~ProgramD3D()
219{
220 reset();
221 SafeDelete(mDynamicHLSL);
222}
223
Brandon Jones44151a92014-09-10 11:32:25 -0700224bool ProgramD3D::usesPointSpriteEmulation() const
225{
226 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
227}
228
229bool ProgramD3D::usesGeometryShader() const
230{
Cooper Partine6664f02015-01-09 16:22:24 -0800231 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
232}
233
234bool ProgramD3D::usesInstancedPointSpriteEmulation() const
235{
236 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700237}
238
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700239GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
240{
241 GLint logicalTextureUnit = -1;
242
243 switch (type)
244 {
245 case gl::SAMPLER_PIXEL:
246 ASSERT(samplerIndex < caps.maxTextureImageUnits);
247 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
248 {
249 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
250 }
251 break;
252 case gl::SAMPLER_VERTEX:
253 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
254 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
255 {
256 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
257 }
258 break;
259 default: UNREACHABLE();
260 }
261
262 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
263 {
264 return logicalTextureUnit;
265 }
266
267 return -1;
268}
269
270// Returns the texture type for a given Direct3D 9 sampler type and
271// index (0-15 for the pixel shader and 0-3 for the vertex shader).
272GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
273{
274 switch (type)
275 {
276 case gl::SAMPLER_PIXEL:
277 ASSERT(samplerIndex < mSamplersPS.size());
278 ASSERT(mSamplersPS[samplerIndex].active);
279 return mSamplersPS[samplerIndex].textureType;
280 case gl::SAMPLER_VERTEX:
281 ASSERT(samplerIndex < mSamplersVS.size());
282 ASSERT(mSamplersVS[samplerIndex].active);
283 return mSamplersVS[samplerIndex].textureType;
284 default: UNREACHABLE();
285 }
286
287 return GL_TEXTURE_2D;
288}
289
290GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
291{
292 switch (type)
293 {
294 case gl::SAMPLER_PIXEL:
295 return mUsedPixelSamplerRange;
296 case gl::SAMPLER_VERTEX:
297 return mUsedVertexSamplerRange;
298 default:
299 UNREACHABLE();
300 return 0;
301 }
302}
303
304void ProgramD3D::updateSamplerMapping()
305{
306 if (!mDirtySamplerMapping)
307 {
308 return;
309 }
310
311 mDirtySamplerMapping = false;
312
313 // Retrieve sampler uniform values
314 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
315 {
316 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
317
318 if (targetUniform->dirty)
319 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500320 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700321 {
322 int count = targetUniform->elementCount();
323 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
324
325 if (targetUniform->isReferencedByFragmentShader())
326 {
327 unsigned int firstIndex = targetUniform->psRegisterIndex;
328
329 for (int i = 0; i < count; i++)
330 {
331 unsigned int samplerIndex = firstIndex + i;
332
333 if (samplerIndex < mSamplersPS.size())
334 {
335 ASSERT(mSamplersPS[samplerIndex].active);
336 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
337 }
338 }
339 }
340
341 if (targetUniform->isReferencedByVertexShader())
342 {
343 unsigned int firstIndex = targetUniform->vsRegisterIndex;
344
345 for (int i = 0; i < count; i++)
346 {
347 unsigned int samplerIndex = firstIndex + i;
348
349 if (samplerIndex < mSamplersVS.size())
350 {
351 ASSERT(mSamplersVS[samplerIndex].active);
352 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
353 }
354 }
355 }
356 }
357 }
358 }
359}
360
361bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
362{
Jamie Madill13776892015-04-28 12:39:06 -0400363 // Skip cache if we're using an infolog, so we get the full error.
364 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
365 if (!mDirtySamplerMapping && infoLog == nullptr && mCachedValidateSamplersResult.valid())
366 {
367 return mCachedValidateSamplersResult.value();
368 }
369
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700370 // if any two active samplers in a program are of different types, but refer to the same
371 // texture image unit, and this is the current program, then ValidateProgram will fail, and
372 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
373 updateSamplerMapping();
374
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400375 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700376
377 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
378 {
379 if (mSamplersPS[i].active)
380 {
381 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
382
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400383 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700384 {
385 if (infoLog)
386 {
Jamie Madillf6113162015-05-07 11:49:21 -0400387 (*infoLog) << "Sampler uniform (" << unit
388 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
389 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700390 }
391
Jamie Madill13776892015-04-28 12:39:06 -0400392 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700393 return false;
394 }
395
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400396 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700397 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400398 if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700399 {
400 if (infoLog)
401 {
Jamie Madillf6113162015-05-07 11:49:21 -0400402 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
403 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700404 }
405
Jamie Madill13776892015-04-28 12:39:06 -0400406 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700407 return false;
408 }
409 }
410 else
411 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400412 mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700413 }
414 }
415 }
416
417 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
418 {
419 if (mSamplersVS[i].active)
420 {
421 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
422
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400423 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700424 {
425 if (infoLog)
426 {
Jamie Madillf6113162015-05-07 11:49:21 -0400427 (*infoLog) << "Sampler uniform (" << unit
428 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
429 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700430 }
431
Jamie Madill13776892015-04-28 12:39:06 -0400432 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700433 return false;
434 }
435
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400436 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700437 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400438 if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700439 {
440 if (infoLog)
441 {
Jamie Madillf6113162015-05-07 11:49:21 -0400442 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
443 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700444 }
445
Jamie Madill13776892015-04-28 12:39:06 -0400446 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700447 return false;
448 }
449 }
450 else
451 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400452 mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700453 }
454 }
455 }
456
Jamie Madill13776892015-04-28 12:39:06 -0400457 mCachedValidateSamplersResult = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700458 return true;
459}
460
Geoff Lang7dd2e102014-11-10 15:19:26 -0500461LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700462{
Austin Kinross137b1512015-06-17 16:14:53 -0700463 DeviceIdentifier binaryDeviceIdentifier = { 0 };
464 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryDeviceIdentifier), sizeof(DeviceIdentifier));
465
466 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
467 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
468 {
469 infoLog << "Invalid program binary, device configuration has changed.";
470 return LinkResult(false, gl::Error(GL_NO_ERROR));
471 }
472
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500473 int compileFlags = stream->readInt<int>();
474 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
475 {
Jamie Madillf6113162015-05-07 11:49:21 -0400476 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500477 return LinkResult(false, gl::Error(GL_NO_ERROR));
478 }
479
Brandon Jones44151a92014-09-10 11:32:25 -0700480 stream->readInt(&mShaderVersion);
481
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700482 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
483 for (unsigned int i = 0; i < psSamplerCount; ++i)
484 {
485 Sampler sampler;
486 stream->readBool(&sampler.active);
487 stream->readInt(&sampler.logicalTextureUnit);
488 stream->readInt(&sampler.textureType);
489 mSamplersPS.push_back(sampler);
490 }
491 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
492 for (unsigned int i = 0; i < vsSamplerCount; ++i)
493 {
494 Sampler sampler;
495 stream->readBool(&sampler.active);
496 stream->readInt(&sampler.logicalTextureUnit);
497 stream->readInt(&sampler.textureType);
498 mSamplersVS.push_back(sampler);
499 }
500
501 stream->readInt(&mUsedVertexSamplerRange);
502 stream->readInt(&mUsedPixelSamplerRange);
503
504 const unsigned int uniformCount = stream->readInt<unsigned int>();
505 if (stream->error())
506 {
Jamie Madillf6113162015-05-07 11:49:21 -0400507 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500508 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700509 }
510
511 mUniforms.resize(uniformCount);
512 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
513 {
514 GLenum type = stream->readInt<GLenum>();
515 GLenum precision = stream->readInt<GLenum>();
516 std::string name = stream->readString();
517 unsigned int arraySize = stream->readInt<unsigned int>();
518 int blockIndex = stream->readInt<int>();
519
520 int offset = stream->readInt<int>();
521 int arrayStride = stream->readInt<int>();
522 int matrixStride = stream->readInt<int>();
523 bool isRowMajorMatrix = stream->readBool();
524
525 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
526
527 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
528
529 stream->readInt(&uniform->psRegisterIndex);
530 stream->readInt(&uniform->vsRegisterIndex);
531 stream->readInt(&uniform->registerCount);
532 stream->readInt(&uniform->registerElement);
533
534 mUniforms[uniformIndex] = uniform;
535 }
536
537 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
538 if (stream->error())
539 {
Jamie Madillf6113162015-05-07 11:49:21 -0400540 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500541 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700542 }
543
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700544 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
545 {
Geoff Lang95137842015-06-02 15:38:43 -0400546 GLuint location;
547 stream->readInt(&location);
548
549 gl::VariableLocation variable;
550 stream->readString(&variable.name);
551 stream->readInt(&variable.element);
552 stream->readInt(&variable.index);
553
554 mUniformIndex[location] = variable;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700555 }
556
557 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
558 if (stream->error())
559 {
Jamie Madillf6113162015-05-07 11:49:21 -0400560 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500561 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700562 }
563
564 mUniformBlocks.resize(uniformBlockCount);
565 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
566 {
567 std::string name = stream->readString();
568 unsigned int elementIndex = stream->readInt<unsigned int>();
569 unsigned int dataSize = stream->readInt<unsigned int>();
570
571 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
572
573 stream->readInt(&uniformBlock->psRegisterIndex);
574 stream->readInt(&uniformBlock->vsRegisterIndex);
575
576 unsigned int numMembers = stream->readInt<unsigned int>();
577 uniformBlock->memberUniformIndexes.resize(numMembers);
578 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
579 {
580 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
581 }
582
583 mUniformBlocks[uniformBlockIndex] = uniformBlock;
584 }
585
Brandon Joneseb994362014-09-24 10:27:28 -0700586 stream->readInt(&mTransformFeedbackBufferMode);
587 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
588 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
589 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
590 {
591 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
592
593 stream->readString(&varying.name);
594 stream->readInt(&varying.type);
595 stream->readInt(&varying.size);
596 stream->readString(&varying.semanticName);
597 stream->readInt(&varying.semanticIndex);
598 stream->readInt(&varying.semanticIndexCount);
599 }
600
Brandon Jones22502d52014-08-29 16:58:36 -0700601 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530602 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700603 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530604 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700605 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700606 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700607
608 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
609 mPixelShaderKey.resize(pixelShaderKeySize);
610 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
611 {
612 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
613 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
614 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
615 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
616 }
617
Brandon Joneseb994362014-09-24 10:27:28 -0700618 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
619
620 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
621 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
622 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400623 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400624 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700625
Jamie Madilld3dfda22015-07-06 08:28:49 -0400626 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700627 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400628 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700629 }
630
631 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
632 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400633
Geoff Lang359ef262015-01-05 14:42:29 -0500634 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400635 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
636 SHADER_VERTEX,
637 mTransformFeedbackLinkedVaryings,
638 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
639 &shaderExecutable);
640 if (error.isError())
641 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500642 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400643 }
644
Brandon Joneseb994362014-09-24 10:27:28 -0700645 if (!shaderExecutable)
646 {
Jamie Madillf6113162015-05-07 11:49:21 -0400647 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500648 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700649 }
650
651 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400652 VertexExecutable::Signature signature;
653 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700654
655 // add new binary
656 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
657
658 stream->skip(vertexShaderSize);
659 }
660
661 const size_t pixelShaderCount = stream->readInt<unsigned int>();
662 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
663 {
664 const size_t outputCount = stream->readInt<unsigned int>();
665 std::vector<GLenum> outputs(outputCount);
666 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
667 {
668 stream->readInt(&outputs[outputIndex]);
669 }
670
671 const size_t pixelShaderSize = stream->readInt<unsigned int>();
672 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500673 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400674 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
675 mTransformFeedbackLinkedVaryings,
676 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
677 &shaderExecutable);
678 if (error.isError())
679 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500680 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400681 }
Brandon Joneseb994362014-09-24 10:27:28 -0700682
683 if (!shaderExecutable)
684 {
Jamie Madillf6113162015-05-07 11:49:21 -0400685 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500686 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700687 }
688
689 // add new binary
690 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
691
692 stream->skip(pixelShaderSize);
693 }
694
695 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
696
697 if (geometryShaderSize > 0)
698 {
699 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400700 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
701 mTransformFeedbackLinkedVaryings,
702 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
703 &mGeometryExecutable);
704 if (error.isError())
705 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500706 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400707 }
Brandon Joneseb994362014-09-24 10:27:28 -0700708
709 if (!mGeometryExecutable)
710 {
Jamie Madillf6113162015-05-07 11:49:21 -0400711 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500712 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700713 }
714 stream->skip(geometryShaderSize);
715 }
716
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700717 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500718 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700719
Geoff Lang7dd2e102014-11-10 15:19:26 -0500720 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700721}
722
Geoff Langb543aff2014-09-30 14:52:54 -0400723gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700724{
Austin Kinross137b1512015-06-17 16:14:53 -0700725 // Output the DeviceIdentifier before we output any shader code
726 // When we load the binary again later, we can validate the device identifier before trying to compile any HLSL
727 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
728 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(DeviceIdentifier));
729
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500730 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
731
Brandon Jones44151a92014-09-10 11:32:25 -0700732 stream->writeInt(mShaderVersion);
733
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700734 stream->writeInt(mSamplersPS.size());
735 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
736 {
737 stream->writeInt(mSamplersPS[i].active);
738 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
739 stream->writeInt(mSamplersPS[i].textureType);
740 }
741
742 stream->writeInt(mSamplersVS.size());
743 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
744 {
745 stream->writeInt(mSamplersVS[i].active);
746 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
747 stream->writeInt(mSamplersVS[i].textureType);
748 }
749
750 stream->writeInt(mUsedVertexSamplerRange);
751 stream->writeInt(mUsedPixelSamplerRange);
752
753 stream->writeInt(mUniforms.size());
754 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
755 {
756 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
757
758 stream->writeInt(uniform.type);
759 stream->writeInt(uniform.precision);
760 stream->writeString(uniform.name);
761 stream->writeInt(uniform.arraySize);
762 stream->writeInt(uniform.blockIndex);
763
764 stream->writeInt(uniform.blockInfo.offset);
765 stream->writeInt(uniform.blockInfo.arrayStride);
766 stream->writeInt(uniform.blockInfo.matrixStride);
767 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
768
769 stream->writeInt(uniform.psRegisterIndex);
770 stream->writeInt(uniform.vsRegisterIndex);
771 stream->writeInt(uniform.registerCount);
772 stream->writeInt(uniform.registerElement);
773 }
774
775 stream->writeInt(mUniformIndex.size());
Geoff Lang95137842015-06-02 15:38:43 -0400776 for (const auto &uniform : mUniformIndex)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700777 {
Geoff Lang95137842015-06-02 15:38:43 -0400778 GLuint location = uniform.first;
779 stream->writeInt(location);
780
781 const gl::VariableLocation &variable = uniform.second;
782 stream->writeString(variable.name);
783 stream->writeInt(variable.element);
784 stream->writeInt(variable.index);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700785 }
786
787 stream->writeInt(mUniformBlocks.size());
788 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
789 {
790 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
791
792 stream->writeString(uniformBlock.name);
793 stream->writeInt(uniformBlock.elementIndex);
794 stream->writeInt(uniformBlock.dataSize);
795
796 stream->writeInt(uniformBlock.memberUniformIndexes.size());
797 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
798 {
799 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
800 }
801
802 stream->writeInt(uniformBlock.psRegisterIndex);
803 stream->writeInt(uniformBlock.vsRegisterIndex);
804 }
805
Brandon Joneseb994362014-09-24 10:27:28 -0700806 stream->writeInt(mTransformFeedbackBufferMode);
807 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
808 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
809 {
810 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
811
812 stream->writeString(varying.name);
813 stream->writeInt(varying.type);
814 stream->writeInt(varying.size);
815 stream->writeString(varying.semanticName);
816 stream->writeInt(varying.semanticIndex);
817 stream->writeInt(varying.semanticIndexCount);
818 }
819
Brandon Jones22502d52014-08-29 16:58:36 -0700820 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530821 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700822 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530823 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700824 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700825 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700826
Brandon Joneseb994362014-09-24 10:27:28 -0700827 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700828 stream->writeInt(pixelShaderKey.size());
829 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
830 {
Brandon Joneseb994362014-09-24 10:27:28 -0700831 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700832 stream->writeInt(variable.type);
833 stream->writeString(variable.name);
834 stream->writeString(variable.source);
835 stream->writeInt(variable.outputIndex);
836 }
837
Brandon Joneseb994362014-09-24 10:27:28 -0700838 stream->writeInt(mVertexExecutables.size());
839 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
840 {
841 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
842
Jamie Madilld3dfda22015-07-06 08:28:49 -0400843 const auto &inputLayout = vertexExecutable->inputs();
844 stream->writeInt(inputLayout.size());
845
846 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700847 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400848 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -0700849 }
850
851 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
852 stream->writeInt(vertexShaderSize);
853
854 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
855 stream->writeBytes(vertexBlob, vertexShaderSize);
856 }
857
858 stream->writeInt(mPixelExecutables.size());
859 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
860 {
861 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
862
863 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
864 stream->writeInt(outputs.size());
865 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
866 {
867 stream->writeInt(outputs[outputIndex]);
868 }
869
870 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
871 stream->writeInt(pixelShaderSize);
872
873 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
874 stream->writeBytes(pixelBlob, pixelShaderSize);
875 }
876
877 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
878 stream->writeInt(geometryShaderSize);
879
880 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
881 {
882 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
883 stream->writeBytes(geometryBlob, geometryShaderSize);
884 }
885
Geoff Langb543aff2014-09-30 14:52:54 -0400886 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700887}
888
Geoff Lang359ef262015-01-05 14:42:29 -0500889gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700890{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400891 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700892
Jamie Madill85a18042015-03-05 15:41:41 -0500893 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
894 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700895
896 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
897 {
898 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
899
900 if (colorbuffer)
901 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400902 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700903 }
904 else
905 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400906 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700907 }
908 }
909
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400910 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700911}
912
Jamie Madill97399232014-12-23 12:31:15 -0500913gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500914 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500915 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700916{
917 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
918 {
919 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
920 {
Geoff Langb543aff2014-09-30 14:52:54 -0400921 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
922 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700923 }
924 }
925
Brandon Jones22502d52014-08-29 16:58:36 -0700926 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
927 outputSignature);
928
929 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500930 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500931
932 gl::InfoLog tempInfoLog;
933 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
934
935 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400936 mTransformFeedbackLinkedVaryings,
937 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
938 mPixelWorkarounds, &pixelExecutable);
939 if (error.isError())
940 {
941 return error;
942 }
Brandon Joneseb994362014-09-24 10:27:28 -0700943
Jamie Madill97399232014-12-23 12:31:15 -0500944 if (pixelExecutable)
945 {
946 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
947 }
948 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700949 {
950 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
951 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
952 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
953 }
Brandon Jones22502d52014-08-29 16:58:36 -0700954
Geoff Langb543aff2014-09-30 14:52:54 -0400955 *outExectuable = pixelExecutable;
956 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700957}
958
Jamie Madilld3dfda22015-07-06 08:28:49 -0400959gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -0500960 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500961 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700962{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400963 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -0700964
965 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
966 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400967 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -0700968 {
Geoff Langb543aff2014-09-30 14:52:54 -0400969 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
970 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700971 }
972 }
973
Brandon Jones22502d52014-08-29 16:58:36 -0700974 // Generate new dynamic layout with attribute conversions
Jamie Madill3da79b72015-04-27 11:09:17 -0400975 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, getShaderAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -0700976
977 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500978 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500979
980 gl::InfoLog tempInfoLog;
981 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
982
983 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400984 mTransformFeedbackLinkedVaryings,
985 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
986 mVertexWorkarounds, &vertexExecutable);
987 if (error.isError())
988 {
989 return error;
990 }
991
Jamie Madill97399232014-12-23 12:31:15 -0500992 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700993 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400994 mVertexExecutables.push_back(new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700995 }
Jamie Madill97399232014-12-23 12:31:15 -0500996 else if (!infoLog)
997 {
998 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
999 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
1000 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1001 }
Brandon Jones22502d52014-08-29 16:58:36 -07001002
Geoff Langb543aff2014-09-30 14:52:54 -04001003 *outExectuable = vertexExecutable;
1004 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001005}
1006
Geoff Lang7dd2e102014-11-10 15:19:26 -05001007LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
1008 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -07001009{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001010 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1011 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones44151a92014-09-10 11:32:25 -07001012
Jamie Madillf8dd7b12015-08-05 13:50:08 -04001013 const gl::InputLayout &defaultInputLayout = GetDefaultInputLayoutFromShader(vertexShader);
Jamie Madille4ea2022015-03-26 20:35:05 +00001014 ShaderExecutableD3D *defaultVertexExecutable = NULL;
1015 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
1016 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001017 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001018 return LinkResult(false, error);
1019 }
Austin Kinross434953e2015-02-20 10:49:51 -08001020
Brandon Joneseb994362014-09-24 10:27:28 -07001021 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001022 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +00001023 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001024 if (error.isError())
1025 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001026 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001027 }
Brandon Jones44151a92014-09-10 11:32:25 -07001028
Brandon Joneseb994362014-09-24 10:27:28 -07001029 if (usesGeometryShader())
1030 {
1031 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001032
Geoff Langb543aff2014-09-30 14:52:54 -04001033
1034 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1035 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +05301036 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001037 if (error.isError())
1038 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001039 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001040 }
Brandon Joneseb994362014-09-24 10:27:28 -07001041 }
1042
Brandon Jones091540d2014-10-29 11:32:04 -07001043#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001044 if (usesGeometryShader() && mGeometryExecutable)
1045 {
1046 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1047 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1048 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1049 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1050 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1051 }
1052
1053 if (defaultVertexExecutable)
1054 {
1055 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1056 }
1057
1058 if (defaultPixelExecutable)
1059 {
1060 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1061 }
1062#endif
1063
Geoff Langb543aff2014-09-30 14:52:54 -04001064 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001065 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001066}
1067
Geoff Lang7dd2e102014-11-10 15:19:26 -05001068LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1069 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1070 const std::vector<std::string> &transformFeedbackVaryings,
1071 GLenum transformFeedbackBufferMode,
1072 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1073 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001074{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001075 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1076 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001077
Jamie Madillde8892b2014-11-11 13:00:22 -05001078 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1079 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001080
Brandon Joneseb994362014-09-24 10:27:28 -07001081 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001082
1083 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301084 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001085
1086 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301087 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001088 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001089
Austin Kinross02df7962015-07-01 10:03:42 -07001090 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1091 {
1092 if (fragmentShaderD3D->usesFrontFacing())
1093 {
1094 infoLog << "The current renderer doesn't support gl_FrontFacing";
1095 return LinkResult(false, gl::Error(GL_NO_ERROR));
1096 }
1097 }
1098
Brandon Jones22502d52014-08-29 16:58:36 -07001099 // Map the varyings to the register file
Daniel Chengf33ab832015-06-30 19:08:16 -07001100 VaryingPacking packing = {};
Brandon Jones22502d52014-08-29 16:58:36 -07001101 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1102
Geoff Langbdee2d52014-09-17 11:02:51 -04001103 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001104 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001105 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001106 }
1107
Geoff Lang7dd2e102014-11-10 15:19:26 -05001108 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001109 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001110 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001111 }
1112
Jamie Madillde8892b2014-11-11 13:00:22 -05001113 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001114 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1115 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1116 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001117 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001118 }
1119
Brandon Jones44151a92014-09-10 11:32:25 -07001120 mUsesPointSize = vertexShaderD3D->usesPointSize();
1121
Jamie Madill437d2662014-12-05 14:23:35 -05001122 initAttributesByLayout();
1123
Geoff Lang7dd2e102014-11-10 15:19:26 -05001124 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001125}
1126
Geoff Lang0ca53782015-05-07 13:49:39 -04001127void ProgramD3D::bindAttributeLocation(GLuint index, const std::string &name)
1128{
1129}
1130
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001131void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001132{
1133 // Compute total default block size
1134 unsigned int vertexRegisters = 0;
1135 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001136 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001137 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001138 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001139
Geoff Lang2ec386b2014-12-03 14:44:38 -05001140 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001141 {
1142 if (uniform.isReferencedByVertexShader())
1143 {
1144 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1145 }
1146 if (uniform.isReferencedByFragmentShader())
1147 {
1148 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1149 }
1150 }
1151 }
1152
1153 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1154 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1155}
1156
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001157gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001158{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001159 updateSamplerMapping();
1160
1161 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1162 if (error.isError())
1163 {
1164 return error;
1165 }
1166
1167 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1168 {
1169 mUniforms[uniformIndex]->dirty = false;
1170 }
1171
1172 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001173}
1174
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001175gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001176{
Jamie Madill03260fa2015-06-22 13:57:22 -04001177 mVertexUBOCache.clear();
1178 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001179
1180 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1181 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1182
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001183 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001184 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001185 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001186 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001187
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001188 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001189
1190 // Unnecessary to apply an unreferenced standard or shared UBO
1191 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1192 {
1193 continue;
1194 }
1195
1196 if (uniformBlock->isReferencedByVertexShader())
1197 {
1198 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001199 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001200
Jamie Madill969194d2015-07-20 14:36:56 -04001201 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001202 {
1203 mVertexUBOCache.resize(registerIndex + 1, -1);
1204 }
1205
1206 ASSERT(mVertexUBOCache[registerIndex] == -1);
1207 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001208 }
1209
1210 if (uniformBlock->isReferencedByFragmentShader())
1211 {
1212 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001213 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001214
1215 if (mFragmentUBOCache.size() <= registerIndex)
1216 {
1217 mFragmentUBOCache.resize(registerIndex + 1, -1);
1218 }
1219
1220 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1221 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001222 }
1223 }
1224
Jamie Madill03260fa2015-06-22 13:57:22 -04001225 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001226}
1227
1228bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1229 unsigned int registerIndex, const gl::Caps &caps)
1230{
1231 if (shader == GL_VERTEX_SHADER)
1232 {
1233 uniformBlock->vsRegisterIndex = registerIndex;
1234 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1235 {
Jamie Madillf6113162015-05-07 11:49:21 -04001236 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (" << caps.maxVertexUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001237 return false;
1238 }
1239 }
1240 else if (shader == GL_FRAGMENT_SHADER)
1241 {
1242 uniformBlock->psRegisterIndex = registerIndex;
1243 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1244 {
Jamie Madillf6113162015-05-07 11:49:21 -04001245 infoLog << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (" << caps.maxFragmentUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001246 return false;
1247 }
1248 }
1249 else UNREACHABLE();
1250
1251 return true;
1252}
1253
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001254void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001255{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001256 unsigned int numUniforms = mUniforms.size();
1257 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001258 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001259 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001260 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001261}
1262
1263void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1264{
1265 setUniform(location, count, v, GL_FLOAT);
1266}
1267
1268void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1269{
1270 setUniform(location, count, v, GL_FLOAT_VEC2);
1271}
1272
1273void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1274{
1275 setUniform(location, count, v, GL_FLOAT_VEC3);
1276}
1277
1278void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1279{
1280 setUniform(location, count, v, GL_FLOAT_VEC4);
1281}
1282
1283void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1284{
1285 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1286}
1287
1288void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1289{
1290 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1291}
1292
1293void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1294{
1295 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1296}
1297
1298void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1299{
1300 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1301}
1302
1303void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1304{
1305 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1306}
1307
1308void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1309{
1310 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1311}
1312
1313void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1314{
1315 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1316}
1317
1318void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1319{
1320 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1321}
1322
1323void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1324{
1325 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1326}
1327
1328void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1329{
1330 setUniform(location, count, v, GL_INT);
1331}
1332
1333void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1334{
1335 setUniform(location, count, v, GL_INT_VEC2);
1336}
1337
1338void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1339{
1340 setUniform(location, count, v, GL_INT_VEC3);
1341}
1342
1343void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1344{
1345 setUniform(location, count, v, GL_INT_VEC4);
1346}
1347
1348void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1349{
1350 setUniform(location, count, v, GL_UNSIGNED_INT);
1351}
1352
1353void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1354{
1355 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1356}
1357
1358void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1359{
1360 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1361}
1362
1363void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1364{
1365 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1366}
1367
1368void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1369{
1370 getUniformv(location, params, GL_FLOAT);
1371}
1372
1373void ProgramD3D::getUniformiv(GLint location, GLint *params)
1374{
1375 getUniformv(location, params, GL_INT);
1376}
1377
1378void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1379{
1380 getUniformv(location, params, GL_UNSIGNED_INT);
1381}
1382
1383bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1384 const gl::Caps &caps)
1385{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001386 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1387 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001388
1389 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1390 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1391
1392 // Check that uniforms defined in the vertex and fragment shaders are identical
1393 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1394 UniformMap linkedUniforms;
1395
1396 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001397 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001398 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1399 linkedUniforms[vertexUniform.name] = &vertexUniform;
1400 }
1401
1402 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1403 {
1404 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1405 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1406 if (entry != linkedUniforms.end())
1407 {
1408 const sh::Uniform &vertexUniform = *entry->second;
1409 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001410 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001411 {
1412 return false;
1413 }
1414 }
1415 }
1416
1417 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1418 {
1419 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1420
1421 if (uniform.staticUse)
1422 {
Jamie Madill55def582015-05-04 11:24:57 -04001423 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1424 vertexShaderD3D->getUniformRegister(uniform.name);
1425 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001426 }
1427 }
1428
1429 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1430 {
1431 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1432
1433 if (uniform.staticUse)
1434 {
Jamie Madill55def582015-05-04 11:24:57 -04001435 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1436 fragmentShaderD3D->getUniformRegister(uniform.name);
1437 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001438 }
1439 }
1440
1441 if (!indexUniforms(infoLog, caps))
1442 {
1443 return false;
1444 }
1445
1446 initializeUniformStorage();
1447
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001448 return true;
1449}
1450
Geoff Lang492a7e42014-11-05 13:27:06 -05001451void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001452{
Jamie Madill55def582015-05-04 11:24:57 -04001453 if (uniformRegister == GL_INVALID_INDEX)
1454 {
1455 defineUniform(shader, uniform, uniform.name, nullptr);
1456 return;
1457 }
1458
Geoff Lang492a7e42014-11-05 13:27:06 -05001459 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001460 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1461 encoder.skipRegisters(uniformRegister);
1462
1463 defineUniform(shader, uniform, uniform.name, &encoder);
1464}
1465
Geoff Lang492a7e42014-11-05 13:27:06 -05001466void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1467 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001468{
1469 if (uniform.isStruct())
1470 {
1471 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1472 {
1473 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1474
Jamie Madill55def582015-05-04 11:24:57 -04001475 if (encoder)
1476 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001477
1478 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1479 {
1480 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1481 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1482
1483 defineUniform(shader, field, fieldFullName, encoder);
1484 }
1485
Jamie Madill55def582015-05-04 11:24:57 -04001486 if (encoder)
1487 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001488 }
1489 }
1490 else // Not a struct
1491 {
1492 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001493 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001494 {
1495 encoder->enterAggregateType();
1496 }
1497
1498 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1499
Jamie Madill2857f482015-02-09 15:35:29 -05001500 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001501 sh::BlockMemberInfo blockInfo = encoder ?
1502 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1503 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001504
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001505 if (!linkedUniform)
1506 {
1507 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001508 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001509 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001510
1511 if (encoder)
1512 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001513 mUniforms.push_back(linkedUniform);
1514 }
1515
Jamie Madill55def582015-05-04 11:24:57 -04001516 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001517 {
Jamie Madill55def582015-05-04 11:24:57 -04001518 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1519 {
1520 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1521 }
1522 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1523 {
1524 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1525 }
1526 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001527 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001528
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001529 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001530 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001531 {
1532 encoder->exitAggregateType();
1533 }
1534 }
1535}
1536
1537template <typename T>
1538static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1539{
1540 ASSERT(dest != NULL);
1541 ASSERT(dirtyFlag != NULL);
1542
1543 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1544 *dest = source;
1545}
1546
1547template <typename T>
1548void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1549{
1550 const int components = gl::VariableComponentCount(targetUniformType);
1551 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1552
1553 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1554
1555 int elementCount = targetUniform->elementCount();
1556
1557 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1558
1559 if (targetUniform->type == targetUniformType)
1560 {
1561 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1562
1563 for (int i = 0; i < count; i++)
1564 {
1565 T *dest = target + (i * 4);
1566 const T *source = v + (i * components);
1567
1568 for (int c = 0; c < components; c++)
1569 {
1570 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1571 }
1572 for (int c = components; c < 4; c++)
1573 {
1574 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1575 }
1576 }
1577 }
1578 else if (targetUniform->type == targetBoolType)
1579 {
1580 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1581
1582 for (int i = 0; i < count; i++)
1583 {
1584 GLint *dest = boolParams + (i * 4);
1585 const T *source = v + (i * components);
1586
1587 for (int c = 0; c < components; c++)
1588 {
1589 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1590 }
1591 for (int c = components; c < 4; c++)
1592 {
1593 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1594 }
1595 }
1596 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001597 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001598 {
1599 ASSERT(targetUniformType == GL_INT);
1600
1601 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1602
1603 bool wasDirty = targetUniform->dirty;
1604
1605 for (int i = 0; i < count; i++)
1606 {
1607 GLint *dest = target + (i * 4);
1608 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1609
1610 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1611 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1612 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1613 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1614 }
1615
1616 if (!wasDirty && targetUniform->dirty)
1617 {
1618 mDirtySamplerMapping = true;
1619 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001620 }
1621 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001622}
Brandon Jones18bd4102014-09-22 14:21:44 -07001623
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001624template<typename T>
1625bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1626{
1627 bool dirty = false;
1628 int copyWidth = std::min(targetHeight, srcWidth);
1629 int copyHeight = std::min(targetWidth, srcHeight);
1630
1631 for (int x = 0; x < copyWidth; x++)
1632 {
1633 for (int y = 0; y < copyHeight; y++)
1634 {
1635 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1636 }
1637 }
1638 // clear unfilled right side
1639 for (int y = 0; y < copyWidth; y++)
1640 {
1641 for (int x = copyHeight; x < targetWidth; x++)
1642 {
1643 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1644 }
1645 }
1646 // clear unfilled bottom.
1647 for (int y = copyWidth; y < targetHeight; y++)
1648 {
1649 for (int x = 0; x < targetWidth; x++)
1650 {
1651 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1652 }
1653 }
1654
1655 return dirty;
1656}
1657
1658template<typename T>
1659bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1660{
1661 bool dirty = false;
1662 int copyWidth = std::min(targetWidth, srcWidth);
1663 int copyHeight = std::min(targetHeight, srcHeight);
1664
1665 for (int y = 0; y < copyHeight; y++)
1666 {
1667 for (int x = 0; x < copyWidth; x++)
1668 {
1669 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1670 }
1671 }
1672 // clear unfilled right side
1673 for (int y = 0; y < copyHeight; y++)
1674 {
1675 for (int x = copyWidth; x < targetWidth; x++)
1676 {
1677 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1678 }
1679 }
1680 // clear unfilled bottom.
1681 for (int y = copyHeight; y < targetHeight; y++)
1682 {
1683 for (int x = 0; x < targetWidth; x++)
1684 {
1685 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1686 }
1687 }
1688
1689 return dirty;
1690}
1691
1692template <int cols, int rows>
1693void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1694{
1695 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1696
1697 int elementCount = targetUniform->elementCount();
1698
1699 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1700 const unsigned int targetMatrixStride = (4 * rows);
1701 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1702
1703 for (int i = 0; i < count; i++)
1704 {
1705 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1706 if (transpose == GL_FALSE)
1707 {
1708 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1709 }
1710 else
1711 {
1712 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1713 }
1714 target += targetMatrixStride;
1715 value += cols * rows;
1716 }
1717}
1718
1719template <typename T>
1720void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1721{
1722 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1723
1724 if (gl::IsMatrixType(targetUniform->type))
1725 {
1726 const int rows = gl::VariableRowCount(targetUniform->type);
1727 const int cols = gl::VariableColumnCount(targetUniform->type);
1728 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1729 }
1730 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1731 {
1732 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1733 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1734 size * sizeof(T));
1735 }
1736 else
1737 {
1738 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1739 switch (gl::VariableComponentType(targetUniform->type))
1740 {
1741 case GL_BOOL:
1742 {
1743 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1744
1745 for (unsigned int i = 0; i < size; i++)
1746 {
1747 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1748 }
1749 }
1750 break;
1751
1752 case GL_FLOAT:
1753 {
1754 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1755
1756 for (unsigned int i = 0; i < size; i++)
1757 {
1758 params[i] = static_cast<T>(floatParams[i]);
1759 }
1760 }
1761 break;
1762
1763 case GL_INT:
1764 {
1765 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1766
1767 for (unsigned int i = 0; i < size; i++)
1768 {
1769 params[i] = static_cast<T>(intParams[i]);
1770 }
1771 }
1772 break;
1773
1774 case GL_UNSIGNED_INT:
1775 {
1776 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1777
1778 for (unsigned int i = 0; i < size; i++)
1779 {
1780 params[i] = static_cast<T>(uintParams[i]);
1781 }
1782 }
1783 break;
1784
1785 default: UNREACHABLE();
1786 }
1787 }
1788}
1789
1790template <typename VarT>
1791void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1792 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1793 bool inRowMajorLayout)
1794{
1795 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1796 {
1797 const VarT &field = fields[uniformIndex];
1798 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1799
1800 if (field.isStruct())
1801 {
1802 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1803
1804 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1805 {
1806 encoder->enterAggregateType();
1807
1808 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1809 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1810
1811 encoder->exitAggregateType();
1812 }
1813 }
1814 else
1815 {
1816 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1817
1818 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1819
1820 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1821 blockIndex, memberInfo);
1822
1823 // add to uniform list, but not index, since uniform block uniforms have no location
1824 blockUniformIndexes->push_back(mUniforms.size());
1825 mUniforms.push_back(newUniform);
1826 }
1827 }
1828}
1829
Jamie Madilld3dfda22015-07-06 08:28:49 -04001830bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog,
1831 const gl::Shader &shader,
1832 const sh::InterfaceBlock &interfaceBlock,
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001833 const gl::Caps &caps)
1834{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001835 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001836
1837 // create uniform block entries if they do not exist
1838 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1839 {
1840 std::vector<unsigned int> blockUniformIndexes;
1841 const unsigned int blockIndex = mUniformBlocks.size();
1842
1843 // define member uniforms
1844 sh::BlockLayoutEncoder *encoder = NULL;
1845
1846 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1847 {
1848 encoder = new sh::Std140BlockEncoder;
1849 }
1850 else
1851 {
1852 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1853 }
1854 ASSERT(encoder);
1855
1856 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1857
1858 size_t dataSize = encoder->getBlockSize();
1859
1860 // create all the uniform blocks
1861 if (interfaceBlock.arraySize > 0)
1862 {
1863 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1864 {
1865 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1866 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1867 mUniformBlocks.push_back(newUniformBlock);
1868 }
1869 }
1870 else
1871 {
1872 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1873 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1874 mUniformBlocks.push_back(newUniformBlock);
1875 }
1876 }
1877
1878 if (interfaceBlock.staticUse)
1879 {
1880 // Assign registers to the uniform blocks
1881 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1882 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1883 ASSERT(blockIndex != GL_INVALID_INDEX);
1884 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1885
1886 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1887
1888 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1889 {
1890 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1891 ASSERT(uniformBlock->name == interfaceBlock.name);
1892
1893 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1894 interfaceBlockRegister + uniformBlockElement, caps))
1895 {
1896 return false;
1897 }
1898 }
1899 }
1900
1901 return true;
1902}
1903
1904bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001905 GLenum samplerType,
1906 unsigned int samplerCount,
1907 std::vector<Sampler> &outSamplers,
1908 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001909{
1910 unsigned int samplerIndex = startSamplerIndex;
1911
1912 do
1913 {
1914 if (samplerIndex < outSamplers.size())
1915 {
1916 Sampler& sampler = outSamplers[samplerIndex];
1917 sampler.active = true;
1918 sampler.textureType = GetTextureType(samplerType);
1919 sampler.logicalTextureUnit = 0;
1920 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1921 }
1922 else
1923 {
1924 return false;
1925 }
1926
1927 samplerIndex++;
1928 } while (samplerIndex < startSamplerIndex + samplerCount);
1929
1930 return true;
1931}
1932
1933bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1934{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001935 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001936 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1937
1938 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1939 {
1940 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1941 &mUsedVertexSamplerRange))
1942 {
Jamie Madillf6113162015-05-07 11:49:21 -04001943 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1944 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945 return false;
1946 }
1947
1948 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1949 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1950 {
Jamie Madillf6113162015-05-07 11:49:21 -04001951 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1952 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001953 return false;
1954 }
1955 }
1956
1957 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1958 {
1959 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1960 &mUsedPixelSamplerRange))
1961 {
Jamie Madillf6113162015-05-07 11:49:21 -04001962 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
1963 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964 return false;
1965 }
1966
1967 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1968 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1969 {
Jamie Madillf6113162015-05-07 11:49:21 -04001970 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
1971 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001972 return false;
1973 }
1974 }
1975
1976 return true;
1977}
1978
1979bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1980{
1981 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1982 {
1983 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1984
Geoff Lang2ec386b2014-12-03 14:44:38 -05001985 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986 {
1987 if (!indexSamplerUniform(uniform, infoLog, caps))
1988 {
1989 return false;
1990 }
1991 }
1992
Jamie Madill55def582015-05-04 11:24:57 -04001993 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001994 {
Jamie Madill55def582015-05-04 11:24:57 -04001995 if (!uniform.isBuiltIn())
1996 {
Geoff Lang95137842015-06-02 15:38:43 -04001997 // Assign in-order uniform locations
1998 mUniformIndex[mUniformIndex.size()] = gl::VariableLocation(uniform.name, arrayIndex, uniformIndex);
Jamie Madill55def582015-05-04 11:24:57 -04001999 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002000 }
2001 }
2002
2003 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07002004}
2005
Brandon Jonesc9610c52014-08-25 17:02:59 -07002006void ProgramD3D::reset()
2007{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002008 ProgramImpl::reset();
2009
Brandon Joneseb994362014-09-24 10:27:28 -07002010 SafeDeleteContainer(mVertexExecutables);
2011 SafeDeleteContainer(mPixelExecutables);
2012 SafeDelete(mGeometryExecutable);
2013
2014 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07002015
Brandon Jones22502d52014-08-29 16:58:36 -07002016 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002017 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07002018 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07002019
2020 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002021 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002022 mUsesFragDepth = false;
2023 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002024 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002025
Brandon Jonesc9610c52014-08-25 17:02:59 -07002026 SafeDelete(mVertexUniformStorage);
2027 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002028
2029 mSamplersPS.clear();
2030 mSamplersVS.clear();
2031
2032 mUsedVertexSamplerRange = 0;
2033 mUsedPixelSamplerRange = 0;
2034 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002035
2036 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002037}
2038
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039unsigned int ProgramD3D::getSerial() const
2040{
2041 return mSerial;
2042}
2043
2044unsigned int ProgramD3D::issueSerial()
2045{
2046 return mCurrentSerial++;
2047}
2048
Jamie Madill437d2662014-12-05 14:23:35 -05002049void ProgramD3D::initAttributesByLayout()
2050{
2051 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2052 {
2053 mAttributesByLayout[i] = i;
2054 }
2055
2056 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2057}
2058
Jamie Madill476682e2015-06-30 10:04:29 -04002059void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002060 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2061 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002062{
Jamie Madill476682e2015-06-30 10:04:29 -04002063 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002064 {
Jamie Madill476682e2015-06-30 10:04:29 -04002065 int oldIndex = mAttributesByLayout[attribIndex];
2066 sortedSemanticIndicesOut[attribIndex] = mSemanticIndex[oldIndex];
2067 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002068 }
2069}
2070
Jamie Madilld3dfda22015-07-06 08:28:49 -04002071void ProgramD3D::updateCachedInputLayout(const gl::Program *program, const gl::State &state)
2072{
Jamie Madillbd136f92015-08-10 14:51:37 -04002073 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002074 const int *semanticIndexes = program->getSemanticIndexes();
2075
2076 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002077
Jamie Madilld3dfda22015-07-06 08:28:49 -04002078 for (unsigned int attributeIndex = 0; attributeIndex < vertexAttributes.size(); attributeIndex++)
2079 {
2080 int semanticIndex = semanticIndexes[attributeIndex];
2081
2082 if (semanticIndex != -1)
2083 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002084 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2085 {
2086 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2087 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002088 mCachedInputLayout[semanticIndex] =
2089 GetVertexFormatType(vertexAttributes[attributeIndex],
2090 state.getVertexAttribCurrentValue(attributeIndex).Type);
2091 }
2092 }
2093}
2094
Brandon Jonesc9610c52014-08-25 17:02:59 -07002095}