blob: 10d14155dc28674ca165d89e4c1ec091b7752bfa [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
11#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050012#include "libANGLE/Framebuffer.h"
13#include "libANGLE/FramebufferAttachment.h"
14#include "libANGLE/Program.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040015#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000016#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050018#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/renderer/d3d/RendererD3D.h"
20#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050021#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050022#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050023
Brandon Jonesc9610c52014-08-25 17:02:59 -070024namespace rx
25{
26
Brandon Joneseb994362014-09-24 10:27:28 -070027namespace
28{
29
Brandon Jones1a8a7e32014-10-01 12:49:30 -070030GLenum GetTextureType(GLenum samplerType)
31{
32 switch (samplerType)
33 {
34 case GL_SAMPLER_2D:
35 case GL_INT_SAMPLER_2D:
36 case GL_UNSIGNED_INT_SAMPLER_2D:
37 case GL_SAMPLER_2D_SHADOW:
38 return GL_TEXTURE_2D;
39 case GL_SAMPLER_3D:
40 case GL_INT_SAMPLER_3D:
41 case GL_UNSIGNED_INT_SAMPLER_3D:
42 return GL_TEXTURE_3D;
43 case GL_SAMPLER_CUBE:
44 case GL_SAMPLER_CUBE_SHADOW:
45 return GL_TEXTURE_CUBE_MAP;
46 case GL_INT_SAMPLER_CUBE:
47 case GL_UNSIGNED_INT_SAMPLER_CUBE:
48 return GL_TEXTURE_CUBE_MAP;
49 case GL_SAMPLER_2D_ARRAY:
50 case GL_INT_SAMPLER_2D_ARRAY:
51 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
52 case GL_SAMPLER_2D_ARRAY_SHADOW:
53 return GL_TEXTURE_2D_ARRAY;
54 default: UNREACHABLE();
55 }
56
57 return GL_TEXTURE_2D;
58}
59
Jamie Madillf8dd7b12015-08-05 13:50:08 -040060gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070061{
Jamie Madillf8dd7b12015-08-05 13:50:08 -040062 gl::InputLayout defaultLayout(gl::MAX_VERTEX_ATTRIBS, gl::VERTEX_FORMAT_INVALID);
63 const auto &shaderAttributes = vertexShader->getActiveAttributes();
64 size_t layoutIndex = 0;
65 for (size_t attribIndex = 0; attribIndex < shaderAttributes.size(); ++attribIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070066 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -040067 const sh::Attribute &shaderAttr = shaderAttributes[attribIndex];
Brandon Joneseb994362014-09-24 10:27:28 -070068 if (shaderAttr.type != GL_NONE)
69 {
70 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
71
Jamie Madilld3dfda22015-07-06 08:28:49 -040072 for (size_t rowIndex = 0;
73 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType);
74 ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070075 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040076 GLenum componentType = gl::VariableComponentType(transposedType);
77 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
78 bool pureInt = (componentType != GL_FLOAT);
79 gl::VertexFormatType defaultType = gl::GetVertexFormatType(
80 componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070081
Jamie Madillf8dd7b12015-08-05 13:50:08 -040082 defaultLayout[layoutIndex++] = defaultType;
Brandon Joneseb994362014-09-24 10:27:28 -070083 }
84 }
85 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040086
87 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070088}
89
90std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
91{
Jamie Madillb4463142014-12-19 14:56:54 -050092 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070093
Jamie Madillb4463142014-12-19 14:56:54 -050094 if (!shaderOutputVars.empty())
95 {
96 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
97 }
Brandon Joneseb994362014-09-24 10:27:28 -070098
99 return defaultPixelOutput;
100}
101
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700102bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
103{
104 return var.isRowMajorLayout;
105}
106
107bool IsRowMajorLayout(const sh::ShaderVariable &var)
108{
109 return false;
110}
111
Jamie Madill437d2662014-12-05 14:23:35 -0500112struct AttributeSorter
113{
114 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -0500115 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -0500116 {
117 }
118
119 bool operator()(int a, int b)
120 {
Jamie Madill80d934b2015-02-19 10:16:12 -0500121 int indexA = (*originalIndices)[a];
122 int indexB = (*originalIndices)[b];
123
124 if (indexA == -1) return false;
125 if (indexB == -1) return true;
126 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -0500127 }
128
Jamie Madill80d934b2015-02-19 10:16:12 -0500129 const ProgramImpl::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500130};
131
Brandon Joneseb994362014-09-24 10:27:28 -0700132}
133
Jamie Madilld3dfda22015-07-06 08:28:49 -0400134ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
135 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500136 ShaderExecutableD3D *shaderExecutable)
Jamie Madilld3dfda22015-07-06 08:28:49 -0400137 : mInputs(inputLayout),
138 mSignature(signature),
139 mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700140{
Brandon Joneseb994362014-09-24 10:27:28 -0700141}
142
143ProgramD3D::VertexExecutable::~VertexExecutable()
144{
145 SafeDelete(mShaderExecutable);
146}
147
Jamie Madilld3dfda22015-07-06 08:28:49 -0400148// static
149void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
150 const gl::InputLayout &inputLayout,
151 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700152{
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400153 signatureOut->assign(inputLayout.size(), false);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400154
155 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700156 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400157 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400158 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700159 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400160 VertexConversionType conversionType =
161 renderer->getVertexConversionType(vertexFormatType);
162 (*signatureOut)[index] = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700163 }
164 }
Brandon Joneseb994362014-09-24 10:27:28 -0700165}
166
Jamie Madilld3dfda22015-07-06 08:28:49 -0400167bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
168{
169 return mSignature == signature;
170}
171
172ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
173 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700174 : mOutputSignature(outputSignature),
175 mShaderExecutable(shaderExecutable)
176{
177}
178
179ProgramD3D::PixelExecutable::~PixelExecutable()
180{
181 SafeDelete(mShaderExecutable);
182}
183
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700184ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
185{
186}
187
Geoff Lang7dd2e102014-11-10 15:19:26 -0500188unsigned int ProgramD3D::mCurrentSerial = 1;
189
Jamie Madill93e13fb2014-11-06 15:27:25 -0500190ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700191 : ProgramImpl(),
192 mRenderer(renderer),
193 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700194 mGeometryExecutable(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700195 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700196 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700197 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700198 mUsedVertexSamplerRange(0),
199 mUsedPixelSamplerRange(0),
200 mDirtySamplerMapping(true),
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400201 mTextureUnitTypesCache(renderer->getRendererCaps().maxCombinedTextureImageUnits),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500202 mShaderVersion(100),
203 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700204{
Brandon Joneseb994362014-09-24 10:27:28 -0700205 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700206}
207
208ProgramD3D::~ProgramD3D()
209{
210 reset();
211 SafeDelete(mDynamicHLSL);
212}
213
Brandon Jones44151a92014-09-10 11:32:25 -0700214bool ProgramD3D::usesPointSpriteEmulation() const
215{
216 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
217}
218
219bool ProgramD3D::usesGeometryShader() const
220{
Cooper Partine6664f02015-01-09 16:22:24 -0800221 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
222}
223
224bool ProgramD3D::usesInstancedPointSpriteEmulation() const
225{
226 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700227}
228
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700229GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
230{
231 GLint logicalTextureUnit = -1;
232
233 switch (type)
234 {
235 case gl::SAMPLER_PIXEL:
236 ASSERT(samplerIndex < caps.maxTextureImageUnits);
237 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
238 {
239 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
240 }
241 break;
242 case gl::SAMPLER_VERTEX:
243 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
244 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
245 {
246 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
247 }
248 break;
249 default: UNREACHABLE();
250 }
251
252 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
253 {
254 return logicalTextureUnit;
255 }
256
257 return -1;
258}
259
260// Returns the texture type for a given Direct3D 9 sampler type and
261// index (0-15 for the pixel shader and 0-3 for the vertex shader).
262GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
263{
264 switch (type)
265 {
266 case gl::SAMPLER_PIXEL:
267 ASSERT(samplerIndex < mSamplersPS.size());
268 ASSERT(mSamplersPS[samplerIndex].active);
269 return mSamplersPS[samplerIndex].textureType;
270 case gl::SAMPLER_VERTEX:
271 ASSERT(samplerIndex < mSamplersVS.size());
272 ASSERT(mSamplersVS[samplerIndex].active);
273 return mSamplersVS[samplerIndex].textureType;
274 default: UNREACHABLE();
275 }
276
277 return GL_TEXTURE_2D;
278}
279
280GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
281{
282 switch (type)
283 {
284 case gl::SAMPLER_PIXEL:
285 return mUsedPixelSamplerRange;
286 case gl::SAMPLER_VERTEX:
287 return mUsedVertexSamplerRange;
288 default:
289 UNREACHABLE();
290 return 0;
291 }
292}
293
294void ProgramD3D::updateSamplerMapping()
295{
296 if (!mDirtySamplerMapping)
297 {
298 return;
299 }
300
301 mDirtySamplerMapping = false;
302
303 // Retrieve sampler uniform values
304 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
305 {
306 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
307
308 if (targetUniform->dirty)
309 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500310 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700311 {
312 int count = targetUniform->elementCount();
313 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
314
315 if (targetUniform->isReferencedByFragmentShader())
316 {
317 unsigned int firstIndex = targetUniform->psRegisterIndex;
318
319 for (int i = 0; i < count; i++)
320 {
321 unsigned int samplerIndex = firstIndex + i;
322
323 if (samplerIndex < mSamplersPS.size())
324 {
325 ASSERT(mSamplersPS[samplerIndex].active);
326 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
327 }
328 }
329 }
330
331 if (targetUniform->isReferencedByVertexShader())
332 {
333 unsigned int firstIndex = targetUniform->vsRegisterIndex;
334
335 for (int i = 0; i < count; i++)
336 {
337 unsigned int samplerIndex = firstIndex + i;
338
339 if (samplerIndex < mSamplersVS.size())
340 {
341 ASSERT(mSamplersVS[samplerIndex].active);
342 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
343 }
344 }
345 }
346 }
347 }
348 }
349}
350
351bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
352{
Jamie Madill13776892015-04-28 12:39:06 -0400353 // Skip cache if we're using an infolog, so we get the full error.
354 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
355 if (!mDirtySamplerMapping && infoLog == nullptr && mCachedValidateSamplersResult.valid())
356 {
357 return mCachedValidateSamplersResult.value();
358 }
359
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700360 // if any two active samplers in a program are of different types, but refer to the same
361 // texture image unit, and this is the current program, then ValidateProgram will fail, and
362 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
363 updateSamplerMapping();
364
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400365 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700366
367 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
368 {
369 if (mSamplersPS[i].active)
370 {
371 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
372
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400373 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700374 {
375 if (infoLog)
376 {
Jamie Madillf6113162015-05-07 11:49:21 -0400377 (*infoLog) << "Sampler uniform (" << unit
378 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
379 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700380 }
381
Jamie Madill13776892015-04-28 12:39:06 -0400382 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700383 return false;
384 }
385
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400386 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700387 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400388 if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700389 {
390 if (infoLog)
391 {
Jamie Madillf6113162015-05-07 11:49:21 -0400392 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
393 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700394 }
395
Jamie Madill13776892015-04-28 12:39:06 -0400396 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700397 return false;
398 }
399 }
400 else
401 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400402 mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700403 }
404 }
405 }
406
407 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
408 {
409 if (mSamplersVS[i].active)
410 {
411 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
412
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400413 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700414 {
415 if (infoLog)
416 {
Jamie Madillf6113162015-05-07 11:49:21 -0400417 (*infoLog) << "Sampler uniform (" << unit
418 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
419 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700420 }
421
Jamie Madill13776892015-04-28 12:39:06 -0400422 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700423 return false;
424 }
425
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400426 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700427 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400428 if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700429 {
430 if (infoLog)
431 {
Jamie Madillf6113162015-05-07 11:49:21 -0400432 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
433 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700434 }
435
Jamie Madill13776892015-04-28 12:39:06 -0400436 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700437 return false;
438 }
439 }
440 else
441 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400442 mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700443 }
444 }
445 }
446
Jamie Madill13776892015-04-28 12:39:06 -0400447 mCachedValidateSamplersResult = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700448 return true;
449}
450
Geoff Lang7dd2e102014-11-10 15:19:26 -0500451LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700452{
Austin Kinross137b1512015-06-17 16:14:53 -0700453 DeviceIdentifier binaryDeviceIdentifier = { 0 };
454 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryDeviceIdentifier), sizeof(DeviceIdentifier));
455
456 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
457 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
458 {
459 infoLog << "Invalid program binary, device configuration has changed.";
460 return LinkResult(false, gl::Error(GL_NO_ERROR));
461 }
462
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500463 int compileFlags = stream->readInt<int>();
464 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
465 {
Jamie Madillf6113162015-05-07 11:49:21 -0400466 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500467 return LinkResult(false, gl::Error(GL_NO_ERROR));
468 }
469
Brandon Jones44151a92014-09-10 11:32:25 -0700470 stream->readInt(&mShaderVersion);
471
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700472 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
473 for (unsigned int i = 0; i < psSamplerCount; ++i)
474 {
475 Sampler sampler;
476 stream->readBool(&sampler.active);
477 stream->readInt(&sampler.logicalTextureUnit);
478 stream->readInt(&sampler.textureType);
479 mSamplersPS.push_back(sampler);
480 }
481 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
482 for (unsigned int i = 0; i < vsSamplerCount; ++i)
483 {
484 Sampler sampler;
485 stream->readBool(&sampler.active);
486 stream->readInt(&sampler.logicalTextureUnit);
487 stream->readInt(&sampler.textureType);
488 mSamplersVS.push_back(sampler);
489 }
490
491 stream->readInt(&mUsedVertexSamplerRange);
492 stream->readInt(&mUsedPixelSamplerRange);
493
494 const unsigned int uniformCount = stream->readInt<unsigned int>();
495 if (stream->error())
496 {
Jamie Madillf6113162015-05-07 11:49:21 -0400497 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500498 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700499 }
500
501 mUniforms.resize(uniformCount);
502 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
503 {
504 GLenum type = stream->readInt<GLenum>();
505 GLenum precision = stream->readInt<GLenum>();
506 std::string name = stream->readString();
507 unsigned int arraySize = stream->readInt<unsigned int>();
508 int blockIndex = stream->readInt<int>();
509
510 int offset = stream->readInt<int>();
511 int arrayStride = stream->readInt<int>();
512 int matrixStride = stream->readInt<int>();
513 bool isRowMajorMatrix = stream->readBool();
514
515 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
516
517 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
518
519 stream->readInt(&uniform->psRegisterIndex);
520 stream->readInt(&uniform->vsRegisterIndex);
521 stream->readInt(&uniform->registerCount);
522 stream->readInt(&uniform->registerElement);
523
524 mUniforms[uniformIndex] = uniform;
525 }
526
527 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
528 if (stream->error())
529 {
Jamie Madillf6113162015-05-07 11:49:21 -0400530 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500531 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700532 }
533
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700534 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
535 {
Geoff Lang95137842015-06-02 15:38:43 -0400536 GLuint location;
537 stream->readInt(&location);
538
539 gl::VariableLocation variable;
540 stream->readString(&variable.name);
541 stream->readInt(&variable.element);
542 stream->readInt(&variable.index);
543
544 mUniformIndex[location] = variable;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700545 }
546
547 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
548 if (stream->error())
549 {
Jamie Madillf6113162015-05-07 11:49:21 -0400550 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500551 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700552 }
553
554 mUniformBlocks.resize(uniformBlockCount);
555 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
556 {
557 std::string name = stream->readString();
558 unsigned int elementIndex = stream->readInt<unsigned int>();
559 unsigned int dataSize = stream->readInt<unsigned int>();
560
561 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
562
563 stream->readInt(&uniformBlock->psRegisterIndex);
564 stream->readInt(&uniformBlock->vsRegisterIndex);
565
566 unsigned int numMembers = stream->readInt<unsigned int>();
567 uniformBlock->memberUniformIndexes.resize(numMembers);
568 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
569 {
570 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
571 }
572
573 mUniformBlocks[uniformBlockIndex] = uniformBlock;
574 }
575
Brandon Joneseb994362014-09-24 10:27:28 -0700576 stream->readInt(&mTransformFeedbackBufferMode);
577 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
578 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
579 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
580 {
581 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
582
583 stream->readString(&varying.name);
584 stream->readInt(&varying.type);
585 stream->readInt(&varying.size);
586 stream->readString(&varying.semanticName);
587 stream->readInt(&varying.semanticIndex);
588 stream->readInt(&varying.semanticIndexCount);
589 }
590
Brandon Jones22502d52014-08-29 16:58:36 -0700591 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530592 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700593 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530594 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700595 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700596 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700597
598 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
599 mPixelShaderKey.resize(pixelShaderKeySize);
600 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
601 {
602 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
603 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
604 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
605 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
606 }
607
Brandon Joneseb994362014-09-24 10:27:28 -0700608 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
609
610 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
611 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
612 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400613 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400614 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700615
Jamie Madilld3dfda22015-07-06 08:28:49 -0400616 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700617 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400618 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700619 }
620
621 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
622 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400623
Geoff Lang359ef262015-01-05 14:42:29 -0500624 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400625 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
626 SHADER_VERTEX,
627 mTransformFeedbackLinkedVaryings,
628 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
629 &shaderExecutable);
630 if (error.isError())
631 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500632 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400633 }
634
Brandon Joneseb994362014-09-24 10:27:28 -0700635 if (!shaderExecutable)
636 {
Jamie Madillf6113162015-05-07 11:49:21 -0400637 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500638 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700639 }
640
641 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400642 VertexExecutable::Signature signature;
643 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700644
645 // add new binary
646 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
647
648 stream->skip(vertexShaderSize);
649 }
650
651 const size_t pixelShaderCount = stream->readInt<unsigned int>();
652 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
653 {
654 const size_t outputCount = stream->readInt<unsigned int>();
655 std::vector<GLenum> outputs(outputCount);
656 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
657 {
658 stream->readInt(&outputs[outputIndex]);
659 }
660
661 const size_t pixelShaderSize = stream->readInt<unsigned int>();
662 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500663 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400664 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
665 mTransformFeedbackLinkedVaryings,
666 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
667 &shaderExecutable);
668 if (error.isError())
669 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500670 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400671 }
Brandon Joneseb994362014-09-24 10:27:28 -0700672
673 if (!shaderExecutable)
674 {
Jamie Madillf6113162015-05-07 11:49:21 -0400675 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500676 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700677 }
678
679 // add new binary
680 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
681
682 stream->skip(pixelShaderSize);
683 }
684
685 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
686
687 if (geometryShaderSize > 0)
688 {
689 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400690 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
691 mTransformFeedbackLinkedVaryings,
692 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
693 &mGeometryExecutable);
694 if (error.isError())
695 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500696 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400697 }
Brandon Joneseb994362014-09-24 10:27:28 -0700698
699 if (!mGeometryExecutable)
700 {
Jamie Madillf6113162015-05-07 11:49:21 -0400701 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500702 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700703 }
704 stream->skip(geometryShaderSize);
705 }
706
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700707 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500708 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700709
Geoff Lang7dd2e102014-11-10 15:19:26 -0500710 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700711}
712
Geoff Langb543aff2014-09-30 14:52:54 -0400713gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700714{
Austin Kinross137b1512015-06-17 16:14:53 -0700715 // Output the DeviceIdentifier before we output any shader code
716 // When we load the binary again later, we can validate the device identifier before trying to compile any HLSL
717 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
718 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(DeviceIdentifier));
719
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500720 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
721
Brandon Jones44151a92014-09-10 11:32:25 -0700722 stream->writeInt(mShaderVersion);
723
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700724 stream->writeInt(mSamplersPS.size());
725 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
726 {
727 stream->writeInt(mSamplersPS[i].active);
728 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
729 stream->writeInt(mSamplersPS[i].textureType);
730 }
731
732 stream->writeInt(mSamplersVS.size());
733 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
734 {
735 stream->writeInt(mSamplersVS[i].active);
736 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
737 stream->writeInt(mSamplersVS[i].textureType);
738 }
739
740 stream->writeInt(mUsedVertexSamplerRange);
741 stream->writeInt(mUsedPixelSamplerRange);
742
743 stream->writeInt(mUniforms.size());
744 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
745 {
746 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
747
748 stream->writeInt(uniform.type);
749 stream->writeInt(uniform.precision);
750 stream->writeString(uniform.name);
751 stream->writeInt(uniform.arraySize);
752 stream->writeInt(uniform.blockIndex);
753
754 stream->writeInt(uniform.blockInfo.offset);
755 stream->writeInt(uniform.blockInfo.arrayStride);
756 stream->writeInt(uniform.blockInfo.matrixStride);
757 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
758
759 stream->writeInt(uniform.psRegisterIndex);
760 stream->writeInt(uniform.vsRegisterIndex);
761 stream->writeInt(uniform.registerCount);
762 stream->writeInt(uniform.registerElement);
763 }
764
765 stream->writeInt(mUniformIndex.size());
Geoff Lang95137842015-06-02 15:38:43 -0400766 for (const auto &uniform : mUniformIndex)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700767 {
Geoff Lang95137842015-06-02 15:38:43 -0400768 GLuint location = uniform.first;
769 stream->writeInt(location);
770
771 const gl::VariableLocation &variable = uniform.second;
772 stream->writeString(variable.name);
773 stream->writeInt(variable.element);
774 stream->writeInt(variable.index);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700775 }
776
777 stream->writeInt(mUniformBlocks.size());
778 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
779 {
780 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
781
782 stream->writeString(uniformBlock.name);
783 stream->writeInt(uniformBlock.elementIndex);
784 stream->writeInt(uniformBlock.dataSize);
785
786 stream->writeInt(uniformBlock.memberUniformIndexes.size());
787 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
788 {
789 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
790 }
791
792 stream->writeInt(uniformBlock.psRegisterIndex);
793 stream->writeInt(uniformBlock.vsRegisterIndex);
794 }
795
Brandon Joneseb994362014-09-24 10:27:28 -0700796 stream->writeInt(mTransformFeedbackBufferMode);
797 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
798 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
799 {
800 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
801
802 stream->writeString(varying.name);
803 stream->writeInt(varying.type);
804 stream->writeInt(varying.size);
805 stream->writeString(varying.semanticName);
806 stream->writeInt(varying.semanticIndex);
807 stream->writeInt(varying.semanticIndexCount);
808 }
809
Brandon Jones22502d52014-08-29 16:58:36 -0700810 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530811 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700812 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530813 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700814 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700815 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700816
Brandon Joneseb994362014-09-24 10:27:28 -0700817 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700818 stream->writeInt(pixelShaderKey.size());
819 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
820 {
Brandon Joneseb994362014-09-24 10:27:28 -0700821 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700822 stream->writeInt(variable.type);
823 stream->writeString(variable.name);
824 stream->writeString(variable.source);
825 stream->writeInt(variable.outputIndex);
826 }
827
Brandon Joneseb994362014-09-24 10:27:28 -0700828 stream->writeInt(mVertexExecutables.size());
829 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
830 {
831 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
832
Jamie Madilld3dfda22015-07-06 08:28:49 -0400833 const auto &inputLayout = vertexExecutable->inputs();
834 stream->writeInt(inputLayout.size());
835
836 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700837 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400838 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -0700839 }
840
841 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
842 stream->writeInt(vertexShaderSize);
843
844 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
845 stream->writeBytes(vertexBlob, vertexShaderSize);
846 }
847
848 stream->writeInt(mPixelExecutables.size());
849 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
850 {
851 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
852
853 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
854 stream->writeInt(outputs.size());
855 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
856 {
857 stream->writeInt(outputs[outputIndex]);
858 }
859
860 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
861 stream->writeInt(pixelShaderSize);
862
863 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
864 stream->writeBytes(pixelBlob, pixelShaderSize);
865 }
866
867 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
868 stream->writeInt(geometryShaderSize);
869
870 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
871 {
872 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
873 stream->writeBytes(geometryBlob, geometryShaderSize);
874 }
875
Geoff Langb543aff2014-09-30 14:52:54 -0400876 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700877}
878
Geoff Lang359ef262015-01-05 14:42:29 -0500879gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700880{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400881 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700882
Jamie Madill85a18042015-03-05 15:41:41 -0500883 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
884 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700885
886 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
887 {
888 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
889
890 if (colorbuffer)
891 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400892 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700893 }
894 else
895 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400896 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700897 }
898 }
899
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400900 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700901}
902
Jamie Madill97399232014-12-23 12:31:15 -0500903gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500904 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500905 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700906{
907 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
908 {
909 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
910 {
Geoff Langb543aff2014-09-30 14:52:54 -0400911 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
912 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700913 }
914 }
915
Brandon Jones22502d52014-08-29 16:58:36 -0700916 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
917 outputSignature);
918
919 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500920 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500921
922 gl::InfoLog tempInfoLog;
923 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
924
925 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400926 mTransformFeedbackLinkedVaryings,
927 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
928 mPixelWorkarounds, &pixelExecutable);
929 if (error.isError())
930 {
931 return error;
932 }
Brandon Joneseb994362014-09-24 10:27:28 -0700933
Jamie Madill97399232014-12-23 12:31:15 -0500934 if (pixelExecutable)
935 {
936 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
937 }
938 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700939 {
940 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
941 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
942 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
943 }
Brandon Jones22502d52014-08-29 16:58:36 -0700944
Geoff Langb543aff2014-09-30 14:52:54 -0400945 *outExectuable = pixelExecutable;
946 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700947}
948
Jamie Madilld3dfda22015-07-06 08:28:49 -0400949gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -0500950 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500951 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700952{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400953 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -0700954
955 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
956 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400957 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -0700958 {
Geoff Langb543aff2014-09-30 14:52:54 -0400959 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
960 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700961 }
962 }
963
Brandon Jones22502d52014-08-29 16:58:36 -0700964 // Generate new dynamic layout with attribute conversions
Jamie Madill3da79b72015-04-27 11:09:17 -0400965 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, getShaderAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -0700966
967 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500968 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500969
970 gl::InfoLog tempInfoLog;
971 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
972
973 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400974 mTransformFeedbackLinkedVaryings,
975 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
976 mVertexWorkarounds, &vertexExecutable);
977 if (error.isError())
978 {
979 return error;
980 }
981
Jamie Madill97399232014-12-23 12:31:15 -0500982 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700983 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400984 mVertexExecutables.push_back(new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700985 }
Jamie Madill97399232014-12-23 12:31:15 -0500986 else if (!infoLog)
987 {
988 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
989 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
990 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
991 }
Brandon Jones22502d52014-08-29 16:58:36 -0700992
Geoff Langb543aff2014-09-30 14:52:54 -0400993 *outExectuable = vertexExecutable;
994 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700995}
996
Geoff Lang7dd2e102014-11-10 15:19:26 -0500997LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
998 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700999{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001000 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1001 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones44151a92014-09-10 11:32:25 -07001002
Jamie Madillf8dd7b12015-08-05 13:50:08 -04001003 const gl::InputLayout &defaultInputLayout = GetDefaultInputLayoutFromShader(vertexShader);
Jamie Madille4ea2022015-03-26 20:35:05 +00001004 ShaderExecutableD3D *defaultVertexExecutable = NULL;
1005 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
1006 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001007 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001008 return LinkResult(false, error);
1009 }
Austin Kinross434953e2015-02-20 10:49:51 -08001010
Brandon Joneseb994362014-09-24 10:27:28 -07001011 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001012 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +00001013 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001014 if (error.isError())
1015 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001016 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001017 }
Brandon Jones44151a92014-09-10 11:32:25 -07001018
Brandon Joneseb994362014-09-24 10:27:28 -07001019 if (usesGeometryShader())
1020 {
1021 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001022
Geoff Langb543aff2014-09-30 14:52:54 -04001023
1024 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1025 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +05301026 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001027 if (error.isError())
1028 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001029 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001030 }
Brandon Joneseb994362014-09-24 10:27:28 -07001031 }
1032
Brandon Jones091540d2014-10-29 11:32:04 -07001033#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001034 if (usesGeometryShader() && mGeometryExecutable)
1035 {
1036 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1037 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1038 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1039 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1040 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1041 }
1042
1043 if (defaultVertexExecutable)
1044 {
1045 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1046 }
1047
1048 if (defaultPixelExecutable)
1049 {
1050 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1051 }
1052#endif
1053
Geoff Langb543aff2014-09-30 14:52:54 -04001054 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001055 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001056}
1057
Geoff Lang7dd2e102014-11-10 15:19:26 -05001058LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1059 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1060 const std::vector<std::string> &transformFeedbackVaryings,
1061 GLenum transformFeedbackBufferMode,
1062 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1063 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001064{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001065 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1066 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001067
Jamie Madillde8892b2014-11-11 13:00:22 -05001068 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1069 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001070
Brandon Joneseb994362014-09-24 10:27:28 -07001071 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001072
1073 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301074 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001075
1076 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301077 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001078 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001079
Austin Kinross02df7962015-07-01 10:03:42 -07001080 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1081 {
1082 if (fragmentShaderD3D->usesFrontFacing())
1083 {
1084 infoLog << "The current renderer doesn't support gl_FrontFacing";
1085 return LinkResult(false, gl::Error(GL_NO_ERROR));
1086 }
1087 }
1088
Brandon Jones22502d52014-08-29 16:58:36 -07001089 // Map the varyings to the register file
Daniel Chengf33ab832015-06-30 19:08:16 -07001090 VaryingPacking packing = {};
Brandon Jones22502d52014-08-29 16:58:36 -07001091 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1092
Geoff Langbdee2d52014-09-17 11:02:51 -04001093 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001094 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001095 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001096 }
1097
Geoff Lang7dd2e102014-11-10 15:19:26 -05001098 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001099 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001100 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001101 }
1102
Jamie Madillde8892b2014-11-11 13:00:22 -05001103 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001104 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1105 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1106 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001107 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001108 }
1109
Brandon Jones44151a92014-09-10 11:32:25 -07001110 mUsesPointSize = vertexShaderD3D->usesPointSize();
1111
Jamie Madill437d2662014-12-05 14:23:35 -05001112 initAttributesByLayout();
1113
Geoff Lang7dd2e102014-11-10 15:19:26 -05001114 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001115}
1116
Geoff Lang0ca53782015-05-07 13:49:39 -04001117void ProgramD3D::bindAttributeLocation(GLuint index, const std::string &name)
1118{
1119}
1120
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001121void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001122{
1123 // Compute total default block size
1124 unsigned int vertexRegisters = 0;
1125 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001126 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001127 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001128 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001129
Geoff Lang2ec386b2014-12-03 14:44:38 -05001130 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001131 {
1132 if (uniform.isReferencedByVertexShader())
1133 {
1134 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1135 }
1136 if (uniform.isReferencedByFragmentShader())
1137 {
1138 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1139 }
1140 }
1141 }
1142
1143 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1144 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1145}
1146
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001147gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001148{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001149 updateSamplerMapping();
1150
1151 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1152 if (error.isError())
1153 {
1154 return error;
1155 }
1156
1157 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1158 {
1159 mUniforms[uniformIndex]->dirty = false;
1160 }
1161
1162 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001163}
1164
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001165gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001166{
Jamie Madill03260fa2015-06-22 13:57:22 -04001167 mVertexUBOCache.clear();
1168 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001169
1170 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1171 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1172
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001173 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001174 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001175 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001176 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001177
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001178 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001179
1180 // Unnecessary to apply an unreferenced standard or shared UBO
1181 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1182 {
1183 continue;
1184 }
1185
1186 if (uniformBlock->isReferencedByVertexShader())
1187 {
1188 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001189 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001190
Jamie Madill969194d2015-07-20 14:36:56 -04001191 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001192 {
1193 mVertexUBOCache.resize(registerIndex + 1, -1);
1194 }
1195
1196 ASSERT(mVertexUBOCache[registerIndex] == -1);
1197 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001198 }
1199
1200 if (uniformBlock->isReferencedByFragmentShader())
1201 {
1202 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001203 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001204
1205 if (mFragmentUBOCache.size() <= registerIndex)
1206 {
1207 mFragmentUBOCache.resize(registerIndex + 1, -1);
1208 }
1209
1210 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1211 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001212 }
1213 }
1214
Jamie Madill03260fa2015-06-22 13:57:22 -04001215 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001216}
1217
1218bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1219 unsigned int registerIndex, const gl::Caps &caps)
1220{
1221 if (shader == GL_VERTEX_SHADER)
1222 {
1223 uniformBlock->vsRegisterIndex = registerIndex;
1224 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1225 {
Jamie Madillf6113162015-05-07 11:49:21 -04001226 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (" << caps.maxVertexUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001227 return false;
1228 }
1229 }
1230 else if (shader == GL_FRAGMENT_SHADER)
1231 {
1232 uniformBlock->psRegisterIndex = registerIndex;
1233 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1234 {
Jamie Madillf6113162015-05-07 11:49:21 -04001235 infoLog << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (" << caps.maxFragmentUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001236 return false;
1237 }
1238 }
1239 else UNREACHABLE();
1240
1241 return true;
1242}
1243
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001244void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001245{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001246 unsigned int numUniforms = mUniforms.size();
1247 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001248 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001249 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001250 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001251}
1252
1253void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1254{
1255 setUniform(location, count, v, GL_FLOAT);
1256}
1257
1258void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1259{
1260 setUniform(location, count, v, GL_FLOAT_VEC2);
1261}
1262
1263void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1264{
1265 setUniform(location, count, v, GL_FLOAT_VEC3);
1266}
1267
1268void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1269{
1270 setUniform(location, count, v, GL_FLOAT_VEC4);
1271}
1272
1273void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1274{
1275 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1276}
1277
1278void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1279{
1280 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1281}
1282
1283void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1284{
1285 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1286}
1287
1288void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1289{
1290 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1291}
1292
1293void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1294{
1295 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1296}
1297
1298void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1299{
1300 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1301}
1302
1303void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1304{
1305 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1306}
1307
1308void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1309{
1310 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1311}
1312
1313void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1314{
1315 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1316}
1317
1318void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1319{
1320 setUniform(location, count, v, GL_INT);
1321}
1322
1323void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1324{
1325 setUniform(location, count, v, GL_INT_VEC2);
1326}
1327
1328void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1329{
1330 setUniform(location, count, v, GL_INT_VEC3);
1331}
1332
1333void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1334{
1335 setUniform(location, count, v, GL_INT_VEC4);
1336}
1337
1338void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1339{
1340 setUniform(location, count, v, GL_UNSIGNED_INT);
1341}
1342
1343void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1344{
1345 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1346}
1347
1348void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1349{
1350 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1351}
1352
1353void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1354{
1355 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1356}
1357
1358void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1359{
1360 getUniformv(location, params, GL_FLOAT);
1361}
1362
1363void ProgramD3D::getUniformiv(GLint location, GLint *params)
1364{
1365 getUniformv(location, params, GL_INT);
1366}
1367
1368void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1369{
1370 getUniformv(location, params, GL_UNSIGNED_INT);
1371}
1372
1373bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1374 const gl::Caps &caps)
1375{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001376 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1377 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001378
1379 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1380 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1381
1382 // Check that uniforms defined in the vertex and fragment shaders are identical
1383 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1384 UniformMap linkedUniforms;
1385
1386 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001387 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001388 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1389 linkedUniforms[vertexUniform.name] = &vertexUniform;
1390 }
1391
1392 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1393 {
1394 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1395 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1396 if (entry != linkedUniforms.end())
1397 {
1398 const sh::Uniform &vertexUniform = *entry->second;
1399 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001400 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001401 {
1402 return false;
1403 }
1404 }
1405 }
1406
1407 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1408 {
1409 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1410
1411 if (uniform.staticUse)
1412 {
Jamie Madill55def582015-05-04 11:24:57 -04001413 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1414 vertexShaderD3D->getUniformRegister(uniform.name);
1415 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001416 }
1417 }
1418
1419 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1420 {
1421 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1422
1423 if (uniform.staticUse)
1424 {
Jamie Madill55def582015-05-04 11:24:57 -04001425 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1426 fragmentShaderD3D->getUniformRegister(uniform.name);
1427 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001428 }
1429 }
1430
1431 if (!indexUniforms(infoLog, caps))
1432 {
1433 return false;
1434 }
1435
1436 initializeUniformStorage();
1437
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001438 return true;
1439}
1440
Geoff Lang492a7e42014-11-05 13:27:06 -05001441void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001442{
Jamie Madill55def582015-05-04 11:24:57 -04001443 if (uniformRegister == GL_INVALID_INDEX)
1444 {
1445 defineUniform(shader, uniform, uniform.name, nullptr);
1446 return;
1447 }
1448
Geoff Lang492a7e42014-11-05 13:27:06 -05001449 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001450 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1451 encoder.skipRegisters(uniformRegister);
1452
1453 defineUniform(shader, uniform, uniform.name, &encoder);
1454}
1455
Geoff Lang492a7e42014-11-05 13:27:06 -05001456void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1457 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001458{
1459 if (uniform.isStruct())
1460 {
1461 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1462 {
1463 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1464
Jamie Madill55def582015-05-04 11:24:57 -04001465 if (encoder)
1466 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001467
1468 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1469 {
1470 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1471 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1472
1473 defineUniform(shader, field, fieldFullName, encoder);
1474 }
1475
Jamie Madill55def582015-05-04 11:24:57 -04001476 if (encoder)
1477 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001478 }
1479 }
1480 else // Not a struct
1481 {
1482 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001483 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001484 {
1485 encoder->enterAggregateType();
1486 }
1487
1488 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1489
Jamie Madill2857f482015-02-09 15:35:29 -05001490 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001491 sh::BlockMemberInfo blockInfo = encoder ?
1492 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1493 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001494
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001495 if (!linkedUniform)
1496 {
1497 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001498 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001499 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001500
1501 if (encoder)
1502 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001503 mUniforms.push_back(linkedUniform);
1504 }
1505
Jamie Madill55def582015-05-04 11:24:57 -04001506 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001507 {
Jamie Madill55def582015-05-04 11:24:57 -04001508 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1509 {
1510 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1511 }
1512 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1513 {
1514 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1515 }
1516 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001517 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001518
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001519 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001520 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001521 {
1522 encoder->exitAggregateType();
1523 }
1524 }
1525}
1526
1527template <typename T>
1528static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1529{
1530 ASSERT(dest != NULL);
1531 ASSERT(dirtyFlag != NULL);
1532
1533 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1534 *dest = source;
1535}
1536
1537template <typename T>
1538void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1539{
1540 const int components = gl::VariableComponentCount(targetUniformType);
1541 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1542
1543 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1544
1545 int elementCount = targetUniform->elementCount();
1546
1547 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1548
1549 if (targetUniform->type == targetUniformType)
1550 {
1551 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1552
1553 for (int i = 0; i < count; i++)
1554 {
1555 T *dest = target + (i * 4);
1556 const T *source = v + (i * components);
1557
1558 for (int c = 0; c < components; c++)
1559 {
1560 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1561 }
1562 for (int c = components; c < 4; c++)
1563 {
1564 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1565 }
1566 }
1567 }
1568 else if (targetUniform->type == targetBoolType)
1569 {
1570 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1571
1572 for (int i = 0; i < count; i++)
1573 {
1574 GLint *dest = boolParams + (i * 4);
1575 const T *source = v + (i * components);
1576
1577 for (int c = 0; c < components; c++)
1578 {
1579 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1580 }
1581 for (int c = components; c < 4; c++)
1582 {
1583 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1584 }
1585 }
1586 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001587 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001588 {
1589 ASSERT(targetUniformType == GL_INT);
1590
1591 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1592
1593 bool wasDirty = targetUniform->dirty;
1594
1595 for (int i = 0; i < count; i++)
1596 {
1597 GLint *dest = target + (i * 4);
1598 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1599
1600 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1601 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1602 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1603 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1604 }
1605
1606 if (!wasDirty && targetUniform->dirty)
1607 {
1608 mDirtySamplerMapping = true;
1609 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001610 }
1611 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001612}
Brandon Jones18bd4102014-09-22 14:21:44 -07001613
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001614template<typename T>
1615bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1616{
1617 bool dirty = false;
1618 int copyWidth = std::min(targetHeight, srcWidth);
1619 int copyHeight = std::min(targetWidth, srcHeight);
1620
1621 for (int x = 0; x < copyWidth; x++)
1622 {
1623 for (int y = 0; y < copyHeight; y++)
1624 {
1625 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1626 }
1627 }
1628 // clear unfilled right side
1629 for (int y = 0; y < copyWidth; y++)
1630 {
1631 for (int x = copyHeight; x < targetWidth; x++)
1632 {
1633 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1634 }
1635 }
1636 // clear unfilled bottom.
1637 for (int y = copyWidth; y < targetHeight; y++)
1638 {
1639 for (int x = 0; x < targetWidth; x++)
1640 {
1641 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1642 }
1643 }
1644
1645 return dirty;
1646}
1647
1648template<typename T>
1649bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1650{
1651 bool dirty = false;
1652 int copyWidth = std::min(targetWidth, srcWidth);
1653 int copyHeight = std::min(targetHeight, srcHeight);
1654
1655 for (int y = 0; y < copyHeight; y++)
1656 {
1657 for (int x = 0; x < copyWidth; x++)
1658 {
1659 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1660 }
1661 }
1662 // clear unfilled right side
1663 for (int y = 0; y < copyHeight; y++)
1664 {
1665 for (int x = copyWidth; x < targetWidth; x++)
1666 {
1667 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1668 }
1669 }
1670 // clear unfilled bottom.
1671 for (int y = copyHeight; y < targetHeight; y++)
1672 {
1673 for (int x = 0; x < targetWidth; x++)
1674 {
1675 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1676 }
1677 }
1678
1679 return dirty;
1680}
1681
1682template <int cols, int rows>
1683void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1684{
1685 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1686
1687 int elementCount = targetUniform->elementCount();
1688
1689 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1690 const unsigned int targetMatrixStride = (4 * rows);
1691 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1692
1693 for (int i = 0; i < count; i++)
1694 {
1695 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1696 if (transpose == GL_FALSE)
1697 {
1698 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1699 }
1700 else
1701 {
1702 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1703 }
1704 target += targetMatrixStride;
1705 value += cols * rows;
1706 }
1707}
1708
1709template <typename T>
1710void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1711{
1712 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1713
1714 if (gl::IsMatrixType(targetUniform->type))
1715 {
1716 const int rows = gl::VariableRowCount(targetUniform->type);
1717 const int cols = gl::VariableColumnCount(targetUniform->type);
1718 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1719 }
1720 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1721 {
1722 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1723 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1724 size * sizeof(T));
1725 }
1726 else
1727 {
1728 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1729 switch (gl::VariableComponentType(targetUniform->type))
1730 {
1731 case GL_BOOL:
1732 {
1733 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1734
1735 for (unsigned int i = 0; i < size; i++)
1736 {
1737 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1738 }
1739 }
1740 break;
1741
1742 case GL_FLOAT:
1743 {
1744 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1745
1746 for (unsigned int i = 0; i < size; i++)
1747 {
1748 params[i] = static_cast<T>(floatParams[i]);
1749 }
1750 }
1751 break;
1752
1753 case GL_INT:
1754 {
1755 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1756
1757 for (unsigned int i = 0; i < size; i++)
1758 {
1759 params[i] = static_cast<T>(intParams[i]);
1760 }
1761 }
1762 break;
1763
1764 case GL_UNSIGNED_INT:
1765 {
1766 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1767
1768 for (unsigned int i = 0; i < size; i++)
1769 {
1770 params[i] = static_cast<T>(uintParams[i]);
1771 }
1772 }
1773 break;
1774
1775 default: UNREACHABLE();
1776 }
1777 }
1778}
1779
1780template <typename VarT>
1781void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1782 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1783 bool inRowMajorLayout)
1784{
1785 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1786 {
1787 const VarT &field = fields[uniformIndex];
1788 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1789
1790 if (field.isStruct())
1791 {
1792 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1793
1794 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1795 {
1796 encoder->enterAggregateType();
1797
1798 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1799 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1800
1801 encoder->exitAggregateType();
1802 }
1803 }
1804 else
1805 {
1806 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1807
1808 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1809
1810 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1811 blockIndex, memberInfo);
1812
1813 // add to uniform list, but not index, since uniform block uniforms have no location
1814 blockUniformIndexes->push_back(mUniforms.size());
1815 mUniforms.push_back(newUniform);
1816 }
1817 }
1818}
1819
Jamie Madilld3dfda22015-07-06 08:28:49 -04001820bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog,
1821 const gl::Shader &shader,
1822 const sh::InterfaceBlock &interfaceBlock,
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001823 const gl::Caps &caps)
1824{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001825 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001826
1827 // create uniform block entries if they do not exist
1828 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1829 {
1830 std::vector<unsigned int> blockUniformIndexes;
1831 const unsigned int blockIndex = mUniformBlocks.size();
1832
1833 // define member uniforms
1834 sh::BlockLayoutEncoder *encoder = NULL;
1835
1836 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1837 {
1838 encoder = new sh::Std140BlockEncoder;
1839 }
1840 else
1841 {
1842 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1843 }
1844 ASSERT(encoder);
1845
1846 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1847
1848 size_t dataSize = encoder->getBlockSize();
1849
1850 // create all the uniform blocks
1851 if (interfaceBlock.arraySize > 0)
1852 {
1853 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1854 {
1855 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1856 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1857 mUniformBlocks.push_back(newUniformBlock);
1858 }
1859 }
1860 else
1861 {
1862 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1863 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1864 mUniformBlocks.push_back(newUniformBlock);
1865 }
1866 }
1867
1868 if (interfaceBlock.staticUse)
1869 {
1870 // Assign registers to the uniform blocks
1871 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1872 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1873 ASSERT(blockIndex != GL_INVALID_INDEX);
1874 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1875
1876 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1877
1878 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1879 {
1880 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1881 ASSERT(uniformBlock->name == interfaceBlock.name);
1882
1883 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1884 interfaceBlockRegister + uniformBlockElement, caps))
1885 {
1886 return false;
1887 }
1888 }
1889 }
1890
1891 return true;
1892}
1893
1894bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001895 GLenum samplerType,
1896 unsigned int samplerCount,
1897 std::vector<Sampler> &outSamplers,
1898 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001899{
1900 unsigned int samplerIndex = startSamplerIndex;
1901
1902 do
1903 {
1904 if (samplerIndex < outSamplers.size())
1905 {
1906 Sampler& sampler = outSamplers[samplerIndex];
1907 sampler.active = true;
1908 sampler.textureType = GetTextureType(samplerType);
1909 sampler.logicalTextureUnit = 0;
1910 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1911 }
1912 else
1913 {
1914 return false;
1915 }
1916
1917 samplerIndex++;
1918 } while (samplerIndex < startSamplerIndex + samplerCount);
1919
1920 return true;
1921}
1922
1923bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1924{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001925 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001926 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1927
1928 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1929 {
1930 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1931 &mUsedVertexSamplerRange))
1932 {
Jamie Madillf6113162015-05-07 11:49:21 -04001933 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1934 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001935 return false;
1936 }
1937
1938 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1939 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1940 {
Jamie Madillf6113162015-05-07 11:49:21 -04001941 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1942 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943 return false;
1944 }
1945 }
1946
1947 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1948 {
1949 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1950 &mUsedPixelSamplerRange))
1951 {
Jamie Madillf6113162015-05-07 11:49:21 -04001952 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
1953 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001954 return false;
1955 }
1956
1957 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1958 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1959 {
Jamie Madillf6113162015-05-07 11:49:21 -04001960 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
1961 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001962 return false;
1963 }
1964 }
1965
1966 return true;
1967}
1968
1969bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1970{
1971 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1972 {
1973 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1974
Geoff Lang2ec386b2014-12-03 14:44:38 -05001975 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001976 {
1977 if (!indexSamplerUniform(uniform, infoLog, caps))
1978 {
1979 return false;
1980 }
1981 }
1982
Jamie Madill55def582015-05-04 11:24:57 -04001983 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001984 {
Jamie Madill55def582015-05-04 11:24:57 -04001985 if (!uniform.isBuiltIn())
1986 {
Geoff Lang95137842015-06-02 15:38:43 -04001987 // Assign in-order uniform locations
1988 mUniformIndex[mUniformIndex.size()] = gl::VariableLocation(uniform.name, arrayIndex, uniformIndex);
Jamie Madill55def582015-05-04 11:24:57 -04001989 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990 }
1991 }
1992
1993 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001994}
1995
Brandon Jonesc9610c52014-08-25 17:02:59 -07001996void ProgramD3D::reset()
1997{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001998 ProgramImpl::reset();
1999
Brandon Joneseb994362014-09-24 10:27:28 -07002000 SafeDeleteContainer(mVertexExecutables);
2001 SafeDeleteContainer(mPixelExecutables);
2002 SafeDelete(mGeometryExecutable);
2003
2004 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07002005
Brandon Jones22502d52014-08-29 16:58:36 -07002006 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002007 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07002008 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07002009
2010 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002011 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002012 mUsesFragDepth = false;
2013 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002014 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002015
Brandon Jonesc9610c52014-08-25 17:02:59 -07002016 SafeDelete(mVertexUniformStorage);
2017 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018
2019 mSamplersPS.clear();
2020 mSamplersVS.clear();
2021
2022 mUsedVertexSamplerRange = 0;
2023 mUsedPixelSamplerRange = 0;
2024 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002025
2026 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002027}
2028
Geoff Lang7dd2e102014-11-10 15:19:26 -05002029unsigned int ProgramD3D::getSerial() const
2030{
2031 return mSerial;
2032}
2033
2034unsigned int ProgramD3D::issueSerial()
2035{
2036 return mCurrentSerial++;
2037}
2038
Jamie Madill437d2662014-12-05 14:23:35 -05002039void ProgramD3D::initAttributesByLayout()
2040{
2041 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2042 {
2043 mAttributesByLayout[i] = i;
2044 }
2045
2046 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2047}
2048
Jamie Madill476682e2015-06-30 10:04:29 -04002049void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002050 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2051 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002052{
Jamie Madill476682e2015-06-30 10:04:29 -04002053 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002054 {
Jamie Madill476682e2015-06-30 10:04:29 -04002055 int oldIndex = mAttributesByLayout[attribIndex];
2056 sortedSemanticIndicesOut[attribIndex] = mSemanticIndex[oldIndex];
2057 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002058 }
2059}
2060
Jamie Madilld3dfda22015-07-06 08:28:49 -04002061void ProgramD3D::updateCachedInputLayout(const gl::Program *program, const gl::State &state)
2062{
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002063 mCachedInputLayout.assign(gl::MAX_VERTEX_ATTRIBS, gl::VERTEX_FORMAT_INVALID);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002064 const int *semanticIndexes = program->getSemanticIndexes();
2065
2066 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002067
Jamie Madilld3dfda22015-07-06 08:28:49 -04002068 for (unsigned int attributeIndex = 0; attributeIndex < vertexAttributes.size(); attributeIndex++)
2069 {
2070 int semanticIndex = semanticIndexes[attributeIndex];
2071
2072 if (semanticIndex != -1)
2073 {
2074 mCachedInputLayout[semanticIndex] =
2075 GetVertexFormatType(vertexAttributes[attributeIndex],
2076 state.getVertexAttribCurrentValue(attributeIndex).Type);
2077 }
2078 }
2079}
2080
Brandon Jonesc9610c52014-08-25 17:02:59 -07002081}