blob: 7c8520a2efe4cd8a39f9e612b37f9c16f132c202 [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
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700532 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
533 {
Geoff Lang95137842015-06-02 15:38:43 -0400534 GLuint location;
535 stream->readInt(&location);
536
537 gl::VariableLocation variable;
538 stream->readString(&variable.name);
539 stream->readInt(&variable.element);
540 stream->readInt(&variable.index);
541
542 mUniformIndex[location] = variable;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700543 }
544
545 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
546 if (stream->error())
547 {
Jamie Madillf6113162015-05-07 11:49:21 -0400548 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500549 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700550 }
551
552 mUniformBlocks.resize(uniformBlockCount);
553 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
554 {
555 std::string name = stream->readString();
556 unsigned int elementIndex = stream->readInt<unsigned int>();
557 unsigned int dataSize = stream->readInt<unsigned int>();
558
559 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
560
561 stream->readInt(&uniformBlock->psRegisterIndex);
562 stream->readInt(&uniformBlock->vsRegisterIndex);
563
564 unsigned int numMembers = stream->readInt<unsigned int>();
565 uniformBlock->memberUniformIndexes.resize(numMembers);
566 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
567 {
568 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
569 }
570
571 mUniformBlocks[uniformBlockIndex] = uniformBlock;
572 }
573
Brandon Joneseb994362014-09-24 10:27:28 -0700574 stream->readInt(&mTransformFeedbackBufferMode);
575 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
576 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
577 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
578 {
579 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
580
581 stream->readString(&varying.name);
582 stream->readInt(&varying.type);
583 stream->readInt(&varying.size);
584 stream->readString(&varying.semanticName);
585 stream->readInt(&varying.semanticIndex);
586 stream->readInt(&varying.semanticIndexCount);
587 }
588
Brandon Jones22502d52014-08-29 16:58:36 -0700589 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530590 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700591 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530592 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700593 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700594 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700595
596 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
597 mPixelShaderKey.resize(pixelShaderKeySize);
598 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
599 {
600 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
601 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
602 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
603 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
604 }
605
Brandon Joneseb994362014-09-24 10:27:28 -0700606 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
607
608 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
609 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
610 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400611 size_t inputLayoutSize = stream->readInt<size_t>();
612 gl::InputLayout inputLayout;
Brandon Joneseb994362014-09-24 10:27:28 -0700613
Jamie Madilld3dfda22015-07-06 08:28:49 -0400614 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700615 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400616 inputLayout.push_back(stream->readInt<gl::VertexFormatType>());
Brandon Joneseb994362014-09-24 10:27:28 -0700617 }
618
619 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
620 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400621
Geoff Lang359ef262015-01-05 14:42:29 -0500622 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400623 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
624 SHADER_VERTEX,
625 mTransformFeedbackLinkedVaryings,
626 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
627 &shaderExecutable);
628 if (error.isError())
629 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500630 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400631 }
632
Brandon Joneseb994362014-09-24 10:27:28 -0700633 if (!shaderExecutable)
634 {
Jamie Madillf6113162015-05-07 11:49:21 -0400635 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500636 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700637 }
638
639 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400640 VertexExecutable::Signature signature;
641 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700642
643 // add new binary
644 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
645
646 stream->skip(vertexShaderSize);
647 }
648
649 const size_t pixelShaderCount = stream->readInt<unsigned int>();
650 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
651 {
652 const size_t outputCount = stream->readInt<unsigned int>();
653 std::vector<GLenum> outputs(outputCount);
654 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
655 {
656 stream->readInt(&outputs[outputIndex]);
657 }
658
659 const size_t pixelShaderSize = stream->readInt<unsigned int>();
660 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500661 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400662 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
663 mTransformFeedbackLinkedVaryings,
664 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
665 &shaderExecutable);
666 if (error.isError())
667 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500668 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400669 }
Brandon Joneseb994362014-09-24 10:27:28 -0700670
671 if (!shaderExecutable)
672 {
Jamie Madillf6113162015-05-07 11:49:21 -0400673 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500674 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700675 }
676
677 // add new binary
678 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
679
680 stream->skip(pixelShaderSize);
681 }
682
683 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
684
685 if (geometryShaderSize > 0)
686 {
687 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400688 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
689 mTransformFeedbackLinkedVaryings,
690 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
691 &mGeometryExecutable);
692 if (error.isError())
693 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500694 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400695 }
Brandon Joneseb994362014-09-24 10:27:28 -0700696
697 if (!mGeometryExecutable)
698 {
Jamie Madillf6113162015-05-07 11:49:21 -0400699 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500700 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700701 }
702 stream->skip(geometryShaderSize);
703 }
704
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700705 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500706 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700707
Geoff Lang7dd2e102014-11-10 15:19:26 -0500708 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700709}
710
Geoff Langb543aff2014-09-30 14:52:54 -0400711gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700712{
Austin Kinross137b1512015-06-17 16:14:53 -0700713 // Output the DeviceIdentifier before we output any shader code
714 // When we load the binary again later, we can validate the device identifier before trying to compile any HLSL
715 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
716 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(DeviceIdentifier));
717
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500718 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
719
Brandon Jones44151a92014-09-10 11:32:25 -0700720 stream->writeInt(mShaderVersion);
721
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700722 stream->writeInt(mSamplersPS.size());
723 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
724 {
725 stream->writeInt(mSamplersPS[i].active);
726 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
727 stream->writeInt(mSamplersPS[i].textureType);
728 }
729
730 stream->writeInt(mSamplersVS.size());
731 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
732 {
733 stream->writeInt(mSamplersVS[i].active);
734 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
735 stream->writeInt(mSamplersVS[i].textureType);
736 }
737
738 stream->writeInt(mUsedVertexSamplerRange);
739 stream->writeInt(mUsedPixelSamplerRange);
740
741 stream->writeInt(mUniforms.size());
742 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
743 {
744 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
745
746 stream->writeInt(uniform.type);
747 stream->writeInt(uniform.precision);
748 stream->writeString(uniform.name);
749 stream->writeInt(uniform.arraySize);
750 stream->writeInt(uniform.blockIndex);
751
752 stream->writeInt(uniform.blockInfo.offset);
753 stream->writeInt(uniform.blockInfo.arrayStride);
754 stream->writeInt(uniform.blockInfo.matrixStride);
755 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
756
757 stream->writeInt(uniform.psRegisterIndex);
758 stream->writeInt(uniform.vsRegisterIndex);
759 stream->writeInt(uniform.registerCount);
760 stream->writeInt(uniform.registerElement);
761 }
762
763 stream->writeInt(mUniformIndex.size());
Geoff Lang95137842015-06-02 15:38:43 -0400764 for (const auto &uniform : mUniformIndex)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700765 {
Geoff Lang95137842015-06-02 15:38:43 -0400766 GLuint location = uniform.first;
767 stream->writeInt(location);
768
769 const gl::VariableLocation &variable = uniform.second;
770 stream->writeString(variable.name);
771 stream->writeInt(variable.element);
772 stream->writeInt(variable.index);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700773 }
774
775 stream->writeInt(mUniformBlocks.size());
776 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
777 {
778 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
779
780 stream->writeString(uniformBlock.name);
781 stream->writeInt(uniformBlock.elementIndex);
782 stream->writeInt(uniformBlock.dataSize);
783
784 stream->writeInt(uniformBlock.memberUniformIndexes.size());
785 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
786 {
787 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
788 }
789
790 stream->writeInt(uniformBlock.psRegisterIndex);
791 stream->writeInt(uniformBlock.vsRegisterIndex);
792 }
793
Brandon Joneseb994362014-09-24 10:27:28 -0700794 stream->writeInt(mTransformFeedbackBufferMode);
795 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
796 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
797 {
798 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
799
800 stream->writeString(varying.name);
801 stream->writeInt(varying.type);
802 stream->writeInt(varying.size);
803 stream->writeString(varying.semanticName);
804 stream->writeInt(varying.semanticIndex);
805 stream->writeInt(varying.semanticIndexCount);
806 }
807
Brandon Jones22502d52014-08-29 16:58:36 -0700808 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530809 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700810 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530811 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700812 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700813 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700814
Brandon Joneseb994362014-09-24 10:27:28 -0700815 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700816 stream->writeInt(pixelShaderKey.size());
817 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
818 {
Brandon Joneseb994362014-09-24 10:27:28 -0700819 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700820 stream->writeInt(variable.type);
821 stream->writeString(variable.name);
822 stream->writeString(variable.source);
823 stream->writeInt(variable.outputIndex);
824 }
825
Brandon Joneseb994362014-09-24 10:27:28 -0700826 stream->writeInt(mVertexExecutables.size());
827 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
828 {
829 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
830
Jamie Madilld3dfda22015-07-06 08:28:49 -0400831 const auto &inputLayout = vertexExecutable->inputs();
832 stream->writeInt(inputLayout.size());
833
834 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700835 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400836 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -0700837 }
838
839 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
840 stream->writeInt(vertexShaderSize);
841
842 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
843 stream->writeBytes(vertexBlob, vertexShaderSize);
844 }
845
846 stream->writeInt(mPixelExecutables.size());
847 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
848 {
849 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
850
851 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
852 stream->writeInt(outputs.size());
853 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
854 {
855 stream->writeInt(outputs[outputIndex]);
856 }
857
858 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
859 stream->writeInt(pixelShaderSize);
860
861 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
862 stream->writeBytes(pixelBlob, pixelShaderSize);
863 }
864
865 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
866 stream->writeInt(geometryShaderSize);
867
868 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
869 {
870 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
871 stream->writeBytes(geometryBlob, geometryShaderSize);
872 }
873
Geoff Langb543aff2014-09-30 14:52:54 -0400874 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700875}
876
Geoff Lang359ef262015-01-05 14:42:29 -0500877gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700878{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400879 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700880
Jamie Madill85a18042015-03-05 15:41:41 -0500881 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
882 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700883
884 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
885 {
886 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
887
888 if (colorbuffer)
889 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400890 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700891 }
892 else
893 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400894 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700895 }
896 }
897
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400898 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700899}
900
Jamie Madill97399232014-12-23 12:31:15 -0500901gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500902 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500903 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700904{
905 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
906 {
907 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
908 {
Geoff Langb543aff2014-09-30 14:52:54 -0400909 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
910 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700911 }
912 }
913
Brandon Jones22502d52014-08-29 16:58:36 -0700914 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
915 outputSignature);
916
917 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500918 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500919
920 gl::InfoLog tempInfoLog;
921 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
922
923 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400924 mTransformFeedbackLinkedVaryings,
925 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
926 mPixelWorkarounds, &pixelExecutable);
927 if (error.isError())
928 {
929 return error;
930 }
Brandon Joneseb994362014-09-24 10:27:28 -0700931
Jamie Madill97399232014-12-23 12:31:15 -0500932 if (pixelExecutable)
933 {
934 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
935 }
936 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700937 {
938 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
939 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
940 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
941 }
Brandon Jones22502d52014-08-29 16:58:36 -0700942
Geoff Langb543aff2014-09-30 14:52:54 -0400943 *outExectuable = pixelExecutable;
944 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700945}
946
Jamie Madilld3dfda22015-07-06 08:28:49 -0400947gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -0500948 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500949 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700950{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400951 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -0700952
953 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
954 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400955 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -0700956 {
Geoff Langb543aff2014-09-30 14:52:54 -0400957 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
958 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700959 }
960 }
961
Brandon Jones22502d52014-08-29 16:58:36 -0700962 // Generate new dynamic layout with attribute conversions
Jamie Madill3da79b72015-04-27 11:09:17 -0400963 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, getShaderAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -0700964
965 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500966 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500967
968 gl::InfoLog tempInfoLog;
969 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
970
971 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400972 mTransformFeedbackLinkedVaryings,
973 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
974 mVertexWorkarounds, &vertexExecutable);
975 if (error.isError())
976 {
977 return error;
978 }
979
Jamie Madill97399232014-12-23 12:31:15 -0500980 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700981 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400982 mVertexExecutables.push_back(new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700983 }
Jamie Madill97399232014-12-23 12:31:15 -0500984 else if (!infoLog)
985 {
986 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
987 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
988 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
989 }
Brandon Jones22502d52014-08-29 16:58:36 -0700990
Geoff Langb543aff2014-09-30 14:52:54 -0400991 *outExectuable = vertexExecutable;
992 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700993}
994
Geoff Lang7dd2e102014-11-10 15:19:26 -0500995LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
996 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700997{
Jamie Madillf4bf3812015-04-01 16:15:32 -0400998 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
999 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones44151a92014-09-10 11:32:25 -07001000
Jamie Madilld3dfda22015-07-06 08:28:49 -04001001 gl::InputLayout defaultInputLayout;
1002 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), &defaultInputLayout);
Jamie Madille4ea2022015-03-26 20:35:05 +00001003 ShaderExecutableD3D *defaultVertexExecutable = NULL;
1004 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
1005 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001006 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001007 return LinkResult(false, error);
1008 }
Austin Kinross434953e2015-02-20 10:49:51 -08001009
Brandon Joneseb994362014-09-24 10:27:28 -07001010 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001011 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +00001012 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001013 if (error.isError())
1014 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001015 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001016 }
Brandon Jones44151a92014-09-10 11:32:25 -07001017
Brandon Joneseb994362014-09-24 10:27:28 -07001018 if (usesGeometryShader())
1019 {
1020 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001021
Geoff Langb543aff2014-09-30 14:52:54 -04001022
1023 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1024 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +05301025 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001026 if (error.isError())
1027 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001028 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001029 }
Brandon Joneseb994362014-09-24 10:27:28 -07001030 }
1031
Brandon Jones091540d2014-10-29 11:32:04 -07001032#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001033 if (usesGeometryShader() && mGeometryExecutable)
1034 {
1035 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1036 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1037 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1038 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1039 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1040 }
1041
1042 if (defaultVertexExecutable)
1043 {
1044 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1045 }
1046
1047 if (defaultPixelExecutable)
1048 {
1049 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1050 }
1051#endif
1052
Geoff Langb543aff2014-09-30 14:52:54 -04001053 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001054 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001055}
1056
Geoff Lang7dd2e102014-11-10 15:19:26 -05001057LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1058 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1059 const std::vector<std::string> &transformFeedbackVaryings,
1060 GLenum transformFeedbackBufferMode,
1061 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1062 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001063{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001064 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1065 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001066
Jamie Madillde8892b2014-11-11 13:00:22 -05001067 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1068 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001069
Brandon Joneseb994362014-09-24 10:27:28 -07001070 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001071
1072 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301073 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001074
1075 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301076 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001077 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001078
Austin Kinross02df7962015-07-01 10:03:42 -07001079 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1080 {
1081 if (fragmentShaderD3D->usesFrontFacing())
1082 {
1083 infoLog << "The current renderer doesn't support gl_FrontFacing";
1084 return LinkResult(false, gl::Error(GL_NO_ERROR));
1085 }
1086 }
1087
Brandon Jones22502d52014-08-29 16:58:36 -07001088 // Map the varyings to the register file
Daniel Chengf33ab832015-06-30 19:08:16 -07001089 VaryingPacking packing = {};
Brandon Jones22502d52014-08-29 16:58:36 -07001090 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1091
Geoff Langbdee2d52014-09-17 11:02:51 -04001092 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001093 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001094 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001095 }
1096
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001098 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001100 }
1101
Jamie Madillde8892b2014-11-11 13:00:22 -05001102 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001103 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1104 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1105 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001107 }
1108
Brandon Jones44151a92014-09-10 11:32:25 -07001109 mUsesPointSize = vertexShaderD3D->usesPointSize();
1110
Jamie Madill437d2662014-12-05 14:23:35 -05001111 initAttributesByLayout();
1112
Geoff Lang7dd2e102014-11-10 15:19:26 -05001113 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001114}
1115
Geoff Lang0ca53782015-05-07 13:49:39 -04001116void ProgramD3D::bindAttributeLocation(GLuint index, const std::string &name)
1117{
1118}
1119
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001120void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001121{
1122 // Compute total default block size
1123 unsigned int vertexRegisters = 0;
1124 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001125 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001126 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001127 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001128
Geoff Lang2ec386b2014-12-03 14:44:38 -05001129 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001130 {
1131 if (uniform.isReferencedByVertexShader())
1132 {
1133 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1134 }
1135 if (uniform.isReferencedByFragmentShader())
1136 {
1137 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1138 }
1139 }
1140 }
1141
1142 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1143 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1144}
1145
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001146gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001147{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001148 updateSamplerMapping();
1149
1150 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1151 if (error.isError())
1152 {
1153 return error;
1154 }
1155
1156 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1157 {
1158 mUniforms[uniformIndex]->dirty = false;
1159 }
1160
1161 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001162}
1163
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001164gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001165{
Jamie Madill03260fa2015-06-22 13:57:22 -04001166 mVertexUBOCache.clear();
1167 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001168
1169 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1170 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1171
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001172 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001173 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001174 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001175 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001176
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001177 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001178
1179 // Unnecessary to apply an unreferenced standard or shared UBO
1180 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1181 {
1182 continue;
1183 }
1184
1185 if (uniformBlock->isReferencedByVertexShader())
1186 {
1187 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001188 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001189
Jamie Madill969194d2015-07-20 14:36:56 -04001190 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001191 {
1192 mVertexUBOCache.resize(registerIndex + 1, -1);
1193 }
1194
1195 ASSERT(mVertexUBOCache[registerIndex] == -1);
1196 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001197 }
1198
1199 if (uniformBlock->isReferencedByFragmentShader())
1200 {
1201 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001202 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001203
1204 if (mFragmentUBOCache.size() <= registerIndex)
1205 {
1206 mFragmentUBOCache.resize(registerIndex + 1, -1);
1207 }
1208
1209 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1210 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001211 }
1212 }
1213
Jamie Madill03260fa2015-06-22 13:57:22 -04001214 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001215}
1216
1217bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1218 unsigned int registerIndex, const gl::Caps &caps)
1219{
1220 if (shader == GL_VERTEX_SHADER)
1221 {
1222 uniformBlock->vsRegisterIndex = registerIndex;
1223 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1224 {
Jamie Madillf6113162015-05-07 11:49:21 -04001225 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (" << caps.maxVertexUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001226 return false;
1227 }
1228 }
1229 else if (shader == GL_FRAGMENT_SHADER)
1230 {
1231 uniformBlock->psRegisterIndex = registerIndex;
1232 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1233 {
Jamie Madillf6113162015-05-07 11:49:21 -04001234 infoLog << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (" << caps.maxFragmentUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001235 return false;
1236 }
1237 }
1238 else UNREACHABLE();
1239
1240 return true;
1241}
1242
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001243void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001244{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001245 unsigned int numUniforms = mUniforms.size();
1246 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001247 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001248 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001249 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001250}
1251
1252void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1253{
1254 setUniform(location, count, v, GL_FLOAT);
1255}
1256
1257void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1258{
1259 setUniform(location, count, v, GL_FLOAT_VEC2);
1260}
1261
1262void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1263{
1264 setUniform(location, count, v, GL_FLOAT_VEC3);
1265}
1266
1267void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1268{
1269 setUniform(location, count, v, GL_FLOAT_VEC4);
1270}
1271
1272void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1273{
1274 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1275}
1276
1277void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1278{
1279 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1280}
1281
1282void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1283{
1284 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1285}
1286
1287void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1288{
1289 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1290}
1291
1292void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1293{
1294 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1295}
1296
1297void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1298{
1299 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1300}
1301
1302void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1303{
1304 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1305}
1306
1307void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1308{
1309 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1310}
1311
1312void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1313{
1314 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1315}
1316
1317void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1318{
1319 setUniform(location, count, v, GL_INT);
1320}
1321
1322void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1323{
1324 setUniform(location, count, v, GL_INT_VEC2);
1325}
1326
1327void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1328{
1329 setUniform(location, count, v, GL_INT_VEC3);
1330}
1331
1332void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1333{
1334 setUniform(location, count, v, GL_INT_VEC4);
1335}
1336
1337void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1338{
1339 setUniform(location, count, v, GL_UNSIGNED_INT);
1340}
1341
1342void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1343{
1344 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1345}
1346
1347void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1348{
1349 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1350}
1351
1352void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1353{
1354 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1355}
1356
1357void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1358{
1359 getUniformv(location, params, GL_FLOAT);
1360}
1361
1362void ProgramD3D::getUniformiv(GLint location, GLint *params)
1363{
1364 getUniformv(location, params, GL_INT);
1365}
1366
1367void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1368{
1369 getUniformv(location, params, GL_UNSIGNED_INT);
1370}
1371
1372bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1373 const gl::Caps &caps)
1374{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001375 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1376 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001377
1378 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1379 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1380
1381 // Check that uniforms defined in the vertex and fragment shaders are identical
1382 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1383 UniformMap linkedUniforms;
1384
1385 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001386 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001387 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1388 linkedUniforms[vertexUniform.name] = &vertexUniform;
1389 }
1390
1391 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1392 {
1393 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1394 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1395 if (entry != linkedUniforms.end())
1396 {
1397 const sh::Uniform &vertexUniform = *entry->second;
1398 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001399 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001400 {
1401 return false;
1402 }
1403 }
1404 }
1405
1406 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1407 {
1408 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1409
1410 if (uniform.staticUse)
1411 {
Jamie Madill55def582015-05-04 11:24:57 -04001412 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1413 vertexShaderD3D->getUniformRegister(uniform.name);
1414 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001415 }
1416 }
1417
1418 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1419 {
1420 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1421
1422 if (uniform.staticUse)
1423 {
Jamie Madill55def582015-05-04 11:24:57 -04001424 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1425 fragmentShaderD3D->getUniformRegister(uniform.name);
1426 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001427 }
1428 }
1429
1430 if (!indexUniforms(infoLog, caps))
1431 {
1432 return false;
1433 }
1434
1435 initializeUniformStorage();
1436
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001437 return true;
1438}
1439
Geoff Lang492a7e42014-11-05 13:27:06 -05001440void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001441{
Jamie Madill55def582015-05-04 11:24:57 -04001442 if (uniformRegister == GL_INVALID_INDEX)
1443 {
1444 defineUniform(shader, uniform, uniform.name, nullptr);
1445 return;
1446 }
1447
Geoff Lang492a7e42014-11-05 13:27:06 -05001448 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001449 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1450 encoder.skipRegisters(uniformRegister);
1451
1452 defineUniform(shader, uniform, uniform.name, &encoder);
1453}
1454
Geoff Lang492a7e42014-11-05 13:27:06 -05001455void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1456 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001457{
1458 if (uniform.isStruct())
1459 {
1460 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1461 {
1462 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1463
Jamie Madill55def582015-05-04 11:24:57 -04001464 if (encoder)
1465 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001466
1467 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1468 {
1469 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1470 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1471
1472 defineUniform(shader, field, fieldFullName, encoder);
1473 }
1474
Jamie Madill55def582015-05-04 11:24:57 -04001475 if (encoder)
1476 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001477 }
1478 }
1479 else // Not a struct
1480 {
1481 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001482 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001483 {
1484 encoder->enterAggregateType();
1485 }
1486
1487 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1488
Jamie Madill2857f482015-02-09 15:35:29 -05001489 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001490 sh::BlockMemberInfo blockInfo = encoder ?
1491 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1492 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001493
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001494 if (!linkedUniform)
1495 {
1496 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001497 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001498 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001499
1500 if (encoder)
1501 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001502 mUniforms.push_back(linkedUniform);
1503 }
1504
Jamie Madill55def582015-05-04 11:24:57 -04001505 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001506 {
Jamie Madill55def582015-05-04 11:24:57 -04001507 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1508 {
1509 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1510 }
1511 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1512 {
1513 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1514 }
1515 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001516 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001517
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001518 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001519 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001520 {
1521 encoder->exitAggregateType();
1522 }
1523 }
1524}
1525
1526template <typename T>
1527static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1528{
1529 ASSERT(dest != NULL);
1530 ASSERT(dirtyFlag != NULL);
1531
1532 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1533 *dest = source;
1534}
1535
1536template <typename T>
1537void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1538{
1539 const int components = gl::VariableComponentCount(targetUniformType);
1540 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1541
1542 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1543
1544 int elementCount = targetUniform->elementCount();
1545
1546 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1547
1548 if (targetUniform->type == targetUniformType)
1549 {
1550 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1551
1552 for (int i = 0; i < count; i++)
1553 {
1554 T *dest = target + (i * 4);
1555 const T *source = v + (i * components);
1556
1557 for (int c = 0; c < components; c++)
1558 {
1559 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1560 }
1561 for (int c = components; c < 4; c++)
1562 {
1563 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1564 }
1565 }
1566 }
1567 else if (targetUniform->type == targetBoolType)
1568 {
1569 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1570
1571 for (int i = 0; i < count; i++)
1572 {
1573 GLint *dest = boolParams + (i * 4);
1574 const T *source = v + (i * components);
1575
1576 for (int c = 0; c < components; c++)
1577 {
1578 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1579 }
1580 for (int c = components; c < 4; c++)
1581 {
1582 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1583 }
1584 }
1585 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001586 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001587 {
1588 ASSERT(targetUniformType == GL_INT);
1589
1590 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1591
1592 bool wasDirty = targetUniform->dirty;
1593
1594 for (int i = 0; i < count; i++)
1595 {
1596 GLint *dest = target + (i * 4);
1597 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1598
1599 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1600 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1601 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1602 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1603 }
1604
1605 if (!wasDirty && targetUniform->dirty)
1606 {
1607 mDirtySamplerMapping = true;
1608 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001609 }
1610 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001611}
Brandon Jones18bd4102014-09-22 14:21:44 -07001612
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001613template<typename T>
1614bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1615{
1616 bool dirty = false;
1617 int copyWidth = std::min(targetHeight, srcWidth);
1618 int copyHeight = std::min(targetWidth, srcHeight);
1619
1620 for (int x = 0; x < copyWidth; x++)
1621 {
1622 for (int y = 0; y < copyHeight; y++)
1623 {
1624 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1625 }
1626 }
1627 // clear unfilled right side
1628 for (int y = 0; y < copyWidth; y++)
1629 {
1630 for (int x = copyHeight; x < targetWidth; x++)
1631 {
1632 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1633 }
1634 }
1635 // clear unfilled bottom.
1636 for (int y = copyWidth; y < targetHeight; y++)
1637 {
1638 for (int x = 0; x < targetWidth; x++)
1639 {
1640 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1641 }
1642 }
1643
1644 return dirty;
1645}
1646
1647template<typename T>
1648bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1649{
1650 bool dirty = false;
1651 int copyWidth = std::min(targetWidth, srcWidth);
1652 int copyHeight = std::min(targetHeight, srcHeight);
1653
1654 for (int y = 0; y < copyHeight; y++)
1655 {
1656 for (int x = 0; x < copyWidth; x++)
1657 {
1658 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1659 }
1660 }
1661 // clear unfilled right side
1662 for (int y = 0; y < copyHeight; y++)
1663 {
1664 for (int x = copyWidth; x < targetWidth; x++)
1665 {
1666 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1667 }
1668 }
1669 // clear unfilled bottom.
1670 for (int y = copyHeight; y < targetHeight; y++)
1671 {
1672 for (int x = 0; x < targetWidth; x++)
1673 {
1674 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1675 }
1676 }
1677
1678 return dirty;
1679}
1680
1681template <int cols, int rows>
1682void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1683{
1684 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1685
1686 int elementCount = targetUniform->elementCount();
1687
1688 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1689 const unsigned int targetMatrixStride = (4 * rows);
1690 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1691
1692 for (int i = 0; i < count; i++)
1693 {
1694 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1695 if (transpose == GL_FALSE)
1696 {
1697 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1698 }
1699 else
1700 {
1701 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1702 }
1703 target += targetMatrixStride;
1704 value += cols * rows;
1705 }
1706}
1707
1708template <typename T>
1709void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1710{
1711 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1712
1713 if (gl::IsMatrixType(targetUniform->type))
1714 {
1715 const int rows = gl::VariableRowCount(targetUniform->type);
1716 const int cols = gl::VariableColumnCount(targetUniform->type);
1717 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1718 }
1719 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1720 {
1721 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1722 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1723 size * sizeof(T));
1724 }
1725 else
1726 {
1727 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1728 switch (gl::VariableComponentType(targetUniform->type))
1729 {
1730 case GL_BOOL:
1731 {
1732 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1733
1734 for (unsigned int i = 0; i < size; i++)
1735 {
1736 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1737 }
1738 }
1739 break;
1740
1741 case GL_FLOAT:
1742 {
1743 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1744
1745 for (unsigned int i = 0; i < size; i++)
1746 {
1747 params[i] = static_cast<T>(floatParams[i]);
1748 }
1749 }
1750 break;
1751
1752 case GL_INT:
1753 {
1754 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1755
1756 for (unsigned int i = 0; i < size; i++)
1757 {
1758 params[i] = static_cast<T>(intParams[i]);
1759 }
1760 }
1761 break;
1762
1763 case GL_UNSIGNED_INT:
1764 {
1765 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1766
1767 for (unsigned int i = 0; i < size; i++)
1768 {
1769 params[i] = static_cast<T>(uintParams[i]);
1770 }
1771 }
1772 break;
1773
1774 default: UNREACHABLE();
1775 }
1776 }
1777}
1778
1779template <typename VarT>
1780void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1781 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1782 bool inRowMajorLayout)
1783{
1784 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1785 {
1786 const VarT &field = fields[uniformIndex];
1787 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1788
1789 if (field.isStruct())
1790 {
1791 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1792
1793 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1794 {
1795 encoder->enterAggregateType();
1796
1797 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1798 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1799
1800 encoder->exitAggregateType();
1801 }
1802 }
1803 else
1804 {
1805 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1806
1807 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1808
1809 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1810 blockIndex, memberInfo);
1811
1812 // add to uniform list, but not index, since uniform block uniforms have no location
1813 blockUniformIndexes->push_back(mUniforms.size());
1814 mUniforms.push_back(newUniform);
1815 }
1816 }
1817}
1818
Jamie Madilld3dfda22015-07-06 08:28:49 -04001819bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog,
1820 const gl::Shader &shader,
1821 const sh::InterfaceBlock &interfaceBlock,
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001822 const gl::Caps &caps)
1823{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001824 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001825
1826 // create uniform block entries if they do not exist
1827 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1828 {
1829 std::vector<unsigned int> blockUniformIndexes;
1830 const unsigned int blockIndex = mUniformBlocks.size();
1831
1832 // define member uniforms
1833 sh::BlockLayoutEncoder *encoder = NULL;
1834
1835 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1836 {
1837 encoder = new sh::Std140BlockEncoder;
1838 }
1839 else
1840 {
1841 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1842 }
1843 ASSERT(encoder);
1844
1845 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1846
1847 size_t dataSize = encoder->getBlockSize();
1848
1849 // create all the uniform blocks
1850 if (interfaceBlock.arraySize > 0)
1851 {
1852 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1853 {
1854 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1855 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1856 mUniformBlocks.push_back(newUniformBlock);
1857 }
1858 }
1859 else
1860 {
1861 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1862 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1863 mUniformBlocks.push_back(newUniformBlock);
1864 }
1865 }
1866
1867 if (interfaceBlock.staticUse)
1868 {
1869 // Assign registers to the uniform blocks
1870 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1871 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1872 ASSERT(blockIndex != GL_INVALID_INDEX);
1873 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1874
1875 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1876
1877 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1878 {
1879 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1880 ASSERT(uniformBlock->name == interfaceBlock.name);
1881
1882 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1883 interfaceBlockRegister + uniformBlockElement, caps))
1884 {
1885 return false;
1886 }
1887 }
1888 }
1889
1890 return true;
1891}
1892
1893bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001894 GLenum samplerType,
1895 unsigned int samplerCount,
1896 std::vector<Sampler> &outSamplers,
1897 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001898{
1899 unsigned int samplerIndex = startSamplerIndex;
1900
1901 do
1902 {
1903 if (samplerIndex < outSamplers.size())
1904 {
1905 Sampler& sampler = outSamplers[samplerIndex];
1906 sampler.active = true;
1907 sampler.textureType = GetTextureType(samplerType);
1908 sampler.logicalTextureUnit = 0;
1909 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1910 }
1911 else
1912 {
1913 return false;
1914 }
1915
1916 samplerIndex++;
1917 } while (samplerIndex < startSamplerIndex + samplerCount);
1918
1919 return true;
1920}
1921
1922bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1923{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001924 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001925 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1926
1927 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1928 {
1929 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1930 &mUsedVertexSamplerRange))
1931 {
Jamie Madillf6113162015-05-07 11:49:21 -04001932 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1933 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001934 return false;
1935 }
1936
1937 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1938 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1939 {
Jamie Madillf6113162015-05-07 11:49:21 -04001940 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1941 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001942 return false;
1943 }
1944 }
1945
1946 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1947 {
1948 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1949 &mUsedPixelSamplerRange))
1950 {
Jamie Madillf6113162015-05-07 11:49:21 -04001951 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
1952 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001953 return false;
1954 }
1955
1956 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1957 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1958 {
Jamie Madillf6113162015-05-07 11:49:21 -04001959 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
1960 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961 return false;
1962 }
1963 }
1964
1965 return true;
1966}
1967
1968bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1969{
1970 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1971 {
1972 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1973
Geoff Lang2ec386b2014-12-03 14:44:38 -05001974 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001975 {
1976 if (!indexSamplerUniform(uniform, infoLog, caps))
1977 {
1978 return false;
1979 }
1980 }
1981
Jamie Madill55def582015-05-04 11:24:57 -04001982 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001983 {
Jamie Madill55def582015-05-04 11:24:57 -04001984 if (!uniform.isBuiltIn())
1985 {
Geoff Lang95137842015-06-02 15:38:43 -04001986 // Assign in-order uniform locations
1987 mUniformIndex[mUniformIndex.size()] = gl::VariableLocation(uniform.name, arrayIndex, uniformIndex);
Jamie Madill55def582015-05-04 11:24:57 -04001988 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001989 }
1990 }
1991
1992 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001993}
1994
Brandon Jonesc9610c52014-08-25 17:02:59 -07001995void ProgramD3D::reset()
1996{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001997 ProgramImpl::reset();
1998
Brandon Joneseb994362014-09-24 10:27:28 -07001999 SafeDeleteContainer(mVertexExecutables);
2000 SafeDeleteContainer(mPixelExecutables);
2001 SafeDelete(mGeometryExecutable);
2002
2003 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07002004
Brandon Jones22502d52014-08-29 16:58:36 -07002005 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002006 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07002007 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07002008
2009 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002010 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002011 mUsesFragDepth = false;
2012 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002013 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002014
Brandon Jonesc9610c52014-08-25 17:02:59 -07002015 SafeDelete(mVertexUniformStorage);
2016 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017
2018 mSamplersPS.clear();
2019 mSamplersVS.clear();
2020
2021 mUsedVertexSamplerRange = 0;
2022 mUsedPixelSamplerRange = 0;
2023 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002024
2025 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002026}
2027
Geoff Lang7dd2e102014-11-10 15:19:26 -05002028unsigned int ProgramD3D::getSerial() const
2029{
2030 return mSerial;
2031}
2032
2033unsigned int ProgramD3D::issueSerial()
2034{
2035 return mCurrentSerial++;
2036}
2037
Jamie Madill437d2662014-12-05 14:23:35 -05002038void ProgramD3D::initAttributesByLayout()
2039{
2040 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2041 {
2042 mAttributesByLayout[i] = i;
2043 }
2044
2045 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2046}
2047
Jamie Madill476682e2015-06-30 10:04:29 -04002048void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002049 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2050 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002051{
Jamie Madill476682e2015-06-30 10:04:29 -04002052 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002053 {
Jamie Madill476682e2015-06-30 10:04:29 -04002054 int oldIndex = mAttributesByLayout[attribIndex];
2055 sortedSemanticIndicesOut[attribIndex] = mSemanticIndex[oldIndex];
2056 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002057 }
2058}
2059
Jamie Madilld3dfda22015-07-06 08:28:49 -04002060void ProgramD3D::updateCachedInputLayout(const gl::Program *program, const gl::State &state)
2061{
2062 mCachedInputLayout.resize(gl::MAX_VERTEX_ATTRIBS, gl::VERTEX_FORMAT_INVALID);
2063 const int *semanticIndexes = program->getSemanticIndexes();
2064
2065 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
2066 for (unsigned int attributeIndex = 0; attributeIndex < vertexAttributes.size(); attributeIndex++)
2067 {
2068 int semanticIndex = semanticIndexes[attributeIndex];
2069
2070 if (semanticIndex != -1)
2071 {
2072 mCachedInputLayout[semanticIndex] =
2073 GetVertexFormatType(vertexAttributes[attributeIndex],
2074 state.getVertexAttribCurrentValue(attributeIndex).Type);
2075 }
2076 }
2077}
2078
Brandon Jonesc9610c52014-08-25 17:02:59 -07002079}