blob: 537461319148277d3852fd2e6c502301382ac999 [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 Madill6df9b372015-02-18 21:28:19 +000015#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050017#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/renderer/d3d/RendererD3D.h"
19#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050020#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050022
Brandon Jonesc9610c52014-08-25 17:02:59 -070023namespace rx
24{
25
Brandon Joneseb994362014-09-24 10:27:28 -070026namespace
27{
28
Brandon Jones1a8a7e32014-10-01 12:49:30 -070029GLenum GetTextureType(GLenum samplerType)
30{
31 switch (samplerType)
32 {
33 case GL_SAMPLER_2D:
34 case GL_INT_SAMPLER_2D:
35 case GL_UNSIGNED_INT_SAMPLER_2D:
36 case GL_SAMPLER_2D_SHADOW:
37 return GL_TEXTURE_2D;
38 case GL_SAMPLER_3D:
39 case GL_INT_SAMPLER_3D:
40 case GL_UNSIGNED_INT_SAMPLER_3D:
41 return GL_TEXTURE_3D;
42 case GL_SAMPLER_CUBE:
43 case GL_SAMPLER_CUBE_SHADOW:
44 return GL_TEXTURE_CUBE_MAP;
45 case GL_INT_SAMPLER_CUBE:
46 case GL_UNSIGNED_INT_SAMPLER_CUBE:
47 return GL_TEXTURE_CUBE_MAP;
48 case GL_SAMPLER_2D_ARRAY:
49 case GL_INT_SAMPLER_2D_ARRAY:
50 case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
51 case GL_SAMPLER_2D_ARRAY_SHADOW:
52 return GL_TEXTURE_2D_ARRAY;
53 default: UNREACHABLE();
54 }
55
56 return GL_TEXTURE_2D;
57}
58
Brandon Joneseb994362014-09-24 10:27:28 -070059void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS])
60{
61 size_t layoutIndex = 0;
62 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
63 {
64 ASSERT(layoutIndex < gl::MAX_VERTEX_ATTRIBS);
65
66 const sh::Attribute &shaderAttr = shaderAttributes[attributeIndex];
67
68 if (shaderAttr.type != GL_NONE)
69 {
70 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
71
72 for (size_t rowIndex = 0; static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); rowIndex++, layoutIndex++)
73 {
74 gl::VertexFormat *defaultFormat = &inputLayout[layoutIndex];
75
76 defaultFormat->mType = gl::VariableComponentType(transposedType);
77 defaultFormat->mNormalized = false;
78 defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool
79 defaultFormat->mComponents = gl::VariableColumnCount(transposedType);
80 }
81 }
82 }
83}
84
85std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
86{
Jamie Madillb4463142014-12-19 14:56:54 -050087 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070088
Jamie Madillb4463142014-12-19 14:56:54 -050089 if (!shaderOutputVars.empty())
90 {
91 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex);
92 }
Brandon Joneseb994362014-09-24 10:27:28 -070093
94 return defaultPixelOutput;
95}
96
Brandon Jones1a8a7e32014-10-01 12:49:30 -070097bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
98{
99 return var.isRowMajorLayout;
100}
101
102bool IsRowMajorLayout(const sh::ShaderVariable &var)
103{
104 return false;
105}
106
Jamie Madill437d2662014-12-05 14:23:35 -0500107struct AttributeSorter
108{
109 AttributeSorter(const ProgramImpl::SemanticIndexArray &semanticIndices)
Jamie Madill80d934b2015-02-19 10:16:12 -0500110 : originalIndices(&semanticIndices)
Jamie Madill437d2662014-12-05 14:23:35 -0500111 {
112 }
113
114 bool operator()(int a, int b)
115 {
Jamie Madill80d934b2015-02-19 10:16:12 -0500116 int indexA = (*originalIndices)[a];
117 int indexB = (*originalIndices)[b];
118
119 if (indexA == -1) return false;
120 if (indexB == -1) return true;
121 return (indexA < indexB);
Jamie Madill437d2662014-12-05 14:23:35 -0500122 }
123
Jamie Madill80d934b2015-02-19 10:16:12 -0500124 const ProgramImpl::SemanticIndexArray *originalIndices;
Jamie Madill437d2662014-12-05 14:23:35 -0500125};
126
Brandon Joneseb994362014-09-24 10:27:28 -0700127}
128
129ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
130 const GLenum signature[],
Geoff Lang359ef262015-01-05 14:42:29 -0500131 ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700132 : mShaderExecutable(shaderExecutable)
133{
134 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
135 {
136 mInputs[attributeIndex] = inputLayout[attributeIndex];
137 mSignature[attributeIndex] = signature[attributeIndex];
138 }
139}
140
141ProgramD3D::VertexExecutable::~VertexExecutable()
142{
143 SafeDelete(mShaderExecutable);
144}
145
146bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
147{
148 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
149 {
150 if (mSignature[attributeIndex] != signature[attributeIndex])
151 {
152 return false;
153 }
154 }
155
156 return true;
157}
158
Geoff Lang359ef262015-01-05 14:42:29 -0500159ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700160 : mOutputSignature(outputSignature),
161 mShaderExecutable(shaderExecutable)
162{
163}
164
165ProgramD3D::PixelExecutable::~PixelExecutable()
166{
167 SafeDelete(mShaderExecutable);
168}
169
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700170ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
171{
172}
173
Geoff Lang7dd2e102014-11-10 15:19:26 -0500174unsigned int ProgramD3D::mCurrentSerial = 1;
175
Jamie Madill93e13fb2014-11-06 15:27:25 -0500176ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700177 : ProgramImpl(),
178 mRenderer(renderer),
179 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700180 mGeometryExecutable(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700181 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700182 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700183 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700184 mUsedVertexSamplerRange(0),
185 mUsedPixelSamplerRange(0),
186 mDirtySamplerMapping(true),
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400187 mTextureUnitTypesCache(renderer->getRendererCaps().maxCombinedTextureImageUnits),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500188 mShaderVersion(100),
189 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700190{
Brandon Joneseb994362014-09-24 10:27:28 -0700191 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700192}
193
194ProgramD3D::~ProgramD3D()
195{
196 reset();
197 SafeDelete(mDynamicHLSL);
198}
199
Brandon Jones44151a92014-09-10 11:32:25 -0700200bool ProgramD3D::usesPointSpriteEmulation() const
201{
202 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
203}
204
205bool ProgramD3D::usesGeometryShader() const
206{
Cooper Partine6664f02015-01-09 16:22:24 -0800207 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
208}
209
210bool ProgramD3D::usesInstancedPointSpriteEmulation() const
211{
212 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700213}
214
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700215GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
216{
217 GLint logicalTextureUnit = -1;
218
219 switch (type)
220 {
221 case gl::SAMPLER_PIXEL:
222 ASSERT(samplerIndex < caps.maxTextureImageUnits);
223 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
224 {
225 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
226 }
227 break;
228 case gl::SAMPLER_VERTEX:
229 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
230 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
231 {
232 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
233 }
234 break;
235 default: UNREACHABLE();
236 }
237
238 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
239 {
240 return logicalTextureUnit;
241 }
242
243 return -1;
244}
245
246// Returns the texture type for a given Direct3D 9 sampler type and
247// index (0-15 for the pixel shader and 0-3 for the vertex shader).
248GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
249{
250 switch (type)
251 {
252 case gl::SAMPLER_PIXEL:
253 ASSERT(samplerIndex < mSamplersPS.size());
254 ASSERT(mSamplersPS[samplerIndex].active);
255 return mSamplersPS[samplerIndex].textureType;
256 case gl::SAMPLER_VERTEX:
257 ASSERT(samplerIndex < mSamplersVS.size());
258 ASSERT(mSamplersVS[samplerIndex].active);
259 return mSamplersVS[samplerIndex].textureType;
260 default: UNREACHABLE();
261 }
262
263 return GL_TEXTURE_2D;
264}
265
266GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
267{
268 switch (type)
269 {
270 case gl::SAMPLER_PIXEL:
271 return mUsedPixelSamplerRange;
272 case gl::SAMPLER_VERTEX:
273 return mUsedVertexSamplerRange;
274 default:
275 UNREACHABLE();
276 return 0;
277 }
278}
279
280void ProgramD3D::updateSamplerMapping()
281{
282 if (!mDirtySamplerMapping)
283 {
284 return;
285 }
286
287 mDirtySamplerMapping = false;
288
289 // Retrieve sampler uniform values
290 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
291 {
292 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
293
294 if (targetUniform->dirty)
295 {
Geoff Lang2ec386b2014-12-03 14:44:38 -0500296 if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700297 {
298 int count = targetUniform->elementCount();
299 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
300
301 if (targetUniform->isReferencedByFragmentShader())
302 {
303 unsigned int firstIndex = targetUniform->psRegisterIndex;
304
305 for (int i = 0; i < count; i++)
306 {
307 unsigned int samplerIndex = firstIndex + i;
308
309 if (samplerIndex < mSamplersPS.size())
310 {
311 ASSERT(mSamplersPS[samplerIndex].active);
312 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
313 }
314 }
315 }
316
317 if (targetUniform->isReferencedByVertexShader())
318 {
319 unsigned int firstIndex = targetUniform->vsRegisterIndex;
320
321 for (int i = 0; i < count; i++)
322 {
323 unsigned int samplerIndex = firstIndex + i;
324
325 if (samplerIndex < mSamplersVS.size())
326 {
327 ASSERT(mSamplersVS[samplerIndex].active);
328 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
329 }
330 }
331 }
332 }
333 }
334 }
335}
336
337bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
338{
Jamie Madill13776892015-04-28 12:39:06 -0400339 // Skip cache if we're using an infolog, so we get the full error.
340 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
341 if (!mDirtySamplerMapping && infoLog == nullptr && mCachedValidateSamplersResult.valid())
342 {
343 return mCachedValidateSamplersResult.value();
344 }
345
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700346 // if any two active samplers in a program are of different types, but refer to the same
347 // texture image unit, and this is the current program, then ValidateProgram will fail, and
348 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
349 updateSamplerMapping();
350
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400351 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700352
353 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
354 {
355 if (mSamplersPS[i].active)
356 {
357 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
358
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400359 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700360 {
361 if (infoLog)
362 {
Jamie Madillf6113162015-05-07 11:49:21 -0400363 (*infoLog) << "Sampler uniform (" << unit
364 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
365 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700366 }
367
Jamie Madill13776892015-04-28 12:39:06 -0400368 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700369 return false;
370 }
371
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400372 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700373 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400374 if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700375 {
376 if (infoLog)
377 {
Jamie Madillf6113162015-05-07 11:49:21 -0400378 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
379 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700380 }
381
Jamie Madill13776892015-04-28 12:39:06 -0400382 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700383 return false;
384 }
385 }
386 else
387 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400388 mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700389 }
390 }
391 }
392
393 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
394 {
395 if (mSamplersVS[i].active)
396 {
397 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
398
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400399 if (unit >= caps.maxCombinedTextureImageUnits)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700400 {
401 if (infoLog)
402 {
Jamie Madillf6113162015-05-07 11:49:21 -0400403 (*infoLog) << "Sampler uniform (" << unit
404 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
405 << caps.maxCombinedTextureImageUnits << ")";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700406 }
407
Jamie Madill13776892015-04-28 12:39:06 -0400408 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700409 return false;
410 }
411
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400412 if (mTextureUnitTypesCache[unit] != GL_NONE)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700413 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400414 if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit])
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700415 {
416 if (infoLog)
417 {
Jamie Madillf6113162015-05-07 11:49:21 -0400418 (*infoLog) << "Samplers of conflicting types refer to the same texture image unit ("
419 << unit << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700420 }
421
Jamie Madill13776892015-04-28 12:39:06 -0400422 mCachedValidateSamplersResult = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700423 return false;
424 }
425 }
426 else
427 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400428 mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700429 }
430 }
431 }
432
Jamie Madill13776892015-04-28 12:39:06 -0400433 mCachedValidateSamplersResult = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700434 return true;
435}
436
Geoff Lang7dd2e102014-11-10 15:19:26 -0500437LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700438{
Austin Kinross137b1512015-06-17 16:14:53 -0700439 DeviceIdentifier binaryDeviceIdentifier = { 0 };
440 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryDeviceIdentifier), sizeof(DeviceIdentifier));
441
442 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
443 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
444 {
445 infoLog << "Invalid program binary, device configuration has changed.";
446 return LinkResult(false, gl::Error(GL_NO_ERROR));
447 }
448
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500449 int compileFlags = stream->readInt<int>();
450 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
451 {
Jamie Madillf6113162015-05-07 11:49:21 -0400452 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500453 return LinkResult(false, gl::Error(GL_NO_ERROR));
454 }
455
Brandon Jones44151a92014-09-10 11:32:25 -0700456 stream->readInt(&mShaderVersion);
457
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700458 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
459 for (unsigned int i = 0; i < psSamplerCount; ++i)
460 {
461 Sampler sampler;
462 stream->readBool(&sampler.active);
463 stream->readInt(&sampler.logicalTextureUnit);
464 stream->readInt(&sampler.textureType);
465 mSamplersPS.push_back(sampler);
466 }
467 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
468 for (unsigned int i = 0; i < vsSamplerCount; ++i)
469 {
470 Sampler sampler;
471 stream->readBool(&sampler.active);
472 stream->readInt(&sampler.logicalTextureUnit);
473 stream->readInt(&sampler.textureType);
474 mSamplersVS.push_back(sampler);
475 }
476
477 stream->readInt(&mUsedVertexSamplerRange);
478 stream->readInt(&mUsedPixelSamplerRange);
479
480 const unsigned int uniformCount = stream->readInt<unsigned int>();
481 if (stream->error())
482 {
Jamie Madillf6113162015-05-07 11:49:21 -0400483 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500484 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700485 }
486
487 mUniforms.resize(uniformCount);
488 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
489 {
490 GLenum type = stream->readInt<GLenum>();
491 GLenum precision = stream->readInt<GLenum>();
492 std::string name = stream->readString();
493 unsigned int arraySize = stream->readInt<unsigned int>();
494 int blockIndex = stream->readInt<int>();
495
496 int offset = stream->readInt<int>();
497 int arrayStride = stream->readInt<int>();
498 int matrixStride = stream->readInt<int>();
499 bool isRowMajorMatrix = stream->readBool();
500
501 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
502
503 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
504
505 stream->readInt(&uniform->psRegisterIndex);
506 stream->readInt(&uniform->vsRegisterIndex);
507 stream->readInt(&uniform->registerCount);
508 stream->readInt(&uniform->registerElement);
509
510 mUniforms[uniformIndex] = uniform;
511 }
512
513 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
514 if (stream->error())
515 {
Jamie Madillf6113162015-05-07 11:49:21 -0400516 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500517 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700518 }
519
520 mUniformIndex.resize(uniformIndexCount);
521 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
522 {
523 stream->readString(&mUniformIndex[uniformIndexIndex].name);
524 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
525 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
526 }
527
528 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
529 if (stream->error())
530 {
Jamie Madillf6113162015-05-07 11:49:21 -0400531 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500532 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700533 }
534
535 mUniformBlocks.resize(uniformBlockCount);
536 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
537 {
538 std::string name = stream->readString();
539 unsigned int elementIndex = stream->readInt<unsigned int>();
540 unsigned int dataSize = stream->readInt<unsigned int>();
541
542 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
543
544 stream->readInt(&uniformBlock->psRegisterIndex);
545 stream->readInt(&uniformBlock->vsRegisterIndex);
546
547 unsigned int numMembers = stream->readInt<unsigned int>();
548 uniformBlock->memberUniformIndexes.resize(numMembers);
549 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
550 {
551 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
552 }
553
554 mUniformBlocks[uniformBlockIndex] = uniformBlock;
555 }
556
Brandon Joneseb994362014-09-24 10:27:28 -0700557 stream->readInt(&mTransformFeedbackBufferMode);
558 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
559 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
560 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
561 {
562 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
563
564 stream->readString(&varying.name);
565 stream->readInt(&varying.type);
566 stream->readInt(&varying.size);
567 stream->readString(&varying.semanticName);
568 stream->readInt(&varying.semanticIndex);
569 stream->readInt(&varying.semanticIndexCount);
570 }
571
Brandon Jones22502d52014-08-29 16:58:36 -0700572 stream->readString(&mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530573 stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700574 stream->readString(&mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530575 stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700576 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700577 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700578
579 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
580 mPixelShaderKey.resize(pixelShaderKeySize);
581 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
582 {
583 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
584 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
585 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
586 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
587 }
588
Brandon Joneseb994362014-09-24 10:27:28 -0700589 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
590
591 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
592 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
593 {
594 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
595
596 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
597 {
598 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
599 stream->readInt(&vertexInput->mType);
600 stream->readInt(&vertexInput->mNormalized);
601 stream->readInt(&vertexInput->mComponents);
602 stream->readBool(&vertexInput->mPureInteger);
603 }
604
605 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
606 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400607
Geoff Lang359ef262015-01-05 14:42:29 -0500608 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400609 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
610 SHADER_VERTEX,
611 mTransformFeedbackLinkedVaryings,
612 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
613 &shaderExecutable);
614 if (error.isError())
615 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500616 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400617 }
618
Brandon Joneseb994362014-09-24 10:27:28 -0700619 if (!shaderExecutable)
620 {
Jamie Madillf6113162015-05-07 11:49:21 -0400621 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500622 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700623 }
624
625 // generated converted input layout
626 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
627 getInputLayoutSignature(inputLayout, signature);
628
629 // add new binary
630 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
631
632 stream->skip(vertexShaderSize);
633 }
634
635 const size_t pixelShaderCount = stream->readInt<unsigned int>();
636 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
637 {
638 const size_t outputCount = stream->readInt<unsigned int>();
639 std::vector<GLenum> outputs(outputCount);
640 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
641 {
642 stream->readInt(&outputs[outputIndex]);
643 }
644
645 const size_t pixelShaderSize = stream->readInt<unsigned int>();
646 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Lang359ef262015-01-05 14:42:29 -0500647 ShaderExecutableD3D *shaderExecutable = NULL;
Geoff Langb543aff2014-09-30 14:52:54 -0400648 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
649 mTransformFeedbackLinkedVaryings,
650 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
651 &shaderExecutable);
652 if (error.isError())
653 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500654 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400655 }
Brandon Joneseb994362014-09-24 10:27:28 -0700656
657 if (!shaderExecutable)
658 {
Jamie Madillf6113162015-05-07 11:49:21 -0400659 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500660 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700661 }
662
663 // add new binary
664 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
665
666 stream->skip(pixelShaderSize);
667 }
668
669 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
670
671 if (geometryShaderSize > 0)
672 {
673 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400674 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
675 mTransformFeedbackLinkedVaryings,
676 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
677 &mGeometryExecutable);
678 if (error.isError())
679 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500680 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400681 }
Brandon Joneseb994362014-09-24 10:27:28 -0700682
683 if (!mGeometryExecutable)
684 {
Jamie Madillf6113162015-05-07 11:49:21 -0400685 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500686 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700687 }
688 stream->skip(geometryShaderSize);
689 }
690
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700691 initializeUniformStorage();
Jamie Madill437d2662014-12-05 14:23:35 -0500692 initAttributesByLayout();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700693
Geoff Lang7dd2e102014-11-10 15:19:26 -0500694 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700695}
696
Geoff Langb543aff2014-09-30 14:52:54 -0400697gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700698{
Austin Kinross137b1512015-06-17 16:14:53 -0700699 // Output the DeviceIdentifier before we output any shader code
700 // When we load the binary again later, we can validate the device identifier before trying to compile any HLSL
701 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
702 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(DeviceIdentifier));
703
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500704 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
705
Brandon Jones44151a92014-09-10 11:32:25 -0700706 stream->writeInt(mShaderVersion);
707
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700708 stream->writeInt(mSamplersPS.size());
709 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
710 {
711 stream->writeInt(mSamplersPS[i].active);
712 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
713 stream->writeInt(mSamplersPS[i].textureType);
714 }
715
716 stream->writeInt(mSamplersVS.size());
717 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
718 {
719 stream->writeInt(mSamplersVS[i].active);
720 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
721 stream->writeInt(mSamplersVS[i].textureType);
722 }
723
724 stream->writeInt(mUsedVertexSamplerRange);
725 stream->writeInt(mUsedPixelSamplerRange);
726
727 stream->writeInt(mUniforms.size());
728 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
729 {
730 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
731
732 stream->writeInt(uniform.type);
733 stream->writeInt(uniform.precision);
734 stream->writeString(uniform.name);
735 stream->writeInt(uniform.arraySize);
736 stream->writeInt(uniform.blockIndex);
737
738 stream->writeInt(uniform.blockInfo.offset);
739 stream->writeInt(uniform.blockInfo.arrayStride);
740 stream->writeInt(uniform.blockInfo.matrixStride);
741 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
742
743 stream->writeInt(uniform.psRegisterIndex);
744 stream->writeInt(uniform.vsRegisterIndex);
745 stream->writeInt(uniform.registerCount);
746 stream->writeInt(uniform.registerElement);
747 }
748
749 stream->writeInt(mUniformIndex.size());
750 for (size_t i = 0; i < mUniformIndex.size(); ++i)
751 {
752 stream->writeString(mUniformIndex[i].name);
753 stream->writeInt(mUniformIndex[i].element);
754 stream->writeInt(mUniformIndex[i].index);
755 }
756
757 stream->writeInt(mUniformBlocks.size());
758 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
759 {
760 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
761
762 stream->writeString(uniformBlock.name);
763 stream->writeInt(uniformBlock.elementIndex);
764 stream->writeInt(uniformBlock.dataSize);
765
766 stream->writeInt(uniformBlock.memberUniformIndexes.size());
767 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
768 {
769 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
770 }
771
772 stream->writeInt(uniformBlock.psRegisterIndex);
773 stream->writeInt(uniformBlock.vsRegisterIndex);
774 }
775
Brandon Joneseb994362014-09-24 10:27:28 -0700776 stream->writeInt(mTransformFeedbackBufferMode);
777 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
778 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
779 {
780 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
781
782 stream->writeString(varying.name);
783 stream->writeInt(varying.type);
784 stream->writeInt(varying.size);
785 stream->writeString(varying.semanticName);
786 stream->writeInt(varying.semanticIndex);
787 stream->writeInt(varying.semanticIndexCount);
788 }
789
Brandon Jones22502d52014-08-29 16:58:36 -0700790 stream->writeString(mVertexHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530791 stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700792 stream->writeString(mPixelHLSL);
Arun Patole44efa0b2015-03-04 17:11:05 +0530793 stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700794 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700795 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700796
Brandon Joneseb994362014-09-24 10:27:28 -0700797 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700798 stream->writeInt(pixelShaderKey.size());
799 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
800 {
Brandon Joneseb994362014-09-24 10:27:28 -0700801 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700802 stream->writeInt(variable.type);
803 stream->writeString(variable.name);
804 stream->writeString(variable.source);
805 stream->writeInt(variable.outputIndex);
806 }
807
Brandon Joneseb994362014-09-24 10:27:28 -0700808 stream->writeInt(mVertexExecutables.size());
809 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
810 {
811 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
812
813 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
814 {
815 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
816 stream->writeInt(vertexInput.mType);
817 stream->writeInt(vertexInput.mNormalized);
818 stream->writeInt(vertexInput.mComponents);
819 stream->writeInt(vertexInput.mPureInteger);
820 }
821
822 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
823 stream->writeInt(vertexShaderSize);
824
825 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
826 stream->writeBytes(vertexBlob, vertexShaderSize);
827 }
828
829 stream->writeInt(mPixelExecutables.size());
830 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
831 {
832 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
833
834 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
835 stream->writeInt(outputs.size());
836 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
837 {
838 stream->writeInt(outputs[outputIndex]);
839 }
840
841 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
842 stream->writeInt(pixelShaderSize);
843
844 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
845 stream->writeBytes(pixelBlob, pixelShaderSize);
846 }
847
848 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
849 stream->writeInt(geometryShaderSize);
850
851 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
852 {
853 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
854 stream->writeBytes(geometryBlob, geometryShaderSize);
855 }
856
Geoff Langb543aff2014-09-30 14:52:54 -0400857 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700858}
859
Geoff Lang359ef262015-01-05 14:42:29 -0500860gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700861{
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400862 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -0700863
Jamie Madill85a18042015-03-05 15:41:41 -0500864 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
865 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700866
867 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
868 {
869 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
870
871 if (colorbuffer)
872 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400873 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -0700874 }
875 else
876 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400877 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -0700878 }
879 }
880
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400881 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -0700882}
883
Jamie Madill97399232014-12-23 12:31:15 -0500884gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -0500885 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500886 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700887{
888 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
889 {
890 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
891 {
Geoff Langb543aff2014-09-30 14:52:54 -0400892 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
893 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700894 }
895 }
896
Brandon Jones22502d52014-08-29 16:58:36 -0700897 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
898 outputSignature);
899
900 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -0500901 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500902
903 gl::InfoLog tempInfoLog;
904 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
905
906 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalPixelHLSL, SHADER_PIXEL,
Geoff Langb543aff2014-09-30 14:52:54 -0400907 mTransformFeedbackLinkedVaryings,
908 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
909 mPixelWorkarounds, &pixelExecutable);
910 if (error.isError())
911 {
912 return error;
913 }
Brandon Joneseb994362014-09-24 10:27:28 -0700914
Jamie Madill97399232014-12-23 12:31:15 -0500915 if (pixelExecutable)
916 {
917 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
918 }
919 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -0700920 {
921 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
922 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
923 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
924 }
Brandon Jones22502d52014-08-29 16:58:36 -0700925
Geoff Langb543aff2014-09-30 14:52:54 -0400926 *outExectuable = pixelExecutable;
927 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700928}
929
Jamie Madill97399232014-12-23 12:31:15 -0500930gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500931 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -0500932 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -0700933{
Brandon Joneseb994362014-09-24 10:27:28 -0700934 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
935 getInputLayoutSignature(inputLayout, signature);
936
937 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
938 {
939 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
940 {
Geoff Langb543aff2014-09-30 14:52:54 -0400941 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
942 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700943 }
944 }
945
Brandon Jones22502d52014-08-29 16:58:36 -0700946 // Generate new dynamic layout with attribute conversions
Jamie Madill3da79b72015-04-27 11:09:17 -0400947 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, getShaderAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -0700948
949 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -0500950 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -0500951
952 gl::InfoLog tempInfoLog;
953 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
954
955 gl::Error error = mRenderer->compileToExecutable(*currentInfoLog, finalVertexHLSL, SHADER_VERTEX,
Geoff Langb543aff2014-09-30 14:52:54 -0400956 mTransformFeedbackLinkedVaryings,
957 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
958 mVertexWorkarounds, &vertexExecutable);
959 if (error.isError())
960 {
961 return error;
962 }
963
Jamie Madill97399232014-12-23 12:31:15 -0500964 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700965 {
966 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
967 }
Jamie Madill97399232014-12-23 12:31:15 -0500968 else if (!infoLog)
969 {
970 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
971 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
972 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
973 }
Brandon Jones22502d52014-08-29 16:58:36 -0700974
Geoff Langb543aff2014-09-30 14:52:54 -0400975 *outExectuable = vertexExecutable;
976 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700977}
978
Geoff Lang7dd2e102014-11-10 15:19:26 -0500979LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
980 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700981{
Jamie Madillf4bf3812015-04-01 16:15:32 -0400982 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
983 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Jones44151a92014-09-10 11:32:25 -0700984
Jamie Madille4ea2022015-03-26 20:35:05 +0000985 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
986 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
987 ShaderExecutableD3D *defaultVertexExecutable = NULL;
988 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
989 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -0800990 {
Jamie Madille4ea2022015-03-26 20:35:05 +0000991 return LinkResult(false, error);
992 }
Austin Kinross434953e2015-02-20 10:49:51 -0800993
Brandon Joneseb994362014-09-24 10:27:28 -0700994 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -0500995 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madille4ea2022015-03-26 20:35:05 +0000996 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -0400997 if (error.isError())
998 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500999 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001000 }
Brandon Jones44151a92014-09-10 11:32:25 -07001001
Brandon Joneseb994362014-09-24 10:27:28 -07001002 if (usesGeometryShader())
1003 {
1004 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -07001005
Geoff Langb543aff2014-09-30 14:52:54 -04001006
1007 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
1008 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
Arun Patole44efa0b2015-03-04 17:11:05 +05301009 D3DCompilerWorkarounds(), &mGeometryExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001010 if (error.isError())
1011 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001012 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001013 }
Brandon Joneseb994362014-09-24 10:27:28 -07001014 }
1015
Brandon Jones091540d2014-10-29 11:32:04 -07001016#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +02001017 if (usesGeometryShader() && mGeometryExecutable)
1018 {
1019 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
1020 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
1021 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
1022 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
1023 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1024 }
1025
1026 if (defaultVertexExecutable)
1027 {
1028 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1029 }
1030
1031 if (defaultPixelExecutable)
1032 {
1033 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1034 }
1035#endif
1036
Geoff Langb543aff2014-09-30 14:52:54 -04001037 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001038 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001039}
1040
Geoff Lang7dd2e102014-11-10 15:19:26 -05001041LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
1042 gl::Shader *fragmentShader, gl::Shader *vertexShader,
1043 const std::vector<std::string> &transformFeedbackVaryings,
1044 GLenum transformFeedbackBufferMode,
1045 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
1046 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -07001047{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001048 ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1049 ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
Brandon Joneseb994362014-09-24 10:27:28 -07001050
Jamie Madillde8892b2014-11-11 13:00:22 -05001051 mSamplersPS.resize(data.caps->maxTextureImageUnits);
1052 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001053
Brandon Joneseb994362014-09-24 10:27:28 -07001054 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -07001055
1056 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301057 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
Brandon Jones22502d52014-08-29 16:58:36 -07001058
1059 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
Arun Patole44efa0b2015-03-04 17:11:05 +05301060 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Brandon Jones44151a92014-09-10 11:32:25 -07001061 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001062
1063 // Map the varyings to the register file
Daniel Chengf33ab832015-06-30 19:08:16 -07001064 VaryingPacking packing = {};
Brandon Jones22502d52014-08-29 16:58:36 -07001065 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1066
Geoff Langbdee2d52014-09-17 11:02:51 -04001067 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001068 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001069 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001070 }
1071
Geoff Lang7dd2e102014-11-10 15:19:26 -05001072 if (!gl::Program::linkVaryings(infoLog, fragmentShader, vertexShader))
Brandon Jones22502d52014-08-29 16:58:36 -07001073 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001074 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001075 }
1076
Jamie Madillde8892b2014-11-11 13:00:22 -05001077 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001078 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1079 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1080 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001082 }
1083
Brandon Jones44151a92014-09-10 11:32:25 -07001084 mUsesPointSize = vertexShaderD3D->usesPointSize();
1085
Jamie Madill437d2662014-12-05 14:23:35 -05001086 initAttributesByLayout();
1087
Geoff Lang7dd2e102014-11-10 15:19:26 -05001088 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001089}
1090
Geoff Lang0ca53782015-05-07 13:49:39 -04001091void ProgramD3D::bindAttributeLocation(GLuint index, const std::string &name)
1092{
1093}
1094
Brandon Jones44151a92014-09-10 11:32:25 -07001095void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1096{
1097 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1098}
1099
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001100void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001101{
1102 // Compute total default block size
1103 unsigned int vertexRegisters = 0;
1104 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001105 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001106 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001107 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001108
Geoff Lang2ec386b2014-12-03 14:44:38 -05001109 if (!gl::IsSamplerType(uniform.type))
Brandon Jonesc9610c52014-08-25 17:02:59 -07001110 {
1111 if (uniform.isReferencedByVertexShader())
1112 {
1113 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1114 }
1115 if (uniform.isReferencedByFragmentShader())
1116 {
1117 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1118 }
1119 }
1120 }
1121
1122 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1123 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1124}
1125
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001126gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001127{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001128 updateSamplerMapping();
1129
1130 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1131 if (error.isError())
1132 {
1133 return error;
1134 }
1135
1136 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1137 {
1138 mUniforms[uniformIndex]->dirty = false;
1139 }
1140
1141 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001142}
1143
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001144gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[])
Brandon Jones18bd4102014-09-22 14:21:44 -07001145{
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001146 GLint vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS];
1147 GLint fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001148
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001149 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1150 {
1151 vertexUniformBuffers[registerIndex] = -1;
1152 }
1153
1154 for (unsigned int registerIndex = 0; registerIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; ++registerIndex)
1155 {
1156 fragmentUniformBuffers[registerIndex] = -1;
1157 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001158
1159 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1160 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1161
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001162 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001163 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001164 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001165 GLuint blockBinding = uniformBlockBindings[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001166
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001167 ASSERT(uniformBlock);
Brandon Jones18bd4102014-09-22 14:21:44 -07001168
1169 // Unnecessary to apply an unreferenced standard or shared UBO
1170 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1171 {
1172 continue;
1173 }
1174
1175 if (uniformBlock->isReferencedByVertexShader())
1176 {
1177 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001178 ASSERT(vertexUniformBuffers[registerIndex] == -1);
1179 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
1180 vertexUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001181 }
1182
1183 if (uniformBlock->isReferencedByFragmentShader())
1184 {
1185 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001186 ASSERT(fragmentUniformBuffers[registerIndex] == -1);
1187 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
1188 fragmentUniformBuffers[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001189 }
1190 }
1191
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001192 return mRenderer->setUniformBuffers(data, vertexUniformBuffers, fragmentUniformBuffers);
Brandon Jones18bd4102014-09-22 14:21:44 -07001193}
1194
1195bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1196 unsigned int registerIndex, const gl::Caps &caps)
1197{
1198 if (shader == GL_VERTEX_SHADER)
1199 {
1200 uniformBlock->vsRegisterIndex = registerIndex;
1201 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1202 {
Jamie Madillf6113162015-05-07 11:49:21 -04001203 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (" << caps.maxVertexUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001204 return false;
1205 }
1206 }
1207 else if (shader == GL_FRAGMENT_SHADER)
1208 {
1209 uniformBlock->psRegisterIndex = registerIndex;
1210 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1211 {
Jamie Madillf6113162015-05-07 11:49:21 -04001212 infoLog << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (" << caps.maxFragmentUniformBlocks << ")";
Brandon Jones18bd4102014-09-22 14:21:44 -07001213 return false;
1214 }
1215 }
1216 else UNREACHABLE();
1217
1218 return true;
1219}
1220
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001221void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001222{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001223 unsigned int numUniforms = mUniforms.size();
1224 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001225 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001226 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001227 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001228}
1229
1230void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1231{
1232 setUniform(location, count, v, GL_FLOAT);
1233}
1234
1235void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1236{
1237 setUniform(location, count, v, GL_FLOAT_VEC2);
1238}
1239
1240void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1241{
1242 setUniform(location, count, v, GL_FLOAT_VEC3);
1243}
1244
1245void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1246{
1247 setUniform(location, count, v, GL_FLOAT_VEC4);
1248}
1249
1250void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1251{
1252 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1253}
1254
1255void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1256{
1257 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1258}
1259
1260void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1261{
1262 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1263}
1264
1265void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1266{
1267 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1268}
1269
1270void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1271{
1272 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1273}
1274
1275void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1276{
1277 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1278}
1279
1280void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1281{
1282 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1283}
1284
1285void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1286{
1287 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1288}
1289
1290void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1291{
1292 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1293}
1294
1295void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1296{
1297 setUniform(location, count, v, GL_INT);
1298}
1299
1300void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1301{
1302 setUniform(location, count, v, GL_INT_VEC2);
1303}
1304
1305void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1306{
1307 setUniform(location, count, v, GL_INT_VEC3);
1308}
1309
1310void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1311{
1312 setUniform(location, count, v, GL_INT_VEC4);
1313}
1314
1315void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1316{
1317 setUniform(location, count, v, GL_UNSIGNED_INT);
1318}
1319
1320void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1321{
1322 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1323}
1324
1325void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1326{
1327 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1328}
1329
1330void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1331{
1332 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1333}
1334
1335void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1336{
1337 getUniformv(location, params, GL_FLOAT);
1338}
1339
1340void ProgramD3D::getUniformiv(GLint location, GLint *params)
1341{
1342 getUniformv(location, params, GL_INT);
1343}
1344
1345void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1346{
1347 getUniformv(location, params, GL_UNSIGNED_INT);
1348}
1349
1350bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1351 const gl::Caps &caps)
1352{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001353 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(&vertexShader);
1354 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(&fragmentShader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001355
1356 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1357 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1358
1359 // Check that uniforms defined in the vertex and fragment shaders are identical
1360 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1361 UniformMap linkedUniforms;
1362
1363 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001364 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001365 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1366 linkedUniforms[vertexUniform.name] = &vertexUniform;
1367 }
1368
1369 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1370 {
1371 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1372 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1373 if (entry != linkedUniforms.end())
1374 {
1375 const sh::Uniform &vertexUniform = *entry->second;
1376 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377 if (!gl::Program::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001378 {
1379 return false;
1380 }
1381 }
1382 }
1383
1384 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1385 {
1386 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1387
1388 if (uniform.staticUse)
1389 {
Jamie Madill55def582015-05-04 11:24:57 -04001390 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1391 vertexShaderD3D->getUniformRegister(uniform.name);
1392 defineUniformBase(vertexShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001393 }
1394 }
1395
1396 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1397 {
1398 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1399
1400 if (uniform.staticUse)
1401 {
Jamie Madill55def582015-05-04 11:24:57 -04001402 unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX :
1403 fragmentShaderD3D->getUniformRegister(uniform.name);
1404 defineUniformBase(fragmentShaderD3D, uniform, registerBase);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001405 }
1406 }
1407
1408 if (!indexUniforms(infoLog, caps))
1409 {
1410 return false;
1411 }
1412
1413 initializeUniformStorage();
1414
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001415 return true;
1416}
1417
Geoff Lang492a7e42014-11-05 13:27:06 -05001418void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001419{
Jamie Madill55def582015-05-04 11:24:57 -04001420 if (uniformRegister == GL_INVALID_INDEX)
1421 {
1422 defineUniform(shader, uniform, uniform.name, nullptr);
1423 return;
1424 }
1425
Geoff Lang492a7e42014-11-05 13:27:06 -05001426 ShShaderOutput outputType = shader->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001427 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1428 encoder.skipRegisters(uniformRegister);
1429
1430 defineUniform(shader, uniform, uniform.name, &encoder);
1431}
1432
Geoff Lang492a7e42014-11-05 13:27:06 -05001433void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform,
1434 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001435{
1436 if (uniform.isStruct())
1437 {
1438 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1439 {
1440 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1441
Jamie Madill55def582015-05-04 11:24:57 -04001442 if (encoder)
1443 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001444
1445 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1446 {
1447 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1448 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1449
1450 defineUniform(shader, field, fieldFullName, encoder);
1451 }
1452
Jamie Madill55def582015-05-04 11:24:57 -04001453 if (encoder)
1454 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001455 }
1456 }
1457 else // Not a struct
1458 {
1459 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001460 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001461 {
1462 encoder->enterAggregateType();
1463 }
1464
1465 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1466
Jamie Madill2857f482015-02-09 15:35:29 -05001467 // Advance the uniform offset, to track registers allocation for structs
Jamie Madill55def582015-05-04 11:24:57 -04001468 sh::BlockMemberInfo blockInfo = encoder ?
1469 encoder->encodeType(uniform.type, uniform.arraySize, false) :
1470 sh::BlockMemberInfo::getDefaultBlockInfo();
Jamie Madill2857f482015-02-09 15:35:29 -05001471
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001472 if (!linkedUniform)
1473 {
1474 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
Jamie Madill2857f482015-02-09 15:35:29 -05001475 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001476 ASSERT(linkedUniform);
Jamie Madill55def582015-05-04 11:24:57 -04001477
1478 if (encoder)
1479 linkedUniform->registerElement = sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001480 mUniforms.push_back(linkedUniform);
1481 }
1482
Jamie Madill55def582015-05-04 11:24:57 -04001483 if (encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001484 {
Jamie Madill55def582015-05-04 11:24:57 -04001485 if (shader->getShaderType() == GL_FRAGMENT_SHADER)
1486 {
1487 linkedUniform->psRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1488 }
1489 else if (shader->getShaderType() == GL_VERTEX_SHADER)
1490 {
1491 linkedUniform->vsRegisterIndex = sh::HLSLBlockEncoder::getBlockRegister(blockInfo);
1492 }
1493 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001494 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001495
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001496 // Arrays are treated as aggregate types
Jamie Madill55def582015-05-04 11:24:57 -04001497 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001498 {
1499 encoder->exitAggregateType();
1500 }
1501 }
1502}
1503
1504template <typename T>
1505static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1506{
1507 ASSERT(dest != NULL);
1508 ASSERT(dirtyFlag != NULL);
1509
1510 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1511 *dest = source;
1512}
1513
1514template <typename T>
1515void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1516{
1517 const int components = gl::VariableComponentCount(targetUniformType);
1518 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1519
1520 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1521
1522 int elementCount = targetUniform->elementCount();
1523
1524 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1525
1526 if (targetUniform->type == targetUniformType)
1527 {
1528 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1529
1530 for (int i = 0; i < count; i++)
1531 {
1532 T *dest = target + (i * 4);
1533 const T *source = v + (i * components);
1534
1535 for (int c = 0; c < components; c++)
1536 {
1537 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1538 }
1539 for (int c = components; c < 4; c++)
1540 {
1541 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1542 }
1543 }
1544 }
1545 else if (targetUniform->type == targetBoolType)
1546 {
1547 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1548
1549 for (int i = 0; i < count; i++)
1550 {
1551 GLint *dest = boolParams + (i * 4);
1552 const T *source = v + (i * components);
1553
1554 for (int c = 0; c < components; c++)
1555 {
1556 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1557 }
1558 for (int c = components; c < 4; c++)
1559 {
1560 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1561 }
1562 }
1563 }
Geoff Lang2ec386b2014-12-03 14:44:38 -05001564 else if (gl::IsSamplerType(targetUniform->type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001565 {
1566 ASSERT(targetUniformType == GL_INT);
1567
1568 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1569
1570 bool wasDirty = targetUniform->dirty;
1571
1572 for (int i = 0; i < count; i++)
1573 {
1574 GLint *dest = target + (i * 4);
1575 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1576
1577 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1578 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1579 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1580 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1581 }
1582
1583 if (!wasDirty && targetUniform->dirty)
1584 {
1585 mDirtySamplerMapping = true;
1586 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001587 }
1588 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001589}
Brandon Jones18bd4102014-09-22 14:21:44 -07001590
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001591template<typename T>
1592bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1593{
1594 bool dirty = false;
1595 int copyWidth = std::min(targetHeight, srcWidth);
1596 int copyHeight = std::min(targetWidth, srcHeight);
1597
1598 for (int x = 0; x < copyWidth; x++)
1599 {
1600 for (int y = 0; y < copyHeight; y++)
1601 {
1602 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1603 }
1604 }
1605 // clear unfilled right side
1606 for (int y = 0; y < copyWidth; y++)
1607 {
1608 for (int x = copyHeight; x < targetWidth; x++)
1609 {
1610 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1611 }
1612 }
1613 // clear unfilled bottom.
1614 for (int y = copyWidth; y < targetHeight; y++)
1615 {
1616 for (int x = 0; x < targetWidth; x++)
1617 {
1618 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1619 }
1620 }
1621
1622 return dirty;
1623}
1624
1625template<typename T>
1626bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1627{
1628 bool dirty = false;
1629 int copyWidth = std::min(targetWidth, srcWidth);
1630 int copyHeight = std::min(targetHeight, srcHeight);
1631
1632 for (int y = 0; y < copyHeight; y++)
1633 {
1634 for (int x = 0; x < copyWidth; x++)
1635 {
1636 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1637 }
1638 }
1639 // clear unfilled right side
1640 for (int y = 0; y < copyHeight; y++)
1641 {
1642 for (int x = copyWidth; x < targetWidth; x++)
1643 {
1644 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1645 }
1646 }
1647 // clear unfilled bottom.
1648 for (int y = copyHeight; y < targetHeight; y++)
1649 {
1650 for (int x = 0; x < targetWidth; x++)
1651 {
1652 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1653 }
1654 }
1655
1656 return dirty;
1657}
1658
1659template <int cols, int rows>
1660void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1661{
1662 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1663
1664 int elementCount = targetUniform->elementCount();
1665
1666 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1667 const unsigned int targetMatrixStride = (4 * rows);
1668 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1669
1670 for (int i = 0; i < count; i++)
1671 {
1672 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1673 if (transpose == GL_FALSE)
1674 {
1675 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1676 }
1677 else
1678 {
1679 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1680 }
1681 target += targetMatrixStride;
1682 value += cols * rows;
1683 }
1684}
1685
1686template <typename T>
1687void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1688{
1689 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1690
1691 if (gl::IsMatrixType(targetUniform->type))
1692 {
1693 const int rows = gl::VariableRowCount(targetUniform->type);
1694 const int cols = gl::VariableColumnCount(targetUniform->type);
1695 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1696 }
1697 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1698 {
1699 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1700 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1701 size * sizeof(T));
1702 }
1703 else
1704 {
1705 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1706 switch (gl::VariableComponentType(targetUniform->type))
1707 {
1708 case GL_BOOL:
1709 {
1710 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1711
1712 for (unsigned int i = 0; i < size; i++)
1713 {
1714 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1715 }
1716 }
1717 break;
1718
1719 case GL_FLOAT:
1720 {
1721 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1722
1723 for (unsigned int i = 0; i < size; i++)
1724 {
1725 params[i] = static_cast<T>(floatParams[i]);
1726 }
1727 }
1728 break;
1729
1730 case GL_INT:
1731 {
1732 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1733
1734 for (unsigned int i = 0; i < size; i++)
1735 {
1736 params[i] = static_cast<T>(intParams[i]);
1737 }
1738 }
1739 break;
1740
1741 case GL_UNSIGNED_INT:
1742 {
1743 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1744
1745 for (unsigned int i = 0; i < size; i++)
1746 {
1747 params[i] = static_cast<T>(uintParams[i]);
1748 }
1749 }
1750 break;
1751
1752 default: UNREACHABLE();
1753 }
1754 }
1755}
1756
1757template <typename VarT>
1758void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1759 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1760 bool inRowMajorLayout)
1761{
1762 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1763 {
1764 const VarT &field = fields[uniformIndex];
1765 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1766
1767 if (field.isStruct())
1768 {
1769 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1770
1771 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1772 {
1773 encoder->enterAggregateType();
1774
1775 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1776 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1777
1778 encoder->exitAggregateType();
1779 }
1780 }
1781 else
1782 {
1783 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1784
1785 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1786
1787 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1788 blockIndex, memberInfo);
1789
1790 // add to uniform list, but not index, since uniform block uniforms have no location
1791 blockUniformIndexes->push_back(mUniforms.size());
1792 mUniforms.push_back(newUniform);
1793 }
1794 }
1795}
1796
1797bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1798 const gl::Caps &caps)
1799{
Jamie Madillf4bf3812015-04-01 16:15:32 -04001800 const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001801
1802 // create uniform block entries if they do not exist
1803 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1804 {
1805 std::vector<unsigned int> blockUniformIndexes;
1806 const unsigned int blockIndex = mUniformBlocks.size();
1807
1808 // define member uniforms
1809 sh::BlockLayoutEncoder *encoder = NULL;
1810
1811 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1812 {
1813 encoder = new sh::Std140BlockEncoder;
1814 }
1815 else
1816 {
1817 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1818 }
1819 ASSERT(encoder);
1820
1821 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1822
1823 size_t dataSize = encoder->getBlockSize();
1824
1825 // create all the uniform blocks
1826 if (interfaceBlock.arraySize > 0)
1827 {
1828 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1829 {
1830 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1831 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1832 mUniformBlocks.push_back(newUniformBlock);
1833 }
1834 }
1835 else
1836 {
1837 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1838 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1839 mUniformBlocks.push_back(newUniformBlock);
1840 }
1841 }
1842
1843 if (interfaceBlock.staticUse)
1844 {
1845 // Assign registers to the uniform blocks
1846 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1847 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1848 ASSERT(blockIndex != GL_INVALID_INDEX);
1849 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1850
1851 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1852
1853 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1854 {
1855 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1856 ASSERT(uniformBlock->name == interfaceBlock.name);
1857
1858 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1859 interfaceBlockRegister + uniformBlockElement, caps))
1860 {
1861 return false;
1862 }
1863 }
1864 }
1865
1866 return true;
1867}
1868
1869bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1870 GLenum samplerType,
1871 unsigned int samplerCount,
1872 std::vector<Sampler> &outSamplers,
1873 GLuint *outUsedRange)
1874{
1875 unsigned int samplerIndex = startSamplerIndex;
1876
1877 do
1878 {
1879 if (samplerIndex < outSamplers.size())
1880 {
1881 Sampler& sampler = outSamplers[samplerIndex];
1882 sampler.active = true;
1883 sampler.textureType = GetTextureType(samplerType);
1884 sampler.logicalTextureUnit = 0;
1885 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1886 }
1887 else
1888 {
1889 return false;
1890 }
1891
1892 samplerIndex++;
1893 } while (samplerIndex < startSamplerIndex + samplerCount);
1894
1895 return true;
1896}
1897
1898bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1899{
Geoff Lang2ec386b2014-12-03 14:44:38 -05001900 ASSERT(gl::IsSamplerType(uniform.type));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001901 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1902
1903 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1904 {
1905 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1906 &mUsedVertexSamplerRange))
1907 {
Jamie Madillf6113162015-05-07 11:49:21 -04001908 infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units ("
1909 << mSamplersVS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001910 return false;
1911 }
1912
1913 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1914 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1915 {
Jamie Madillf6113162015-05-07 11:49:21 -04001916 infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS ("
1917 << caps.maxVertexUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001918 return false;
1919 }
1920 }
1921
1922 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1923 {
1924 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1925 &mUsedPixelSamplerRange))
1926 {
Jamie Madillf6113162015-05-07 11:49:21 -04001927 infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
1928 << mSamplersPS.size() << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001929 return false;
1930 }
1931
1932 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1933 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1934 {
Jamie Madillf6113162015-05-07 11:49:21 -04001935 infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS ("
1936 << caps.maxFragmentUniformVectors << ").";
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001937 return false;
1938 }
1939 }
1940
1941 return true;
1942}
1943
1944bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1945{
1946 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1947 {
1948 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1949
Geoff Lang2ec386b2014-12-03 14:44:38 -05001950 if (gl::IsSamplerType(uniform.type))
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001951 {
1952 if (!indexSamplerUniform(uniform, infoLog, caps))
1953 {
1954 return false;
1955 }
1956 }
1957
Jamie Madill55def582015-05-04 11:24:57 -04001958 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001959 {
Jamie Madill55def582015-05-04 11:24:57 -04001960 if (!uniform.isBuiltIn())
1961 {
1962 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayIndex, uniformIndex));
1963 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964 }
1965 }
1966
1967 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001968}
1969
Brandon Jonesc9610c52014-08-25 17:02:59 -07001970void ProgramD3D::reset()
1971{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001972 ProgramImpl::reset();
1973
Brandon Joneseb994362014-09-24 10:27:28 -07001974 SafeDeleteContainer(mVertexExecutables);
1975 SafeDeleteContainer(mPixelExecutables);
1976 SafeDelete(mGeometryExecutable);
1977
1978 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001979
Brandon Jones22502d52014-08-29 16:58:36 -07001980 mVertexHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301981 mVertexWorkarounds.reset();
Brandon Jones44151a92014-09-10 11:32:25 -07001982 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001983
1984 mPixelHLSL.clear();
Arun Patole44efa0b2015-03-04 17:11:05 +05301985 mPixelWorkarounds.reset();
Brandon Jones22502d52014-08-29 16:58:36 -07001986 mUsesFragDepth = false;
1987 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001988 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001989
Brandon Jonesc9610c52014-08-25 17:02:59 -07001990 SafeDelete(mVertexUniformStorage);
1991 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001992
1993 mSamplersPS.clear();
1994 mSamplersVS.clear();
1995
1996 mUsedVertexSamplerRange = 0;
1997 mUsedPixelSamplerRange = 0;
1998 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05001999
2000 std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1);
Brandon Jonesc9610c52014-08-25 17:02:59 -07002001}
2002
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003unsigned int ProgramD3D::getSerial() const
2004{
2005 return mSerial;
2006}
2007
2008unsigned int ProgramD3D::issueSerial()
2009{
2010 return mCurrentSerial++;
2011}
2012
Jamie Madill437d2662014-12-05 14:23:35 -05002013void ProgramD3D::initAttributesByLayout()
2014{
2015 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
2016 {
2017 mAttributesByLayout[i] = i;
2018 }
2019
2020 std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
2021}
2022
Jamie Madill476682e2015-06-30 10:04:29 -04002023void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -04002024 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
2025 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const
Jamie Madill437d2662014-12-05 14:23:35 -05002026{
Jamie Madill476682e2015-06-30 10:04:29 -04002027 for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex)
Jamie Madill437d2662014-12-05 14:23:35 -05002028 {
Jamie Madill476682e2015-06-30 10:04:29 -04002029 int oldIndex = mAttributesByLayout[attribIndex];
2030 sortedSemanticIndicesOut[attribIndex] = mSemanticIndex[oldIndex];
2031 sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex];
Jamie Madill437d2662014-12-05 14:23:35 -05002032 }
2033}
2034
Brandon Jonesc9610c52014-08-25 17:02:59 -07002035}