blob: 6681d756974f243e82831486c62e16dd235ebc3b [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"
17#include "libGLESv2/renderer/Renderer.h"
18#include "libGLESv2/renderer/ShaderExecutable.h"
19#include "libGLESv2/renderer/d3d/DynamicHLSL.h"
Brandon Jones22502d52014-08-29 16:58:36 -070020#include "libGLESv2/renderer/d3d/ShaderD3D.h"
Brandon Jonesc9610c52014-08-25 17:02:59 -070021#include "libGLESv2/main.h"
22
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
Brandon Joneseb994362014-09-24 10:27:28 -0700152ProgramD3D::ProgramD3D(Renderer *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
817 const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender();
818
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
Geoff Langb543aff2014-09-30 14:52:54 -0400983gl::LinkResult ProgramD3D::link(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
984 const std::vector<std::string> &transformFeedbackVaryings, GLenum transformFeedbackBufferMode,
985 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
986 std::map<int, gl::VariableLocation> *outputVariables, const gl::Caps &caps)
Brandon Jones22502d52014-08-29 16:58:36 -0700987{
Brandon Joneseb994362014-09-24 10:27:28 -0700988 ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
989 ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation());
990
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700991 mSamplersPS.resize(caps.maxTextureImageUnits);
992 mSamplersVS.resize(caps.maxVertexTextureImageUnits);
993
Brandon Joneseb994362014-09-24 10:27:28 -0700994 mTransformFeedbackBufferMode = transformFeedbackBufferMode;
Brandon Jones22502d52014-08-29 16:58:36 -0700995
996 mPixelHLSL = fragmentShaderD3D->getTranslatedSource();
997 mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds();
998
999 mVertexHLSL = vertexShaderD3D->getTranslatedSource();
1000 mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds();
Brandon Jones44151a92014-09-10 11:32:25 -07001001 mShaderVersion = vertexShaderD3D->getShaderVersion();
Brandon Jones22502d52014-08-29 16:58:36 -07001002
1003 // Map the varyings to the register file
Brandon Joneseb994362014-09-24 10:27:28 -07001004 VaryingPacking packing = { NULL };
Brandon Jones22502d52014-08-29 16:58:36 -07001005 *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings);
1006
Geoff Langbdee2d52014-09-17 11:02:51 -04001007 if (*registers < 0)
Brandon Jones22502d52014-08-29 16:58:36 -07001008 {
Geoff Langb543aff2014-09-30 14:52:54 -04001009 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001010 }
1011
1012 if (!gl::ProgramBinary::linkVaryings(infoLog, fragmentShader, vertexShader))
1013 {
Geoff Langb543aff2014-09-30 14:52:54 -04001014 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001015 }
1016
1017 if (!mDynamicHLSL->generateShaderLinkHLSL(infoLog, *registers, packing, mPixelHLSL, mVertexHLSL,
1018 fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings,
1019 linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth))
1020 {
Geoff Langb543aff2014-09-30 14:52:54 -04001021 return gl::LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001022 }
1023
Brandon Jones44151a92014-09-10 11:32:25 -07001024 mUsesPointSize = vertexShaderD3D->usesPointSize();
1025
Geoff Langb543aff2014-09-30 14:52:54 -04001026 return gl::LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001027}
1028
Brandon Jones44151a92014-09-10 11:32:25 -07001029void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const
1030{
1031 mDynamicHLSL->getInputLayoutSignature(inputLayout, signature);
1032}
1033
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001034void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001035{
1036 // Compute total default block size
1037 unsigned int vertexRegisters = 0;
1038 unsigned int fragmentRegisters = 0;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001039 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001040 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001041 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
Brandon Jonesc9610c52014-08-25 17:02:59 -07001042
1043 if (!gl::IsSampler(uniform.type))
1044 {
1045 if (uniform.isReferencedByVertexShader())
1046 {
1047 vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount);
1048 }
1049 if (uniform.isReferencedByFragmentShader())
1050 {
1051 fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount);
1052 }
1053 }
1054 }
1055
1056 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1057 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1058}
1059
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001060gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001061{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001062 updateSamplerMapping();
1063
1064 gl::Error error = mRenderer->applyUniforms(*this, mUniforms);
1065 if (error.isError())
1066 {
1067 return error;
1068 }
1069
1070 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1071 {
1072 mUniforms[uniformIndex]->dirty = false;
1073 }
1074
1075 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001076}
1077
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001078gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps)
Brandon Jones18bd4102014-09-22 14:21:44 -07001079{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001080 ASSERT(boundBuffers.size() == mUniformBlocks.size());
1081
Brandon Jones18bd4102014-09-22 14:21:44 -07001082 const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL};
1083 const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL};
1084
1085 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1086 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1087
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001088 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001089 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001090 gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex];
Brandon Jones18bd4102014-09-22 14:21:44 -07001091 gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex];
1092
1093 ASSERT(uniformBlock && uniformBuffer);
1094
1095 if (uniformBuffer->getSize() < uniformBlock->dataSize)
1096 {
1097 // undefined behaviour
1098 return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small.");
1099 }
1100
1101 // Unnecessary to apply an unreferenced standard or shared UBO
1102 if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader())
1103 {
1104 continue;
1105 }
1106
1107 if (uniformBlock->isReferencedByVertexShader())
1108 {
1109 unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS;
1110 ASSERT(vertexUniformBuffers[registerIndex] == NULL);
1111 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
1112 vertexUniformBuffers[registerIndex] = uniformBuffer;
1113 }
1114
1115 if (uniformBlock->isReferencedByFragmentShader())
1116 {
1117 unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS;
1118 ASSERT(fragmentUniformBuffers[registerIndex] == NULL);
1119 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
1120 fragmentUniformBuffers[registerIndex] = uniformBuffer;
1121 }
1122 }
1123
1124 return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers);
1125}
1126
1127bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
1128 unsigned int registerIndex, const gl::Caps &caps)
1129{
1130 if (shader == GL_VERTEX_SHADER)
1131 {
1132 uniformBlock->vsRegisterIndex = registerIndex;
1133 if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks)
1134 {
1135 infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks);
1136 return false;
1137 }
1138 }
1139 else if (shader == GL_FRAGMENT_SHADER)
1140 {
1141 uniformBlock->psRegisterIndex = registerIndex;
1142 if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks)
1143 {
1144 infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks);
1145 return false;
1146 }
1147 }
1148 else UNREACHABLE();
1149
1150 return true;
1151}
1152
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001153void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001154{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001155 unsigned int numUniforms = mUniforms.size();
1156 for (unsigned int index = 0; index < numUniforms; index++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001157 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001158 mUniforms[index]->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001159 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001160}
1161
1162void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
1163{
1164 setUniform(location, count, v, GL_FLOAT);
1165}
1166
1167void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1168{
1169 setUniform(location, count, v, GL_FLOAT_VEC2);
1170}
1171
1172void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1173{
1174 setUniform(location, count, v, GL_FLOAT_VEC3);
1175}
1176
1177void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1178{
1179 setUniform(location, count, v, GL_FLOAT_VEC4);
1180}
1181
1182void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1183{
1184 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1185}
1186
1187void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1188{
1189 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1190}
1191
1192void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1193{
1194 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1195}
1196
1197void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1198{
1199 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1200}
1201
1202void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1203{
1204 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1205}
1206
1207void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1208{
1209 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1210}
1211
1212void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1213{
1214 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1215}
1216
1217void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1218{
1219 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1220}
1221
1222void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
1223{
1224 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1225}
1226
1227void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1228{
1229 setUniform(location, count, v, GL_INT);
1230}
1231
1232void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1233{
1234 setUniform(location, count, v, GL_INT_VEC2);
1235}
1236
1237void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1238{
1239 setUniform(location, count, v, GL_INT_VEC3);
1240}
1241
1242void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1243{
1244 setUniform(location, count, v, GL_INT_VEC4);
1245}
1246
1247void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1248{
1249 setUniform(location, count, v, GL_UNSIGNED_INT);
1250}
1251
1252void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1253{
1254 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1255}
1256
1257void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1258{
1259 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1260}
1261
1262void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1263{
1264 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1265}
1266
1267void ProgramD3D::getUniformfv(GLint location, GLfloat *params)
1268{
1269 getUniformv(location, params, GL_FLOAT);
1270}
1271
1272void ProgramD3D::getUniformiv(GLint location, GLint *params)
1273{
1274 getUniformv(location, params, GL_INT);
1275}
1276
1277void ProgramD3D::getUniformuiv(GLint location, GLuint *params)
1278{
1279 getUniformv(location, params, GL_UNSIGNED_INT);
1280}
1281
1282bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
1283 const gl::Caps &caps)
1284{
1285 const rx::ShaderD3D *vertexShaderD3D = rx::ShaderD3D::makeShaderD3D(vertexShader.getImplementation());
1286 const rx::ShaderD3D *fragmentShaderD3D = rx::ShaderD3D::makeShaderD3D(fragmentShader.getImplementation());
1287
1288 const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms();
1289 const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms();
1290
1291 // Check that uniforms defined in the vertex and fragment shaders are identical
1292 typedef std::map<std::string, const sh::Uniform*> UniformMap;
1293 UniformMap linkedUniforms;
1294
1295 for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001296 {
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001297 const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex];
1298 linkedUniforms[vertexUniform.name] = &vertexUniform;
1299 }
1300
1301 for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++)
1302 {
1303 const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex];
1304 UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name);
1305 if (entry != linkedUniforms.end())
1306 {
1307 const sh::Uniform &vertexUniform = *entry->second;
1308 const std::string &uniformName = "uniform '" + vertexUniform.name + "'";
1309 if (!gl::ProgramBinary::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform))
1310 {
1311 return false;
1312 }
1313 }
1314 }
1315
1316 for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++)
1317 {
1318 const sh::Uniform &uniform = vertexUniforms[uniformIndex];
1319
1320 if (uniform.staticUse)
1321 {
1322 defineUniformBase(GL_VERTEX_SHADER, uniform, vertexShaderD3D->getUniformRegister(uniform.name));
1323 }
1324 }
1325
1326 for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++)
1327 {
1328 const sh::Uniform &uniform = fragmentUniforms[uniformIndex];
1329
1330 if (uniform.staticUse)
1331 {
1332 defineUniformBase(GL_FRAGMENT_SHADER, uniform, fragmentShaderD3D->getUniformRegister(uniform.name));
1333 }
1334 }
1335
1336 if (!indexUniforms(infoLog, caps))
1337 {
1338 return false;
1339 }
1340
1341 initializeUniformStorage();
1342
1343 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1344 if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange())
1345 {
1346 const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo();
1347
1348 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo));
1349 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo));
1350 mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo));
1351 }
1352
1353 return true;
1354}
1355
1356void ProgramD3D::defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister)
1357{
1358 ShShaderOutput outputType = rx::ShaderD3D::getCompilerOutputType(shader);
1359 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
1360 encoder.skipRegisters(uniformRegister);
1361
1362 defineUniform(shader, uniform, uniform.name, &encoder);
1363}
1364
1365void ProgramD3D::defineUniform(GLenum shader, const sh::ShaderVariable &uniform,
1366 const std::string &fullName, sh::HLSLBlockEncoder *encoder)
1367{
1368 if (uniform.isStruct())
1369 {
1370 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1371 {
1372 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1373
1374 encoder->enterAggregateType();
1375
1376 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1377 {
1378 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
1379 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1380
1381 defineUniform(shader, field, fieldFullName, encoder);
1382 }
1383
1384 encoder->exitAggregateType();
1385 }
1386 }
1387 else // Not a struct
1388 {
1389 // Arrays are treated as aggregate types
1390 if (uniform.isArray())
1391 {
1392 encoder->enterAggregateType();
1393 }
1394
1395 gl::LinkedUniform *linkedUniform = getUniformByName(fullName);
1396
1397 if (!linkedUniform)
1398 {
1399 linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
1400 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
1401 ASSERT(linkedUniform);
1402 linkedUniform->registerElement = encoder->getCurrentElement();
1403 mUniforms.push_back(linkedUniform);
1404 }
1405
1406 ASSERT(linkedUniform->registerElement == encoder->getCurrentElement());
1407
1408 if (shader == GL_FRAGMENT_SHADER)
1409 {
1410 linkedUniform->psRegisterIndex = encoder->getCurrentRegister();
1411 }
1412 else if (shader == GL_VERTEX_SHADER)
1413 {
1414 linkedUniform->vsRegisterIndex = encoder->getCurrentRegister();
1415 }
1416 else UNREACHABLE();
1417
1418 // Advance the uniform offset, to track registers allocation for structs
1419 encoder->encodeType(uniform.type, uniform.arraySize, false);
1420
1421 // Arrays are treated as aggregate types
1422 if (uniform.isArray())
1423 {
1424 encoder->exitAggregateType();
1425 }
1426 }
1427}
1428
1429template <typename T>
1430static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag)
1431{
1432 ASSERT(dest != NULL);
1433 ASSERT(dirtyFlag != NULL);
1434
1435 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
1436 *dest = source;
1437}
1438
1439template <typename T>
1440void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
1441{
1442 const int components = gl::VariableComponentCount(targetUniformType);
1443 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1444
1445 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1446
1447 int elementCount = targetUniform->elementCount();
1448
1449 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1450
1451 if (targetUniform->type == targetUniformType)
1452 {
1453 T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4;
1454
1455 for (int i = 0; i < count; i++)
1456 {
1457 T *dest = target + (i * 4);
1458 const T *source = v + (i * components);
1459
1460 for (int c = 0; c < components; c++)
1461 {
1462 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1463 }
1464 for (int c = components; c < 4; c++)
1465 {
1466 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1467 }
1468 }
1469 }
1470 else if (targetUniform->type == targetBoolType)
1471 {
1472 GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1473
1474 for (int i = 0; i < count; i++)
1475 {
1476 GLint *dest = boolParams + (i * 4);
1477 const T *source = v + (i * components);
1478
1479 for (int c = 0; c < components; c++)
1480 {
1481 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty);
1482 }
1483 for (int c = components; c < 4; c++)
1484 {
1485 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1486 }
1487 }
1488 }
1489 else if (gl::IsSampler(targetUniform->type))
1490 {
1491 ASSERT(targetUniformType == GL_INT);
1492
1493 GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4;
1494
1495 bool wasDirty = targetUniform->dirty;
1496
1497 for (int i = 0; i < count; i++)
1498 {
1499 GLint *dest = target + (i * 4);
1500 const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components);
1501
1502 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1503 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1504 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1505 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1506 }
1507
1508 if (!wasDirty && targetUniform->dirty)
1509 {
1510 mDirtySamplerMapping = true;
1511 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001512 }
1513 else UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001514}
Brandon Jones18bd4102014-09-22 14:21:44 -07001515
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001516template<typename T>
1517bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1518{
1519 bool dirty = false;
1520 int copyWidth = std::min(targetHeight, srcWidth);
1521 int copyHeight = std::min(targetWidth, srcHeight);
1522
1523 for (int x = 0; x < copyWidth; x++)
1524 {
1525 for (int y = 0; y < copyHeight; y++)
1526 {
1527 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty);
1528 }
1529 }
1530 // clear unfilled right side
1531 for (int y = 0; y < copyWidth; y++)
1532 {
1533 for (int x = copyHeight; x < targetWidth; x++)
1534 {
1535 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1536 }
1537 }
1538 // clear unfilled bottom.
1539 for (int y = copyWidth; y < targetHeight; y++)
1540 {
1541 for (int x = 0; x < targetWidth; x++)
1542 {
1543 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1544 }
1545 }
1546
1547 return dirty;
1548}
1549
1550template<typename T>
1551bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
1552{
1553 bool dirty = false;
1554 int copyWidth = std::min(targetWidth, srcWidth);
1555 int copyHeight = std::min(targetHeight, srcHeight);
1556
1557 for (int y = 0; y < copyHeight; y++)
1558 {
1559 for (int x = 0; x < copyWidth; x++)
1560 {
1561 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty);
1562 }
1563 }
1564 // clear unfilled right side
1565 for (int y = 0; y < copyHeight; y++)
1566 {
1567 for (int x = copyWidth; x < targetWidth; x++)
1568 {
1569 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1570 }
1571 }
1572 // clear unfilled bottom.
1573 for (int y = copyHeight; y < targetHeight; y++)
1574 {
1575 for (int x = 0; x < targetWidth; x++)
1576 {
1577 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
1578 }
1579 }
1580
1581 return dirty;
1582}
1583
1584template <int cols, int rows>
1585void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
1586{
1587 gl::LinkedUniform *targetUniform = getUniformByLocation(location);
1588
1589 int elementCount = targetUniform->elementCount();
1590
1591 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
1592 const unsigned int targetMatrixStride = (4 * rows);
1593 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride);
1594
1595 for (int i = 0; i < count; i++)
1596 {
1597 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1598 if (transpose == GL_FALSE)
1599 {
1600 targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty;
1601 }
1602 else
1603 {
1604 targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
1605 }
1606 target += targetMatrixStride;
1607 value += cols * rows;
1608 }
1609}
1610
1611template <typename T>
1612void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType)
1613{
1614 gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index];
1615
1616 if (gl::IsMatrixType(targetUniform->type))
1617 {
1618 const int rows = gl::VariableRowCount(targetUniform->type);
1619 const int cols = gl::VariableColumnCount(targetUniform->type);
1620 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows);
1621 }
1622 else if (uniformType == gl::VariableComponentType(targetUniform->type))
1623 {
1624 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1625 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
1626 size * sizeof(T));
1627 }
1628 else
1629 {
1630 unsigned int size = gl::VariableComponentCount(targetUniform->type);
1631 switch (gl::VariableComponentType(targetUniform->type))
1632 {
1633 case GL_BOOL:
1634 {
1635 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1636
1637 for (unsigned int i = 0; i < size; i++)
1638 {
1639 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
1640 }
1641 }
1642 break;
1643
1644 case GL_FLOAT:
1645 {
1646 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
1647
1648 for (unsigned int i = 0; i < size; i++)
1649 {
1650 params[i] = static_cast<T>(floatParams[i]);
1651 }
1652 }
1653 break;
1654
1655 case GL_INT:
1656 {
1657 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
1658
1659 for (unsigned int i = 0; i < size; i++)
1660 {
1661 params[i] = static_cast<T>(intParams[i]);
1662 }
1663 }
1664 break;
1665
1666 case GL_UNSIGNED_INT:
1667 {
1668 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
1669
1670 for (unsigned int i = 0; i < size; i++)
1671 {
1672 params[i] = static_cast<T>(uintParams[i]);
1673 }
1674 }
1675 break;
1676
1677 default: UNREACHABLE();
1678 }
1679 }
1680}
1681
1682template <typename VarT>
1683void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
1684 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
1685 bool inRowMajorLayout)
1686{
1687 for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++)
1688 {
1689 const VarT &field = fields[uniformIndex];
1690 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
1691
1692 if (field.isStruct())
1693 {
1694 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
1695
1696 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
1697 {
1698 encoder->enterAggregateType();
1699
1700 const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
1701 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout);
1702
1703 encoder->exitAggregateType();
1704 }
1705 }
1706 else
1707 {
1708 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
1709
1710 sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
1711
1712 gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize,
1713 blockIndex, memberInfo);
1714
1715 // add to uniform list, but not index, since uniform block uniforms have no location
1716 blockUniformIndexes->push_back(mUniforms.size());
1717 mUniforms.push_back(newUniform);
1718 }
1719 }
1720}
1721
1722bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock,
1723 const gl::Caps &caps)
1724{
1725 const rx::ShaderD3D* shaderD3D = rx::ShaderD3D::makeShaderD3D(shader.getImplementation());
1726
1727 // create uniform block entries if they do not exist
1728 if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX)
1729 {
1730 std::vector<unsigned int> blockUniformIndexes;
1731 const unsigned int blockIndex = mUniformBlocks.size();
1732
1733 // define member uniforms
1734 sh::BlockLayoutEncoder *encoder = NULL;
1735
1736 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
1737 {
1738 encoder = new sh::Std140BlockEncoder;
1739 }
1740 else
1741 {
1742 encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
1743 }
1744 ASSERT(encoder);
1745
1746 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout);
1747
1748 size_t dataSize = encoder->getBlockSize();
1749
1750 // create all the uniform blocks
1751 if (interfaceBlock.arraySize > 0)
1752 {
1753 for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++)
1754 {
1755 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize);
1756 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1757 mUniformBlocks.push_back(newUniformBlock);
1758 }
1759 }
1760 else
1761 {
1762 gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize);
1763 newUniformBlock->memberUniformIndexes = blockUniformIndexes;
1764 mUniformBlocks.push_back(newUniformBlock);
1765 }
1766 }
1767
1768 if (interfaceBlock.staticUse)
1769 {
1770 // Assign registers to the uniform blocks
1771 const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name);
1772 const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize);
1773 ASSERT(blockIndex != GL_INVALID_INDEX);
1774 ASSERT(blockIndex + elementCount <= mUniformBlocks.size());
1775
1776 unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name);
1777
1778 for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++)
1779 {
1780 gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement];
1781 ASSERT(uniformBlock->name == interfaceBlock.name);
1782
1783 if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(),
1784 interfaceBlockRegister + uniformBlockElement, caps))
1785 {
1786 return false;
1787 }
1788 }
1789 }
1790
1791 return true;
1792}
1793
1794bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex,
1795 GLenum samplerType,
1796 unsigned int samplerCount,
1797 std::vector<Sampler> &outSamplers,
1798 GLuint *outUsedRange)
1799{
1800 unsigned int samplerIndex = startSamplerIndex;
1801
1802 do
1803 {
1804 if (samplerIndex < outSamplers.size())
1805 {
1806 Sampler& sampler = outSamplers[samplerIndex];
1807 sampler.active = true;
1808 sampler.textureType = GetTextureType(samplerType);
1809 sampler.logicalTextureUnit = 0;
1810 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
1811 }
1812 else
1813 {
1814 return false;
1815 }
1816
1817 samplerIndex++;
1818 } while (samplerIndex < startSamplerIndex + samplerCount);
1819
1820 return true;
1821}
1822
1823bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps)
1824{
1825 ASSERT(gl::IsSampler(uniform.type));
1826 ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX);
1827
1828 if (uniform.vsRegisterIndex != GL_INVALID_INDEX)
1829 {
1830 if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS,
1831 &mUsedVertexSamplerRange))
1832 {
1833 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).",
1834 mSamplersVS.size());
1835 return false;
1836 }
1837
1838 unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors;
1839 if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors)
1840 {
1841 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)",
1842 caps.maxVertexUniformVectors);
1843 return false;
1844 }
1845 }
1846
1847 if (uniform.psRegisterIndex != GL_INVALID_INDEX)
1848 {
1849 if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS,
1850 &mUsedPixelSamplerRange))
1851 {
1852 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).",
1853 mSamplersPS.size());
1854 return false;
1855 }
1856
1857 unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors;
1858 if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors)
1859 {
1860 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)",
1861 caps.maxFragmentUniformVectors);
1862 return false;
1863 }
1864 }
1865
1866 return true;
1867}
1868
1869bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
1870{
1871 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
1872 {
1873 const gl::LinkedUniform &uniform = *mUniforms[uniformIndex];
1874
1875 if (gl::IsSampler(uniform.type))
1876 {
1877 if (!indexSamplerUniform(uniform, infoLog, caps))
1878 {
1879 return false;
1880 }
1881 }
1882
1883 for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++)
1884 {
1885 mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex));
1886 }
1887 }
1888
1889 return true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001890}
1891
Brandon Jonesc9610c52014-08-25 17:02:59 -07001892void ProgramD3D::reset()
1893{
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001894 ProgramImpl::reset();
1895
Brandon Joneseb994362014-09-24 10:27:28 -07001896 SafeDeleteContainer(mVertexExecutables);
1897 SafeDeleteContainer(mPixelExecutables);
1898 SafeDelete(mGeometryExecutable);
1899
1900 mTransformFeedbackBufferMode = GL_NONE;
Brandon Joneseb994362014-09-24 10:27:28 -07001901
Brandon Jones22502d52014-08-29 16:58:36 -07001902 mVertexHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001903 mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones44151a92014-09-10 11:32:25 -07001904 mShaderVersion = 100;
Brandon Jones22502d52014-08-29 16:58:36 -07001905
1906 mPixelHLSL.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001907 mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE;
Brandon Jones22502d52014-08-29 16:58:36 -07001908 mUsesFragDepth = false;
1909 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07001910 mUsesPointSize = false;
Brandon Jones22502d52014-08-29 16:58:36 -07001911
Brandon Jonesc9610c52014-08-25 17:02:59 -07001912 SafeDelete(mVertexUniformStorage);
1913 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001914
1915 mSamplersPS.clear();
1916 mSamplersVS.clear();
1917
1918 mUsedVertexSamplerRange = 0;
1919 mUsedPixelSamplerRange = 0;
1920 mDirtySamplerMapping = true;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001921}
1922
1923}