blob: 75da78110e7acf8336ec5f7836bfe6cbd6ce9832 [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
9#include "libGLESv2/renderer/d3d/ProgramD3D.h"
10
Brandon Jones091540d2014-10-29 11:32:04 -070011#include "common/features.h"
Brandon Jonesc9610c52014-08-25 17:02:59 -070012#include "common/utilities.h"
Brandon Joneseb994362014-09-24 10:27:28 -070013#include "libGLESv2/Framebuffer.h"
14#include "libGLESv2/FramebufferAttachment.h"
Brandon Jones18bd4102014-09-22 14:21:44 -070015#include "libGLESv2/Program.h"
Brandon Jonesc9610c52014-08-25 17:02:59 -070016#include "libGLESv2/ProgramBinary.h"
Jamie Madill93e13fb2014-11-06 15:27:25 -050017#include "libGLESv2/main.h"
Brandon Jonesc9610c52014-08-25 17:02:59 -070018#include "libGLESv2/renderer/ShaderExecutable.h"
19#include "libGLESv2/renderer/d3d/DynamicHLSL.h"
Jamie Madill93e13fb2014-11-06 15:27:25 -050020#include "libGLESv2/renderer/d3d/RendererD3D.h"
Brandon Jones22502d52014-08-29 16:58:36 -070021#include "libGLESv2/renderer/d3d/ShaderD3D.h"
Brandon Jonesc9610c52014-08-25 17:02:59 -070022
23namespace 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{
87 std::vector<GLenum> defaultPixelOutput(1);
88
89 ASSERT(!shaderOutputVars.empty());
90 defaultPixelOutput[0] = GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex;
91
92 return defaultPixelOutput;
93}
94
Brandon Jones1a8a7e32014-10-01 12:49:30 -070095bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
96{
97 return var.isRowMajorLayout;
98}
99
100bool IsRowMajorLayout(const sh::ShaderVariable &var)
101{
102 return false;
103}
104
Brandon Joneseb994362014-09-24 10:27:28 -0700105}
106
107ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[],
108 const GLenum signature[],
109 ShaderExecutable *shaderExecutable)
110 : mShaderExecutable(shaderExecutable)
111{
112 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
113 {
114 mInputs[attributeIndex] = inputLayout[attributeIndex];
115 mSignature[attributeIndex] = signature[attributeIndex];
116 }
117}
118
119ProgramD3D::VertexExecutable::~VertexExecutable()
120{
121 SafeDelete(mShaderExecutable);
122}
123
124bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const
125{
126 for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++)
127 {
128 if (mSignature[attributeIndex] != signature[attributeIndex])
129 {
130 return false;
131 }
132 }
133
134 return true;
135}
136
137ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutable *shaderExecutable)
138 : mOutputSignature(outputSignature),
139 mShaderExecutable(shaderExecutable)
140{
141}
142
143ProgramD3D::PixelExecutable::~PixelExecutable()
144{
145 SafeDelete(mShaderExecutable);
146}
147
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700148ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
149{
150}
151
Jamie Madill93e13fb2014-11-06 15:27:25 -0500152ProgramD3D::ProgramD3D(RendererD3D *renderer)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700153 : ProgramImpl(),
154 mRenderer(renderer),
155 mDynamicHLSL(NULL),
Brandon Joneseb994362014-09-24 10:27:28 -0700156 mGeometryExecutable(NULL),
157 mVertexWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
158 mPixelWorkarounds(ANGLE_D3D_WORKAROUND_NONE),
Brandon Jones44151a92014-09-10 11:32:25 -0700159 mUsesPointSize(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700160 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700161 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700162 mUsedVertexSamplerRange(0),
163 mUsedPixelSamplerRange(0),
164 mDirtySamplerMapping(true),
Brandon Jones44151a92014-09-10 11:32:25 -0700165 mShaderVersion(100)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700166{
Brandon Joneseb994362014-09-24 10:27:28 -0700167 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700168}
169
170ProgramD3D::~ProgramD3D()
171{
172 reset();
173 SafeDelete(mDynamicHLSL);
174}
175
176ProgramD3D *ProgramD3D::makeProgramD3D(ProgramImpl *impl)
177{
178 ASSERT(HAS_DYNAMIC_TYPE(ProgramD3D*, impl));
179 return static_cast<ProgramD3D*>(impl);
180}
181
182const ProgramD3D *ProgramD3D::makeProgramD3D(const ProgramImpl *impl)
183{
184 ASSERT(HAS_DYNAMIC_TYPE(const ProgramD3D*, impl));
185 return static_cast<const ProgramD3D*>(impl);
186}
187
Brandon Jones44151a92014-09-10 11:32:25 -0700188bool ProgramD3D::usesPointSpriteEmulation() const
189{
190 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
191}
192
193bool ProgramD3D::usesGeometryShader() const
194{
195 return usesPointSpriteEmulation();
196}
197
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700198GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const
199{
200 GLint logicalTextureUnit = -1;
201
202 switch (type)
203 {
204 case gl::SAMPLER_PIXEL:
205 ASSERT(samplerIndex < caps.maxTextureImageUnits);
206 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
207 {
208 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
209 }
210 break;
211 case gl::SAMPLER_VERTEX:
212 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
213 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
214 {
215 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
216 }
217 break;
218 default: UNREACHABLE();
219 }
220
221 if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
222 {
223 return logicalTextureUnit;
224 }
225
226 return -1;
227}
228
229// Returns the texture type for a given Direct3D 9 sampler type and
230// index (0-15 for the pixel shader and 0-3 for the vertex shader).
231GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
232{
233 switch (type)
234 {
235 case gl::SAMPLER_PIXEL:
236 ASSERT(samplerIndex < mSamplersPS.size());
237 ASSERT(mSamplersPS[samplerIndex].active);
238 return mSamplersPS[samplerIndex].textureType;
239 case gl::SAMPLER_VERTEX:
240 ASSERT(samplerIndex < mSamplersVS.size());
241 ASSERT(mSamplersVS[samplerIndex].active);
242 return mSamplersVS[samplerIndex].textureType;
243 default: UNREACHABLE();
244 }
245
246 return GL_TEXTURE_2D;
247}
248
249GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
250{
251 switch (type)
252 {
253 case gl::SAMPLER_PIXEL:
254 return mUsedPixelSamplerRange;
255 case gl::SAMPLER_VERTEX:
256 return mUsedVertexSamplerRange;
257 default:
258 UNREACHABLE();
259 return 0;
260 }
261}
262
263void ProgramD3D::updateSamplerMapping()
264{
265 if (!mDirtySamplerMapping)
266 {
267 return;
268 }
269
270 mDirtySamplerMapping = false;
271
272 // Retrieve sampler uniform values
273 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
274 {
275 gl::LinkedUniform *targetUniform = mUniforms[uniformIndex];
276
277 if (targetUniform->dirty)
278 {
279 if (gl::IsSampler(targetUniform->type))
280 {
281 int count = targetUniform->elementCount();
282 GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data);
283
284 if (targetUniform->isReferencedByFragmentShader())
285 {
286 unsigned int firstIndex = targetUniform->psRegisterIndex;
287
288 for (int i = 0; i < count; i++)
289 {
290 unsigned int samplerIndex = firstIndex + i;
291
292 if (samplerIndex < mSamplersPS.size())
293 {
294 ASSERT(mSamplersPS[samplerIndex].active);
295 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
296 }
297 }
298 }
299
300 if (targetUniform->isReferencedByVertexShader())
301 {
302 unsigned int firstIndex = targetUniform->vsRegisterIndex;
303
304 for (int i = 0; i < count; i++)
305 {
306 unsigned int samplerIndex = firstIndex + i;
307
308 if (samplerIndex < mSamplersVS.size())
309 {
310 ASSERT(mSamplersVS[samplerIndex].active);
311 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
312 }
313 }
314 }
315 }
316 }
317 }
318}
319
320bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps)
321{
322 // if any two active samplers in a program are of different types, but refer to the same
323 // texture image unit, and this is the current program, then ValidateProgram will fail, and
324 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
325 updateSamplerMapping();
326
327 std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE);
328
329 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
330 {
331 if (mSamplersPS[i].active)
332 {
333 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
334
335 if (unit >= textureUnitTypes.size())
336 {
337 if (infoLog)
338 {
339 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
340 }
341
342 return false;
343 }
344
345 if (textureUnitTypes[unit] != GL_NONE)
346 {
347 if (mSamplersPS[i].textureType != textureUnitTypes[unit])
348 {
349 if (infoLog)
350 {
351 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
352 }
353
354 return false;
355 }
356 }
357 else
358 {
359 textureUnitTypes[unit] = mSamplersPS[i].textureType;
360 }
361 }
362 }
363
364 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
365 {
366 if (mSamplersVS[i].active)
367 {
368 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
369
370 if (unit >= textureUnitTypes.size())
371 {
372 if (infoLog)
373 {
374 infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size());
375 }
376
377 return false;
378 }
379
380 if (textureUnitTypes[unit] != GL_NONE)
381 {
382 if (mSamplersVS[i].textureType != textureUnitTypes[unit])
383 {
384 if (infoLog)
385 {
386 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
387 }
388
389 return false;
390 }
391 }
392 else
393 {
394 textureUnitTypes[unit] = mSamplersVS[i].textureType;
395 }
396 }
397 }
398
399 return true;
400}
401
Geoff Langb543aff2014-09-30 14:52:54 -0400402gl::LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700403{
Brandon Jones44151a92014-09-10 11:32:25 -0700404 stream->readInt(&mShaderVersion);
405
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700406 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
407 for (unsigned int i = 0; i < psSamplerCount; ++i)
408 {
409 Sampler sampler;
410 stream->readBool(&sampler.active);
411 stream->readInt(&sampler.logicalTextureUnit);
412 stream->readInt(&sampler.textureType);
413 mSamplersPS.push_back(sampler);
414 }
415 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
416 for (unsigned int i = 0; i < vsSamplerCount; ++i)
417 {
418 Sampler sampler;
419 stream->readBool(&sampler.active);
420 stream->readInt(&sampler.logicalTextureUnit);
421 stream->readInt(&sampler.textureType);
422 mSamplersVS.push_back(sampler);
423 }
424
425 stream->readInt(&mUsedVertexSamplerRange);
426 stream->readInt(&mUsedPixelSamplerRange);
427
428 const unsigned int uniformCount = stream->readInt<unsigned int>();
429 if (stream->error())
430 {
431 infoLog.append("Invalid program binary.");
432 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
433 }
434
435 mUniforms.resize(uniformCount);
436 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
437 {
438 GLenum type = stream->readInt<GLenum>();
439 GLenum precision = stream->readInt<GLenum>();
440 std::string name = stream->readString();
441 unsigned int arraySize = stream->readInt<unsigned int>();
442 int blockIndex = stream->readInt<int>();
443
444 int offset = stream->readInt<int>();
445 int arrayStride = stream->readInt<int>();
446 int matrixStride = stream->readInt<int>();
447 bool isRowMajorMatrix = stream->readBool();
448
449 const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix);
450
451 gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo);
452
453 stream->readInt(&uniform->psRegisterIndex);
454 stream->readInt(&uniform->vsRegisterIndex);
455 stream->readInt(&uniform->registerCount);
456 stream->readInt(&uniform->registerElement);
457
458 mUniforms[uniformIndex] = uniform;
459 }
460
461 const unsigned int uniformIndexCount = stream->readInt<unsigned int>();
462 if (stream->error())
463 {
464 infoLog.append("Invalid program binary.");
465 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
466 }
467
468 mUniformIndex.resize(uniformIndexCount);
469 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++)
470 {
471 stream->readString(&mUniformIndex[uniformIndexIndex].name);
472 stream->readInt(&mUniformIndex[uniformIndexIndex].element);
473 stream->readInt(&mUniformIndex[uniformIndexIndex].index);
474 }
475
476 unsigned int uniformBlockCount = stream->readInt<unsigned int>();
477 if (stream->error())
478 {
479 infoLog.append("Invalid program binary.");
480 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
481 }
482
483 mUniformBlocks.resize(uniformBlockCount);
484 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex)
485 {
486 std::string name = stream->readString();
487 unsigned int elementIndex = stream->readInt<unsigned int>();
488 unsigned int dataSize = stream->readInt<unsigned int>();
489
490 gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize);
491
492 stream->readInt(&uniformBlock->psRegisterIndex);
493 stream->readInt(&uniformBlock->vsRegisterIndex);
494
495 unsigned int numMembers = stream->readInt<unsigned int>();
496 uniformBlock->memberUniformIndexes.resize(numMembers);
497 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
498 {
499 stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]);
500 }
501
502 mUniformBlocks[uniformBlockIndex] = uniformBlock;
503 }
504
Brandon Joneseb994362014-09-24 10:27:28 -0700505 stream->readInt(&mTransformFeedbackBufferMode);
506 const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>();
507 mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount);
508 for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++)
509 {
510 gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex];
511
512 stream->readString(&varying.name);
513 stream->readInt(&varying.type);
514 stream->readInt(&varying.size);
515 stream->readString(&varying.semanticName);
516 stream->readInt(&varying.semanticIndex);
517 stream->readInt(&varying.semanticIndexCount);
518 }
519
Brandon Jones22502d52014-08-29 16:58:36 -0700520 stream->readString(&mVertexHLSL);
521 stream->readInt(&mVertexWorkarounds);
522 stream->readString(&mPixelHLSL);
523 stream->readInt(&mPixelWorkarounds);
524 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700525 stream->readBool(&mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700526
527 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
528 mPixelShaderKey.resize(pixelShaderKeySize);
529 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++)
530 {
531 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
532 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
533 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
534 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
535 }
536
Brandon Joneseb994362014-09-24 10:27:28 -0700537 const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data());
538
539 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
540 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++)
541 {
542 gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS];
543
544 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
545 {
546 gl::VertexFormat *vertexInput = &inputLayout[inputIndex];
547 stream->readInt(&vertexInput->mType);
548 stream->readInt(&vertexInput->mNormalized);
549 stream->readInt(&vertexInput->mComponents);
550 stream->readBool(&vertexInput->mPureInteger);
551 }
552
553 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
554 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400555
556 ShaderExecutable *shaderExecutable = NULL;
557 gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
558 SHADER_VERTEX,
559 mTransformFeedbackLinkedVaryings,
560 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
561 &shaderExecutable);
562 if (error.isError())
563 {
564 return gl::LinkResult(false, error);
565 }
566
Brandon Joneseb994362014-09-24 10:27:28 -0700567 if (!shaderExecutable)
568 {
569 infoLog.append("Could not create vertex shader.");
Geoff Langb543aff2014-09-30 14:52:54 -0400570 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700571 }
572
573 // generated converted input layout
574 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
575 getInputLayoutSignature(inputLayout, signature);
576
577 // add new binary
578 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable));
579
580 stream->skip(vertexShaderSize);
581 }
582
583 const size_t pixelShaderCount = stream->readInt<unsigned int>();
584 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
585 {
586 const size_t outputCount = stream->readInt<unsigned int>();
587 std::vector<GLenum> outputs(outputCount);
588 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
589 {
590 stream->readInt(&outputs[outputIndex]);
591 }
592
593 const size_t pixelShaderSize = stream->readInt<unsigned int>();
594 const unsigned char *pixelShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400595 ShaderExecutable *shaderExecutable = NULL;
596 gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
597 mTransformFeedbackLinkedVaryings,
598 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
599 &shaderExecutable);
600 if (error.isError())
601 {
602 return gl::LinkResult(false, error);
603 }
Brandon Joneseb994362014-09-24 10:27:28 -0700604
605 if (!shaderExecutable)
606 {
607 infoLog.append("Could not create pixel shader.");
Geoff Langb543aff2014-09-30 14:52:54 -0400608 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700609 }
610
611 // add new binary
612 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
613
614 stream->skip(pixelShaderSize);
615 }
616
617 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
618
619 if (geometryShaderSize > 0)
620 {
621 const unsigned char *geometryShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400622 gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY,
623 mTransformFeedbackLinkedVaryings,
624 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
625 &mGeometryExecutable);
626 if (error.isError())
627 {
628 return gl::LinkResult(false, error);
629 }
Brandon Joneseb994362014-09-24 10:27:28 -0700630
631 if (!mGeometryExecutable)
632 {
633 infoLog.append("Could not create geometry shader.");
Geoff Langb543aff2014-09-30 14:52:54 -0400634 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700635 }
636 stream->skip(geometryShaderSize);
637 }
638
Brandon Jones18bd4102014-09-22 14:21:44 -0700639 GUID binaryIdentifier = {0};
640 stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
641
642 GUID identifier = mRenderer->getAdapterIdentifier();
643 if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0)
644 {
645 infoLog.append("Invalid program binary.");
Geoff Langb543aff2014-09-30 14:52:54 -0400646 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700647 }
648
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700649 initializeUniformStorage();
650
Geoff Langb543aff2014-09-30 14:52:54 -0400651 return gl::LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700652}
653
Geoff Langb543aff2014-09-30 14:52:54 -0400654gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700655{
Brandon Jones44151a92014-09-10 11:32:25 -0700656 stream->writeInt(mShaderVersion);
657
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700658 stream->writeInt(mSamplersPS.size());
659 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
660 {
661 stream->writeInt(mSamplersPS[i].active);
662 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
663 stream->writeInt(mSamplersPS[i].textureType);
664 }
665
666 stream->writeInt(mSamplersVS.size());
667 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
668 {
669 stream->writeInt(mSamplersVS[i].active);
670 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
671 stream->writeInt(mSamplersVS[i].textureType);
672 }
673
674 stream->writeInt(mUsedVertexSamplerRange);
675 stream->writeInt(mUsedPixelSamplerRange);
676
677 stream->writeInt(mUniforms.size());
678 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
679 {
680 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
681
682 stream->writeInt(uniform.type);
683 stream->writeInt(uniform.precision);
684 stream->writeString(uniform.name);
685 stream->writeInt(uniform.arraySize);
686 stream->writeInt(uniform.blockIndex);
687
688 stream->writeInt(uniform.blockInfo.offset);
689 stream->writeInt(uniform.blockInfo.arrayStride);
690 stream->writeInt(uniform.blockInfo.matrixStride);
691 stream->writeInt(uniform.blockInfo.isRowMajorMatrix);
692
693 stream->writeInt(uniform.psRegisterIndex);
694 stream->writeInt(uniform.vsRegisterIndex);
695 stream->writeInt(uniform.registerCount);
696 stream->writeInt(uniform.registerElement);
697 }
698
699 stream->writeInt(mUniformIndex.size());
700 for (size_t i = 0; i < mUniformIndex.size(); ++i)
701 {
702 stream->writeString(mUniformIndex[i].name);
703 stream->writeInt(mUniformIndex[i].element);
704 stream->writeInt(mUniformIndex[i].index);
705 }
706
707 stream->writeInt(mUniformBlocks.size());
708 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex)
709 {
710 const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex];
711
712 stream->writeString(uniformBlock.name);
713 stream->writeInt(uniformBlock.elementIndex);
714 stream->writeInt(uniformBlock.dataSize);
715
716 stream->writeInt(uniformBlock.memberUniformIndexes.size());
717 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
718 {
719 stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]);
720 }
721
722 stream->writeInt(uniformBlock.psRegisterIndex);
723 stream->writeInt(uniformBlock.vsRegisterIndex);
724 }
725
Brandon Joneseb994362014-09-24 10:27:28 -0700726 stream->writeInt(mTransformFeedbackBufferMode);
727 stream->writeInt(mTransformFeedbackLinkedVaryings.size());
728 for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++)
729 {
730 const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i];
731
732 stream->writeString(varying.name);
733 stream->writeInt(varying.type);
734 stream->writeInt(varying.size);
735 stream->writeString(varying.semanticName);
736 stream->writeInt(varying.semanticIndex);
737 stream->writeInt(varying.semanticIndexCount);
738 }
739
Brandon Jones22502d52014-08-29 16:58:36 -0700740 stream->writeString(mVertexHLSL);
741 stream->writeInt(mVertexWorkarounds);
742 stream->writeString(mPixelHLSL);
743 stream->writeInt(mPixelWorkarounds);
744 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700745 stream->writeInt(mUsesPointSize);
Brandon Jones22502d52014-08-29 16:58:36 -0700746
Brandon Joneseb994362014-09-24 10:27:28 -0700747 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700748 stream->writeInt(pixelShaderKey.size());
749 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++)
750 {
Brandon Joneseb994362014-09-24 10:27:28 -0700751 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700752 stream->writeInt(variable.type);
753 stream->writeString(variable.name);
754 stream->writeString(variable.source);
755 stream->writeInt(variable.outputIndex);
756 }
757
Brandon Joneseb994362014-09-24 10:27:28 -0700758 stream->writeInt(mVertexExecutables.size());
759 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++)
760 {
761 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
762
763 for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++)
764 {
765 const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex];
766 stream->writeInt(vertexInput.mType);
767 stream->writeInt(vertexInput.mNormalized);
768 stream->writeInt(vertexInput.mComponents);
769 stream->writeInt(vertexInput.mPureInteger);
770 }
771
772 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
773 stream->writeInt(vertexShaderSize);
774
775 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
776 stream->writeBytes(vertexBlob, vertexShaderSize);
777 }
778
779 stream->writeInt(mPixelExecutables.size());
780 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++)
781 {
782 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
783
784 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
785 stream->writeInt(outputs.size());
786 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
787 {
788 stream->writeInt(outputs[outputIndex]);
789 }
790
791 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
792 stream->writeInt(pixelShaderSize);
793
794 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
795 stream->writeBytes(pixelBlob, pixelShaderSize);
796 }
797
798 size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
799 stream->writeInt(geometryShaderSize);
800
801 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
802 {
803 const uint8_t *geometryBlob = mGeometryExecutable->getFunction();
804 stream->writeBytes(geometryBlob, geometryShaderSize);
805 }
806
Brandon Jones18bd4102014-09-22 14:21:44 -0700807 GUID binaryIdentifier = mRenderer->getAdapterIdentifier();
Geoff Langb543aff2014-09-30 14:52:54 -0400808 stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID));
Brandon Jones18bd4102014-09-22 14:21:44 -0700809
Geoff Langb543aff2014-09-30 14:52:54 -0400810 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700811}
812
Geoff Langb543aff2014-09-30 14:52:54 -0400813gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutable **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -0700814{
Brandon Joneseb994362014-09-24 10:27:28 -0700815 std::vector<GLenum> outputs;
816
Jamie Madill48faf802014-11-06 15:27:22 -0500817 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(mRenderer->getWorkarounds());
Brandon Joneseb994362014-09-24 10:27:28 -0700818
819 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
820 {
821 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
822
823 if (colorbuffer)
824 {
825 outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding());
826 }
827 else
828 {
829 outputs.push_back(GL_NONE);
830 }
831 }
832
Geoff Langb543aff2014-09-30 14:52:54 -0400833 return getPixelExecutableForOutputLayout(outputs, outExecutable);
Brandon Joneseb994362014-09-24 10:27:28 -0700834}
835
Geoff Langb543aff2014-09-30 14:52:54 -0400836gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature, ShaderExecutable **outExectuable)
Brandon Joneseb994362014-09-24 10:27:28 -0700837{
838 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
839 {
840 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
841 {
Geoff Langb543aff2014-09-30 14:52:54 -0400842 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
843 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700844 }
845 }
846
Brandon Jones22502d52014-08-29 16:58:36 -0700847 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth,
848 outputSignature);
849
850 // Generate new pixel executable
Brandon Joneseb994362014-09-24 10:27:28 -0700851 gl::InfoLog tempInfoLog;
Geoff Langb543aff2014-09-30 14:52:54 -0400852 ShaderExecutable *pixelExecutable = NULL;
853 gl::Error error = mRenderer->compileToExecutable(tempInfoLog, finalPixelHLSL, SHADER_PIXEL,
854 mTransformFeedbackLinkedVaryings,
855 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
856 mPixelWorkarounds, &pixelExecutable);
857 if (error.isError())
858 {
859 return error;
860 }
Brandon Joneseb994362014-09-24 10:27:28 -0700861
862 if (!pixelExecutable)
863 {
864 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
865 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
866 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
867 }
868 else
869 {
870 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
871 }
Brandon Jones22502d52014-08-29 16:58:36 -0700872
Geoff Langb543aff2014-09-30 14:52:54 -0400873 *outExectuable = pixelExecutable;
874 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700875}
876
Geoff Langb543aff2014-09-30 14:52:54 -0400877gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS], ShaderExecutable **outExectuable)
Brandon Jones22502d52014-08-29 16:58:36 -0700878{
Brandon Joneseb994362014-09-24 10:27:28 -0700879 GLenum signature[gl::MAX_VERTEX_ATTRIBS];
880 getInputLayoutSignature(inputLayout, signature);
881
882 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
883 {
884 if (mVertexExecutables[executableIndex]->matchesSignature(signature))
885 {
Geoff Langb543aff2014-09-30 14:52:54 -0400886 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
887 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -0700888 }
889 }
890
Brandon Jones22502d52014-08-29 16:58:36 -0700891 // Generate new dynamic layout with attribute conversions
Brandon Joneseb994362014-09-24 10:27:28 -0700892 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes);
Brandon Jones22502d52014-08-29 16:58:36 -0700893
894 // Generate new vertex executable
Brandon Joneseb994362014-09-24 10:27:28 -0700895 gl::InfoLog tempInfoLog;
Geoff Langb543aff2014-09-30 14:52:54 -0400896 ShaderExecutable *vertexExecutable = NULL;
897 gl::Error error = mRenderer->compileToExecutable(tempInfoLog, finalVertexHLSL, SHADER_VERTEX,
898 mTransformFeedbackLinkedVaryings,
899 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
900 mVertexWorkarounds, &vertexExecutable);
901 if (error.isError())
902 {
903 return error;
904 }
905
Brandon Joneseb994362014-09-24 10:27:28 -0700906 if (!vertexExecutable)
907 {
908 std::vector<char> tempCharBuffer(tempInfoLog.getLength()+3);
909 tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]);
910 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
911 }
912 else
913 {
914 mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable));
915 }
Brandon Jones22502d52014-08-29 16:58:36 -0700916
Geoff Langb543aff2014-09-30 14:52:54 -0400917 *outExectuable = vertexExecutable;
918 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -0700919}
920
Geoff Langb543aff2014-09-30 14:52:54 -0400921gl::LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
922 int registers)
Brandon Jones44151a92014-09-10 11:32:25 -0700923{
Brandon Jones18bd4102014-09-22 14:21:44 -0700924 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
925 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
Brandon Jones44151a92014-09-10 11:32:25 -0700926
Brandon Joneseb994362014-09-24 10:27:28 -0700927 gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS];
928 GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout);
Geoff Langb543aff2014-09-30 14:52:54 -0400929 ShaderExecutable *defaultVertexExecutable = NULL;
930 gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable);
931 if (error.isError())
932 {
933 return gl::LinkResult(false, error);
934 }
Brandon Jones44151a92014-09-10 11:32:25 -0700935
Brandon Joneseb994362014-09-24 10:27:28 -0700936 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Langb543aff2014-09-30 14:52:54 -0400937 ShaderExecutable *defaultPixelExecutable = NULL;
938 error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable);
939 if (error.isError())
940 {
941 return gl::LinkResult(false, error);
942 }
Brandon Jones44151a92014-09-10 11:32:25 -0700943
Brandon Joneseb994362014-09-24 10:27:28 -0700944 if (usesGeometryShader())
945 {
946 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D);
Brandon Jones44151a92014-09-10 11:32:25 -0700947
Geoff Langb543aff2014-09-30 14:52:54 -0400948
949 error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings,
950 (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
951 ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable);
952 if (error.isError())
953 {
954 return gl::LinkResult(false, error);
955 }
Brandon Joneseb994362014-09-24 10:27:28 -0700956 }
957
Brandon Jones091540d2014-10-29 11:32:04 -0700958#if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED
Tibor den Ouden97049c62014-10-06 21:39:16 +0200959 if (usesGeometryShader() && mGeometryExecutable)
960 {
961 // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level
962 // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch
963 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
964 vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo());
965 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
966 }
967
968 if (defaultVertexExecutable)
969 {
970 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
971 }
972
973 if (defaultPixelExecutable)
974 {
975 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
976 }
977#endif
978
Geoff Langb543aff2014-09-30 14:52:54 -0400979 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable));
980 return gl::LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -0700981}
982
Jamie Madillde8892b2014-11-11 13:00:22 -0500983gl::LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog,
984 gl::Shader *fragmentShader, gl::Shader *vertexShader,
985 const std::vector<std::string> &transformFeedbackVaryings,
986 GLenum transformFeedbackBufferMode,
Geoff Langb543aff2014-09-30 14:52:54 -0400987 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
Jamie Madillde8892b2014-11-11 13:00:22 -0500988 std::map<int, gl::VariableLocation> *outputVariables)
Brandon Jones22502d52014-08-29 16:58:36 -0700989{
Brandon Joneseb994362014-09-24 10:27:28 -0700990 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
991 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
992
Jamie Madillde8892b2014-11-11 13:00:22 -0500993 mSamplersPS.resize(data.caps->maxTextureImageUnits);
994 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700995
Brandon Joneseb994362014-09-24 10:27:28 -0700996 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -0700997
998 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
999 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
1000
1001 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1002 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001003 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001004
1005 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001006 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001007 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1008
Geoff Langbdee2d52014-09-17 11:02:51 -04001009 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001010 {
Geoff Langb543aff2014-09-30 14:52:54 -04001011 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001012 }
1013
1014 if (!gl::ProgramBinary::linkVaryings(infoLog, fragmentShader, vertexShader))
1015 {
Geoff Langb543aff2014-09-30 14:52:54 -04001016 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001017 }
1018
Jamie Madillde8892b2014-11-11 13:00:22 -05001019 if (!mDynamicHLSL->generateShaderLinkHLSL(data, infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
Brandon Jones22502d52014-08-29 16:58:36 -07001020 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1021 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1022 {
Geoff Langb543aff2014-09-30 14:52:54 -04001023 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001024 }
1025
Brandon Jones44151a92014-09-10 11:32:25 -07001026 mUsesPointSize = vertexShaderD3D->usesPointSize();
1027
Geoff Langb543aff2014-09-30 14:52:54 -04001028 return gl::LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001029}
1030
Brandon Jones44151a92014-09-10 11:32:25 -07001031void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1032{
1033 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1034}
1035
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001036void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001037{
1038 // Compute total default block size
1039 unsigned int vertexRegisters = 0;
1040 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001041 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001042 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001043 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001044
1045 if (!gl::IsSampler(uniform.type))
1046 {
1047 if (uniform.isReferencedByVertexShader())
1048 {
1049 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1050 }
1051 if (uniform.isReferencedByFragmentShader())
1052 {
1053 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1054 }
1055 }
1056 }
1057
1058 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1059 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1060}
1061
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001062gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001063{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001064 updateSamplerMapping();
1065
1066 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1067 if (error.isError())
1068 {
1069 return error;
1070 }
1071
1072 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1073 {
1074 mUniforms[uniformIndex]->dirty = false;
1075 }
1076
1077 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001078}
1079
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001080gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001081{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001082 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1083
Brandon Jones18bd4102014-09-22 14:21:44 -07001084 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1085 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1086
1087 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1088 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1089
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001090 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001091 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001092 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001093 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1094
1095 ASSERT(uniformBlock && uniformBuffer);
1096
1097 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1098 {
1099 // undefined behaviour
1100 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1101 }
1102
1103 // Unnecessary to apply an unreferenced standard or shared UBO
1104 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1105 {
1106 continue;
1107 }
1108
1109 if (uniformBlock->isReferencedByVertexShader())
1110 {
1111 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1112 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1113 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1114 vertexUniformBuffers[registerIndex] = uniformBuffer;
1115 }
1116
1117 if (uniformBlock->isReferencedByFragmentShader())
1118 {
1119 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1120 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1121 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1122 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1123 }
1124 }
1125
1126 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1127}
1128
1129bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1130 unsigned int registerIndex, const gl::Caps &caps)
1131{
1132 if (shader == GL_VERTEX_SHADER)
1133 {
1134 uniformBlock->vsRegisterIndex = registerIndex;
1135 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1136 {
1137 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1138 return false;
1139 }
1140 }
1141 else if (shader == GL_FRAGMENT_SHADER)
1142 {
1143 uniformBlock->psRegisterIndex = registerIndex;
1144 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1145 {
1146 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1147 return false;
1148 }
1149 }
1150 else UNREACHABLE();
1151
1152 return true;
1153}
1154
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001155void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001156{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001157 unsigned int numUniforms = mUniforms.size();
1158 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001159 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001160 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001161 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001162}
1163
1164void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1165{
1166 setUniform(location, count, v, GL_FLOAT);
1167}
1168
1169void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1170{
1171 setUniform(location, count, v, GL_FLOAT_VEC2);
1172}
1173
1174void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1175{
1176 setUniform(location, count, v, GL_FLOAT_VEC3);
1177}
1178
1179void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1180{
1181 setUniform(location, count, v, GL_FLOAT_VEC4);
1182}
1183
1184void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1185{
1186 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1187}
1188
1189void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1190{
1191 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1192}
1193
1194void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1195{
1196 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1197}
1198
1199void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1200{
1201 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1202}
1203
1204void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1205{
1206 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1207}
1208
1209void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1210{
1211 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1212}
1213
1214void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1215{
1216 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1217}
1218
1219void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1220{
1221 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1222}
1223
1224void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1225{
1226 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1227}
1228
1229void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1230{
1231 setUniform(location, count, v, GL_INT);
1232}
1233
1234void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1235{
1236 setUniform(location, count, v, GL_INT_VEC2);
1237}
1238
1239void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1240{
1241 setUniform(location, count, v, GL_INT_VEC3);
1242}
1243
1244void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1245{
1246 setUniform(location, count, v, GL_INT_VEC4);
1247}
1248
1249void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1250{
1251 setUniform(location, count, v, GL_UNSIGNED_INT);
1252}
1253
1254void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1255{
1256 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1257}
1258
1259void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1260{
1261 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1262}
1263
1264void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1265{
1266 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1267}
1268
1269void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1270{
1271 getUniformv(location, params, GL_FLOAT);
1272}
1273
1274void ProgramD3D::getUniformiv(GLint location, GLint *params)
1275{
1276 getUniformv(location, params, GL_INT);
1277}
1278
1279void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1280{
1281 getUniformv(location, params, GL_UNSIGNED_INT);
1282}
1283
1284bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1285 const gl::Caps &caps)
1286{
Jamie Madill30d6c252014-11-13 10:03:33 -05001287 const ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1288 const ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001289
1290 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1291 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1292
1293 // Check that uniforms defined in the vertex and fragment shaders are identical
1294 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1295 UniformMap linkedUniforms;
1296
1297 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001298 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001299 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1300 linkedUniforms[vertexUniform.name] = &vertexUniform;
1301 }
1302
1303 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1304 {
1305 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1306 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1307 if (entry != linkedUniforms.end())
1308 {
1309 const sh::Uniform &vertexUniform = *entry->second;
1310 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
1311 if (!gl::ProgramBinary::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
1312 {
1313 return false;
1314 }
1315 }
1316 }
1317
1318 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1319 {
1320 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1321
1322 if (uniform.staticUse)
1323 {
1324 defineUniformBase(GL_VERTEX_SHADER, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
1325 }
1326 }
1327
1328 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1329 {
1330 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1331
1332 if (uniform.staticUse)
1333 {
1334 defineUniformBase(GL_FRAGMENT_SHADER, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
1335 }
1336 }
1337
1338 if (!indexUniforms(infoLog, caps))
1339 {
1340 return false;
1341 }
1342
1343 initializeUniformStorage();
1344
1345 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1346 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1347 {
1348 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1349
1350 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1351 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1352 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1353 }
1354
1355 return true;
1356}
1357
1358void ProgramD3D::defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister)
1359{
Jamie Madill30d6c252014-11-13 10:03:33 -05001360 ShShaderOutput outputType = ShaderD3D::getCompilerOutputType(shader);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001361 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1362 encoder.skipRegisters(uniformRegister);
1363
1364 defineUniform(shader, uniform, uniform.name, &encoder);
1365}
1366
1367void ProgramD3D::defineUniform(GLenum shader, const sh::ShaderVariable &uniform,
1368 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
1369{
1370 if (uniform.isStruct())
1371 {
1372 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1373 {
1374 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1375
1376 encoder->enterAggregateType();
1377
1378 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1379 {
1380 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1381 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1382
1383 defineUniform(shader, field, fieldFullName, encoder);
1384 }
1385
1386 encoder->exitAggregateType();
1387 }
1388 }
1389 else // Not a struct
1390 {
1391 // Arrays are treated as aggregate types
1392 if (uniform.isArray())
1393 {
1394 encoder->enterAggregateType();
1395 }
1396
1397 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1398
1399 if (!linkedUniform)
1400 {
1401 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
1402 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
1403 ASSERT(linkedUniform);
1404 linkedUniform->registerElement = encoder->getCurrentElement();
1405 mUniforms.push_back(linkedUniform);
1406 }
1407
1408 ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
1409
1410 if (shader == GL_FRAGMENT_SHADER)
1411 {
1412 linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
1413 }
1414 else if (shader == GL_VERTEX_SHADER)
1415 {
1416 linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
1417 }
1418 else UNREACHABLE();
1419
1420 // Advance the uniform offset, to track registers allocation for structs
1421 encoder->encodeType(uniform.type, uniform.arraySize, false);
1422
1423 // Arrays are treated as aggregate types
1424 if (uniform.isArray())
1425 {
1426 encoder->exitAggregateType();
1427 }
1428 }
1429}
1430
1431template <typename T>
1432static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1433{
1434 ASSERT(dest != NULL);
1435 ASSERT(dirtyFlag != NULL);
1436
1437 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1438 *dest = source;
1439}
1440
1441template <typename T>
1442void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1443{
1444 const int components = gl::VariableComponentCount(targetUniformType);
1445 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1446
1447 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1448
1449 int elementCount = targetUniform->elementCount();
1450
1451 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1452
1453 if (targetUniform->type == targetUniformType)
1454 {
1455 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1456
1457 for (int i = 0; i < count; i++)
1458 {
1459 T *dest = target + (i * 4);
1460 const T *source = v + (i * components);
1461
1462 for (int c = 0; c < components; c++)
1463 {
1464 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1465 }
1466 for (int c = components; c < 4; c++)
1467 {
1468 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1469 }
1470 }
1471 }
1472 else if (targetUniform->type == targetBoolType)
1473 {
1474 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1475
1476 for (int i = 0; i < count; i++)
1477 {
1478 GLint *dest = boolParams + (i * 4);
1479 const T *source = v + (i * components);
1480
1481 for (int c = 0; c < components; c++)
1482 {
1483 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1484 }
1485 for (int c = components; c < 4; c++)
1486 {
1487 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1488 }
1489 }
1490 }
1491 else if (gl::IsSampler(targetUniform->type))
1492 {
1493 ASSERT(targetUniformType == GL_INT);
1494
1495 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1496
1497 bool wasDirty = targetUniform->dirty;
1498
1499 for (int i = 0; i < count; i++)
1500 {
1501 GLint *dest = target + (i * 4);
1502 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1503
1504 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1505 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1506 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1507 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1508 }
1509
1510 if (!wasDirty && targetUniform->dirty)
1511 {
1512 mDirtySamplerMapping = true;
1513 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001514 }
1515 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001516}
Brandon Jones18bd4102014-09-22 14:21:44 -07001517
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001518template<typename T>
1519bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1520{
1521 bool dirty = false;
1522 int copyWidth = std::min(targetHeight, srcWidth);
1523 int copyHeight = std::min(targetWidth, srcHeight);
1524
1525 for (int x = 0; x < copyWidth; x++)
1526 {
1527 for (int y = 0; y < copyHeight; y++)
1528 {
1529 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1530 }
1531 }
1532 // clear unfilled right side
1533 for (int y = 0; y < copyWidth; y++)
1534 {
1535 for (int x = copyHeight; x < targetWidth; x++)
1536 {
1537 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1538 }
1539 }
1540 // clear unfilled bottom.
1541 for (int y = copyWidth; y < targetHeight; y++)
1542 {
1543 for (int x = 0; x < targetWidth; x++)
1544 {
1545 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1546 }
1547 }
1548
1549 return dirty;
1550}
1551
1552template<typename T>
1553bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1554{
1555 bool dirty = false;
1556 int copyWidth = std::min(targetWidth, srcWidth);
1557 int copyHeight = std::min(targetHeight, srcHeight);
1558
1559 for (int y = 0; y < copyHeight; y++)
1560 {
1561 for (int x = 0; x < copyWidth; x++)
1562 {
1563 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1564 }
1565 }
1566 // clear unfilled right side
1567 for (int y = 0; y < copyHeight; y++)
1568 {
1569 for (int x = copyWidth; x < targetWidth; x++)
1570 {
1571 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1572 }
1573 }
1574 // clear unfilled bottom.
1575 for (int y = copyHeight; y < targetHeight; y++)
1576 {
1577 for (int x = 0; x < targetWidth; x++)
1578 {
1579 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1580 }
1581 }
1582
1583 return dirty;
1584}
1585
1586template <int cols, int rows>
1587void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1588{
1589 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1590
1591 int elementCount = targetUniform->elementCount();
1592
1593 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1594 const unsigned int targetMatrixStride = (4 * rows);
1595 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1596
1597 for (int i = 0; i < count; i++)
1598 {
1599 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1600 if (transpose == GL_FALSE)
1601 {
1602 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1603 }
1604 else
1605 {
1606 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1607 }
1608 target += targetMatrixStride;
1609 value += cols * rows;
1610 }
1611}
1612
1613template <typename T>
1614void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1615{
1616 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1617
1618 if (gl::IsMatrixType(targetUniform->type))
1619 {
1620 const int rows = gl::VariableRowCount(targetUniform->type);
1621 const int cols = gl::VariableColumnCount(targetUniform->type);
1622 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1623 }
1624 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1625 {
1626 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1627 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1628 size * sizeof(T));
1629 }
1630 else
1631 {
1632 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1633 switch (gl::VariableComponentType(targetUniform->type))
1634 {
1635 case GL_BOOL:
1636 {
1637 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1638
1639 for (unsigned int i = 0; i < size; i++)
1640 {
1641 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1642 }
1643 }
1644 break;
1645
1646 case GL_FLOAT:
1647 {
1648 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1649
1650 for (unsigned int i = 0; i < size; i++)
1651 {
1652 params[i] = static_cast<T>(floatParams[i]);
1653 }
1654 }
1655 break;
1656
1657 case GL_INT:
1658 {
1659 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1660
1661 for (unsigned int i = 0; i < size; i++)
1662 {
1663 params[i] = static_cast<T>(intParams[i]);
1664 }
1665 }
1666 break;
1667
1668 case GL_UNSIGNED_INT:
1669 {
1670 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1671
1672 for (unsigned int i = 0; i < size; i++)
1673 {
1674 params[i] = static_cast<T>(uintParams[i]);
1675 }
1676 }
1677 break;
1678
1679 default: UNREACHABLE();
1680 }
1681 }
1682}
1683
1684template <typename VarT>
1685void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1686 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1687 bool inRowMajorLayout)
1688{
1689 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1690 {
1691 const VarT &field = fields[uniformIndex];
1692 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1693
1694 if (field.isStruct())
1695 {
1696 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1697
1698 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1699 {
1700 encoder->enterAggregateType();
1701
1702 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1703 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1704
1705 encoder->exitAggregateType();
1706 }
1707 }
1708 else
1709 {
1710 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1711
1712 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1713
1714 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1715 blockIndex, memberInfo);
1716
1717 // add to uniform list, but not index, since uniform block uniforms have no location
1718 blockUniformIndexes->push_back(mUniforms.size());
1719 mUniforms.push_back(newUniform);
1720 }
1721 }
1722}
1723
1724bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1725 const gl::Caps &caps)
1726{
Jamie Madill30d6c252014-11-13 10:03:33 -05001727 const ShaderD3D* shaderD3D = ShaderD3D::makeShaderD3D(shader.getImplementation());
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001728
1729 // create uniform block entries if they do not exist
1730 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1731 {
1732 std::vector<unsigned int> blockUniformIndexes;
1733 const unsigned int blockIndex = mUniformBlocks.size();
1734
1735 // define member uniforms
1736 sh::BlockLayoutEncoder *encoder = NULL;
1737
1738 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1739 {
1740 encoder = new sh::Std140BlockEncoder;
1741 }
1742 else
1743 {
1744 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1745 }
1746 ASSERT(encoder);
1747
1748 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1749
1750 size_t dataSize = encoder->getBlockSize();
1751
1752 // create all the uniform blocks
1753 if (interfaceBlock.arraySize > 0)
1754 {
1755 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1756 {
1757 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1758 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1759 mUniformBlocks.push_back(newUniformBlock);
1760 }
1761 }
1762 else
1763 {
1764 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1765 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1766 mUniformBlocks.push_back(newUniformBlock);
1767 }
1768 }
1769
1770 if (interfaceBlock.staticUse)
1771 {
1772 // Assign registers to the uniform blocks
1773 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1774 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1775 ASSERT(blockIndex != GL_INVALID_INDEX);
1776 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1777
1778 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1779
1780 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1781 {
1782 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1783 ASSERT(uniformBlock->name == interfaceBlock.name);
1784
1785 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1786 interfaceBlockRegister + uniformBlockElement, caps))
1787 {
1788 return false;
1789 }
1790 }
1791 }
1792
1793 return true;
1794}
1795
1796bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1797 GLenum samplerType,
1798 unsigned int samplerCount,
1799 std::vector<Sampler> &outSamplers,
1800 GLuint *outUsedRange)
1801{
1802 unsigned int samplerIndex = startSamplerIndex;
1803
1804 do
1805 {
1806 if (samplerIndex < outSamplers.size())
1807 {
1808 Sampler& sampler = outSamplers[samplerIndex];
1809 sampler.active = true;
1810 sampler.textureType = GetTextureType(samplerType);
1811 sampler.logicalTextureUnit = 0;
1812 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1813 }
1814 else
1815 {
1816 return false;
1817 }
1818
1819 samplerIndex++;
1820 } while (samplerIndex < startSamplerIndex + samplerCount);
1821
1822 return true;
1823}
1824
1825bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1826{
1827 ASSERT(gl::IsSampler(uniform.type));
1828 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1829
1830 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1831 {
1832 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1833 &mUsedVertexSamplerRange))
1834 {
1835 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1836 mSamplersVS.size());
1837 return false;
1838 }
1839
1840 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1841 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1842 {
1843 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1844 caps.maxVertexUniformVectors);
1845 return false;
1846 }
1847 }
1848
1849 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1850 {
1851 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1852 &mUsedPixelSamplerRange))
1853 {
1854 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1855 mSamplersPS.size());
1856 return false;
1857 }
1858
1859 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1860 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1861 {
1862 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1863 caps.maxFragmentUniformVectors);
1864 return false;
1865 }
1866 }
1867
1868 return true;
1869}
1870
1871bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1872{
1873 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1874 {
1875 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1876
1877 if (gl::IsSampler(uniform.type))
1878 {
1879 if (!indexSamplerUniform(uniform, infoLog, caps))
1880 {
1881 return false;
1882 }
1883 }
1884
1885 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1886 {
1887 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1888 }
1889 }
1890
1891 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001892}
1893
Brandon Jonesc9610c52014-08-25 17:02:59 -07001894void ProgramD3D::reset()
1895{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001896 ProgramImpl::reset();
1897
Brandon Joneseb994362014-09-24 10:27:28 -07001898 SafeDeleteContainer(mVertexExecutables);
1899 SafeDeleteContainer(mPixelExecutables);
1900 SafeDelete(mGeometryExecutable);
1901
1902 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001903
Brandon Jones22502d52014-08-29 16:58:36 -07001904 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001905 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001906 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001907
1908 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001909 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001910 mUsesFragDepth = false;
1911 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001912 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001913
Brandon Jonesc9610c52014-08-25 17:02:59 -07001914 SafeDelete(mVertexUniformStorage);
1915 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001916
1917 mSamplersPS.clear();
1918 mSamplersVS.clear();
1919
1920 mUsedVertexSamplerRange = 0;
1921 mUsedPixelSamplerRange = 0;
1922 mDirtySamplerMapping = true;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001923}
1924
1925}