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