blob: 0059516aca0e031eedc2911fdef265792af0d9d4 [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
Austin Kinross02df7962015-07-01 10:03:42 -07001070 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1071 {
1072 if (fragmentShaderD3D->usesFrontFacing())
1073 {
1074 infoLog << "The current renderer doesn't support gl_FrontFacing";
1075 return LinkResult(false, gl::Error(GL_NO_ERROR));
1076 }
1077 }
1078
Brandon Jones22502d52014-08-29 16:58:36 -07001079 // Map the varyings to the register file
Daniel Chengf33ab832015-06-30 19:08:16 -07001080 VaryingPacking packing = {};
Brandon Jones22502d52014-08-29 16:58:36 -07001081 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1082
Geoff Langbdee2d52014-09-17 11:02:51 -04001083 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001084 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001085 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001086 }
1087
Geoff Lang7dd2e102014-11-10 15:19:26 -05001088 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001089 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001091 }
1092
Jamie Madillde8892b2014-11-11 13:00:22 -05001093 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001094 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1095 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1096 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001098 }
1099
Brandon Jones44151a92014-09-10 11:32:25 -07001100 mUsesPointSize = vertexShaderD3D->usesPointSize();
1101
Jamie Madill437d2662014-12-05 14:23:35 -05001102 initAttributesByLayout();
1103
Geoff Lang7dd2e102014-11-10 15:19:26 -05001104 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001105}
1106
Geoff Lang0ca53782015-05-07 13:49:39 -04001107void ProgramD3D::bindAttributeLocation(GLuint index, const std::string &name)
1108{
1109}
1110
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001111void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001112{
1113 // Compute total default block size
1114 unsigned int vertexRegisters = 0;
1115 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001116 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001117 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001118 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001119
Geoff Lang2ec386b2014-12-03 14:44:38 -05001120 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001121 {
1122 if (uniform.isReferencedByVertexShader())
1123 {
1124 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1125 }
1126 if (uniform.isReferencedByFragmentShader())
1127 {
1128 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1129 }
1130 }
1131 }
1132
1133 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1134 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1135}
1136
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001137gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001138{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001139 updateSamplerMapping();
1140
1141 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1142 if (error.isError())
1143 {
1144 return error;
1145 }
1146
1147 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1148 {
1149 mUniforms[uniformIndex]->dirty = false;
1150 }
1151
1152 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001153}
1154
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001155gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001156{
Jamie Madill03260fa2015-06-22 13:57:22 -04001157 mVertexUBOCache.clear();
1158 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001159
1160 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1161 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1162
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001163 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001164 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001165 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001166 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001167
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001168 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001169
1170 // Unnecessary to apply an unreferenced standard or shared UBO
1171 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1172 {
1173 continue;
1174 }
1175
1176 if (uniformBlock->isReferencedByVertexShader())
1177 {
1178 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001179 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001180
1181 if (mFragmentUBOCache.size() <= registerIndex)
1182 {
1183 mVertexUBOCache.resize(registerIndex + 1, -1);
1184 }
1185
1186 ASSERT(mVertexUBOCache[registerIndex] == -1);
1187 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001188 }
1189
1190 if (uniformBlock->isReferencedByFragmentShader())
1191 {
1192 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001193 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001194
1195 if (mFragmentUBOCache.size() <= registerIndex)
1196 {
1197 mFragmentUBOCache.resize(registerIndex + 1, -1);
1198 }
1199
1200 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1201 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001202 }
1203 }
1204
Jamie Madill03260fa2015-06-22 13:57:22 -04001205 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001206}
1207
1208bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1209 unsigned int registerIndex, const gl::Caps &caps)
1210{
1211 if (shader == GL_VERTEX_SHADER)
1212 {
1213 uniformBlock->vsRegisterIndex = registerIndex;
1214 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1215 {
Jamie Madillf6113162015-05-07 11:49:21 -04001216 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (" << caps.maxVertexUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001217 return false;
1218 }
1219 }
1220 else if (shader == GL_FRAGMENT_SHADER)
1221 {
1222 uniformBlock->psRegisterIndex = registerIndex;
1223 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1224 {
Jamie Madillf6113162015-05-07 11:49:21 -04001225 infoLog << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (" << caps.maxFragmentUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001226 return false;
1227 }
1228 }
1229 else UNREACHABLE();
1230
1231 return true;
1232}
1233
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001234void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001235{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001236 unsigned int numUniforms = mUniforms.size();
1237 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001238 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001239 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001240 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001241}
1242
1243void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1244{
1245 setUniform(location, count, v, GL_FLOAT);
1246}
1247
1248void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1249{
1250 setUniform(location, count, v, GL_FLOAT_VEC2);
1251}
1252
1253void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1254{
1255 setUniform(location, count, v, GL_FLOAT_VEC3);
1256}
1257
1258void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1259{
1260 setUniform(location, count, v, GL_FLOAT_VEC4);
1261}
1262
1263void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1264{
1265 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1266}
1267
1268void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1269{
1270 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1271}
1272
1273void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1274{
1275 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1276}
1277
1278void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1279{
1280 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1281}
1282
1283void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1284{
1285 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1286}
1287
1288void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1289{
1290 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1291}
1292
1293void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1294{
1295 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1296}
1297
1298void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1299{
1300 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1301}
1302
1303void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1304{
1305 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1306}
1307
1308void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1309{
1310 setUniform(location, count, v, GL_INT);
1311}
1312
1313void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1314{
1315 setUniform(location, count, v, GL_INT_VEC2);
1316}
1317
1318void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1319{
1320 setUniform(location, count, v, GL_INT_VEC3);
1321}
1322
1323void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1324{
1325 setUniform(location, count, v, GL_INT_VEC4);
1326}
1327
1328void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1329{
1330 setUniform(location, count, v, GL_UNSIGNED_INT);
1331}
1332
1333void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1334{
1335 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1336}
1337
1338void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1339{
1340 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1341}
1342
1343void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1344{
1345 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1346}
1347
1348void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1349{
1350 getUniformv(location, params, GL_FLOAT);
1351}
1352
1353void ProgramD3D::getUniformiv(GLint location, GLint *params)
1354{
1355 getUniformv(location, params, GL_INT);
1356}
1357
1358void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1359{
1360 getUniformv(location, params, GL_UNSIGNED_INT);
1361}
1362
1363bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1364 const gl::Caps &caps)
1365{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001366 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1367 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001368
1369 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1370 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1371
1372 // Check that uniforms defined in the vertex and fragment shaders are identical
1373 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1374 UniformMap linkedUniforms;
1375
1376 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001377 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001378 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1379 linkedUniforms[vertexUniform.name] = &vertexUniform;
1380 }
1381
1382 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1383 {
1384 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1385 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1386 if (entry != linkedUniforms.end())
1387 {
1388 const sh::Uniform &vertexUniform = *entry->second;
1389 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001391 {
1392 return false;
1393 }
1394 }
1395 }
1396
1397 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1398 {
1399 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1400
1401 if (uniform.staticUse)
1402 {
Jamie Madill55def582015-05-04 11:24:57 -04001403 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1404 vertexShaderD3D->getUniformRegister(uniform.name);
1405 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001406 }
1407 }
1408
1409 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1410 {
1411 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1412
1413 if (uniform.staticUse)
1414 {
Jamie Madill55def582015-05-04 11:24:57 -04001415 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1416 fragmentShaderD3D->getUniformRegister(uniform.name);
1417 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001418 }
1419 }
1420
1421 if (!indexUniforms(infoLog, caps))
1422 {
1423 return false;
1424 }
1425
1426 initializeUniformStorage();
1427
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001428 return true;
1429}
1430
Geoff Lang492a7e42014-11-05 13:27:06 -05001431void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001432{
Jamie Madill55def582015-05-04 11:24:57 -04001433 if (uniformRegister == GL_INVALID_INDEX)
1434 {
1435 defineUniform(shader, uniform, uniform.name, nullptr);
1436 return;
1437 }
1438
Geoff Lang492a7e42014-11-05 13:27:06 -05001439 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001440 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1441 encoder.skipRegisters(uniformRegister);
1442
1443 defineUniform(shader, uniform, uniform.name, &encoder);
1444}
1445
Geoff Lang492a7e42014-11-05 13:27:06 -05001446void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1447 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001448{
1449 if (uniform.isStruct())
1450 {
1451 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1452 {
1453 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1454
Jamie Madill55def582015-05-04 11:24:57 -04001455 if (encoder)
1456 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001457
1458 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1459 {
1460 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1461 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1462
1463 defineUniform(shader, field, fieldFullName, encoder);
1464 }
1465
Jamie Madill55def582015-05-04 11:24:57 -04001466 if (encoder)
1467 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001468 }
1469 }
1470 else // Not a struct
1471 {
1472 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001473 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001474 {
1475 encoder->enterAggregateType();
1476 }
1477
1478 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1479
Jamie Madill2857f482015-02-09 15:35:29 -05001480 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001481 sh::BlockMemberInfo blockInfo = encoder ?
1482 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1483 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001484
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001485 if (!linkedUniform)
1486 {
1487 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001488 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001489 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001490
1491 if (encoder)
1492 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001493 mUniforms.push_back(linkedUniform);
1494 }
1495
Jamie Madill55def582015-05-04 11:24:57 -04001496 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001497 {
Jamie Madill55def582015-05-04 11:24:57 -04001498 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1499 {
1500 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1501 }
1502 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1503 {
1504 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1505 }
1506 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001507 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001508
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001509 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001510 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001511 {
1512 encoder->exitAggregateType();
1513 }
1514 }
1515}
1516
1517template <typename T>
1518static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1519{
1520 ASSERT(dest != NULL);
1521 ASSERT(dirtyFlag != NULL);
1522
1523 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1524 *dest = source;
1525}
1526
1527template <typename T>
1528void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1529{
1530 const int components = gl::VariableComponentCount(targetUniformType);
1531 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1532
1533 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1534
1535 int elementCount = targetUniform->elementCount();
1536
1537 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1538
1539 if (targetUniform->type == targetUniformType)
1540 {
1541 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1542
1543 for (int i = 0; i < count; i++)
1544 {
1545 T *dest = target + (i * 4);
1546 const T *source = v + (i * components);
1547
1548 for (int c = 0; c < components; c++)
1549 {
1550 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1551 }
1552 for (int c = components; c < 4; c++)
1553 {
1554 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1555 }
1556 }
1557 }
1558 else if (targetUniform->type == targetBoolType)
1559 {
1560 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1561
1562 for (int i = 0; i < count; i++)
1563 {
1564 GLint *dest = boolParams + (i * 4);
1565 const T *source = v + (i * components);
1566
1567 for (int c = 0; c < components; c++)
1568 {
1569 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1570 }
1571 for (int c = components; c < 4; c++)
1572 {
1573 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1574 }
1575 }
1576 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001577 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001578 {
1579 ASSERT(targetUniformType == GL_INT);
1580
1581 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1582
1583 bool wasDirty = targetUniform->dirty;
1584
1585 for (int i = 0; i < count; i++)
1586 {
1587 GLint *dest = target + (i * 4);
1588 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1589
1590 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1591 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1592 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1593 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1594 }
1595
1596 if (!wasDirty && targetUniform->dirty)
1597 {
1598 mDirtySamplerMapping = true;
1599 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001600 }
1601 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001602}
Brandon Jones18bd4102014-09-22 14:21:44 -07001603
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001604template<typename T>
1605bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1606{
1607 bool dirty = false;
1608 int copyWidth = std::min(targetHeight, srcWidth);
1609 int copyHeight = std::min(targetWidth, srcHeight);
1610
1611 for (int x = 0; x < copyWidth; x++)
1612 {
1613 for (int y = 0; y < copyHeight; y++)
1614 {
1615 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1616 }
1617 }
1618 // clear unfilled right side
1619 for (int y = 0; y < copyWidth; y++)
1620 {
1621 for (int x = copyHeight; x < targetWidth; x++)
1622 {
1623 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1624 }
1625 }
1626 // clear unfilled bottom.
1627 for (int y = copyWidth; y < targetHeight; y++)
1628 {
1629 for (int x = 0; x < targetWidth; x++)
1630 {
1631 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1632 }
1633 }
1634
1635 return dirty;
1636}
1637
1638template<typename T>
1639bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1640{
1641 bool dirty = false;
1642 int copyWidth = std::min(targetWidth, srcWidth);
1643 int copyHeight = std::min(targetHeight, srcHeight);
1644
1645 for (int y = 0; y < copyHeight; y++)
1646 {
1647 for (int x = 0; x < copyWidth; x++)
1648 {
1649 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1650 }
1651 }
1652 // clear unfilled right side
1653 for (int y = 0; y < copyHeight; y++)
1654 {
1655 for (int x = copyWidth; x < targetWidth; x++)
1656 {
1657 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1658 }
1659 }
1660 // clear unfilled bottom.
1661 for (int y = copyHeight; y < targetHeight; y++)
1662 {
1663 for (int x = 0; x < targetWidth; x++)
1664 {
1665 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1666 }
1667 }
1668
1669 return dirty;
1670}
1671
1672template <int cols, int rows>
1673void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1674{
1675 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1676
1677 int elementCount = targetUniform->elementCount();
1678
1679 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1680 const unsigned int targetMatrixStride = (4 * rows);
1681 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1682
1683 for (int i = 0; i < count; i++)
1684 {
1685 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1686 if (transpose == GL_FALSE)
1687 {
1688 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1689 }
1690 else
1691 {
1692 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1693 }
1694 target += targetMatrixStride;
1695 value += cols * rows;
1696 }
1697}
1698
1699template <typename T>
1700void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1701{
1702 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1703
1704 if (gl::IsMatrixType(targetUniform->type))
1705 {
1706 const int rows = gl::VariableRowCount(targetUniform->type);
1707 const int cols = gl::VariableColumnCount(targetUniform->type);
1708 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1709 }
1710 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1711 {
1712 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1713 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1714 size * sizeof(T));
1715 }
1716 else
1717 {
1718 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1719 switch (gl::VariableComponentType(targetUniform->type))
1720 {
1721 case GL_BOOL:
1722 {
1723 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1724
1725 for (unsigned int i = 0; i < size; i++)
1726 {
1727 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1728 }
1729 }
1730 break;
1731
1732 case GL_FLOAT:
1733 {
1734 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1735
1736 for (unsigned int i = 0; i < size; i++)
1737 {
1738 params[i] = static_cast<T>(floatParams[i]);
1739 }
1740 }
1741 break;
1742
1743 case GL_INT:
1744 {
1745 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1746
1747 for (unsigned int i = 0; i < size; i++)
1748 {
1749 params[i] = static_cast<T>(intParams[i]);
1750 }
1751 }
1752 break;
1753
1754 case GL_UNSIGNED_INT:
1755 {
1756 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1757
1758 for (unsigned int i = 0; i < size; i++)
1759 {
1760 params[i] = static_cast<T>(uintParams[i]);
1761 }
1762 }
1763 break;
1764
1765 default: UNREACHABLE();
1766 }
1767 }
1768}
1769
1770template <typename VarT>
1771void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1772 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1773 bool inRowMajorLayout)
1774{
1775 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1776 {
1777 const VarT &field = fields[uniformIndex];
1778 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1779
1780 if (field.isStruct())
1781 {
1782 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1783
1784 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1785 {
1786 encoder->enterAggregateType();
1787
1788 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1789 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1790
1791 encoder->exitAggregateType();
1792 }
1793 }
1794 else
1795 {
1796 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1797
1798 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1799
1800 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1801 blockIndex, memberInfo);
1802
1803 // add to uniform list, but not index, since uniform block uniforms have no location
1804 blockUniformIndexes->push_back(mUniforms.size());
1805 mUniforms.push_back(newUniform);
1806 }
1807 }
1808}
1809
Jamie Madilld3dfda22015-07-06 08:28:49 -04001810bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog,
1811 const gl::Shader &shader,
1812 const sh::InterfaceBlock &interfaceBlock,
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001813 const gl::Caps &caps)
1814{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001815 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001816
1817 // create uniform block entries if they do not exist
1818 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1819 {
1820 std::vector<unsigned int> blockUniformIndexes;
1821 const unsigned int blockIndex = mUniformBlocks.size();
1822
1823 // define member uniforms
1824 sh::BlockLayoutEncoder *encoder = NULL;
1825
1826 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1827 {
1828 encoder = new sh::Std140BlockEncoder;
1829 }
1830 else
1831 {
1832 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1833 }
1834 ASSERT(encoder);
1835
1836 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1837
1838 size_t dataSize = encoder->getBlockSize();
1839
1840 // create all the uniform blocks
1841 if (interfaceBlock.arraySize > 0)
1842 {
1843 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1844 {
1845 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1846 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1847 mUniformBlocks.push_back(newUniformBlock);
1848 }
1849 }
1850 else
1851 {
1852 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1853 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1854 mUniformBlocks.push_back(newUniformBlock);
1855 }
1856 }
1857
1858 if (interfaceBlock.staticUse)
1859 {
1860 // Assign registers to the uniform blocks
1861 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1862 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1863 ASSERT(blockIndex != GL_INVALID_INDEX);
1864 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1865
1866 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1867
1868 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1869 {
1870 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1871 ASSERT(uniformBlock->name == interfaceBlock.name);
1872
1873 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1874 interfaceBlockRegister + uniformBlockElement, caps))
1875 {
1876 return false;
1877 }
1878 }
1879 }
1880
1881 return true;
1882}
1883
1884bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001885 GLenum samplerType,
1886 unsigned int samplerCount,
1887 std::vector<Sampler> &outSamplers,
1888 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001889{
1890 unsigned int samplerIndex = startSamplerIndex;
1891
1892 do
1893 {
1894 if (samplerIndex < outSamplers.size())
1895 {
1896 Sampler& sampler = outSamplers[samplerIndex];
1897 sampler.active = true;
1898 sampler.textureType = GetTextureType(samplerType);
1899 sampler.logicalTextureUnit = 0;
1900 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1901 }
1902 else
1903 {
1904 return false;
1905 }
1906
1907 samplerIndex++;
1908 } while (samplerIndex < startSamplerIndex + samplerCount);
1909
1910 return true;
1911}
1912
1913bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1914{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001915 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001916 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1917
1918 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1919 {
1920 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1921 &mUsedVertexSamplerRange))
1922 {
Jamie Madillf6113162015-05-07 11:49:21 -04001923 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1924 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001925 return false;
1926 }
1927
1928 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1929 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1930 {
Jamie Madillf6113162015-05-07 11:49:21 -04001931 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1932 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001933 return false;
1934 }
1935 }
1936
1937 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1938 {
1939 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1940 &mUsedPixelSamplerRange))
1941 {
Jamie Madillf6113162015-05-07 11:49:21 -04001942 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
1943 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001944 return false;
1945 }
1946
1947 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1948 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1949 {
Jamie Madillf6113162015-05-07 11:49:21 -04001950 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
1951 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001952 return false;
1953 }
1954 }
1955
1956 return true;
1957}
1958
1959bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1960{
1961 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1962 {
1963 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1964
Geoff Lang2ec386b2014-12-03 14:44:38 -05001965 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001966 {
1967 if (!indexSamplerUniform(uniform, infoLog, caps))
1968 {
1969 return false;
1970 }
1971 }
1972
Jamie Madill55def582015-05-04 11:24:57 -04001973 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001974 {
Jamie Madill55def582015-05-04 11:24:57 -04001975 if (!uniform.isBuiltIn())
1976 {
1977 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayIndex, uniformIndex));
1978 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001979 }
1980 }
1981
1982 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001983}
1984
Brandon Jonesc9610c52014-08-25 17:02:59 -07001985void ProgramD3D::reset()
1986{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001987 ProgramImpl::reset();
1988
Brandon Joneseb994362014-09-24 10:27:28 -07001989 SafeDeleteContainer(mVertexExecutables);
1990 SafeDeleteContainer(mPixelExecutables);
1991 SafeDelete(mGeometryExecutable);
1992
1993 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001994
Brandon Jones22502d52014-08-29 16:58:36 -07001995 mVertexHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301996 mVertexWorkarounds.reset();
Brandon Jones44151a92014-09-10 11:32:25 -07001997 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001998
1999 mPixelHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05302000 mPixelWorkarounds.reset();
Brandon Jones22502d52014-08-29 16:58:36 -07002001 mUsesFragDepth = false;
2002 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002003 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002004
Brandon Jonesc9610c52014-08-25 17:02:59 -07002005 SafeDelete(mVertexUniformStorage);
2006 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002007
2008 mSamplersPS.clear();
2009 mSamplersVS.clear();
2010
2011 mUsedVertexSamplerRange = 0;
2012 mUsedPixelSamplerRange = 0;
2013 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002014
2015 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002016}
2017
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018unsigned int ProgramD3D::getSerial() const
2019{
2020 return mSerial;
2021}
2022
2023unsigned int ProgramD3D::issueSerial()
2024{
2025 return mCurrentSerial++;
2026}
2027
Jamie Madill437d2662014-12-05 14:23:35 -05002028void ProgramD3D::initAttributesByLayout()
2029{
2030 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2031 {
2032 mAttributesByLayout[i] = i;
2033 }
2034
2035 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2036}
2037
Jamie Madill476682e2015-06-30 10:04:29 -04002038void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002039 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2040 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002041{
Jamie Madill476682e2015-06-30 10:04:29 -04002042 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002043 {
Jamie Madill476682e2015-06-30 10:04:29 -04002044 int oldIndex = mAttributesByLayout[attribIndex];
2045 sortedSemanticIndicesOut[attribIndex] = mSemanticIndex[oldIndex];
2046 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002047 }
2048}
2049
Jamie Madilld3dfda22015-07-06 08:28:49 -04002050void ProgramD3D::updateCachedInputLayout(const gl::Program *program, const gl::State &state)
2051{
2052 mCachedInputLayout.resize(gl::MAX_VERTEX_ATTRIBS, gl::VERTEX_FORMAT_INVALID);
2053 const int *semanticIndexes = program->getSemanticIndexes();
2054
2055 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
2056 for (unsigned int attributeIndex = 0; attributeIndex < vertexAttributes.size(); attributeIndex++)
2057 {
2058 int semanticIndex = semanticIndexes[attributeIndex];
2059
2060 if (semanticIndex != -1)
2061 {
2062 mCachedInputLayout[semanticIndex] =
2063 GetVertexFormatType(vertexAttributes[attributeIndex],
2064 state.getVertexAttribCurrentValue(attributeIndex).Type);
2065 }
2066 }
2067}
2068
Brandon Jonesc9610c52014-08-25 17:02:59 -07002069}