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