blob: 92909149568f58f4455ee34849592f6b41450470 [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
11#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050012#include "libANGLE/Framebuffer.h"
13#include "libANGLE/FramebufferAttachment.h"
14#include "libANGLE/Program.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040015#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000016#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050018#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/renderer/d3d/RendererD3D.h"
20#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050021#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050022#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050023
Brandon Jonesc9610c52014-08-25 17:02:59 -070024namespace rx
25{
26
Brandon Joneseb994362014-09-24 10:27:28 -070027namespace
28{
29
Brandon Jones1a8a7e32014-10-01 12:49:30 -070030GLenum GetTextureType(GLenum samplerType)
31{
32 switch (samplerType)
33 {
34 case GL_SAMPLER_2D:
35 case GL_INT_SAMPLER_2D:
36 case GL_UNSIGNED_INT_SAMPLER_2D:
37 case GL_SAMPLER_2D_SHADOW:
38 return GL_TEXTURE_2D;
39 case GL_SAMPLER_3D:
40 case GL_INT_SAMPLER_3D:
41 case GL_UNSIGNED_INT_SAMPLER_3D:
42 return GL_TEXTURE_3D;
43 case GL_SAMPLER_CUBE:
44 case GL_SAMPLER_CUBE_SHADOW:
45 return GL_TEXTURE_CUBE_MAP;
46 case GL_INT_SAMPLER_CUBE:
47 case GL_UNSIGNED_INT_SAMPLER_CUBE:
48 return GL_TEXTURE_CUBE_MAP;
49 case GL_SAMPLER_2D_ARRAY:
50 case GL_INT_SAMPLER_2D_ARRAY:
51 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
52 case GL_SAMPLER_2D_ARRAY_SHADOW:
53 return GL_TEXTURE_2D_ARRAY;
54 default: UNREACHABLE();
55 }
56
57 return GL_TEXTURE_2D;
58}
59
Jamie Madillf8dd7b12015-08-05 13:50:08 -040060gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070061{
Jamie Madillbd136f92015-08-10 14:51:37 -040062 gl::InputLayout defaultLayout;
63 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070064 {
Brandon Joneseb994362014-09-24 10:27:28 -070065 if (shaderAttr.type != GL_NONE)
66 {
67 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
68
Jamie Madilld3dfda22015-07-06 08:28:49 -040069 for (size_t rowIndex = 0;
70 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType);
71 ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070072 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040073 GLenum componentType = gl::VariableComponentType(transposedType);
74 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
75 bool pureInt = (componentType != GL_FLOAT);
76 gl::VertexFormatType defaultType = gl::GetVertexFormatType(
77 componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070078
Jamie Madillbd136f92015-08-10 14:51:37 -040079 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070080 }
81 }
82 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040083
84 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070085}
86
87std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
88{
Jamie Madillb4463142014-12-19 14:56:54 -050089 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070090
Jamie Madillb4463142014-12-19 14:56:54 -050091 if (!shaderOutputVars.empty())
92 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070093 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
94 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050095 }
Brandon Joneseb994362014-09-24 10:27:28 -070096
97 return defaultPixelOutput;
98}
99
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700100bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
101{
102 return var.isRowMajorLayout;
103}
104
105bool IsRowMajorLayout(const sh::ShaderVariable &var)
106{
107 return false;
108}
109
Jamie Madill437d2662014-12-05 14:23:35 -0500110struct AttributeSorter
111{
112 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -0500113 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -0500114 {
115 }
116
117 bool operator()(int a, int b)
118 {
Jamie Madill80d934b2015-02-19 10:16:12 -0500119 int indexA = (*originalIndices)[a];
120 int indexB = (*originalIndices)[b];
121
122 if (indexA == -1) return false;
123 if (indexB == -1) return true;
124 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -0500125 }
126
Jamie Madill80d934b2015-02-19 10:16:12 -0500127 const ProgramImpl::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500128};
129
Brandon Joneseb994362014-09-24 10:27:28 -0700130}
131
Jamie Madilld3dfda22015-07-06 08:28:49 -0400132ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
133 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500134 ShaderExecutableD3D *shaderExecutable)
Jamie Madilld3dfda22015-07-06 08:28:49 -0400135 : mInputs(inputLayout),
136 mSignature(signature),
137 mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700138{
Brandon Joneseb994362014-09-24 10:27:28 -0700139}
140
141ProgramD3D::VertexExecutable::~VertexExecutable()
142{
143 SafeDelete(mShaderExecutable);
144}
145
Jamie Madilld3dfda22015-07-06 08:28:49 -0400146// static
147void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
148 const gl::InputLayout &inputLayout,
149 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700150{
Jamie Madillbd136f92015-08-10 14:51:37 -0400151 signatureOut->resize(inputLayout.size());
Jamie Madilld3dfda22015-07-06 08:28:49 -0400152
153 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700154 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400155 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbd136f92015-08-10 14:51:37 -0400156 bool converted = false;
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400157 if (vertexFormatType != gl::VERTEX_FORMAT_INVALID)
Brandon Joneseb994362014-09-24 10:27:28 -0700158 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400159 VertexConversionType conversionType =
160 renderer->getVertexConversionType(vertexFormatType);
Jamie Madillbd136f92015-08-10 14:51:37 -0400161 converted = ((conversionType & VERTEX_CONVERT_GPU) != 0);
Brandon Joneseb994362014-09-24 10:27:28 -0700162 }
Jamie Madillbd136f92015-08-10 14:51:37 -0400163
164 (*signatureOut)[index] = converted;
Brandon Joneseb994362014-09-24 10:27:28 -0700165 }
Brandon Joneseb994362014-09-24 10:27:28 -0700166}
167
Jamie Madilld3dfda22015-07-06 08:28:49 -0400168bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
169{
Jamie Madillbd136f92015-08-10 14:51:37 -0400170 size_t limit = std::max(mSignature.size(), signature.size());
171 for (size_t index = 0; index < limit; ++index)
172 {
173 // treat undefined indexes as 'not converted'
174 bool a = index < signature.size() ? signature[index] : false;
175 bool b = index < mSignature.size() ? mSignature[index] : false;
176 if (a != b)
177 return false;
178 }
179
180 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400181}
182
183ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
184 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700185 : mOutputSignature(outputSignature),
186 mShaderExecutable(shaderExecutable)
187{
188}
189
190ProgramD3D::PixelExecutable::~PixelExecutable()
191{
192 SafeDelete(mShaderExecutable);
193}
194
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700195ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
196{
197}
198
Geoff Lang7dd2e102014-11-10 15:19:26 -0500199unsigned int ProgramD3D::mCurrentSerial = 1;
200
Jamie Madill93e13fb2014-11-06 15:27:25 -0500201ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700202 : ProgramImpl(),
203 mRenderer(renderer),
204 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700205 mGeometryExecutable(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700206 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700207 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700208 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700209 mUsedVertexSamplerRange(0),
210 mUsedPixelSamplerRange(0),
211 mDirtySamplerMapping(true),
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400212 mTextureUnitTypesCache(renderer->getRendererCaps().maxCombinedTextureImageUnits),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500213 mShaderVersion(100),
214 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700215{
Brandon Joneseb994362014-09-24 10:27:28 -0700216 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700217}
218
219ProgramD3D::~ProgramD3D()
220{
221 reset();
222 SafeDelete(mDynamicHLSL);
223}
224
Brandon Jones44151a92014-09-10 11:32:25 -0700225bool ProgramD3D::usesPointSpriteEmulation() const
226{
227 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
228}
229
230bool ProgramD3D::usesGeometryShader() const
231{
Cooper Partine6664f02015-01-09 16:22:24 -0800232 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
233}
234
235bool ProgramD3D::usesInstancedPointSpriteEmulation() const
236{
237 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700238}
239
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700240GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
241{
242 GLint logicalTextureUnit = -1;
243
244 switch (type)
245 {
246 case gl::SAMPLER_PIXEL:
247 ASSERT(samplerIndex < caps.maxTextureImageUnits);
248 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
249 {
250 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
251 }
252 break;
253 case gl::SAMPLER_VERTEX:
254 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
255 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
256 {
257 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
258 }
259 break;
260 default: UNREACHABLE();
261 }
262
263 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
264 {
265 return logicalTextureUnit;
266 }
267
268 return -1;
269}
270
271// Returns the texture type for a given Direct3D 9 sampler type and
272// index (0-15 for the pixel shader and 0-3 for the vertex shader).
273GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
274{
275 switch (type)
276 {
277 case gl::SAMPLER_PIXEL:
278 ASSERT(samplerIndex < mSamplersPS.size());
279 ASSERT(mSamplersPS[samplerIndex].active);
280 return mSamplersPS[samplerIndex].textureType;
281 case gl::SAMPLER_VERTEX:
282 ASSERT(samplerIndex < mSamplersVS.size());
283 ASSERT(mSamplersVS[samplerIndex].active);
284 return mSamplersVS[samplerIndex].textureType;
285 default: UNREACHABLE();
286 }
287
288 return GL_TEXTURE_2D;
289}
290
291GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
292{
293 switch (type)
294 {
295 case gl::SAMPLER_PIXEL:
296 return mUsedPixelSamplerRange;
297 case gl::SAMPLER_VERTEX:
298 return mUsedVertexSamplerRange;
299 default:
300 UNREACHABLE();
301 return 0;
302 }
303}
304
305void ProgramD3D::updateSamplerMapping()
306{
307 if (!mDirtySamplerMapping)
308 {
309 return;
310 }
311
312 mDirtySamplerMapping = false;
313
314 // Retrieve sampler uniform values
315 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
316 {
317 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
318
319 if (targetUniform->dirty)
320 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500321 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700322 {
323 int count = targetUniform->elementCount();
324 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
325
326 if (targetUniform->isReferencedByFragmentShader())
327 {
328 unsigned int firstIndex = targetUniform->psRegisterIndex;
329
330 for (int i = 0; i < count; i++)
331 {
332 unsigned int samplerIndex = firstIndex + i;
333
334 if (samplerIndex < mSamplersPS.size())
335 {
336 ASSERT(mSamplersPS[samplerIndex].active);
337 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
338 }
339 }
340 }
341
342 if (targetUniform->isReferencedByVertexShader())
343 {
344 unsigned int firstIndex = targetUniform->vsRegisterIndex;
345
346 for (int i = 0; i < count; i++)
347 {
348 unsigned int samplerIndex = firstIndex + i;
349
350 if (samplerIndex < mSamplersVS.size())
351 {
352 ASSERT(mSamplersVS[samplerIndex].active);
353 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
354 }
355 }
356 }
357 }
358 }
359 }
360}
361
362bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
363{
Jamie Madill13776892015-04-28 12:39:06 -0400364 // Skip cache if we're using an infolog, so we get the full error.
365 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
366 if (!mDirtySamplerMapping && infoLog == nullptr && mCachedValidateSamplersResult.valid())
367 {
368 return mCachedValidateSamplersResult.value();
369 }
370
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700371 // if any two active samplers in a program are of different types, but refer to the same
372 // texture image unit, and this is the current program, then ValidateProgram will fail, and
373 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
374 updateSamplerMapping();
375
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400376 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700377
378 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
379 {
380 if (mSamplersPS[i].active)
381 {
382 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
383
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400384 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700385 {
386 if (infoLog)
387 {
Jamie Madillf6113162015-05-07 11:49:21 -0400388 (*infoLog) << "Sampler uniform (" << unit
389 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
390 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700391 }
392
Jamie Madill13776892015-04-28 12:39:06 -0400393 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700394 return false;
395 }
396
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400397 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700398 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400399 if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700400 {
401 if (infoLog)
402 {
Jamie Madillf6113162015-05-07 11:49:21 -0400403 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
404 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700405 }
406
Jamie Madill13776892015-04-28 12:39:06 -0400407 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700408 return false;
409 }
410 }
411 else
412 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400413 mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700414 }
415 }
416 }
417
418 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
419 {
420 if (mSamplersVS[i].active)
421 {
422 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
423
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400424 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700425 {
426 if (infoLog)
427 {
Jamie Madillf6113162015-05-07 11:49:21 -0400428 (*infoLog) << "Sampler uniform (" << unit
429 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
430 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700431 }
432
Jamie Madill13776892015-04-28 12:39:06 -0400433 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700434 return false;
435 }
436
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400437 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700438 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400439 if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700440 {
441 if (infoLog)
442 {
Jamie Madillf6113162015-05-07 11:49:21 -0400443 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
444 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700445 }
446
Jamie Madill13776892015-04-28 12:39:06 -0400447 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700448 return false;
449 }
450 }
451 else
452 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400453 mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700454 }
455 }
456 }
457
Jamie Madill13776892015-04-28 12:39:06 -0400458 mCachedValidateSamplersResult = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700459 return true;
460}
461
Geoff Lang7dd2e102014-11-10 15:19:26 -0500462LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700463{
Austin Kinross137b1512015-06-17 16:14:53 -0700464 DeviceIdentifier binaryDeviceIdentifier = { 0 };
465 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryDeviceIdentifier), sizeof(DeviceIdentifier));
466
467 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
468 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
469 {
470 infoLog << "Invalid program binary, device configuration has changed.";
471 return LinkResult(false, gl::Error(GL_NO_ERROR));
472 }
473
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500474 int compileFlags = stream->readInt<int>();
475 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
476 {
Jamie Madillf6113162015-05-07 11:49:21 -0400477 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500478 return LinkResult(false, gl::Error(GL_NO_ERROR));
479 }
480
Brandon Jones44151a92014-09-10 11:32:25 -0700481 stream->readInt(&mShaderVersion);
482
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700483 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
484 for (unsigned int i = 0; i < psSamplerCount; ++i)
485 {
486 Sampler sampler;
487 stream->readBool(&sampler.active);
488 stream->readInt(&sampler.logicalTextureUnit);
489 stream->readInt(&sampler.textureType);
490 mSamplersPS.push_back(sampler);
491 }
492 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
493 for (unsigned int i = 0; i < vsSamplerCount; ++i)
494 {
495 Sampler sampler;
496 stream->readBool(&sampler.active);
497 stream->readInt(&sampler.logicalTextureUnit);
498 stream->readInt(&sampler.textureType);
499 mSamplersVS.push_back(sampler);
500 }
501
502 stream->readInt(&mUsedVertexSamplerRange);
503 stream->readInt(&mUsedPixelSamplerRange);
504
505 const unsigned int uniformCount = stream->readInt<unsigned int>();
506 if (stream->error())
507 {
Jamie Madillf6113162015-05-07 11:49:21 -0400508 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500509 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700510 }
511
512 mUniforms.resize(uniformCount);
513 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
514 {
515 GLenum type = stream->readInt<GLenum>();
516 GLenum precision = stream->readInt<GLenum>();
517 std::string name = stream->readString();
518 unsigned int arraySize = stream->readInt<unsigned int>();
519 int blockIndex = stream->readInt<int>();
520
521 int offset = stream->readInt<int>();
522 int arrayStride = stream->readInt<int>();
523 int matrixStride = stream->readInt<int>();
524 bool isRowMajorMatrix = stream->readBool();
525
526 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
527
528 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
529
530 stream->readInt(&uniform->psRegisterIndex);
531 stream->readInt(&uniform->vsRegisterIndex);
532 stream->readInt(&uniform->registerCount);
533 stream->readInt(&uniform->registerElement);
534
535 mUniforms[uniformIndex] = uniform;
536 }
537
538 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
539 if (stream->error())
540 {
Jamie Madillf6113162015-05-07 11:49:21 -0400541 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500542 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700543 }
544
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700545 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
546 {
Geoff Lang95137842015-06-02 15:38:43 -0400547 GLuint location;
548 stream->readInt(&location);
549
550 gl::VariableLocation variable;
551 stream->readString(&variable.name);
552 stream->readInt(&variable.element);
553 stream->readInt(&variable.index);
554
555 mUniformIndex[location] = variable;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700556 }
557
558 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
559 if (stream->error())
560 {
Jamie Madillf6113162015-05-07 11:49:21 -0400561 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500562 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700563 }
564
565 mUniformBlocks.resize(uniformBlockCount);
566 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
567 {
568 std::string name = stream->readString();
569 unsigned int elementIndex = stream->readInt<unsigned int>();
570 unsigned int dataSize = stream->readInt<unsigned int>();
571
572 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
573
574 stream->readInt(&uniformBlock->psRegisterIndex);
575 stream->readInt(&uniformBlock->vsRegisterIndex);
576
577 unsigned int numMembers = stream->readInt<unsigned int>();
578 uniformBlock->memberUniformIndexes.resize(numMembers);
579 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
580 {
581 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
582 }
583
584 mUniformBlocks[uniformBlockIndex] = uniformBlock;
585 }
586
Brandon Joneseb994362014-09-24 10:27:28 -0700587 stream->readInt(&mTransformFeedbackBufferMode);
588 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
589 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
590 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
591 {
592 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
593
594 stream->readString(&varying.name);
595 stream->readInt(&varying.type);
596 stream->readInt(&varying.size);
597 stream->readString(&varying.semanticName);
598 stream->readInt(&varying.semanticIndex);
599 stream->readInt(&varying.semanticIndexCount);
600 }
601
Brandon Jones22502d52014-08-29 16:58:36 -0700602 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530603 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700604 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530605 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700606 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700607 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700608
609 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
610 mPixelShaderKey.resize(pixelShaderKeySize);
611 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
612 {
613 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
614 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
615 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
616 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
617 }
618
Brandon Joneseb994362014-09-24 10:27:28 -0700619 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
620
621 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
622 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
623 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400624 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400625 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700626
Jamie Madilld3dfda22015-07-06 08:28:49 -0400627 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700628 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400629 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700630 }
631
632 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
633 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400634
Geoff Lang359ef262015-01-05 14:42:29 -0500635 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400636 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
637 SHADER_VERTEX,
638 mTransformFeedbackLinkedVaryings,
639 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
640 &shaderExecutable);
641 if (error.isError())
642 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500643 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400644 }
645
Brandon Joneseb994362014-09-24 10:27:28 -0700646 if (!shaderExecutable)
647 {
Jamie Madillf6113162015-05-07 11:49:21 -0400648 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500649 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700650 }
651
652 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400653 VertexExecutable::Signature signature;
654 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700655
656 // add new binary
657 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
658
659 stream->skip(vertexShaderSize);
660 }
661
662 const size_t pixelShaderCount = stream->readInt<unsigned int>();
663 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
664 {
665 const size_t outputCount = stream->readInt<unsigned int>();
666 std::vector<GLenum> outputs(outputCount);
667 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
668 {
669 stream->readInt(&outputs[outputIndex]);
670 }
671
672 const size_t pixelShaderSize = stream->readInt<unsigned int>();
673 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500674 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400675 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
676 mTransformFeedbackLinkedVaryings,
677 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
678 &shaderExecutable);
679 if (error.isError())
680 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500681 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400682 }
Brandon Joneseb994362014-09-24 10:27:28 -0700683
684 if (!shaderExecutable)
685 {
Jamie Madillf6113162015-05-07 11:49:21 -0400686 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500687 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700688 }
689
690 // add new binary
691 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
692
693 stream->skip(pixelShaderSize);
694 }
695
696 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
697
698 if (geometryShaderSize > 0)
699 {
700 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400701 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
702 mTransformFeedbackLinkedVaryings,
703 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
704 &mGeometryExecutable);
705 if (error.isError())
706 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500707 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400708 }
Brandon Joneseb994362014-09-24 10:27:28 -0700709
710 if (!mGeometryExecutable)
711 {
Jamie Madillf6113162015-05-07 11:49:21 -0400712 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500713 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700714 }
715 stream->skip(geometryShaderSize);
716 }
717
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700718 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500719 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700720
Geoff Lang7dd2e102014-11-10 15:19:26 -0500721 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700722}
723
Geoff Langb543aff2014-09-30 14:52:54 -0400724gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700725{
Austin Kinross137b1512015-06-17 16:14:53 -0700726 // Output the DeviceIdentifier before we output any shader code
727 // When we load the binary again later, we can validate the device identifier before trying to compile any HLSL
728 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
729 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(DeviceIdentifier));
730
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500731 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
732
Brandon Jones44151a92014-09-10 11:32:25 -0700733 stream->writeInt(mShaderVersion);
734
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700735 stream->writeInt(mSamplersPS.size());
736 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
737 {
738 stream->writeInt(mSamplersPS[i].active);
739 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
740 stream->writeInt(mSamplersPS[i].textureType);
741 }
742
743 stream->writeInt(mSamplersVS.size());
744 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
745 {
746 stream->writeInt(mSamplersVS[i].active);
747 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
748 stream->writeInt(mSamplersVS[i].textureType);
749 }
750
751 stream->writeInt(mUsedVertexSamplerRange);
752 stream->writeInt(mUsedPixelSamplerRange);
753
754 stream->writeInt(mUniforms.size());
755 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
756 {
757 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
758
759 stream->writeInt(uniform.type);
760 stream->writeInt(uniform.precision);
761 stream->writeString(uniform.name);
762 stream->writeInt(uniform.arraySize);
763 stream->writeInt(uniform.blockIndex);
764
765 stream->writeInt(uniform.blockInfo.offset);
766 stream->writeInt(uniform.blockInfo.arrayStride);
767 stream->writeInt(uniform.blockInfo.matrixStride);
768 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
769
770 stream->writeInt(uniform.psRegisterIndex);
771 stream->writeInt(uniform.vsRegisterIndex);
772 stream->writeInt(uniform.registerCount);
773 stream->writeInt(uniform.registerElement);
774 }
775
776 stream->writeInt(mUniformIndex.size());
Geoff Lang95137842015-06-02 15:38:43 -0400777 for (const auto &uniform : mUniformIndex)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700778 {
Geoff Lang95137842015-06-02 15:38:43 -0400779 GLuint location = uniform.first;
780 stream->writeInt(location);
781
782 const gl::VariableLocation &variable = uniform.second;
783 stream->writeString(variable.name);
784 stream->writeInt(variable.element);
785 stream->writeInt(variable.index);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700786 }
787
788 stream->writeInt(mUniformBlocks.size());
789 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
790 {
791 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
792
793 stream->writeString(uniformBlock.name);
794 stream->writeInt(uniformBlock.elementIndex);
795 stream->writeInt(uniformBlock.dataSize);
796
797 stream->writeInt(uniformBlock.memberUniformIndexes.size());
798 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
799 {
800 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
801 }
802
803 stream->writeInt(uniformBlock.psRegisterIndex);
804 stream->writeInt(uniformBlock.vsRegisterIndex);
805 }
806
Brandon Joneseb994362014-09-24 10:27:28 -0700807 stream->writeInt(mTransformFeedbackBufferMode);
808 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
809 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
810 {
811 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
812
813 stream->writeString(varying.name);
814 stream->writeInt(varying.type);
815 stream->writeInt(varying.size);
816 stream->writeString(varying.semanticName);
817 stream->writeInt(varying.semanticIndex);
818 stream->writeInt(varying.semanticIndexCount);
819 }
820
Brandon Jones22502d52014-08-29 16:58:36 -0700821 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530822 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700823 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530824 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700825 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700826 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700827
Brandon Joneseb994362014-09-24 10:27:28 -0700828 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700829 stream->writeInt(pixelShaderKey.size());
830 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
831 {
Brandon Joneseb994362014-09-24 10:27:28 -0700832 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700833 stream->writeInt(variable.type);
834 stream->writeString(variable.name);
835 stream->writeString(variable.source);
836 stream->writeInt(variable.outputIndex);
837 }
838
Brandon Joneseb994362014-09-24 10:27:28 -0700839 stream->writeInt(mVertexExecutables.size());
840 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
841 {
842 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
843
Jamie Madilld3dfda22015-07-06 08:28:49 -0400844 const auto &inputLayout = vertexExecutable->inputs();
845 stream->writeInt(inputLayout.size());
846
847 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700848 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400849 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -0700850 }
851
852 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
853 stream->writeInt(vertexShaderSize);
854
855 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
856 stream->writeBytes(vertexBlob, vertexShaderSize);
857 }
858
859 stream->writeInt(mPixelExecutables.size());
860 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
861 {
862 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
863
864 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
865 stream->writeInt(outputs.size());
866 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
867 {
868 stream->writeInt(outputs[outputIndex]);
869 }
870
871 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
872 stream->writeInt(pixelShaderSize);
873
874 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
875 stream->writeBytes(pixelBlob, pixelShaderSize);
876 }
877
878 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
879 stream->writeInt(geometryShaderSize);
880
881 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
882 {
883 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
884 stream->writeBytes(geometryBlob, geometryShaderSize);
885 }
886
Geoff Langb543aff2014-09-30 14:52:54 -0400887 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700888}
889
Geoff Lang359ef262015-01-05 14:42:29 -0500890gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700891{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400892 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700893
Jamie Madill85a18042015-03-05 15:41:41 -0500894 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
895 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700896
897 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
898 {
899 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
900
901 if (colorbuffer)
902 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400903 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700904 }
905 else
906 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400907 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700908 }
909 }
910
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400911 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700912}
913
Jamie Madill97399232014-12-23 12:31:15 -0500914gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500915 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500916 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700917{
918 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
919 {
920 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
921 {
Geoff Langb543aff2014-09-30 14:52:54 -0400922 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
923 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700924 }
925 }
926
Brandon Jones22502d52014-08-29 16:58:36 -0700927 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
928 outputSignature);
929
930 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500931 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500932
933 gl::InfoLog tempInfoLog;
934 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
935
936 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400937 mTransformFeedbackLinkedVaryings,
938 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
939 mPixelWorkarounds, &pixelExecutable);
940 if (error.isError())
941 {
942 return error;
943 }
Brandon Joneseb994362014-09-24 10:27:28 -0700944
Jamie Madill97399232014-12-23 12:31:15 -0500945 if (pixelExecutable)
946 {
947 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
948 }
949 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700950 {
951 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700952 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -0700953 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
954 }
Brandon Jones22502d52014-08-29 16:58:36 -0700955
Geoff Langb543aff2014-09-30 14:52:54 -0400956 *outExectuable = pixelExecutable;
957 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700958}
959
Jamie Madilld3dfda22015-07-06 08:28:49 -0400960gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -0500961 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500962 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700963{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400964 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -0700965
966 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
967 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400968 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -0700969 {
Geoff Langb543aff2014-09-30 14:52:54 -0400970 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
971 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700972 }
973 }
974
Brandon Jones22502d52014-08-29 16:58:36 -0700975 // Generate new dynamic layout with attribute conversions
Jamie Madill3da79b72015-04-27 11:09:17 -0400976 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, getShaderAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -0700977
978 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500979 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500980
981 gl::InfoLog tempInfoLog;
982 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
983
984 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400985 mTransformFeedbackLinkedVaryings,
986 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
987 mVertexWorkarounds, &vertexExecutable);
988 if (error.isError())
989 {
990 return error;
991 }
992
Jamie Madill97399232014-12-23 12:31:15 -0500993 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700994 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400995 mVertexExecutables.push_back(new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700996 }
Jamie Madill97399232014-12-23 12:31:15 -0500997 else if (!infoLog)
998 {
999 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001000 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001001 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1002 }
Brandon Jones22502d52014-08-29 16:58:36 -07001003
Geoff Langb543aff2014-09-30 14:52:54 -04001004 *outExectuable = vertexExecutable;
1005 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001006}
1007
Geoff Lang7dd2e102014-11-10 15:19:26 -05001008LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
1009 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -07001010{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001011 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1012 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones44151a92014-09-10 11:32:25 -07001013
Jamie Madillf8dd7b12015-08-05 13:50:08 -04001014 const gl::InputLayout &defaultInputLayout = GetDefaultInputLayoutFromShader(vertexShader);
Jamie Madille4ea2022015-03-26 20:35:05 +00001015 ShaderExecutableD3D *defaultVertexExecutable = NULL;
1016 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
1017 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001018 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001019 return LinkResult(false, error);
1020 }
Austin Kinross434953e2015-02-20 10:49:51 -08001021
Brandon Joneseb994362014-09-24 10:27:28 -07001022 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001023 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +00001024 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001025 if (error.isError())
1026 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001027 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001028 }
Brandon Jones44151a92014-09-10 11:32:25 -07001029
Brandon Joneseb994362014-09-24 10:27:28 -07001030 if (usesGeometryShader())
1031 {
1032 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001033
Geoff Langb543aff2014-09-30 14:52:54 -04001034
1035 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1036 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +05301037 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001038 if (error.isError())
1039 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001040 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001041 }
Brandon Joneseb994362014-09-24 10:27:28 -07001042 }
1043
Brandon Jones091540d2014-10-29 11:32:04 -07001044#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001045 if (usesGeometryShader() && mGeometryExecutable)
1046 {
1047 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1048 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1049 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1050 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1051 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1052 }
1053
1054 if (defaultVertexExecutable)
1055 {
1056 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1057 }
1058
1059 if (defaultPixelExecutable)
1060 {
1061 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1062 }
1063#endif
1064
Geoff Langb543aff2014-09-30 14:52:54 -04001065 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001066 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001067}
1068
Geoff Lang7dd2e102014-11-10 15:19:26 -05001069LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1070 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1071 const std::vector<std::string> &transformFeedbackVaryings,
1072 GLenum transformFeedbackBufferMode,
1073 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1074 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001075{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001076 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1077 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001078
Jamie Madillde8892b2014-11-11 13:00:22 -05001079 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1080 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001081
Brandon Joneseb994362014-09-24 10:27:28 -07001082 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001083
1084 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301085 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001086
1087 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301088 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001089 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001090
Austin Kinross02df7962015-07-01 10:03:42 -07001091 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1092 {
1093 if (fragmentShaderD3D->usesFrontFacing())
1094 {
1095 infoLog << "The current renderer doesn't support gl_FrontFacing";
1096 return LinkResult(false, gl::Error(GL_NO_ERROR));
1097 }
1098 }
1099
Brandon Jones22502d52014-08-29 16:58:36 -07001100 // Map the varyings to the register file
Daniel Chengf33ab832015-06-30 19:08:16 -07001101 VaryingPacking packing = {};
Brandon Jones22502d52014-08-29 16:58:36 -07001102 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1103
Geoff Langbdee2d52014-09-17 11:02:51 -04001104 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001105 {
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
Geoff Lang7dd2e102014-11-10 15:19:26 -05001109 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001110 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001111 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001112 }
1113
Jamie Madillde8892b2014-11-11 13:00:22 -05001114 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001115 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1116 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1117 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001118 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001119 }
1120
Brandon Jones44151a92014-09-10 11:32:25 -07001121 mUsesPointSize = vertexShaderD3D->usesPointSize();
1122
Jamie Madill437d2662014-12-05 14:23:35 -05001123 initAttributesByLayout();
1124
Geoff Lang7dd2e102014-11-10 15:19:26 -05001125 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001126}
1127
Geoff Lang0ca53782015-05-07 13:49:39 -04001128void ProgramD3D::bindAttributeLocation(GLuint index, const std::string &name)
1129{
1130}
1131
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001132void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001133{
1134 // Compute total default block size
1135 unsigned int vertexRegisters = 0;
1136 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001137 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001138 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001139 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001140
Geoff Lang2ec386b2014-12-03 14:44:38 -05001141 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001142 {
1143 if (uniform.isReferencedByVertexShader())
1144 {
1145 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1146 }
1147 if (uniform.isReferencedByFragmentShader())
1148 {
1149 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1150 }
1151 }
1152 }
1153
1154 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1155 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1156}
1157
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001158gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001159{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001160 updateSamplerMapping();
1161
1162 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1163 if (error.isError())
1164 {
1165 return error;
1166 }
1167
1168 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1169 {
1170 mUniforms[uniformIndex]->dirty = false;
1171 }
1172
1173 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001174}
1175
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001176gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001177{
Jamie Madill03260fa2015-06-22 13:57:22 -04001178 mVertexUBOCache.clear();
1179 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001180
1181 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1182 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1183
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001184 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001185 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001186 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001187 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001188
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001189 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001190
1191 // Unnecessary to apply an unreferenced standard or shared UBO
1192 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1193 {
1194 continue;
1195 }
1196
1197 if (uniformBlock->isReferencedByVertexShader())
1198 {
1199 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001200 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001201
Jamie Madill969194d2015-07-20 14:36:56 -04001202 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001203 {
1204 mVertexUBOCache.resize(registerIndex + 1, -1);
1205 }
1206
1207 ASSERT(mVertexUBOCache[registerIndex] == -1);
1208 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001209 }
1210
1211 if (uniformBlock->isReferencedByFragmentShader())
1212 {
1213 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001214 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001215
1216 if (mFragmentUBOCache.size() <= registerIndex)
1217 {
1218 mFragmentUBOCache.resize(registerIndex + 1, -1);
1219 }
1220
1221 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1222 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001223 }
1224 }
1225
Jamie Madill03260fa2015-06-22 13:57:22 -04001226 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001227}
1228
1229bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1230 unsigned int registerIndex, const gl::Caps &caps)
1231{
1232 if (shader == GL_VERTEX_SHADER)
1233 {
1234 uniformBlock->vsRegisterIndex = registerIndex;
1235 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1236 {
Jamie Madillf6113162015-05-07 11:49:21 -04001237 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (" << caps.maxVertexUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001238 return false;
1239 }
1240 }
1241 else if (shader == GL_FRAGMENT_SHADER)
1242 {
1243 uniformBlock->psRegisterIndex = registerIndex;
1244 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1245 {
Jamie Madillf6113162015-05-07 11:49:21 -04001246 infoLog << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (" << caps.maxFragmentUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001247 return false;
1248 }
1249 }
1250 else UNREACHABLE();
1251
1252 return true;
1253}
1254
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001255void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001256{
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001257 unsigned int numUniforms = static_cast<unsigned int>(mUniforms.size());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001258 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001259 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001260 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001261 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001262}
1263
1264void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1265{
1266 setUniform(location, count, v, GL_FLOAT);
1267}
1268
1269void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1270{
1271 setUniform(location, count, v, GL_FLOAT_VEC2);
1272}
1273
1274void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1275{
1276 setUniform(location, count, v, GL_FLOAT_VEC3);
1277}
1278
1279void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1280{
1281 setUniform(location, count, v, GL_FLOAT_VEC4);
1282}
1283
1284void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1285{
1286 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1287}
1288
1289void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1290{
1291 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1292}
1293
1294void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1295{
1296 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1297}
1298
1299void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1300{
1301 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1302}
1303
1304void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1305{
1306 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1307}
1308
1309void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1310{
1311 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1312}
1313
1314void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1315{
1316 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1317}
1318
1319void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1320{
1321 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1322}
1323
1324void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1325{
1326 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1327}
1328
1329void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1330{
1331 setUniform(location, count, v, GL_INT);
1332}
1333
1334void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1335{
1336 setUniform(location, count, v, GL_INT_VEC2);
1337}
1338
1339void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1340{
1341 setUniform(location, count, v, GL_INT_VEC3);
1342}
1343
1344void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1345{
1346 setUniform(location, count, v, GL_INT_VEC4);
1347}
1348
1349void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1350{
1351 setUniform(location, count, v, GL_UNSIGNED_INT);
1352}
1353
1354void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1355{
1356 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1357}
1358
1359void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1360{
1361 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1362}
1363
1364void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1365{
1366 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1367}
1368
1369void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1370{
1371 getUniformv(location, params, GL_FLOAT);
1372}
1373
1374void ProgramD3D::getUniformiv(GLint location, GLint *params)
1375{
1376 getUniformv(location, params, GL_INT);
1377}
1378
1379void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1380{
1381 getUniformv(location, params, GL_UNSIGNED_INT);
1382}
1383
1384bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1385 const gl::Caps &caps)
1386{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001387 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1388 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001389
1390 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1391 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1392
1393 // Check that uniforms defined in the vertex and fragment shaders are identical
1394 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1395 UniformMap linkedUniforms;
1396
1397 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001398 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001399 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1400 linkedUniforms[vertexUniform.name] = &vertexUniform;
1401 }
1402
1403 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1404 {
1405 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1406 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1407 if (entry != linkedUniforms.end())
1408 {
1409 const sh::Uniform &vertexUniform = *entry->second;
1410 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001411 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001412 {
1413 return false;
1414 }
1415 }
1416 }
1417
1418 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1419 {
1420 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1421
1422 if (uniform.staticUse)
1423 {
Jamie Madill55def582015-05-04 11:24:57 -04001424 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1425 vertexShaderD3D->getUniformRegister(uniform.name);
1426 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001427 }
1428 }
1429
1430 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1431 {
1432 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1433
1434 if (uniform.staticUse)
1435 {
Jamie Madill55def582015-05-04 11:24:57 -04001436 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1437 fragmentShaderD3D->getUniformRegister(uniform.name);
1438 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001439 }
1440 }
1441
1442 if (!indexUniforms(infoLog, caps))
1443 {
1444 return false;
1445 }
1446
1447 initializeUniformStorage();
1448
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001449 return true;
1450}
1451
Geoff Lang492a7e42014-11-05 13:27:06 -05001452void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001453{
Jamie Madill55def582015-05-04 11:24:57 -04001454 if (uniformRegister == GL_INVALID_INDEX)
1455 {
1456 defineUniform(shader, uniform, uniform.name, nullptr);
1457 return;
1458 }
1459
Geoff Lang492a7e42014-11-05 13:27:06 -05001460 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001461 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1462 encoder.skipRegisters(uniformRegister);
1463
1464 defineUniform(shader, uniform, uniform.name, &encoder);
1465}
1466
Geoff Lang492a7e42014-11-05 13:27:06 -05001467void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1468 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001469{
1470 if (uniform.isStruct())
1471 {
1472 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1473 {
1474 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1475
Jamie Madill55def582015-05-04 11:24:57 -04001476 if (encoder)
1477 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001478
1479 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1480 {
1481 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1482 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1483
1484 defineUniform(shader, field, fieldFullName, encoder);
1485 }
1486
Jamie Madill55def582015-05-04 11:24:57 -04001487 if (encoder)
1488 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001489 }
1490 }
1491 else // Not a struct
1492 {
1493 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001494 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001495 {
1496 encoder->enterAggregateType();
1497 }
1498
1499 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1500
Jamie Madill2857f482015-02-09 15:35:29 -05001501 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001502 sh::BlockMemberInfo blockInfo = encoder ?
1503 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1504 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001505
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001506 if (!linkedUniform)
1507 {
1508 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001509 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001510 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001511
1512 if (encoder)
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001513 linkedUniform->registerElement = static_cast<unsigned int>(
1514 sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001515 mUniforms.push_back(linkedUniform);
1516 }
1517
Jamie Madill55def582015-05-04 11:24:57 -04001518 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001519 {
Jamie Madill55def582015-05-04 11:24:57 -04001520 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1521 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001522 linkedUniform->psRegisterIndex =
1523 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill55def582015-05-04 11:24:57 -04001524 }
1525 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1526 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001527 linkedUniform->vsRegisterIndex =
1528 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill55def582015-05-04 11:24:57 -04001529 }
1530 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001531 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001532
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001533 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001534 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001535 {
1536 encoder->exitAggregateType();
1537 }
1538 }
1539}
1540
1541template <typename T>
1542static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1543{
1544 ASSERT(dest != NULL);
1545 ASSERT(dirtyFlag != NULL);
1546
1547 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1548 *dest = source;
1549}
1550
1551template <typename T>
1552void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1553{
1554 const int components = gl::VariableComponentCount(targetUniformType);
1555 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1556
1557 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1558
1559 int elementCount = targetUniform->elementCount();
1560
1561 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1562
1563 if (targetUniform->type == targetUniformType)
1564 {
1565 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1566
1567 for (int i = 0; i < count; i++)
1568 {
1569 T *dest = target + (i * 4);
1570 const T *source = v + (i * components);
1571
1572 for (int c = 0; c < components; c++)
1573 {
1574 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1575 }
1576 for (int c = components; c < 4; c++)
1577 {
1578 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1579 }
1580 }
1581 }
1582 else if (targetUniform->type == targetBoolType)
1583 {
1584 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1585
1586 for (int i = 0; i < count; i++)
1587 {
1588 GLint *dest = boolParams + (i * 4);
1589 const T *source = v + (i * components);
1590
1591 for (int c = 0; c < components; c++)
1592 {
1593 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1594 }
1595 for (int c = components; c < 4; c++)
1596 {
1597 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1598 }
1599 }
1600 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001601 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001602 {
1603 ASSERT(targetUniformType == GL_INT);
1604
1605 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1606
1607 bool wasDirty = targetUniform->dirty;
1608
1609 for (int i = 0; i < count; i++)
1610 {
1611 GLint *dest = target + (i * 4);
1612 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1613
1614 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1615 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1616 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1617 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1618 }
1619
1620 if (!wasDirty && targetUniform->dirty)
1621 {
1622 mDirtySamplerMapping = true;
1623 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001624 }
1625 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001626}
Brandon Jones18bd4102014-09-22 14:21:44 -07001627
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001628template<typename T>
1629bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1630{
1631 bool dirty = false;
1632 int copyWidth = std::min(targetHeight, srcWidth);
1633 int copyHeight = std::min(targetWidth, srcHeight);
1634
1635 for (int x = 0; x < copyWidth; x++)
1636 {
1637 for (int y = 0; y < copyHeight; y++)
1638 {
1639 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1640 }
1641 }
1642 // clear unfilled right side
1643 for (int y = 0; y < copyWidth; y++)
1644 {
1645 for (int x = copyHeight; x < targetWidth; x++)
1646 {
1647 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1648 }
1649 }
1650 // clear unfilled bottom.
1651 for (int y = copyWidth; y < targetHeight; y++)
1652 {
1653 for (int x = 0; x < targetWidth; x++)
1654 {
1655 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1656 }
1657 }
1658
1659 return dirty;
1660}
1661
1662template<typename T>
1663bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1664{
1665 bool dirty = false;
1666 int copyWidth = std::min(targetWidth, srcWidth);
1667 int copyHeight = std::min(targetHeight, srcHeight);
1668
1669 for (int y = 0; y < copyHeight; y++)
1670 {
1671 for (int x = 0; x < copyWidth; x++)
1672 {
1673 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1674 }
1675 }
1676 // clear unfilled right side
1677 for (int y = 0; y < copyHeight; y++)
1678 {
1679 for (int x = copyWidth; x < targetWidth; x++)
1680 {
1681 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1682 }
1683 }
1684 // clear unfilled bottom.
1685 for (int y = copyHeight; y < targetHeight; y++)
1686 {
1687 for (int x = 0; x < targetWidth; x++)
1688 {
1689 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1690 }
1691 }
1692
1693 return dirty;
1694}
1695
1696template <int cols, int rows>
1697void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1698{
1699 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1700
1701 int elementCount = targetUniform->elementCount();
1702
1703 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1704 const unsigned int targetMatrixStride = (4 * rows);
1705 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1706
1707 for (int i = 0; i < count; i++)
1708 {
1709 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1710 if (transpose == GL_FALSE)
1711 {
1712 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1713 }
1714 else
1715 {
1716 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1717 }
1718 target += targetMatrixStride;
1719 value += cols * rows;
1720 }
1721}
1722
1723template <typename T>
1724void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1725{
1726 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1727
1728 if (gl::IsMatrixType(targetUniform->type))
1729 {
1730 const int rows = gl::VariableRowCount(targetUniform->type);
1731 const int cols = gl::VariableColumnCount(targetUniform->type);
1732 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1733 }
1734 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1735 {
1736 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1737 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1738 size * sizeof(T));
1739 }
1740 else
1741 {
1742 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1743 switch (gl::VariableComponentType(targetUniform->type))
1744 {
1745 case GL_BOOL:
1746 {
1747 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1748
1749 for (unsigned int i = 0; i < size; i++)
1750 {
1751 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1752 }
1753 }
1754 break;
1755
1756 case GL_FLOAT:
1757 {
1758 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1759
1760 for (unsigned int i = 0; i < size; i++)
1761 {
1762 params[i] = static_cast<T>(floatParams[i]);
1763 }
1764 }
1765 break;
1766
1767 case GL_INT:
1768 {
1769 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1770
1771 for (unsigned int i = 0; i < size; i++)
1772 {
1773 params[i] = static_cast<T>(intParams[i]);
1774 }
1775 }
1776 break;
1777
1778 case GL_UNSIGNED_INT:
1779 {
1780 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1781
1782 for (unsigned int i = 0; i < size; i++)
1783 {
1784 params[i] = static_cast<T>(uintParams[i]);
1785 }
1786 }
1787 break;
1788
1789 default: UNREACHABLE();
1790 }
1791 }
1792}
1793
1794template <typename VarT>
1795void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1796 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1797 bool inRowMajorLayout)
1798{
1799 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1800 {
1801 const VarT &field = fields[uniformIndex];
1802 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1803
1804 if (field.isStruct())
1805 {
1806 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1807
1808 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1809 {
1810 encoder->enterAggregateType();
1811
1812 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1813 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1814
1815 encoder->exitAggregateType();
1816 }
1817 }
1818 else
1819 {
1820 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1821
1822 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1823
1824 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1825 blockIndex, memberInfo);
1826
1827 // add to uniform list, but not index, since uniform block uniforms have no location
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001828 blockUniformIndexes->push_back(static_cast<GLenum>(mUniforms.size()));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001829 mUniforms.push_back(newUniform);
1830 }
1831 }
1832}
1833
Jamie Madilld3dfda22015-07-06 08:28:49 -04001834bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog,
1835 const gl::Shader &shader,
1836 const sh::InterfaceBlock &interfaceBlock,
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001837 const gl::Caps &caps)
1838{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001839 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001840
1841 // create uniform block entries if they do not exist
1842 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1843 {
1844 std::vector<unsigned int> blockUniformIndexes;
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001845 const unsigned int blockIndex = static_cast<unsigned int>(mUniformBlocks.size());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001846
1847 // define member uniforms
1848 sh::BlockLayoutEncoder *encoder = NULL;
1849
1850 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1851 {
1852 encoder = new sh::Std140BlockEncoder;
1853 }
1854 else
1855 {
1856 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1857 }
1858 ASSERT(encoder);
1859
1860 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1861
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001862 unsigned int dataSize = static_cast<unsigned int>(encoder->getBlockSize());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001863
1864 // create all the uniform blocks
1865 if (interfaceBlock.arraySize > 0)
1866 {
1867 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1868 {
1869 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1870 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1871 mUniformBlocks.push_back(newUniformBlock);
1872 }
1873 }
1874 else
1875 {
1876 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1877 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1878 mUniformBlocks.push_back(newUniformBlock);
1879 }
1880 }
1881
1882 if (interfaceBlock.staticUse)
1883 {
1884 // Assign registers to the uniform blocks
1885 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1886 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1887 ASSERT(blockIndex != GL_INVALID_INDEX);
1888 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1889
1890 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1891
1892 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1893 {
1894 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1895 ASSERT(uniformBlock->name == interfaceBlock.name);
1896
1897 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1898 interfaceBlockRegister + uniformBlockElement, caps))
1899 {
1900 return false;
1901 }
1902 }
1903 }
1904
1905 return true;
1906}
1907
1908bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04001909 GLenum samplerType,
1910 unsigned int samplerCount,
1911 std::vector<Sampler> &outSamplers,
1912 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001913{
1914 unsigned int samplerIndex = startSamplerIndex;
1915
1916 do
1917 {
1918 if (samplerIndex < outSamplers.size())
1919 {
1920 Sampler& sampler = outSamplers[samplerIndex];
1921 sampler.active = true;
1922 sampler.textureType = GetTextureType(samplerType);
1923 sampler.logicalTextureUnit = 0;
1924 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1925 }
1926 else
1927 {
1928 return false;
1929 }
1930
1931 samplerIndex++;
1932 } while (samplerIndex < startSamplerIndex + samplerCount);
1933
1934 return true;
1935}
1936
1937bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1938{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001939 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001940 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1941
1942 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1943 {
1944 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1945 &mUsedVertexSamplerRange))
1946 {
Jamie Madillf6113162015-05-07 11:49:21 -04001947 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1948 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001949 return false;
1950 }
1951
1952 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1953 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1954 {
Jamie Madillf6113162015-05-07 11:49:21 -04001955 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1956 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001957 return false;
1958 }
1959 }
1960
1961 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1962 {
1963 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1964 &mUsedPixelSamplerRange))
1965 {
Jamie Madillf6113162015-05-07 11:49:21 -04001966 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
1967 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001968 return false;
1969 }
1970
1971 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1972 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1973 {
Jamie Madillf6113162015-05-07 11:49:21 -04001974 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
1975 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001976 return false;
1977 }
1978 }
1979
1980 return true;
1981}
1982
1983bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1984{
1985 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1986 {
1987 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1988
Geoff Lang2ec386b2014-12-03 14:44:38 -05001989 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990 {
1991 if (!indexSamplerUniform(uniform, infoLog, caps))
1992 {
1993 return false;
1994 }
1995 }
1996
Jamie Madill55def582015-05-04 11:24:57 -04001997 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001998 {
Jamie Madill55def582015-05-04 11:24:57 -04001999 if (!uniform.isBuiltIn())
2000 {
Geoff Lang95137842015-06-02 15:38:43 -04002001 // Assign in-order uniform locations
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002002 mUniformIndex[static_cast<GLuint>(mUniformIndex.size())] = gl::VariableLocation(
2003 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex));
Jamie Madill55def582015-05-04 11:24:57 -04002004 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002005 }
2006 }
2007
2008 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07002009}
2010
Brandon Jonesc9610c52014-08-25 17:02:59 -07002011void ProgramD3D::reset()
2012{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002013 ProgramImpl::reset();
2014
Brandon Joneseb994362014-09-24 10:27:28 -07002015 SafeDeleteContainer(mVertexExecutables);
2016 SafeDeleteContainer(mPixelExecutables);
2017 SafeDelete(mGeometryExecutable);
2018
2019 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07002020
Brandon Jones22502d52014-08-29 16:58:36 -07002021 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002022 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07002023 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07002024
2025 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002026 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002027 mUsesFragDepth = false;
2028 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002029 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002030
Brandon Jonesc9610c52014-08-25 17:02:59 -07002031 SafeDelete(mVertexUniformStorage);
2032 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002033
2034 mSamplersPS.clear();
2035 mSamplersVS.clear();
2036
2037 mUsedVertexSamplerRange = 0;
2038 mUsedPixelSamplerRange = 0;
2039 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002040
2041 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002042}
2043
Geoff Lang7dd2e102014-11-10 15:19:26 -05002044unsigned int ProgramD3D::getSerial() const
2045{
2046 return mSerial;
2047}
2048
2049unsigned int ProgramD3D::issueSerial()
2050{
2051 return mCurrentSerial++;
2052}
2053
Jamie Madill437d2662014-12-05 14:23:35 -05002054void ProgramD3D::initAttributesByLayout()
2055{
2056 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2057 {
2058 mAttributesByLayout[i] = i;
2059 }
2060
2061 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2062}
2063
Jamie Madill476682e2015-06-30 10:04:29 -04002064void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002065 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2066 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002067{
Jamie Madill476682e2015-06-30 10:04:29 -04002068 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002069 {
Jamie Madill476682e2015-06-30 10:04:29 -04002070 int oldIndex = mAttributesByLayout[attribIndex];
2071 sortedSemanticIndicesOut[attribIndex] = mSemanticIndex[oldIndex];
2072 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002073 }
2074}
2075
Jamie Madilld3dfda22015-07-06 08:28:49 -04002076void ProgramD3D::updateCachedInputLayout(const gl::Program *program, const gl::State &state)
2077{
Jamie Madillbd136f92015-08-10 14:51:37 -04002078 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002079 const int *semanticIndexes = program->getSemanticIndexes();
2080
2081 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002082
Jamie Madilld3dfda22015-07-06 08:28:49 -04002083 for (unsigned int attributeIndex = 0; attributeIndex < vertexAttributes.size(); attributeIndex++)
2084 {
2085 int semanticIndex = semanticIndexes[attributeIndex];
2086
2087 if (semanticIndex != -1)
2088 {
Jamie Madillbd136f92015-08-10 14:51:37 -04002089 if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1))
2090 {
2091 mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID);
2092 }
Jamie Madilld3dfda22015-07-06 08:28:49 -04002093 mCachedInputLayout[semanticIndex] =
2094 GetVertexFormatType(vertexAttributes[attributeIndex],
2095 state.getVertexAttribCurrentValue(attributeIndex).Type);
2096 }
2097 }
2098}
2099
Brandon Jonesc9610c52014-08-25 17:02:59 -07002100}