blob: edbe3d5e163d8d89c821bd54811da6f4c11ae2dd [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 Madilld3dfda22015-07-06 08:28:49 -040060void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes,
61 gl::InputLayout *inputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070062{
Jamie Madilld3dfda22015-07-06 08:28:49 -040063 for (const sh::Attribute &shaderAttr : shaderAttributes)
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 Madilld3dfda22015-07-06 08:28:49 -040079 inputLayoutOut->push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070080 }
81 }
82 }
83}
84
85std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
86{
Jamie Madillb4463142014-12-19 14:56:54 -050087 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070088
Jamie Madillb4463142014-12-19 14:56:54 -050089 if (!shaderOutputVars.empty())
90 {
91 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
92 }
Brandon Joneseb994362014-09-24 10:27:28 -070093
94 return defaultPixelOutput;
95}
96
Brandon Jones1a8a7e32014-10-01 12:49:30 -070097bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
98{
99 return var.isRowMajorLayout;
100}
101
102bool IsRowMajorLayout(const sh::ShaderVariable &var)
103{
104 return false;
105}
106
Jamie Madill437d2662014-12-05 14:23:35 -0500107struct AttributeSorter
108{
109 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -0500110 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -0500111 {
112 }
113
114 bool operator()(int a, int b)
115 {
Jamie Madill80d934b2015-02-19 10:16:12 -0500116 int indexA = (*originalIndices)[a];
117 int indexB = (*originalIndices)[b];
118
119 if (indexA == -1) return false;
120 if (indexB == -1) return true;
121 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -0500122 }
123
Jamie Madill80d934b2015-02-19 10:16:12 -0500124 const ProgramImpl::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500125};
126
Brandon Joneseb994362014-09-24 10:27:28 -0700127}
128
Jamie Madilld3dfda22015-07-06 08:28:49 -0400129ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
130 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500131 ShaderExecutableD3D *shaderExecutable)
Jamie Madilld3dfda22015-07-06 08:28:49 -0400132 : mInputs(inputLayout),
133 mSignature(signature),
134 mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700135{
Brandon Joneseb994362014-09-24 10:27:28 -0700136}
137
138ProgramD3D::VertexExecutable::~VertexExecutable()
139{
140 SafeDelete(mShaderExecutable);
141}
142
Jamie Madilld3dfda22015-07-06 08:28:49 -0400143// static
144void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
145 const gl::InputLayout &inputLayout,
146 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700147{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400148 signatureOut->resize(inputLayout.size(), gl::VERTEX_FORMAT_INVALID);
149
150 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700151 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400152 gl::VertexFormatType vertexFormatType = inputLayout[index];
153 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700154 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400155 (*signatureOut)[index] = GL_NONE;
156 }
157 else
158 {
159 bool gpuConverted = ((renderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0);
160 (*signatureOut)[index] = (gpuConverted ? GL_TRUE : GL_FALSE);
Brandon Joneseb994362014-09-24 10:27:28 -0700161 }
162 }
Brandon Joneseb994362014-09-24 10:27:28 -0700163}
164
Jamie Madilld3dfda22015-07-06 08:28:49 -0400165bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
166{
167 return mSignature == signature;
168}
169
170ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
171 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700172 : mOutputSignature(outputSignature),
173 mShaderExecutable(shaderExecutable)
174{
175}
176
177ProgramD3D::PixelExecutable::~PixelExecutable()
178{
179 SafeDelete(mShaderExecutable);
180}
181
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700182ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
183{
184}
185
Geoff Lang7dd2e102014-11-10 15:19:26 -0500186unsigned int ProgramD3D::mCurrentSerial = 1;
187
Jamie Madill93e13fb2014-11-06 15:27:25 -0500188ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700189 : ProgramImpl(),
190 mRenderer(renderer),
191 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700192 mGeometryExecutable(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700193 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700194 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700195 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700196 mUsedVertexSamplerRange(0),
197 mUsedPixelSamplerRange(0),
198 mDirtySamplerMapping(true),
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400199 mTextureUnitTypesCache(renderer->getRendererCaps().maxCombinedTextureImageUnits),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500200 mShaderVersion(100),
201 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700202{
Brandon Joneseb994362014-09-24 10:27:28 -0700203 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700204}
205
206ProgramD3D::~ProgramD3D()
207{
208 reset();
209 SafeDelete(mDynamicHLSL);
210}
211
Brandon Jones44151a92014-09-10 11:32:25 -0700212bool ProgramD3D::usesPointSpriteEmulation() const
213{
214 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
215}
216
217bool ProgramD3D::usesGeometryShader() const
218{
Cooper Partine6664f02015-01-09 16:22:24 -0800219 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
220}
221
222bool ProgramD3D::usesInstancedPointSpriteEmulation() const
223{
224 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700225}
226
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700227GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
228{
229 GLint logicalTextureUnit = -1;
230
231 switch (type)
232 {
233 case gl::SAMPLER_PIXEL:
234 ASSERT(samplerIndex < caps.maxTextureImageUnits);
235 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
236 {
237 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
238 }
239 break;
240 case gl::SAMPLER_VERTEX:
241 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
242 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
243 {
244 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
245 }
246 break;
247 default: UNREACHABLE();
248 }
249
250 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
251 {
252 return logicalTextureUnit;
253 }
254
255 return -1;
256}
257
258// Returns the texture type for a given Direct3D 9 sampler type and
259// index (0-15 for the pixel shader and 0-3 for the vertex shader).
260GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
261{
262 switch (type)
263 {
264 case gl::SAMPLER_PIXEL:
265 ASSERT(samplerIndex < mSamplersPS.size());
266 ASSERT(mSamplersPS[samplerIndex].active);
267 return mSamplersPS[samplerIndex].textureType;
268 case gl::SAMPLER_VERTEX:
269 ASSERT(samplerIndex < mSamplersVS.size());
270 ASSERT(mSamplersVS[samplerIndex].active);
271 return mSamplersVS[samplerIndex].textureType;
272 default: UNREACHABLE();
273 }
274
275 return GL_TEXTURE_2D;
276}
277
278GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
279{
280 switch (type)
281 {
282 case gl::SAMPLER_PIXEL:
283 return mUsedPixelSamplerRange;
284 case gl::SAMPLER_VERTEX:
285 return mUsedVertexSamplerRange;
286 default:
287 UNREACHABLE();
288 return 0;
289 }
290}
291
292void ProgramD3D::updateSamplerMapping()
293{
294 if (!mDirtySamplerMapping)
295 {
296 return;
297 }
298
299 mDirtySamplerMapping = false;
300
301 // Retrieve sampler uniform values
302 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
303 {
304 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
305
306 if (targetUniform->dirty)
307 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500308 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700309 {
310 int count = targetUniform->elementCount();
311 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
312
313 if (targetUniform->isReferencedByFragmentShader())
314 {
315 unsigned int firstIndex = targetUniform->psRegisterIndex;
316
317 for (int i = 0; i < count; i++)
318 {
319 unsigned int samplerIndex = firstIndex + i;
320
321 if (samplerIndex < mSamplersPS.size())
322 {
323 ASSERT(mSamplersPS[samplerIndex].active);
324 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
325 }
326 }
327 }
328
329 if (targetUniform->isReferencedByVertexShader())
330 {
331 unsigned int firstIndex = targetUniform->vsRegisterIndex;
332
333 for (int i = 0; i < count; i++)
334 {
335 unsigned int samplerIndex = firstIndex + i;
336
337 if (samplerIndex < mSamplersVS.size())
338 {
339 ASSERT(mSamplersVS[samplerIndex].active);
340 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
341 }
342 }
343 }
344 }
345 }
346 }
347}
348
349bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
350{
Jamie Madill13776892015-04-28 12:39:06 -0400351 // Skip cache if we're using an infolog, so we get the full error.
352 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
353 if (!mDirtySamplerMapping && infoLog == nullptr && mCachedValidateSamplersResult.valid())
354 {
355 return mCachedValidateSamplersResult.value();
356 }
357
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700358 // if any two active samplers in a program are of different types, but refer to the same
359 // texture image unit, and this is the current program, then ValidateProgram will fail, and
360 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
361 updateSamplerMapping();
362
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400363 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700364
365 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
366 {
367 if (mSamplersPS[i].active)
368 {
369 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
370
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400371 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700372 {
373 if (infoLog)
374 {
Jamie Madillf6113162015-05-07 11:49:21 -0400375 (*infoLog) << "Sampler uniform (" << unit
376 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
377 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700378 }
379
Jamie Madill13776892015-04-28 12:39:06 -0400380 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700381 return false;
382 }
383
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400384 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700385 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400386 if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700387 {
388 if (infoLog)
389 {
Jamie Madillf6113162015-05-07 11:49:21 -0400390 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
391 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700392 }
393
Jamie Madill13776892015-04-28 12:39:06 -0400394 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700395 return false;
396 }
397 }
398 else
399 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400400 mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700401 }
402 }
403 }
404
405 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
406 {
407 if (mSamplersVS[i].active)
408 {
409 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
410
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400411 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700412 {
413 if (infoLog)
414 {
Jamie Madillf6113162015-05-07 11:49:21 -0400415 (*infoLog) << "Sampler uniform (" << unit
416 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
417 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700418 }
419
Jamie Madill13776892015-04-28 12:39:06 -0400420 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700421 return false;
422 }
423
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400424 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700425 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400426 if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700427 {
428 if (infoLog)
429 {
Jamie Madillf6113162015-05-07 11:49:21 -0400430 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
431 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700432 }
433
Jamie Madill13776892015-04-28 12:39:06 -0400434 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700435 return false;
436 }
437 }
438 else
439 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400440 mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700441 }
442 }
443 }
444
Jamie Madill13776892015-04-28 12:39:06 -0400445 mCachedValidateSamplersResult = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700446 return true;
447}
448
Geoff Lang7dd2e102014-11-10 15:19:26 -0500449LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700450{
Austin Kinross137b1512015-06-17 16:14:53 -0700451 DeviceIdentifier binaryDeviceIdentifier = { 0 };
452 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryDeviceIdentifier), sizeof(DeviceIdentifier));
453
454 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
455 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
456 {
457 infoLog << "Invalid program binary, device configuration has changed.";
458 return LinkResult(false, gl::Error(GL_NO_ERROR));
459 }
460
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500461 int compileFlags = stream->readInt<int>();
462 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
463 {
Jamie Madillf6113162015-05-07 11:49:21 -0400464 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500465 return LinkResult(false, gl::Error(GL_NO_ERROR));
466 }
467
Brandon Jones44151a92014-09-10 11:32:25 -0700468 stream->readInt(&mShaderVersion);
469
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700470 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
471 for (unsigned int i = 0; i < psSamplerCount; ++i)
472 {
473 Sampler sampler;
474 stream->readBool(&sampler.active);
475 stream->readInt(&sampler.logicalTextureUnit);
476 stream->readInt(&sampler.textureType);
477 mSamplersPS.push_back(sampler);
478 }
479 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
480 for (unsigned int i = 0; i < vsSamplerCount; ++i)
481 {
482 Sampler sampler;
483 stream->readBool(&sampler.active);
484 stream->readInt(&sampler.logicalTextureUnit);
485 stream->readInt(&sampler.textureType);
486 mSamplersVS.push_back(sampler);
487 }
488
489 stream->readInt(&mUsedVertexSamplerRange);
490 stream->readInt(&mUsedPixelSamplerRange);
491
492 const unsigned int uniformCount = stream->readInt<unsigned int>();
493 if (stream->error())
494 {
Jamie Madillf6113162015-05-07 11:49:21 -0400495 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500496 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700497 }
498
499 mUniforms.resize(uniformCount);
500 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
501 {
502 GLenum type = stream->readInt<GLenum>();
503 GLenum precision = stream->readInt<GLenum>();
504 std::string name = stream->readString();
505 unsigned int arraySize = stream->readInt<unsigned int>();
506 int blockIndex = stream->readInt<int>();
507
508 int offset = stream->readInt<int>();
509 int arrayStride = stream->readInt<int>();
510 int matrixStride = stream->readInt<int>();
511 bool isRowMajorMatrix = stream->readBool();
512
513 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
514
515 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
516
517 stream->readInt(&uniform->psRegisterIndex);
518 stream->readInt(&uniform->vsRegisterIndex);
519 stream->readInt(&uniform->registerCount);
520 stream->readInt(&uniform->registerElement);
521
522 mUniforms[uniformIndex] = uniform;
523 }
524
525 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
526 if (stream->error())
527 {
Jamie Madillf6113162015-05-07 11:49:21 -0400528 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500529 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700530 }
531
532 mUniformIndex.resize(uniformIndexCount);
533 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
534 {
535 stream->readString(&mUniformIndex[uniformIndexIndex].name);
536 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
537 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
538 }
539
540 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
541 if (stream->error())
542 {
Jamie Madillf6113162015-05-07 11:49:21 -0400543 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500544 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700545 }
546
547 mUniformBlocks.resize(uniformBlockCount);
548 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
549 {
550 std::string name = stream->readString();
551 unsigned int elementIndex = stream->readInt<unsigned int>();
552 unsigned int dataSize = stream->readInt<unsigned int>();
553
554 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
555
556 stream->readInt(&uniformBlock->psRegisterIndex);
557 stream->readInt(&uniformBlock->vsRegisterIndex);
558
559 unsigned int numMembers = stream->readInt<unsigned int>();
560 uniformBlock->memberUniformIndexes.resize(numMembers);
561 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
562 {
563 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
564 }
565
566 mUniformBlocks[uniformBlockIndex] = uniformBlock;
567 }
568
Brandon Joneseb994362014-09-24 10:27:28 -0700569 stream->readInt(&mTransformFeedbackBufferMode);
570 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
571 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
572 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
573 {
574 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
575
576 stream->readString(&varying.name);
577 stream->readInt(&varying.type);
578 stream->readInt(&varying.size);
579 stream->readString(&varying.semanticName);
580 stream->readInt(&varying.semanticIndex);
581 stream->readInt(&varying.semanticIndexCount);
582 }
583
Brandon Jones22502d52014-08-29 16:58:36 -0700584 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530585 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700586 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530587 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700588 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700589 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700590
591 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
592 mPixelShaderKey.resize(pixelShaderKeySize);
593 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
594 {
595 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
596 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
597 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
598 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
599 }
600
Brandon Joneseb994362014-09-24 10:27:28 -0700601 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
602
603 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
604 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
605 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400606 size_t inputLayoutSize = stream->readInt<size_t>();
607 gl::InputLayout inputLayout;
Brandon Joneseb994362014-09-24 10:27:28 -0700608
Jamie Madilld3dfda22015-07-06 08:28:49 -0400609 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700610 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400611 inputLayout.push_back(stream->readInt<gl::VertexFormatType>());
Brandon Joneseb994362014-09-24 10:27:28 -0700612 }
613
614 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
615 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400616
Geoff Lang359ef262015-01-05 14:42:29 -0500617 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400618 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
619 SHADER_VERTEX,
620 mTransformFeedbackLinkedVaryings,
621 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
622 &shaderExecutable);
623 if (error.isError())
624 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500625 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400626 }
627
Brandon Joneseb994362014-09-24 10:27:28 -0700628 if (!shaderExecutable)
629 {
Jamie Madillf6113162015-05-07 11:49:21 -0400630 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500631 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700632 }
633
634 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400635 VertexExecutable::Signature signature;
636 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700637
638 // add new binary
639 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
640
641 stream->skip(vertexShaderSize);
642 }
643
644 const size_t pixelShaderCount = stream->readInt<unsigned int>();
645 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
646 {
647 const size_t outputCount = stream->readInt<unsigned int>();
648 std::vector<GLenum> outputs(outputCount);
649 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
650 {
651 stream->readInt(&outputs[outputIndex]);
652 }
653
654 const size_t pixelShaderSize = stream->readInt<unsigned int>();
655 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500656 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400657 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
658 mTransformFeedbackLinkedVaryings,
659 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
660 &shaderExecutable);
661 if (error.isError())
662 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500663 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400664 }
Brandon Joneseb994362014-09-24 10:27:28 -0700665
666 if (!shaderExecutable)
667 {
Jamie Madillf6113162015-05-07 11:49:21 -0400668 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500669 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700670 }
671
672 // add new binary
673 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
674
675 stream->skip(pixelShaderSize);
676 }
677
678 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
679
680 if (geometryShaderSize > 0)
681 {
682 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400683 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
684 mTransformFeedbackLinkedVaryings,
685 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
686 &mGeometryExecutable);
687 if (error.isError())
688 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500689 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400690 }
Brandon Joneseb994362014-09-24 10:27:28 -0700691
692 if (!mGeometryExecutable)
693 {
Jamie Madillf6113162015-05-07 11:49:21 -0400694 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500695 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700696 }
697 stream->skip(geometryShaderSize);
698 }
699
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700700 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500701 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700702
Geoff Lang7dd2e102014-11-10 15:19:26 -0500703 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700704}
705
Geoff Langb543aff2014-09-30 14:52:54 -0400706gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700707{
Austin Kinross137b1512015-06-17 16:14:53 -0700708 // Output the DeviceIdentifier before we output any shader code
709 // When we load the binary again later, we can validate the device identifier before trying to compile any HLSL
710 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
711 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(DeviceIdentifier));
712
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500713 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
714
Brandon Jones44151a92014-09-10 11:32:25 -0700715 stream->writeInt(mShaderVersion);
716
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700717 stream->writeInt(mSamplersPS.size());
718 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
719 {
720 stream->writeInt(mSamplersPS[i].active);
721 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
722 stream->writeInt(mSamplersPS[i].textureType);
723 }
724
725 stream->writeInt(mSamplersVS.size());
726 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
727 {
728 stream->writeInt(mSamplersVS[i].active);
729 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
730 stream->writeInt(mSamplersVS[i].textureType);
731 }
732
733 stream->writeInt(mUsedVertexSamplerRange);
734 stream->writeInt(mUsedPixelSamplerRange);
735
736 stream->writeInt(mUniforms.size());
737 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
738 {
739 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
740
741 stream->writeInt(uniform.type);
742 stream->writeInt(uniform.precision);
743 stream->writeString(uniform.name);
744 stream->writeInt(uniform.arraySize);
745 stream->writeInt(uniform.blockIndex);
746
747 stream->writeInt(uniform.blockInfo.offset);
748 stream->writeInt(uniform.blockInfo.arrayStride);
749 stream->writeInt(uniform.blockInfo.matrixStride);
750 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
751
752 stream->writeInt(uniform.psRegisterIndex);
753 stream->writeInt(uniform.vsRegisterIndex);
754 stream->writeInt(uniform.registerCount);
755 stream->writeInt(uniform.registerElement);
756 }
757
758 stream->writeInt(mUniformIndex.size());
759 for (size_t i = 0; i < mUniformIndex.size(); ++i)
760 {
761 stream->writeString(mUniformIndex[i].name);
762 stream->writeInt(mUniformIndex[i].element);
763 stream->writeInt(mUniformIndex[i].index);
764 }
765
766 stream->writeInt(mUniformBlocks.size());
767 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
768 {
769 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
770
771 stream->writeString(uniformBlock.name);
772 stream->writeInt(uniformBlock.elementIndex);
773 stream->writeInt(uniformBlock.dataSize);
774
775 stream->writeInt(uniformBlock.memberUniformIndexes.size());
776 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
777 {
778 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
779 }
780
781 stream->writeInt(uniformBlock.psRegisterIndex);
782 stream->writeInt(uniformBlock.vsRegisterIndex);
783 }
784
Brandon Joneseb994362014-09-24 10:27:28 -0700785 stream->writeInt(mTransformFeedbackBufferMode);
786 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
787 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
788 {
789 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
790
791 stream->writeString(varying.name);
792 stream->writeInt(varying.type);
793 stream->writeInt(varying.size);
794 stream->writeString(varying.semanticName);
795 stream->writeInt(varying.semanticIndex);
796 stream->writeInt(varying.semanticIndexCount);
797 }
798
Brandon Jones22502d52014-08-29 16:58:36 -0700799 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530800 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700801 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530802 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700803 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700804 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700805
Brandon Joneseb994362014-09-24 10:27:28 -0700806 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700807 stream->writeInt(pixelShaderKey.size());
808 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
809 {
Brandon Joneseb994362014-09-24 10:27:28 -0700810 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700811 stream->writeInt(variable.type);
812 stream->writeString(variable.name);
813 stream->writeString(variable.source);
814 stream->writeInt(variable.outputIndex);
815 }
816
Brandon Joneseb994362014-09-24 10:27:28 -0700817 stream->writeInt(mVertexExecutables.size());
818 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
819 {
820 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
821
Jamie Madilld3dfda22015-07-06 08:28:49 -0400822 const auto &inputLayout = vertexExecutable->inputs();
823 stream->writeInt(inputLayout.size());
824
825 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700826 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400827 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -0700828 }
829
830 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
831 stream->writeInt(vertexShaderSize);
832
833 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
834 stream->writeBytes(vertexBlob, vertexShaderSize);
835 }
836
837 stream->writeInt(mPixelExecutables.size());
838 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
839 {
840 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
841
842 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
843 stream->writeInt(outputs.size());
844 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
845 {
846 stream->writeInt(outputs[outputIndex]);
847 }
848
849 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
850 stream->writeInt(pixelShaderSize);
851
852 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
853 stream->writeBytes(pixelBlob, pixelShaderSize);
854 }
855
856 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
857 stream->writeInt(geometryShaderSize);
858
859 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
860 {
861 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
862 stream->writeBytes(geometryBlob, geometryShaderSize);
863 }
864
Geoff Langb543aff2014-09-30 14:52:54 -0400865 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700866}
867
Geoff Lang359ef262015-01-05 14:42:29 -0500868gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700869{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400870 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700871
Jamie Madill85a18042015-03-05 15:41:41 -0500872 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
873 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700874
875 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
876 {
877 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
878
879 if (colorbuffer)
880 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400881 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700882 }
883 else
884 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400885 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700886 }
887 }
888
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400889 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700890}
891
Jamie Madill97399232014-12-23 12:31:15 -0500892gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500893 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500894 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700895{
896 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
897 {
898 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
899 {
Geoff Langb543aff2014-09-30 14:52:54 -0400900 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
901 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700902 }
903 }
904
Brandon Jones22502d52014-08-29 16:58:36 -0700905 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
906 outputSignature);
907
908 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500909 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500910
911 gl::InfoLog tempInfoLog;
912 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
913
914 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400915 mTransformFeedbackLinkedVaryings,
916 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
917 mPixelWorkarounds, &pixelExecutable);
918 if (error.isError())
919 {
920 return error;
921 }
Brandon Joneseb994362014-09-24 10:27:28 -0700922
Jamie Madill97399232014-12-23 12:31:15 -0500923 if (pixelExecutable)
924 {
925 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
926 }
927 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700928 {
929 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
930 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
931 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
932 }
Brandon Jones22502d52014-08-29 16:58:36 -0700933
Geoff Langb543aff2014-09-30 14:52:54 -0400934 *outExectuable = pixelExecutable;
935 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700936}
937
Jamie Madilld3dfda22015-07-06 08:28:49 -0400938gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -0500939 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500940 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700941{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400942 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -0700943
944 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
945 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400946 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -0700947 {
Geoff Langb543aff2014-09-30 14:52:54 -0400948 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
949 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700950 }
951 }
952
Brandon Jones22502d52014-08-29 16:58:36 -0700953 // Generate new dynamic layout with attribute conversions
Jamie Madill3da79b72015-04-27 11:09:17 -0400954 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, getShaderAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -0700955
956 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500957 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500958
959 gl::InfoLog tempInfoLog;
960 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
961
962 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400963 mTransformFeedbackLinkedVaryings,
964 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
965 mVertexWorkarounds, &vertexExecutable);
966 if (error.isError())
967 {
968 return error;
969 }
970
Jamie Madill97399232014-12-23 12:31:15 -0500971 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700972 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400973 mVertexExecutables.push_back(new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700974 }
Jamie Madill97399232014-12-23 12:31:15 -0500975 else if (!infoLog)
976 {
977 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
978 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
979 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
980 }
Brandon Jones22502d52014-08-29 16:58:36 -0700981
Geoff Langb543aff2014-09-30 14:52:54 -0400982 *outExectuable = vertexExecutable;
983 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700984}
985
Geoff Lang7dd2e102014-11-10 15:19:26 -0500986LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
987 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700988{
Jamie Madillf4bf3812015-04-01 16:15:32 -0400989 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
990 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones44151a92014-09-10 11:32:25 -0700991
Jamie Madilld3dfda22015-07-06 08:28:49 -0400992 gl::InputLayout defaultInputLayout;
993 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), &defaultInputLayout);
Jamie Madille4ea2022015-03-26 20:35:05 +0000994 ShaderExecutableD3D *defaultVertexExecutable = NULL;
995 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
996 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -0800997 {
Jamie Madille4ea2022015-03-26 20:35:05 +0000998 return LinkResult(false, error);
999 }
Austin Kinross434953e2015-02-20 10:49:51 -08001000
Brandon Joneseb994362014-09-24 10:27:28 -07001001 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001002 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +00001003 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001004 if (error.isError())
1005 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001006 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001007 }
Brandon Jones44151a92014-09-10 11:32:25 -07001008
Brandon Joneseb994362014-09-24 10:27:28 -07001009 if (usesGeometryShader())
1010 {
1011 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001012
Geoff Langb543aff2014-09-30 14:52:54 -04001013
1014 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1015 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +05301016 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001017 if (error.isError())
1018 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001019 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001020 }
Brandon Joneseb994362014-09-24 10:27:28 -07001021 }
1022
Brandon Jones091540d2014-10-29 11:32:04 -07001023#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001024 if (usesGeometryShader() && mGeometryExecutable)
1025 {
1026 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1027 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1028 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1029 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1030 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1031 }
1032
1033 if (defaultVertexExecutable)
1034 {
1035 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1036 }
1037
1038 if (defaultPixelExecutable)
1039 {
1040 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1041 }
1042#endif
1043
Geoff Langb543aff2014-09-30 14:52:54 -04001044 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001045 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001046}
1047
Geoff Lang7dd2e102014-11-10 15:19:26 -05001048LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1049 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1050 const std::vector<std::string> &transformFeedbackVaryings,
1051 GLenum transformFeedbackBufferMode,
1052 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1053 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001054{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001055 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1056 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001057
Jamie Madillde8892b2014-11-11 13:00:22 -05001058 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1059 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001060
Brandon Joneseb994362014-09-24 10:27:28 -07001061 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001062
1063 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301064 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001065
1066 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301067 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001068 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001069
1070 // Map the varyings to the register file
Daniel Chengf33ab832015-06-30 19:08:16 -07001071 VaryingPacking packing = {};
Brandon Jones22502d52014-08-29 16:58:36 -07001072 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1073
Geoff Langbdee2d52014-09-17 11:02:51 -04001074 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001075 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001077 }
1078
Geoff Lang7dd2e102014-11-10 15:19:26 -05001079 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001080 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001082 }
1083
Jamie Madillde8892b2014-11-11 13:00:22 -05001084 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001085 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1086 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1087 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001088 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001089 }
1090
Brandon Jones44151a92014-09-10 11:32:25 -07001091 mUsesPointSize = vertexShaderD3D->usesPointSize();
1092
Jamie Madill437d2662014-12-05 14:23:35 -05001093 initAttributesByLayout();
1094
Geoff Lang7dd2e102014-11-10 15:19:26 -05001095 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001096}
1097
Geoff Lang0ca53782015-05-07 13:49:39 -04001098void ProgramD3D::bindAttributeLocation(GLuint index, const std::string &name)
1099{
1100}
1101
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001102void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001103{
1104 // Compute total default block size
1105 unsigned int vertexRegisters = 0;
1106 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001107 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001108 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001109 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001110
Geoff Lang2ec386b2014-12-03 14:44:38 -05001111 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001112 {
1113 if (uniform.isReferencedByVertexShader())
1114 {
1115 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1116 }
1117 if (uniform.isReferencedByFragmentShader())
1118 {
1119 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1120 }
1121 }
1122 }
1123
1124 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1125 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1126}
1127
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001128gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001129{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001130 updateSamplerMapping();
1131
1132 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1133 if (error.isError())
1134 {
1135 return error;
1136 }
1137
1138 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1139 {
1140 mUniforms[uniformIndex]->dirty = false;
1141 }
1142
1143 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001144}
1145
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001146gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001147{
Jamie Madill03260fa2015-06-22 13:57:22 -04001148 mVertexUBOCache.clear();
1149 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001150
1151 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1152 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1153
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001154 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001155 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001156 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001157 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001158
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001159 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001160
1161 // Unnecessary to apply an unreferenced standard or shared UBO
1162 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1163 {
1164 continue;
1165 }
1166
1167 if (uniformBlock->isReferencedByVertexShader())
1168 {
1169 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001170 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001171
1172 if (mFragmentUBOCache.size() <= registerIndex)
1173 {
1174 mVertexUBOCache.resize(registerIndex + 1, -1);
1175 }
1176
1177 ASSERT(mVertexUBOCache[registerIndex] == -1);
1178 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001179 }
1180
1181 if (uniformBlock->isReferencedByFragmentShader())
1182 {
1183 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001184 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001185
1186 if (mFragmentUBOCache.size() <= registerIndex)
1187 {
1188 mFragmentUBOCache.resize(registerIndex + 1, -1);
1189 }
1190
1191 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1192 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001193 }
1194 }
1195
Jamie Madill03260fa2015-06-22 13:57:22 -04001196 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001197}
1198
1199bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1200 unsigned int registerIndex, const gl::Caps &caps)
1201{
1202 if (shader == GL_VERTEX_SHADER)
1203 {
1204 uniformBlock->vsRegisterIndex = registerIndex;
1205 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1206 {
Jamie Madillf6113162015-05-07 11:49:21 -04001207 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (" << caps.maxVertexUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001208 return false;
1209 }
1210 }
1211 else if (shader == GL_FRAGMENT_SHADER)
1212 {
1213 uniformBlock->psRegisterIndex = registerIndex;
1214 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1215 {
Jamie Madillf6113162015-05-07 11:49:21 -04001216 infoLog << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (" << caps.maxFragmentUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001217 return false;
1218 }
1219 }
1220 else UNREACHABLE();
1221
1222 return true;
1223}
1224
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001225void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001226{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001227 unsigned int numUniforms = mUniforms.size();
1228 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001229 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001230 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001231 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001232}
1233
1234void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1235{
1236 setUniform(location, count, v, GL_FLOAT);
1237}
1238
1239void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1240{
1241 setUniform(location, count, v, GL_FLOAT_VEC2);
1242}
1243
1244void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1245{
1246 setUniform(location, count, v, GL_FLOAT_VEC3);
1247}
1248
1249void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1250{
1251 setUniform(location, count, v, GL_FLOAT_VEC4);
1252}
1253
1254void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1255{
1256 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1257}
1258
1259void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1260{
1261 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1262}
1263
1264void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1265{
1266 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1267}
1268
1269void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1270{
1271 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1272}
1273
1274void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1275{
1276 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1277}
1278
1279void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1280{
1281 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1282}
1283
1284void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1285{
1286 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1287}
1288
1289void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1290{
1291 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1292}
1293
1294void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1295{
1296 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1297}
1298
1299void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1300{
1301 setUniform(location, count, v, GL_INT);
1302}
1303
1304void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1305{
1306 setUniform(location, count, v, GL_INT_VEC2);
1307}
1308
1309void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1310{
1311 setUniform(location, count, v, GL_INT_VEC3);
1312}
1313
1314void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1315{
1316 setUniform(location, count, v, GL_INT_VEC4);
1317}
1318
1319void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1320{
1321 setUniform(location, count, v, GL_UNSIGNED_INT);
1322}
1323
1324void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1325{
1326 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1327}
1328
1329void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1330{
1331 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1332}
1333
1334void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1335{
1336 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1337}
1338
1339void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1340{
1341 getUniformv(location, params, GL_FLOAT);
1342}
1343
1344void ProgramD3D::getUniformiv(GLint location, GLint *params)
1345{
1346 getUniformv(location, params, GL_INT);
1347}
1348
1349void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1350{
1351 getUniformv(location, params, GL_UNSIGNED_INT);
1352}
1353
1354bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1355 const gl::Caps &caps)
1356{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001357 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1358 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001359
1360 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1361 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1362
1363 // Check that uniforms defined in the vertex and fragment shaders are identical
1364 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1365 UniformMap linkedUniforms;
1366
1367 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001368 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001369 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1370 linkedUniforms[vertexUniform.name] = &vertexUniform;
1371 }
1372
1373 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1374 {
1375 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1376 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1377 if (entry != linkedUniforms.end())
1378 {
1379 const sh::Uniform &vertexUniform = *entry->second;
1380 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001381 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001382 {
1383 return false;
1384 }
1385 }
1386 }
1387
1388 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1389 {
1390 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1391
1392 if (uniform.staticUse)
1393 {
Jamie Madill55def582015-05-04 11:24:57 -04001394 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1395 vertexShaderD3D->getUniformRegister(uniform.name);
1396 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001397 }
1398 }
1399
1400 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1401 {
1402 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1403
1404 if (uniform.staticUse)
1405 {
Jamie Madill55def582015-05-04 11:24:57 -04001406 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1407 fragmentShaderD3D->getUniformRegister(uniform.name);
1408 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001409 }
1410 }
1411
1412 if (!indexUniforms(infoLog, caps))
1413 {
1414 return false;
1415 }
1416
1417 initializeUniformStorage();
1418
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001419 return true;
1420}
1421
Geoff Lang492a7e42014-11-05 13:27:06 -05001422void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001423{
Jamie Madill55def582015-05-04 11:24:57 -04001424 if (uniformRegister == GL_INVALID_INDEX)
1425 {
1426 defineUniform(shader, uniform, uniform.name, nullptr);
1427 return;
1428 }
1429
Geoff Lang492a7e42014-11-05 13:27:06 -05001430 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001431 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1432 encoder.skipRegisters(uniformRegister);
1433
1434 defineUniform(shader, uniform, uniform.name, &encoder);
1435}
1436
Geoff Lang492a7e42014-11-05 13:27:06 -05001437void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1438 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001439{
1440 if (uniform.isStruct())
1441 {
1442 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1443 {
1444 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1445
Jamie Madill55def582015-05-04 11:24:57 -04001446 if (encoder)
1447 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001448
1449 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1450 {
1451 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1452 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1453
1454 defineUniform(shader, field, fieldFullName, encoder);
1455 }
1456
Jamie Madill55def582015-05-04 11:24:57 -04001457 if (encoder)
1458 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001459 }
1460 }
1461 else // Not a struct
1462 {
1463 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001464 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001465 {
1466 encoder->enterAggregateType();
1467 }
1468
1469 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1470
Jamie Madill2857f482015-02-09 15:35:29 -05001471 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001472 sh::BlockMemberInfo blockInfo = encoder ?
1473 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1474 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001475
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001476 if (!linkedUniform)
1477 {
1478 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001479 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001480 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001481
1482 if (encoder)
1483 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001484 mUniforms.push_back(linkedUniform);
1485 }
1486
Jamie Madill55def582015-05-04 11:24:57 -04001487 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001488 {
Jamie Madill55def582015-05-04 11:24:57 -04001489 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1490 {
1491 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1492 }
1493 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1494 {
1495 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1496 }
1497 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001498 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001499
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001500 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001501 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001502 {
1503 encoder->exitAggregateType();
1504 }
1505 }
1506}
1507
1508template <typename T>
1509static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1510{
1511 ASSERT(dest != NULL);
1512 ASSERT(dirtyFlag != NULL);
1513
1514 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1515 *dest = source;
1516}
1517
1518template <typename T>
1519void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1520{
1521 const int components = gl::VariableComponentCount(targetUniformType);
1522 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1523
1524 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1525
1526 int elementCount = targetUniform->elementCount();
1527
1528 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1529
1530 if (targetUniform->type == targetUniformType)
1531 {
1532 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1533
1534 for (int i = 0; i < count; i++)
1535 {
1536 T *dest = target + (i * 4);
1537 const T *source = v + (i * components);
1538
1539 for (int c = 0; c < components; c++)
1540 {
1541 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1542 }
1543 for (int c = components; c < 4; c++)
1544 {
1545 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1546 }
1547 }
1548 }
1549 else if (targetUniform->type == targetBoolType)
1550 {
1551 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1552
1553 for (int i = 0; i < count; i++)
1554 {
1555 GLint *dest = boolParams + (i * 4);
1556 const T *source = v + (i * components);
1557
1558 for (int c = 0; c < components; c++)
1559 {
1560 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1561 }
1562 for (int c = components; c < 4; c++)
1563 {
1564 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1565 }
1566 }
1567 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001568 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001569 {
1570 ASSERT(targetUniformType == GL_INT);
1571
1572 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1573
1574 bool wasDirty = targetUniform->dirty;
1575
1576 for (int i = 0; i < count; i++)
1577 {
1578 GLint *dest = target + (i * 4);
1579 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1580
1581 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1582 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1583 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1584 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1585 }
1586
1587 if (!wasDirty && targetUniform->dirty)
1588 {
1589 mDirtySamplerMapping = true;
1590 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001591 }
1592 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001593}
Brandon Jones18bd4102014-09-22 14:21:44 -07001594
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001595template<typename T>
1596bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1597{
1598 bool dirty = false;
1599 int copyWidth = std::min(targetHeight, srcWidth);
1600 int copyHeight = std::min(targetWidth, srcHeight);
1601
1602 for (int x = 0; x < copyWidth; x++)
1603 {
1604 for (int y = 0; y < copyHeight; y++)
1605 {
1606 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1607 }
1608 }
1609 // clear unfilled right side
1610 for (int y = 0; y < copyWidth; y++)
1611 {
1612 for (int x = copyHeight; x < targetWidth; x++)
1613 {
1614 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1615 }
1616 }
1617 // clear unfilled bottom.
1618 for (int y = copyWidth; y < targetHeight; y++)
1619 {
1620 for (int x = 0; x < targetWidth; x++)
1621 {
1622 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1623 }
1624 }
1625
1626 return dirty;
1627}
1628
1629template<typename T>
1630bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1631{
1632 bool dirty = false;
1633 int copyWidth = std::min(targetWidth, srcWidth);
1634 int copyHeight = std::min(targetHeight, srcHeight);
1635
1636 for (int y = 0; y < copyHeight; y++)
1637 {
1638 for (int x = 0; x < copyWidth; x++)
1639 {
1640 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1641 }
1642 }
1643 // clear unfilled right side
1644 for (int y = 0; y < copyHeight; y++)
1645 {
1646 for (int x = copyWidth; x < targetWidth; x++)
1647 {
1648 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1649 }
1650 }
1651 // clear unfilled bottom.
1652 for (int y = copyHeight; y < targetHeight; y++)
1653 {
1654 for (int x = 0; x < targetWidth; x++)
1655 {
1656 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1657 }
1658 }
1659
1660 return dirty;
1661}
1662
1663template <int cols, int rows>
1664void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1665{
1666 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1667
1668 int elementCount = targetUniform->elementCount();
1669
1670 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1671 const unsigned int targetMatrixStride = (4 * rows);
1672 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1673
1674 for (int i = 0; i < count; i++)
1675 {
1676 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1677 if (transpose == GL_FALSE)
1678 {
1679 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1680 }
1681 else
1682 {
1683 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1684 }
1685 target += targetMatrixStride;
1686 value += cols * rows;
1687 }
1688}
1689
1690template <typename T>
1691void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1692{
1693 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1694
1695 if (gl::IsMatrixType(targetUniform->type))
1696 {
1697 const int rows = gl::VariableRowCount(targetUniform->type);
1698 const int cols = gl::VariableColumnCount(targetUniform->type);
1699 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1700 }
1701 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1702 {
1703 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1704 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1705 size * sizeof(T));
1706 }
1707 else
1708 {
1709 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1710 switch (gl::VariableComponentType(targetUniform->type))
1711 {
1712 case GL_BOOL:
1713 {
1714 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1715
1716 for (unsigned int i = 0; i < size; i++)
1717 {
1718 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1719 }
1720 }
1721 break;
1722
1723 case GL_FLOAT:
1724 {
1725 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1726
1727 for (unsigned int i = 0; i < size; i++)
1728 {
1729 params[i] = static_cast<T>(floatParams[i]);
1730 }
1731 }
1732 break;
1733
1734 case GL_INT:
1735 {
1736 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1737
1738 for (unsigned int i = 0; i < size; i++)
1739 {
1740 params[i] = static_cast<T>(intParams[i]);
1741 }
1742 }
1743 break;
1744
1745 case GL_UNSIGNED_INT:
1746 {
1747 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1748
1749 for (unsigned int i = 0; i < size; i++)
1750 {
1751 params[i] = static_cast<T>(uintParams[i]);
1752 }
1753 }
1754 break;
1755
1756 default: UNREACHABLE();
1757 }
1758 }
1759}
1760
1761template <typename VarT>
1762void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1763 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1764 bool inRowMajorLayout)
1765{
1766 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1767 {
1768 const VarT &field = fields[uniformIndex];
1769 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1770
1771 if (field.isStruct())
1772 {
1773 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1774
1775 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1776 {
1777 encoder->enterAggregateType();
1778
1779 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1780 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1781
1782 encoder->exitAggregateType();
1783 }
1784 }
1785 else
1786 {
1787 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1788
1789 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1790
1791 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1792 blockIndex, memberInfo);
1793
1794 // add to uniform list, but not index, since uniform block uniforms have no location
1795 blockUniformIndexes->push_back(mUniforms.size());
1796 mUniforms.push_back(newUniform);
1797 }
1798 }
1799}
1800
Jamie Madilld3dfda22015-07-06 08:28:49 -04001801bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog,
1802 const gl::Shader &shader,
1803 const sh::InterfaceBlock &interfaceBlock,
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001804 const gl::Caps &caps)
1805{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001806 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001807
1808 // create uniform block entries if they do not exist
1809 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1810 {
1811 std::vector<unsigned int> blockUniformIndexes;
1812 const unsigned int blockIndex = mUniformBlocks.size();
1813
1814 // define member uniforms
1815 sh::BlockLayoutEncoder *encoder = NULL;
1816
1817 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1818 {
1819 encoder = new sh::Std140BlockEncoder;
1820 }
1821 else
1822 {
1823 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1824 }
1825 ASSERT(encoder);
1826
1827 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1828
1829 size_t dataSize = encoder->getBlockSize();
1830
1831 // create all the uniform blocks
1832 if (interfaceBlock.arraySize > 0)
1833 {
1834 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1835 {
1836 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1837 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1838 mUniformBlocks.push_back(newUniformBlock);
1839 }
1840 }
1841 else
1842 {
1843 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1844 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1845 mUniformBlocks.push_back(newUniformBlock);
1846 }
1847 }
1848
1849 if (interfaceBlock.staticUse)
1850 {
1851 // Assign registers to the uniform blocks
1852 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1853 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1854 ASSERT(blockIndex != GL_INVALID_INDEX);
1855 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1856
1857 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1858
1859 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1860 {
1861 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1862 ASSERT(uniformBlock->name == interfaceBlock.name);
1863
1864 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1865 interfaceBlockRegister + uniformBlockElement, caps))
1866 {
1867 return false;
1868 }
1869 }
1870 }
1871
1872 return true;
1873}
1874
1875bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001876 GLenum samplerType,
1877 unsigned int samplerCount,
1878 std::vector<Sampler> &outSamplers,
1879 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001880{
1881 unsigned int samplerIndex = startSamplerIndex;
1882
1883 do
1884 {
1885 if (samplerIndex < outSamplers.size())
1886 {
1887 Sampler& sampler = outSamplers[samplerIndex];
1888 sampler.active = true;
1889 sampler.textureType = GetTextureType(samplerType);
1890 sampler.logicalTextureUnit = 0;
1891 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1892 }
1893 else
1894 {
1895 return false;
1896 }
1897
1898 samplerIndex++;
1899 } while (samplerIndex < startSamplerIndex + samplerCount);
1900
1901 return true;
1902}
1903
1904bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1905{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001906 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001907 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1908
1909 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1910 {
1911 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1912 &mUsedVertexSamplerRange))
1913 {
Jamie Madillf6113162015-05-07 11:49:21 -04001914 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1915 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001916 return false;
1917 }
1918
1919 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1920 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1921 {
Jamie Madillf6113162015-05-07 11:49:21 -04001922 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1923 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001924 return false;
1925 }
1926 }
1927
1928 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1929 {
1930 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1931 &mUsedPixelSamplerRange))
1932 {
Jamie Madillf6113162015-05-07 11:49:21 -04001933 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
1934 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001935 return false;
1936 }
1937
1938 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1939 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1940 {
Jamie Madillf6113162015-05-07 11:49:21 -04001941 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
1942 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943 return false;
1944 }
1945 }
1946
1947 return true;
1948}
1949
1950bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1951{
1952 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1953 {
1954 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1955
Geoff Lang2ec386b2014-12-03 14:44:38 -05001956 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001957 {
1958 if (!indexSamplerUniform(uniform, infoLog, caps))
1959 {
1960 return false;
1961 }
1962 }
1963
Jamie Madill55def582015-05-04 11:24:57 -04001964 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001965 {
Jamie Madill55def582015-05-04 11:24:57 -04001966 if (!uniform.isBuiltIn())
1967 {
1968 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayIndex, uniformIndex));
1969 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001970 }
1971 }
1972
1973 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001974}
1975
Brandon Jonesc9610c52014-08-25 17:02:59 -07001976void ProgramD3D::reset()
1977{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001978 ProgramImpl::reset();
1979
Brandon Joneseb994362014-09-24 10:27:28 -07001980 SafeDeleteContainer(mVertexExecutables);
1981 SafeDeleteContainer(mPixelExecutables);
1982 SafeDelete(mGeometryExecutable);
1983
1984 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001985
Brandon Jones22502d52014-08-29 16:58:36 -07001986 mVertexHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301987 mVertexWorkarounds.reset();
Brandon Jones44151a92014-09-10 11:32:25 -07001988 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001989
1990 mPixelHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301991 mPixelWorkarounds.reset();
Brandon Jones22502d52014-08-29 16:58:36 -07001992 mUsesFragDepth = false;
1993 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001994 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001995
Brandon Jonesc9610c52014-08-25 17:02:59 -07001996 SafeDelete(mVertexUniformStorage);
1997 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001998
1999 mSamplersPS.clear();
2000 mSamplersVS.clear();
2001
2002 mUsedVertexSamplerRange = 0;
2003 mUsedPixelSamplerRange = 0;
2004 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002005
2006 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002007}
2008
Geoff Lang7dd2e102014-11-10 15:19:26 -05002009unsigned int ProgramD3D::getSerial() const
2010{
2011 return mSerial;
2012}
2013
2014unsigned int ProgramD3D::issueSerial()
2015{
2016 return mCurrentSerial++;
2017}
2018
Jamie Madill437d2662014-12-05 14:23:35 -05002019void ProgramD3D::initAttributesByLayout()
2020{
2021 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2022 {
2023 mAttributesByLayout[i] = i;
2024 }
2025
2026 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2027}
2028
Jamie Madill476682e2015-06-30 10:04:29 -04002029void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002030 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2031 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002032{
Jamie Madill476682e2015-06-30 10:04:29 -04002033 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002034 {
Jamie Madill476682e2015-06-30 10:04:29 -04002035 int oldIndex = mAttributesByLayout[attribIndex];
2036 sortedSemanticIndicesOut[attribIndex] = mSemanticIndex[oldIndex];
2037 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002038 }
2039}
2040
Jamie Madilld3dfda22015-07-06 08:28:49 -04002041void ProgramD3D::updateCachedInputLayout(const gl::Program *program, const gl::State &state)
2042{
2043 mCachedInputLayout.resize(gl::MAX_VERTEX_ATTRIBS, gl::VERTEX_FORMAT_INVALID);
2044 const int *semanticIndexes = program->getSemanticIndexes();
2045
2046 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
2047 for (unsigned int attributeIndex = 0; attributeIndex < vertexAttributes.size(); attributeIndex++)
2048 {
2049 int semanticIndex = semanticIndexes[attributeIndex];
2050
2051 if (semanticIndex != -1)
2052 {
2053 mCachedInputLayout[semanticIndex] =
2054 GetVertexFormatType(vertexAttributes[attributeIndex],
2055 state.getVertexAttribCurrentValue(attributeIndex).Type);
2056 }
2057 }
2058}
2059
Brandon Jonesc9610c52014-08-25 17:02:59 -07002060}