blob: b8514ff47c65ae87a7041c93bf89c85e24d1f473 [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Dian Xianga4928832015-09-15 10:11:17 -070011#include "common/BitSetIterator.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Framebuffer.h"
14#include "libANGLE/FramebufferAttachment.h"
15#include "libANGLE/Program.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040016#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000017#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050018#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050019#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/renderer/d3d/RendererD3D.h"
21#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050022#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill65345da2015-11-13 11:25:23 -050023#include "libANGLE/renderer/d3d/VaryingPacking.h"
Jamie Madill437d2662014-12-05 14:23:35 -050024#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050025
Brandon Jonesc9610c52014-08-25 17:02:59 -070026namespace rx
27{
28
Brandon Joneseb994362014-09-24 10:27:28 -070029namespace
30{
31
Jamie Madillf8dd7b12015-08-05 13:50:08 -040032gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070033{
Jamie Madillbd136f92015-08-10 14:51:37 -040034 gl::InputLayout defaultLayout;
35 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070036 {
Brandon Joneseb994362014-09-24 10:27:28 -070037 if (shaderAttr.type != GL_NONE)
38 {
39 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
40
Jamie Madilld3dfda22015-07-06 08:28:49 -040041 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040042 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070043 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040044 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040045 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040046 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040047 gl::VertexFormatType defaultType =
48 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070049
Jamie Madillbd136f92015-08-10 14:51:37 -040050 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070051 }
52 }
53 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040054
55 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070056}
57
Jamie Madill334d6152015-10-22 14:00:28 -040058std::vector<GLenum> GetDefaultOutputLayoutFromShader(
59 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070060{
Jamie Madillb4463142014-12-19 14:56:54 -050061 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070062
Jamie Madillb4463142014-12-19 14:56:54 -050063 if (!shaderOutputVars.empty())
64 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070065 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
66 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050067 }
Brandon Joneseb994362014-09-24 10:27:28 -070068
69 return defaultPixelOutput;
70}
71
Brandon Jones1a8a7e32014-10-01 12:49:30 -070072bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
73{
74 return var.isRowMajorLayout;
75}
76
77bool IsRowMajorLayout(const sh::ShaderVariable &var)
78{
79 return false;
80}
81
Jamie Madill9fc36822015-11-18 13:08:07 -050082// true if varying x has a higher priority in packing than y
83bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
84{
Jamie Madill55c25d02015-11-18 13:08:08 -050085 return gl::CompareShaderVar(*x.varying, *y.varying);
Jamie Madill9fc36822015-11-18 13:08:07 -050086}
87
Jamie Madillca03b352015-09-02 12:38:13 -040088std::vector<PackedVarying> MergeVaryings(const gl::Shader &vertexShader,
89 const gl::Shader &fragmentShader,
90 const std::vector<std::string> &tfVaryings)
Jamie Madillada9ecc2015-08-17 12:53:37 -040091{
Jamie Madillca03b352015-09-02 12:38:13 -040092 std::vector<PackedVarying> packedVaryings;
93
94 for (const sh::Varying &output : vertexShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -040095 {
Jamie Madillca03b352015-09-02 12:38:13 -040096 bool packed = false;
Jamie Madillada9ecc2015-08-17 12:53:37 -040097
98 // Built-in varyings obey special rules
Jamie Madillca03b352015-09-02 12:38:13 -040099 if (output.isBuiltIn())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400100 {
101 continue;
102 }
103
Jamie Madillca03b352015-09-02 12:38:13 -0400104 for (const sh::Varying &input : fragmentShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400105 {
Jamie Madillca03b352015-09-02 12:38:13 -0400106 if (output.name == input.name)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400107 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500108 if (output.isStruct())
109 {
110 ASSERT(!output.isArray());
111 for (const auto &field : output.fields)
112 {
113 ASSERT(!field.isStruct() && !field.isArray());
114 packedVaryings.push_back(
115 PackedVarying(field, input.interpolation, input.name));
116 }
117 }
118 else
119 {
120 packedVaryings.push_back(PackedVarying(input, input.interpolation));
121 }
Jamie Madillca03b352015-09-02 12:38:13 -0400122 packed = true;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400123 break;
124 }
125 }
126
Jamie Madillca03b352015-09-02 12:38:13 -0400127 // Keep Transform FB varyings in the merged list always.
128 if (!packed)
129 {
130 for (const std::string &tfVarying : tfVaryings)
131 {
132 if (tfVarying == output.name)
133 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500134 // Transform feedback for varying structs is underspecified.
135 // See Khronos bug 9856.
136 // TODO(jmadill): Figure out how to be spec-compliant here.
137 if (!output.isStruct())
138 {
139 packedVaryings.push_back(PackedVarying(output, output.interpolation));
140 packedVaryings.back().vertexOnly = true;
141 }
Jamie Madillca03b352015-09-02 12:38:13 -0400142 break;
143 }
144 }
145 }
Jamie Madillada9ecc2015-08-17 12:53:37 -0400146 }
147
Jamie Madill9fc36822015-11-18 13:08:07 -0500148 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
149
Jamie Madillca03b352015-09-02 12:38:13 -0400150 return packedVaryings;
Brandon Joneseb994362014-09-24 10:27:28 -0700151}
152
Jamie Madill62d31cb2015-09-11 13:25:51 -0400153template <typename VarT>
154void GetUniformBlockInfo(const std::vector<VarT> &fields,
155 const std::string &prefix,
156 sh::BlockLayoutEncoder *encoder,
157 bool inRowMajorLayout,
158 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
159{
160 for (const VarT &field : fields)
161 {
162 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
163
164 if (field.isStruct())
165 {
166 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
167
168 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
169 {
170 encoder->enterAggregateType();
171
172 const std::string uniformElementName =
173 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
174 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
175 blockInfoOut);
176
177 encoder->exitAggregateType();
178 }
179 }
180 else
181 {
182 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
183 (*blockInfoOut)[fieldName] =
184 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
185 }
186 }
187}
188
Jamie Madill334d6152015-10-22 14:00:28 -0400189template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100190static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
191{
192 ASSERT(dest != NULL);
193 ASSERT(dirtyFlag != NULL);
194
195 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
196 *dest = source;
197}
198
199template <typename T>
Jamie Madill334d6152015-10-22 14:00:28 -0400200bool TransposeMatrix(T *target,
201 const GLfloat *value,
202 int targetWidth,
203 int targetHeight,
204 int srcWidth,
205 int srcHeight)
206{
207 bool dirty = false;
208 int copyWidth = std::min(targetHeight, srcWidth);
209 int copyHeight = std::min(targetWidth, srcHeight);
210
211 for (int x = 0; x < copyWidth; x++)
212 {
213 for (int y = 0; y < copyHeight; y++)
214 {
215 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]),
216 &dirty);
217 }
218 }
219 // clear unfilled right side
220 for (int y = 0; y < copyWidth; y++)
221 {
222 for (int x = copyHeight; x < targetWidth; x++)
223 {
224 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
225 }
226 }
227 // clear unfilled bottom.
228 for (int y = copyWidth; y < targetHeight; y++)
229 {
230 for (int x = 0; x < targetWidth; x++)
231 {
232 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
233 }
234 }
235
236 return dirty;
237}
238
239template <typename T>
240bool ExpandMatrix(T *target,
241 const GLfloat *value,
242 int targetWidth,
243 int targetHeight,
244 int srcWidth,
245 int srcHeight)
246{
247 bool dirty = false;
248 int copyWidth = std::min(targetWidth, srcWidth);
249 int copyHeight = std::min(targetHeight, srcHeight);
250
251 for (int y = 0; y < copyHeight; y++)
252 {
253 for (int x = 0; x < copyWidth; x++)
254 {
255 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]),
256 &dirty);
257 }
258 }
259 // clear unfilled right side
260 for (int y = 0; y < copyHeight; y++)
261 {
262 for (int x = copyWidth; x < targetWidth; x++)
263 {
264 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
265 }
266 }
267 // clear unfilled bottom.
268 for (int y = copyHeight; y < targetHeight; y++)
269 {
270 for (int x = 0; x < targetWidth; x++)
271 {
272 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
273 }
274 }
275
276 return dirty;
277}
278
Jamie Madill4e31ad52015-10-29 10:32:57 -0400279gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
280{
281 switch (drawMode)
282 {
283 // Uses the point sprite geometry shader.
284 case GL_POINTS:
285 return gl::PRIMITIVE_POINTS;
286
287 // All line drawing uses the same geometry shader.
288 case GL_LINES:
289 case GL_LINE_STRIP:
290 case GL_LINE_LOOP:
291 return gl::PRIMITIVE_LINES;
292
293 // The triangle fan primitive is emulated with strips in D3D11.
294 case GL_TRIANGLES:
295 case GL_TRIANGLE_FAN:
296 return gl::PRIMITIVE_TRIANGLES;
297
298 // Special case for triangle strips.
299 case GL_TRIANGLE_STRIP:
300 return gl::PRIMITIVE_TRIANGLE_STRIP;
301
302 default:
303 UNREACHABLE();
304 return gl::PRIMITIVE_TYPE_MAX;
305 }
306}
307
Jamie Madillada9ecc2015-08-17 12:53:37 -0400308} // anonymous namespace
309
Jamie Madill28afae52015-11-09 15:07:57 -0500310// D3DUniform Implementation
311
Jamie Madill62d31cb2015-09-11 13:25:51 -0400312D3DUniform::D3DUniform(GLenum typeIn,
313 const std::string &nameIn,
314 unsigned int arraySizeIn,
315 bool defaultBlock)
316 : type(typeIn),
317 name(nameIn),
318 arraySize(arraySizeIn),
319 data(nullptr),
320 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400321 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400322 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400323 registerCount(0),
324 registerElement(0)
325{
326 // We use data storage for default block uniforms to cache values that are sent to D3D during
327 // rendering
328 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
329 if (defaultBlock)
330 {
331 size_t bytes = gl::VariableInternalSize(type) * elementCount();
332 data = new uint8_t[bytes];
333 memset(data, 0, bytes);
334
335 // TODO(jmadill): is this correct with non-square matrices?
336 registerCount = gl::VariableRowCount(type) * elementCount();
337 }
338}
339
340D3DUniform::~D3DUniform()
341{
342 SafeDeleteArray(data);
343}
344
345bool D3DUniform::isSampler() const
346{
347 return gl::IsSamplerType(type);
348}
349
350bool D3DUniform::isReferencedByVertexShader() const
351{
352 return vsRegisterIndex != GL_INVALID_INDEX;
353}
354
355bool D3DUniform::isReferencedByFragmentShader() const
356{
357 return psRegisterIndex != GL_INVALID_INDEX;
358}
359
Jamie Madill28afae52015-11-09 15:07:57 -0500360// D3DVarying Implementation
361
Jamie Madill9fc36822015-11-18 13:08:07 -0500362D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500363{
364}
365
Jamie Madill9fc36822015-11-18 13:08:07 -0500366D3DVarying::D3DVarying(const std::string &semanticNameIn,
367 unsigned int semanticIndexIn,
368 unsigned int componentCountIn,
369 unsigned int outputSlotIn)
370 : semanticName(semanticNameIn),
371 semanticIndex(semanticIndexIn),
372 componentCount(componentCountIn),
373 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500374{
375}
376
Jamie Madille39a3f02015-11-17 20:42:15 -0500377// ProgramD3DMetadata Implementation
378
379ProgramD3DMetadata::ProgramD3DMetadata(int rendererMajorShaderModel,
380 const std::string &shaderModelSuffix,
381 bool usesInstancedPointSpriteEmulation,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800382 bool usesViewScale,
Jamie Madille39a3f02015-11-17 20:42:15 -0500383 const ShaderD3D *vertexShader,
384 const ShaderD3D *fragmentShader)
385 : mRendererMajorShaderModel(rendererMajorShaderModel),
386 mShaderModelSuffix(shaderModelSuffix),
387 mUsesInstancedPointSpriteEmulation(usesInstancedPointSpriteEmulation),
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800388 mUsesViewScale(usesViewScale),
Jamie Madille39a3f02015-11-17 20:42:15 -0500389 mVertexShader(vertexShader),
390 mFragmentShader(fragmentShader)
391{
392}
393
394int ProgramD3DMetadata::getRendererMajorShaderModel() const
395{
396 return mRendererMajorShaderModel;
397}
398
399bool ProgramD3DMetadata::usesBroadcast(const gl::Data &data) const
400{
401 return (mFragmentShader->usesFragColor() && data.clientVersion < 3);
402}
403
404bool ProgramD3DMetadata::usesFragDepth(const gl::Program::Data &programData) const
405{
Jamie Madill63286672015-11-24 13:00:08 -0500406 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500407}
408
409bool ProgramD3DMetadata::usesPointCoord() const
410{
411 return mFragmentShader->usesPointCoord();
412}
413
414bool ProgramD3DMetadata::usesFragCoord() const
415{
416 return mFragmentShader->usesFragCoord();
417}
418
419bool ProgramD3DMetadata::usesPointSize() const
420{
421 return mVertexShader->usesPointSize();
422}
423
424bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
425{
426 return !usesPointSize() && usesPointCoord() && mRendererMajorShaderModel >= 4;
427}
428
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800429bool ProgramD3DMetadata::usesViewScale() const
430{
431 return mUsesViewScale;
432}
433
Jamie Madille39a3f02015-11-17 20:42:15 -0500434bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
435{
436 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
437 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
438 // GeometryShader PointSprite emulation does not require this additional entry because the
439 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
440 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
441 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
442 // gl_PointSize in VS_OUTPUT.
443 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
444 usesInsertedPointCoordValue();
445}
446
447bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
448{
449 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
450 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
451 // the vertex shader in this case. This saves us 1 output vector.
452 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
453}
454
455bool ProgramD3DMetadata::usesSystemValuePointSize() const
456{
457 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
458}
459
460bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
461{
462 return mFragmentShader->usesMultipleRenderTargets();
463}
464
465GLint ProgramD3DMetadata::getMajorShaderVersion() const
466{
467 return mVertexShader->getData().getShaderVersion();
468}
469
470const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
471{
472 return mFragmentShader;
473}
474
Jamie Madill28afae52015-11-09 15:07:57 -0500475// ProgramD3D Implementation
476
Jamie Madilld3dfda22015-07-06 08:28:49 -0400477ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
478 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500479 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400480 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700481{
Brandon Joneseb994362014-09-24 10:27:28 -0700482}
483
484ProgramD3D::VertexExecutable::~VertexExecutable()
485{
486 SafeDelete(mShaderExecutable);
487}
488
Jamie Madilld3dfda22015-07-06 08:28:49 -0400489// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500490ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
491 GLenum type)
492{
493 switch (type)
494 {
495 case GL_INT:
496 return HLSLAttribType::SIGNED_INT;
497 case GL_UNSIGNED_INT:
498 return HLSLAttribType::UNSIGNED_INT;
499 case GL_SIGNED_NORMALIZED:
500 case GL_UNSIGNED_NORMALIZED:
501 case GL_FLOAT:
502 return HLSLAttribType::FLOAT;
503 default:
504 UNREACHABLE();
505 return HLSLAttribType::FLOAT;
506 }
507}
508
509// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400510void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
511 const gl::InputLayout &inputLayout,
512 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700513{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500514 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400515
516 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700517 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400518 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500519 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
520 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400521
Jamie Madillbdec2f42016-03-02 16:35:32 -0500522 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
523 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
524 continue;
525
526 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
527 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700528 }
Brandon Joneseb994362014-09-24 10:27:28 -0700529}
530
Jamie Madilld3dfda22015-07-06 08:28:49 -0400531bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
532{
Jamie Madillbd136f92015-08-10 14:51:37 -0400533 size_t limit = std::max(mSignature.size(), signature.size());
534 for (size_t index = 0; index < limit; ++index)
535 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500536 // treat undefined indexes as FLOAT
537 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
538 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400539 if (a != b)
540 return false;
541 }
542
543 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400544}
545
546ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
547 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400548 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700549{
550}
551
552ProgramD3D::PixelExecutable::~PixelExecutable()
553{
554 SafeDelete(mShaderExecutable);
555}
556
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700557ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
558{
559}
560
Geoff Lang7dd2e102014-11-10 15:19:26 -0500561unsigned int ProgramD3D::mCurrentSerial = 1;
562
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400563ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer)
564 : ProgramImpl(data),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700565 mRenderer(renderer),
566 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400567 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700568 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400569 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700570 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700571 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700572 mUsedVertexSamplerRange(0),
573 mUsedPixelSamplerRange(0),
574 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500575 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700576{
Brandon Joneseb994362014-09-24 10:27:28 -0700577 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700578}
579
580ProgramD3D::~ProgramD3D()
581{
582 reset();
583 SafeDelete(mDynamicHLSL);
584}
585
Brandon Jones44151a92014-09-10 11:32:25 -0700586bool ProgramD3D::usesPointSpriteEmulation() const
587{
588 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
589}
590
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400591bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700592{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400593 if (drawMode != GL_POINTS)
594 {
595 return mUsesFlatInterpolation;
596 }
597
Cooper Partine6664f02015-01-09 16:22:24 -0800598 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
599}
600
601bool ProgramD3D::usesInstancedPointSpriteEmulation() const
602{
603 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700604}
605
Jamie Madill334d6152015-10-22 14:00:28 -0400606GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
607 unsigned int samplerIndex,
608 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700609{
610 GLint logicalTextureUnit = -1;
611
612 switch (type)
613 {
Jamie Madill334d6152015-10-22 14:00:28 -0400614 case gl::SAMPLER_PIXEL:
615 ASSERT(samplerIndex < caps.maxTextureImageUnits);
616 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
617 {
618 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
619 }
620 break;
621 case gl::SAMPLER_VERTEX:
622 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
623 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
624 {
625 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
626 }
627 break;
628 default:
629 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700630 }
631
Jamie Madill334d6152015-10-22 14:00:28 -0400632 if (logicalTextureUnit >= 0 &&
633 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700634 {
635 return logicalTextureUnit;
636 }
637
638 return -1;
639}
640
641// Returns the texture type for a given Direct3D 9 sampler type and
642// index (0-15 for the pixel shader and 0-3 for the vertex shader).
643GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
644{
645 switch (type)
646 {
Jamie Madill334d6152015-10-22 14:00:28 -0400647 case gl::SAMPLER_PIXEL:
648 ASSERT(samplerIndex < mSamplersPS.size());
649 ASSERT(mSamplersPS[samplerIndex].active);
650 return mSamplersPS[samplerIndex].textureType;
651 case gl::SAMPLER_VERTEX:
652 ASSERT(samplerIndex < mSamplersVS.size());
653 ASSERT(mSamplersVS[samplerIndex].active);
654 return mSamplersVS[samplerIndex].textureType;
655 default:
656 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700657 }
658
659 return GL_TEXTURE_2D;
660}
661
Olli Etuaho618bebc2016-01-15 16:40:00 +0200662GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700663{
664 switch (type)
665 {
Jamie Madill334d6152015-10-22 14:00:28 -0400666 case gl::SAMPLER_PIXEL:
667 return mUsedPixelSamplerRange;
668 case gl::SAMPLER_VERTEX:
669 return mUsedVertexSamplerRange;
670 default:
671 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200672 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700673 }
674}
675
676void ProgramD3D::updateSamplerMapping()
677{
678 if (!mDirtySamplerMapping)
679 {
680 return;
681 }
682
683 mDirtySamplerMapping = false;
684
685 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400686 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700687 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400688 if (!d3dUniform->dirty)
689 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700690
Jamie Madill62d31cb2015-09-11 13:25:51 -0400691 if (!d3dUniform->isSampler())
692 continue;
693
694 int count = d3dUniform->elementCount();
695 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
696
697 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700698 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400699 unsigned int firstIndex = d3dUniform->psRegisterIndex;
700
701 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700702 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400703 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700704
Jamie Madill62d31cb2015-09-11 13:25:51 -0400705 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700706 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400707 ASSERT(mSamplersPS[samplerIndex].active);
708 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700709 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400710 }
711 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700712
Jamie Madill62d31cb2015-09-11 13:25:51 -0400713 if (d3dUniform->isReferencedByVertexShader())
714 {
715 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
716
717 for (int i = 0; i < count; i++)
718 {
719 unsigned int samplerIndex = firstIndex + i;
720
721 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700722 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400723 ASSERT(mSamplersVS[samplerIndex].active);
724 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700725 }
726 }
727 }
728 }
729}
730
Geoff Lang7dd2e102014-11-10 15:19:26 -0500731LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700732{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400733 reset();
734
Jamie Madill334d6152015-10-22 14:00:28 -0400735 DeviceIdentifier binaryDeviceIdentifier = {0};
736 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
737 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700738
739 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
740 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
741 {
742 infoLog << "Invalid program binary, device configuration has changed.";
743 return LinkResult(false, gl::Error(GL_NO_ERROR));
744 }
745
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500746 int compileFlags = stream->readInt<int>();
747 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
748 {
Jamie Madillf6113162015-05-07 11:49:21 -0400749 infoLog << "Mismatched compilation flags.";
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500750 return LinkResult(false, gl::Error(GL_NO_ERROR));
751 }
752
Jamie Madill8047c0d2016-03-07 13:02:12 -0500753 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400754 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500755 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400756 }
757
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700758 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
759 for (unsigned int i = 0; i < psSamplerCount; ++i)
760 {
761 Sampler sampler;
762 stream->readBool(&sampler.active);
763 stream->readInt(&sampler.logicalTextureUnit);
764 stream->readInt(&sampler.textureType);
765 mSamplersPS.push_back(sampler);
766 }
767 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
768 for (unsigned int i = 0; i < vsSamplerCount; ++i)
769 {
770 Sampler sampler;
771 stream->readBool(&sampler.active);
772 stream->readInt(&sampler.logicalTextureUnit);
773 stream->readInt(&sampler.textureType);
774 mSamplersVS.push_back(sampler);
775 }
776
777 stream->readInt(&mUsedVertexSamplerRange);
778 stream->readInt(&mUsedPixelSamplerRange);
779
780 const unsigned int uniformCount = stream->readInt<unsigned int>();
781 if (stream->error())
782 {
Jamie Madillf6113162015-05-07 11:49:21 -0400783 infoLog << "Invalid program binary.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500784 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700785 }
786
Jamie Madill62d31cb2015-09-11 13:25:51 -0400787 const auto &linkedUniforms = mData.getUniforms();
788 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700789 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
790 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400791 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700792
Jamie Madill62d31cb2015-09-11 13:25:51 -0400793 D3DUniform *d3dUniform =
794 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
795 linkedUniform.isInDefaultBlock());
796 stream->readInt(&d3dUniform->psRegisterIndex);
797 stream->readInt(&d3dUniform->vsRegisterIndex);
798 stream->readInt(&d3dUniform->registerCount);
799 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700800
Jamie Madill62d31cb2015-09-11 13:25:51 -0400801 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700802 }
803
Jamie Madill4a3c2342015-10-08 12:58:45 -0400804 const unsigned int blockCount = stream->readInt<unsigned int>();
805 if (stream->error())
806 {
807 infoLog << "Invalid program binary.";
808 return LinkResult(false, gl::Error(GL_NO_ERROR));
809 }
810
811 ASSERT(mD3DUniformBlocks.empty());
812 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
813 {
814 D3DUniformBlock uniformBlock;
815 stream->readInt(&uniformBlock.psRegisterIndex);
816 stream->readInt(&uniformBlock.vsRegisterIndex);
817 mD3DUniformBlocks.push_back(uniformBlock);
818 }
819
Jamie Madill9fc36822015-11-18 13:08:07 -0500820 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
821 mStreamOutVaryings.resize(streamOutVaryingCount);
822 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700823 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500824 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700825
Jamie Madill28afae52015-11-09 15:07:57 -0500826 stream->readString(&varying->semanticName);
827 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500828 stream->readInt(&varying->componentCount);
829 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700830 }
831
Brandon Jones22502d52014-08-29 16:58:36 -0700832 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400833 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
834 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700835 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400836 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
837 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700838 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700839 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400840 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700841
842 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
843 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400844 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
845 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700846 {
847 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
848 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
849 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
850 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
851 }
852
Jamie Madill4e31ad52015-10-29 10:32:57 -0400853 stream->readString(&mGeometryShaderPreamble);
854
Jamie Madill334d6152015-10-22 14:00:28 -0400855 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700856
857 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400858 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
859 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700860 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400861 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400862 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700863
Jamie Madilld3dfda22015-07-06 08:28:49 -0400864 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700865 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400866 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700867 }
868
Jamie Madill334d6152015-10-22 14:00:28 -0400869 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700870 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400871
Jamie Madillada9ecc2015-08-17 12:53:37 -0400872 ShaderExecutableD3D *shaderExecutable = nullptr;
873
874 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500875 vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400876 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400877 if (error.isError())
878 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500879 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400880 }
881
Brandon Joneseb994362014-09-24 10:27:28 -0700882 if (!shaderExecutable)
883 {
Jamie Madillf6113162015-05-07 11:49:21 -0400884 infoLog << "Could not create vertex shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500885 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700886 }
887
888 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400889 VertexExecutable::Signature signature;
890 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700891
892 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400893 mVertexExecutables.push_back(
894 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700895
896 stream->skip(vertexShaderSize);
897 }
898
899 const size_t pixelShaderCount = stream->readInt<unsigned int>();
900 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
901 {
902 const size_t outputCount = stream->readInt<unsigned int>();
903 std::vector<GLenum> outputs(outputCount);
904 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
905 {
906 stream->readInt(&outputs[outputIndex]);
907 }
908
Jamie Madill334d6152015-10-22 14:00:28 -0400909 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700910 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400911 ShaderExecutableD3D *shaderExecutable = nullptr;
912
913 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500914 pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400915 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -0400916 if (error.isError())
917 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500918 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400919 }
Brandon Joneseb994362014-09-24 10:27:28 -0700920
921 if (!shaderExecutable)
922 {
Jamie Madillf6113162015-05-07 11:49:21 -0400923 infoLog << "Could not create pixel shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500924 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700925 }
926
927 // add new binary
928 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
929
930 stream->skip(pixelShaderSize);
931 }
932
Jamie Madill4e31ad52015-10-29 10:32:57 -0400933 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
934 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700935 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400936 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
937 if (geometryShaderSize == 0)
938 {
939 mGeometryExecutables[geometryExeIndex] = nullptr;
940 continue;
941 }
942
Brandon Joneseb994362014-09-24 10:27:28 -0700943 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400944 bool splitAttribs = (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
945
Jamie Madill28afae52015-11-09 15:07:57 -0500946 gl::Error error = mRenderer->loadExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -0500947 geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, mStreamOutVaryings,
948 splitAttribs, &mGeometryExecutables[geometryExeIndex]);
Geoff Langb543aff2014-09-30 14:52:54 -0400949 if (error.isError())
950 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500951 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -0400952 }
Brandon Joneseb994362014-09-24 10:27:28 -0700953
Jamie Madill4e31ad52015-10-29 10:32:57 -0400954 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700955 {
Jamie Madillf6113162015-05-07 11:49:21 -0400956 infoLog << "Could not create geometry shader.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500957 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Joneseb994362014-09-24 10:27:28 -0700958 }
959 stream->skip(geometryShaderSize);
960 }
961
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700962 initializeUniformStorage();
963
Geoff Lang7dd2e102014-11-10 15:19:26 -0500964 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -0700965}
966
Geoff Langb543aff2014-09-30 14:52:54 -0400967gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700968{
Austin Kinross137b1512015-06-17 16:14:53 -0700969 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400970 // When we load the binary again later, we can validate the device identifier before trying to
971 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700972 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400973 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
974 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700975
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500976 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
977
Jamie Madill8047c0d2016-03-07 13:02:12 -0500978 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400979 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500980 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400981 }
982
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700983 stream->writeInt(mSamplersPS.size());
984 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
985 {
986 stream->writeInt(mSamplersPS[i].active);
987 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
988 stream->writeInt(mSamplersPS[i].textureType);
989 }
990
991 stream->writeInt(mSamplersVS.size());
992 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
993 {
994 stream->writeInt(mSamplersVS[i].active);
995 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
996 stream->writeInt(mSamplersVS[i].textureType);
997 }
998
999 stream->writeInt(mUsedVertexSamplerRange);
1000 stream->writeInt(mUsedPixelSamplerRange);
1001
Jamie Madill62d31cb2015-09-11 13:25:51 -04001002 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001003 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001004 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001005 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001006 stream->writeInt(uniform->psRegisterIndex);
1007 stream->writeInt(uniform->vsRegisterIndex);
1008 stream->writeInt(uniform->registerCount);
1009 stream->writeInt(uniform->registerElement);
1010 }
1011
1012 stream->writeInt(mD3DUniformBlocks.size());
1013 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1014 {
1015 stream->writeInt(uniformBlock.psRegisterIndex);
1016 stream->writeInt(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001017 }
1018
Jamie Madill9fc36822015-11-18 13:08:07 -05001019 stream->writeInt(mStreamOutVaryings.size());
1020 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001021 {
Brandon Joneseb994362014-09-24 10:27:28 -07001022 stream->writeString(varying.semanticName);
1023 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001024 stream->writeInt(varying.componentCount);
1025 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001026 }
1027
Brandon Jones22502d52014-08-29 16:58:36 -07001028 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001029 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1030 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001031 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001032 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1033 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001034 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001035 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001036 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001037
Brandon Joneseb994362014-09-24 10:27:28 -07001038 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001039 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001040 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1041 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001042 {
Brandon Joneseb994362014-09-24 10:27:28 -07001043 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001044 stream->writeInt(variable.type);
1045 stream->writeString(variable.name);
1046 stream->writeString(variable.source);
1047 stream->writeInt(variable.outputIndex);
1048 }
1049
Jamie Madill4e31ad52015-10-29 10:32:57 -04001050 stream->writeString(mGeometryShaderPreamble);
1051
Brandon Joneseb994362014-09-24 10:27:28 -07001052 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001053 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1054 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001055 {
1056 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1057
Jamie Madilld3dfda22015-07-06 08:28:49 -04001058 const auto &inputLayout = vertexExecutable->inputs();
1059 stream->writeInt(inputLayout.size());
1060
1061 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001062 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001063 stream->writeInt(inputLayout[inputIndex]);
Brandon Joneseb994362014-09-24 10:27:28 -07001064 }
1065
1066 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1067 stream->writeInt(vertexShaderSize);
1068
1069 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1070 stream->writeBytes(vertexBlob, vertexShaderSize);
1071 }
1072
1073 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001074 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1075 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001076 {
1077 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1078
1079 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1080 stream->writeInt(outputs.size());
1081 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1082 {
1083 stream->writeInt(outputs[outputIndex]);
1084 }
1085
1086 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1087 stream->writeInt(pixelShaderSize);
1088
1089 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1090 stream->writeBytes(pixelBlob, pixelShaderSize);
1091 }
1092
Jamie Madill4e31ad52015-10-29 10:32:57 -04001093 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001094 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001095 if (geometryExe == nullptr)
1096 {
1097 stream->writeInt(0);
1098 continue;
1099 }
1100
1101 size_t geometryShaderSize = geometryExe->getLength();
1102 stream->writeInt(geometryShaderSize);
1103 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001104 }
1105
Geoff Langb543aff2014-09-30 14:52:54 -04001106 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001107}
1108
Geoff Langc5629752015-12-07 16:29:04 -05001109void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1110{
1111}
1112
Jamie Madill334d6152015-10-22 14:00:28 -04001113gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1114 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001115{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001116 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001117
Jamie Madill85a18042015-03-05 15:41:41 -05001118 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001119 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001120
1121 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1122 {
1123 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1124
1125 if (colorbuffer)
1126 {
Jamie Madill334d6152015-10-22 14:00:28 -04001127 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1128 ? GL_COLOR_ATTACHMENT0
1129 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001130 }
1131 else
1132 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001133 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001134 }
1135 }
1136
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001137 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001138}
1139
Jamie Madill97399232014-12-23 12:31:15 -05001140gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001141 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001142 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001143{
1144 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1145 {
1146 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1147 {
Geoff Langb543aff2014-09-30 14:52:54 -04001148 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1149 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001150 }
1151 }
1152
Jamie Madill334d6152015-10-22 14:00:28 -04001153 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1154 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001155
1156 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001157 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001158
1159 gl::InfoLog tempInfoLog;
1160 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1161
Jamie Madillada9ecc2015-08-17 12:53:37 -04001162 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001163 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001164 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
1165 &pixelExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001166 if (error.isError())
1167 {
1168 return error;
1169 }
Brandon Joneseb994362014-09-24 10:27:28 -07001170
Jamie Madill97399232014-12-23 12:31:15 -05001171 if (pixelExecutable)
1172 {
1173 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1174 }
1175 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001176 {
1177 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001178 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001179 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1180 }
Brandon Jones22502d52014-08-29 16:58:36 -07001181
Geoff Langb543aff2014-09-30 14:52:54 -04001182 *outExectuable = pixelExecutable;
1183 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001184}
1185
Jamie Madilld3dfda22015-07-06 08:28:49 -04001186gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001187 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001188 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001189{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001190 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001191
1192 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1193 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001194 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001195 {
Geoff Langb543aff2014-09-30 14:52:54 -04001196 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1197 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001198 }
1199 }
1200
Brandon Jones22502d52014-08-29 16:58:36 -07001201 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001202 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
1203 mVertexHLSL, inputLayout, mData.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001204
1205 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001206 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001207
1208 gl::InfoLog tempInfoLog;
1209 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1210
Jamie Madillada9ecc2015-08-17 12:53:37 -04001211 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001212 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madillada9ecc2015-08-17 12:53:37 -04001213 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
1214 &vertexExecutable);
Geoff Langb543aff2014-09-30 14:52:54 -04001215 if (error.isError())
1216 {
1217 return error;
1218 }
1219
Jamie Madill97399232014-12-23 12:31:15 -05001220 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001221 {
Jamie Madill334d6152015-10-22 14:00:28 -04001222 mVertexExecutables.push_back(
1223 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001224 }
Jamie Madill97399232014-12-23 12:31:15 -05001225 else if (!infoLog)
1226 {
1227 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001228 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001229 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1230 }
Brandon Jones22502d52014-08-29 16:58:36 -07001231
Geoff Langb543aff2014-09-30 14:52:54 -04001232 *outExectuable = vertexExecutable;
1233 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001234}
1235
Jamie Madill4e31ad52015-10-29 10:32:57 -04001236gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Data &data,
1237 GLenum drawMode,
1238 ShaderExecutableD3D **outExecutable,
1239 gl::InfoLog *infoLog)
1240{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001241 if (outExecutable)
1242 {
1243 *outExecutable = nullptr;
1244 }
1245
Austin Kinross88829e82016-01-12 13:04:41 -08001246 // Return a null shader if the current rendering doesn't use a geometry shader
1247 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001248 {
1249 return gl::Error(GL_NO_ERROR);
1250 }
1251
Jamie Madill4e31ad52015-10-29 10:32:57 -04001252 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1253
1254 if (mGeometryExecutables[geometryShaderType] != nullptr)
1255 {
1256 if (outExecutable)
1257 {
1258 *outExecutable = mGeometryExecutables[geometryShaderType];
1259 }
1260 return gl::Error(GL_NO_ERROR);
1261 }
1262
1263 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001264 geometryShaderType, data, mData, mRenderer->presentPathFastEnabled(),
1265 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001266
1267 gl::InfoLog tempInfoLog;
1268 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1269
1270 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001271 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001272 (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
1273 &mGeometryExecutables[geometryShaderType]);
1274
1275 if (!infoLog && error.isError())
1276 {
1277 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1278 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1279 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1280 }
1281
1282 if (outExecutable)
1283 {
1284 *outExecutable = mGeometryExecutables[geometryShaderType];
1285 }
1286 return error;
1287}
1288
1289LinkResult ProgramD3D::compileProgramExecutables(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones44151a92014-09-10 11:32:25 -07001290{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001291 const gl::InputLayout &defaultInputLayout =
1292 GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader());
Jamie Madille4ea2022015-03-26 20:35:05 +00001293 ShaderExecutableD3D *defaultVertexExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001294 gl::Error error =
1295 getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog);
Jamie Madille4ea2022015-03-26 20:35:05 +00001296 if (error.isError())
Austin Kinross434953e2015-02-20 10:49:51 -08001297 {
Jamie Madille4ea2022015-03-26 20:35:05 +00001298 return LinkResult(false, error);
1299 }
Austin Kinross434953e2015-02-20 10:49:51 -08001300
Jamie Madill334d6152015-10-22 14:00:28 -04001301 std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey());
Geoff Lang359ef262015-01-05 14:42:29 -05001302 ShaderExecutableD3D *defaultPixelExecutable = NULL;
Jamie Madill334d6152015-10-22 14:00:28 -04001303 error =
1304 getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog);
Geoff Langb543aff2014-09-30 14:52:54 -04001305 if (error.isError())
1306 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001307 return LinkResult(false, error);
Geoff Langb543aff2014-09-30 14:52:54 -04001308 }
Brandon Jones44151a92014-09-10 11:32:25 -07001309
Jamie Madill4e31ad52015-10-29 10:32:57 -04001310 // Auto-generate the geometry shader here, if we expect to be using point rendering in D3D11.
Jamie Madill847638a2015-11-20 13:01:41 -05001311 ShaderExecutableD3D *pointGS = nullptr;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001312 if (usesGeometryShader(GL_POINTS))
Brandon Joneseb994362014-09-24 10:27:28 -07001313 {
Jamie Madill847638a2015-11-20 13:01:41 -05001314 getGeometryExecutableForPrimitiveType(data, GL_POINTS, &pointGS, &infoLog);
Brandon Joneseb994362014-09-24 10:27:28 -07001315 }
1316
Jamie Madillca03b352015-09-02 12:38:13 -04001317 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001318
1319 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001320 {
Jamie Madill334d6152015-10-22 14:00:28 -04001321 // Geometry shaders are currently only used internally, so there is no corresponding shader
1322 // object at the interface level. For now the geometry shader debug info is prepended to
1323 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001324 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001325 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001326 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1327 }
1328
1329 if (defaultVertexExecutable)
1330 {
1331 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1332 }
1333
1334 if (defaultPixelExecutable)
1335 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001336 const ShaderD3D *fragmentShaderD3D =
1337 GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001338 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1339 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001340
Jamie Madill847638a2015-11-20 13:01:41 -05001341 bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable &&
1342 (!usesGeometryShader(GL_POINTS) || pointGS));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343 return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR));
Brandon Jones18bd4102014-09-22 14:21:44 -07001344}
1345
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001346LinkResult ProgramD3D::link(const gl::Data &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001347{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001348 reset();
1349
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001350 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1351 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001352
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001353 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1354 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1355
Jamie Madill63069df2015-09-01 17:26:41 +00001356 mSamplersVS.resize(data.caps->maxVertexTextureImageUnits);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001357 mSamplersPS.resize(data.caps->maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001358
Arun Patole44efa0b2015-03-04 17:11:05 +05301359 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001360 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1361
Austin Kinross02df7962015-07-01 10:03:42 -07001362 if (mRenderer->getRendererLimitations().noFrontFacingSupport)
1363 {
1364 if (fragmentShaderD3D->usesFrontFacing())
1365 {
1366 infoLog << "The current renderer doesn't support gl_FrontFacing";
1367 return LinkResult(false, gl::Error(GL_NO_ERROR));
1368 }
1369 }
1370
Jamie Madillca03b352015-09-02 12:38:13 -04001371 std::vector<PackedVarying> packedVaryings =
1372 MergeVaryings(*vertexShader, *fragmentShader, mData.getTransformFeedbackVaryingNames());
1373
Brandon Jones22502d52014-08-29 16:58:36 -07001374 // Map the varyings to the register file
Jamie Madill9fc36822015-11-18 13:08:07 -05001375 VaryingPacking varyingPacking(data.caps->maxVaryingVectors);
1376 if (!varyingPacking.packVaryings(infoLog, packedVaryings,
1377 mData.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001378 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001380 }
1381
Jamie Madille39a3f02015-11-17 20:42:15 -05001382 ProgramD3DMetadata metadata(mRenderer->getMajorShaderModel(), mRenderer->getShaderModelSuffix(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001383 usesInstancedPointSpriteEmulation(),
1384 mRenderer->presentPathFastEnabled(), vertexShaderD3D,
Jamie Madille39a3f02015-11-17 20:42:15 -05001385 fragmentShaderD3D);
1386
Jamie Madill9fc36822015-11-18 13:08:07 -05001387 varyingPacking.enableBuiltins(SHADER_VERTEX, metadata);
1388 varyingPacking.enableBuiltins(SHADER_PIXEL, metadata);
1389
1390 if (static_cast<GLuint>(varyingPacking.getRegisterCount()) > data.caps->maxVaryingVectors)
1391 {
1392 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
1393 return LinkResult(false, gl::Error(GL_NO_ERROR));
1394 }
1395
1396 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1397 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1398 // intelligently, but D3D9 assumes one semantic per register.
1399 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1400 varyingPacking.getMaxSemanticIndex() > data.caps->maxVaryingVectors)
1401 {
1402 infoLog << "Cannot pack these varyings on D3D9.";
1403 return LinkResult(false, gl::Error(GL_NO_ERROR));
1404 }
1405
1406 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, metadata, varyingPacking, &mPixelHLSL,
1407 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001408 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409 return LinkResult(false, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001410 }
1411
Brandon Jones44151a92014-09-10 11:32:25 -07001412 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madille39a3f02015-11-17 20:42:15 -05001413 mDynamicHLSL->getPixelShaderOutputKey(data, mData, metadata, &mPixelShaderKey);
1414 mUsesFragDepth = metadata.usesFragDepth(mData);
Brandon Jones44151a92014-09-10 11:32:25 -07001415
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001416 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001417 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001418 for (const auto &varying : packedVaryings)
1419 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001420 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001421 {
1422 mUsesFlatInterpolation = true;
1423 break;
1424 }
1425 }
1426
Jamie Madill4e31ad52015-10-29 10:32:57 -04001427 if (mRenderer->getMajorShaderModel() >= 4)
1428 {
Jamie Madill9fc36822015-11-18 13:08:07 -05001429 varyingPacking.enableBuiltins(SHADER_GEOMETRY, metadata);
1430 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001431 }
1432
Jamie Madill8047c0d2016-03-07 13:02:12 -05001433 initAttribLocationsToD3DSemantic();
Jamie Madill437d2662014-12-05 14:23:35 -05001434
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001435 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001436
Jamie Madill9fc36822015-11-18 13:08:07 -05001437 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001438
Jamie Madill4e31ad52015-10-29 10:32:57 -04001439 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madill31c8c562015-08-19 14:08:03 -04001440 if (result.error.isError() || !result.linkSuccess)
1441 {
1442 infoLog << "Failed to create D3D shaders.";
1443 return result;
1444 }
1445
Jamie Madill4a3c2342015-10-08 12:58:45 -04001446 initUniformBlockInfo();
1447
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448 return LinkResult(true, gl::Error(GL_NO_ERROR));
Brandon Jones22502d52014-08-29 16:58:36 -07001449}
1450
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001451GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001452{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001453 // TODO(jmadill): Do something useful here?
1454 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001455}
1456
Jamie Madill4a3c2342015-10-08 12:58:45 -04001457void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001458{
1459 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
1460
Jamie Madill62d31cb2015-09-11 13:25:51 -04001461 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1462 {
1463 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1464 continue;
1465
Jamie Madill4a3c2342015-10-08 12:58:45 -04001466 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001467 continue;
1468
Jamie Madill4a3c2342015-10-08 12:58:45 -04001469 size_t dataSize = getUniformBlockInfo(vertexBlock);
1470 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001471 }
1472
1473 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
1474
1475 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1476 {
1477 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1478 continue;
1479
Jamie Madill4a3c2342015-10-08 12:58:45 -04001480 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001481 continue;
1482
Jamie Madill4a3c2342015-10-08 12:58:45 -04001483 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1484 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001485 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001486}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001487
Jamie Madill4a3c2342015-10-08 12:58:45 -04001488void ProgramD3D::assignUniformBlockRegisters()
1489{
1490 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001491
1492 // Assign registers and update sizes.
Jamie Madill4a3c2342015-10-08 12:58:45 -04001493 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
1494 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001495
Jamie Madill4a3c2342015-10-08 12:58:45 -04001496 for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001497 {
1498 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1499
Jamie Madill4a3c2342015-10-08 12:58:45 -04001500 D3DUniformBlock d3dUniformBlock;
1501
Jamie Madill62d31cb2015-09-11 13:25:51 -04001502 if (uniformBlock.vertexStaticUse)
1503 {
1504 unsigned int baseRegister =
1505 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001506 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001507 }
1508
1509 if (uniformBlock.fragmentStaticUse)
1510 {
1511 unsigned int baseRegister =
1512 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001513 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001514 }
1515
Jamie Madill4a3c2342015-10-08 12:58:45 -04001516 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001517 }
1518}
1519
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001520void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001521{
1522 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001523 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001524 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001525 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001526 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001527 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001528 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001529 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001530 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001531 vertexRegisters = std::max(vertexRegisters,
1532 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001533 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001534 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001535 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001536 fragmentRegisters = std::max(
1537 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001538 }
1539 }
1540 }
1541
Jamie Madill334d6152015-10-22 14:00:28 -04001542 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001543 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1544}
1545
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001546gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001547{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001548 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001549
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001550 gl::Error error = mRenderer->applyUniforms(*this, drawMode, mD3DUniforms);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001551 if (error.isError())
1552 {
1553 return error;
1554 }
1555
Jamie Madill62d31cb2015-09-11 13:25:51 -04001556 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001557 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001558 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001559 }
1560
1561 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001562}
1563
Jamie Madilld1fe1642015-08-21 16:26:04 -04001564gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001565{
Jamie Madill4a3c2342015-10-08 12:58:45 -04001566 if (mData.getUniformBlocks().empty())
1567 {
1568 return gl::Error(GL_NO_ERROR);
1569 }
1570
1571 // Lazy init.
1572 if (mD3DUniformBlocks.empty())
1573 {
1574 assignUniformBlockRegisters();
1575 }
1576
Jamie Madill03260fa2015-06-22 13:57:22 -04001577 mVertexUBOCache.clear();
1578 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001579
1580 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1581 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1582
Jamie Madill4a3c2342015-10-08 12:58:45 -04001583 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001584 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001585 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001586 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
1587 GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001588
Brandon Jones18bd4102014-09-22 14:21:44 -07001589 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001590 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001591 {
1592 continue;
1593 }
1594
Jamie Madill4a3c2342015-10-08 12:58:45 -04001595 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001596 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001597 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001598 ASSERT(registerIndex < data.caps->maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001599
Jamie Madill969194d2015-07-20 14:36:56 -04001600 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001601 {
1602 mVertexUBOCache.resize(registerIndex + 1, -1);
1603 }
1604
1605 ASSERT(mVertexUBOCache[registerIndex] == -1);
1606 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001607 }
1608
Jamie Madill4a3c2342015-10-08 12:58:45 -04001609 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001610 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001611 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001612 ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001613
1614 if (mFragmentUBOCache.size() <= registerIndex)
1615 {
1616 mFragmentUBOCache.resize(registerIndex + 1, -1);
1617 }
1618
1619 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1620 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001621 }
1622 }
1623
Jamie Madill03260fa2015-06-22 13:57:22 -04001624 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001625}
1626
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001627void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001628{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001629 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001630 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001631 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001632 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001633}
1634
Jamie Madill334d6152015-10-22 14:00:28 -04001635void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001636{
1637 setUniform(location, count, v, GL_FLOAT);
1638}
1639
1640void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1641{
1642 setUniform(location, count, v, GL_FLOAT_VEC2);
1643}
1644
1645void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1646{
1647 setUniform(location, count, v, GL_FLOAT_VEC3);
1648}
1649
1650void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1651{
1652 setUniform(location, count, v, GL_FLOAT_VEC4);
1653}
1654
Jamie Madill334d6152015-10-22 14:00:28 -04001655void ProgramD3D::setUniformMatrix2fv(GLint location,
1656 GLsizei count,
1657 GLboolean transpose,
1658 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001659{
1660 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1661}
1662
Jamie Madill334d6152015-10-22 14:00:28 -04001663void ProgramD3D::setUniformMatrix3fv(GLint location,
1664 GLsizei count,
1665 GLboolean transpose,
1666 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001667{
1668 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1669}
1670
Jamie Madill334d6152015-10-22 14:00:28 -04001671void ProgramD3D::setUniformMatrix4fv(GLint location,
1672 GLsizei count,
1673 GLboolean transpose,
1674 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001675{
1676 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1677}
1678
Jamie Madill334d6152015-10-22 14:00:28 -04001679void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1680 GLsizei count,
1681 GLboolean transpose,
1682 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001683{
1684 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1685}
1686
Jamie Madill334d6152015-10-22 14:00:28 -04001687void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1688 GLsizei count,
1689 GLboolean transpose,
1690 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001691{
1692 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1693}
1694
Jamie Madill334d6152015-10-22 14:00:28 -04001695void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1696 GLsizei count,
1697 GLboolean transpose,
1698 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001699{
1700 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1701}
1702
Jamie Madill334d6152015-10-22 14:00:28 -04001703void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1704 GLsizei count,
1705 GLboolean transpose,
1706 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001707{
1708 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1709}
1710
Jamie Madill334d6152015-10-22 14:00:28 -04001711void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1712 GLsizei count,
1713 GLboolean transpose,
1714 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001715{
1716 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1717}
1718
Jamie Madill334d6152015-10-22 14:00:28 -04001719void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1720 GLsizei count,
1721 GLboolean transpose,
1722 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001723{
1724 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1725}
1726
1727void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1728{
1729 setUniform(location, count, v, GL_INT);
1730}
1731
1732void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1733{
1734 setUniform(location, count, v, GL_INT_VEC2);
1735}
1736
1737void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1738{
1739 setUniform(location, count, v, GL_INT_VEC3);
1740}
1741
1742void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1743{
1744 setUniform(location, count, v, GL_INT_VEC4);
1745}
1746
1747void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1748{
1749 setUniform(location, count, v, GL_UNSIGNED_INT);
1750}
1751
1752void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1753{
1754 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1755}
1756
1757void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1758{
1759 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1760}
1761
1762void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1763{
1764 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1765}
1766
Jamie Madill4a3c2342015-10-08 12:58:45 -04001767void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1768 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001769{
1770}
1771
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001772void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001773{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001774 D3DUniformMap uniformMap;
Jamie Madill334d6152015-10-22 14:00:28 -04001775 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001776 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001777
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001778 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001779 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001780 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001781 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001782 }
1783 }
1784
Jamie Madill334d6152015-10-22 14:00:28 -04001785 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001786 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001787 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001788 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001789 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001790 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001791 }
1792 }
1793
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001794 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
1795 for (const gl::LinkedUniform &glUniform : mData.getUniforms())
1796 {
1797 if (!glUniform.isInDefaultBlock())
1798 continue;
1799
1800 auto mapEntry = uniformMap.find(glUniform.name);
1801 ASSERT(mapEntry != uniformMap.end());
1802 mD3DUniforms.push_back(mapEntry->second);
1803 }
1804
Jamie Madill62d31cb2015-09-11 13:25:51 -04001805 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001806 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001807}
1808
Jamie Madill91445bc2015-09-23 16:47:53 -04001809void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001810 const sh::Uniform &uniform,
1811 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001812{
Olli Etuaho96963162016-03-21 11:54:33 +02001813 // Samplers get their registers assigned in assignAllSamplerRegisters.
1814 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001815 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001816 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001817 return;
1818 }
1819
Jamie Madill91445bc2015-09-23 16:47:53 -04001820 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1821
1822 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1823 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001824 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001825 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001826
Jamie Madill91445bc2015-09-23 16:47:53 -04001827 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001828}
1829
Jamie Madill62d31cb2015-09-11 13:25:51 -04001830D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1831{
1832 for (D3DUniform *d3dUniform : mD3DUniforms)
1833 {
1834 if (d3dUniform->name == name)
1835 {
1836 return d3dUniform;
1837 }
1838 }
1839
1840 return nullptr;
1841}
1842
Jamie Madill91445bc2015-09-23 16:47:53 -04001843void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001844 const sh::ShaderVariable &uniform,
1845 const std::string &fullName,
1846 sh::HLSLBlockEncoder *encoder,
1847 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001848{
1849 if (uniform.isStruct())
1850 {
1851 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1852 {
1853 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1854
Jamie Madill55def582015-05-04 11:24:57 -04001855 if (encoder)
1856 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001857
1858 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1859 {
Jamie Madill334d6152015-10-22 14:00:28 -04001860 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001861 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1862
Olli Etuaho96963162016-03-21 11:54:33 +02001863 // Samplers get their registers assigned in assignAllSamplerRegisters.
1864 // Also they couldn't use the same encoder as the rest of the struct, since they are
1865 // extracted out of the struct by the shader translator.
1866 if (gl::IsSamplerType(field.type))
1867 {
1868 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
1869 }
1870 else
1871 {
1872 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
1873 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001874 }
1875
Jamie Madill55def582015-05-04 11:24:57 -04001876 if (encoder)
1877 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001878 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001879 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001880 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001881
1882 // Not a struct. Arrays are treated as aggregate types.
1883 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001884 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001885 encoder->enterAggregateType();
1886 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001887
Jamie Madill62d31cb2015-09-11 13:25:51 -04001888 // Advance the uniform offset, to track registers allocation for structs
1889 sh::BlockMemberInfo blockInfo =
1890 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1891 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001892
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001893 auto uniformMapEntry = uniformMap->find(fullName);
1894 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001895
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001896 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001897 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001898 d3dUniform = uniformMapEntry->second;
1899 }
1900 else
1901 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001902 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001903 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001904 }
1905
1906 if (encoder)
1907 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001908 d3dUniform->registerElement =
1909 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001910 unsigned int reg =
1911 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001912 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001913 {
1914 d3dUniform->psRegisterIndex = reg;
1915 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001916 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001917 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001918 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001919 d3dUniform->vsRegisterIndex = reg;
1920 }
Jamie Madillfb536032015-09-11 13:19:49 -04001921
1922 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001923 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001924 {
1925 encoder->exitAggregateType();
1926 }
1927 }
1928}
1929
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001930template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001931void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001932{
Jamie Madill334d6152015-10-22 14:00:28 -04001933 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001934 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1935
Jamie Madill62d31cb2015-09-11 13:25:51 -04001936 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001937
Jamie Madill62d31cb2015-09-11 13:25:51 -04001938 unsigned int elementCount = targetUniform->elementCount();
1939 unsigned int arrayElement = mData.getUniformLocations()[location].element;
1940 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001941
1942 if (targetUniform->type == targetUniformType)
1943 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001944 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945
Jamie Madill62d31cb2015-09-11 13:25:51 -04001946 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001947 {
Jamie Madill334d6152015-10-22 14:00:28 -04001948 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001949 const T *source = v + (i * components);
1950
1951 for (int c = 0; c < components; c++)
1952 {
1953 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1954 }
1955 for (int c = components; c < 4; c++)
1956 {
1957 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1958 }
1959 }
1960 }
1961 else if (targetUniform->type == targetBoolType)
1962 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001963 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964
Jamie Madill62d31cb2015-09-11 13:25:51 -04001965 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001966 {
Jamie Madill334d6152015-10-22 14:00:28 -04001967 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001968 const T *source = v + (i * components);
1969
1970 for (int c = 0; c < components; c++)
1971 {
Jamie Madill334d6152015-10-22 14:00:28 -04001972 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1973 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001974 }
1975 for (int c = components; c < 4; c++)
1976 {
1977 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1978 }
1979 }
1980 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001981 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001982 {
1983 ASSERT(targetUniformType == GL_INT);
1984
Jamie Madill62d31cb2015-09-11 13:25:51 -04001985 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986
1987 bool wasDirty = targetUniform->dirty;
1988
Jamie Madill62d31cb2015-09-11 13:25:51 -04001989 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990 {
Jamie Madill334d6152015-10-22 14:00:28 -04001991 GLint *dest = target + (i * 4);
1992 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001993
1994 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1995 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1996 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1997 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1998 }
1999
2000 if (!wasDirty && targetUniform->dirty)
2001 {
2002 mDirtySamplerMapping = true;
2003 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002004 }
Jamie Madill334d6152015-10-22 14:00:28 -04002005 else
2006 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002007}
2008
2009template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002010void ProgramD3D::setUniformMatrixfv(GLint location,
2011 GLsizei countIn,
2012 GLboolean transpose,
2013 const GLfloat *value,
2014 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002015{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002016 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017
Jamie Madill62d31cb2015-09-11 13:25:51 -04002018 unsigned int elementCount = targetUniform->elementCount();
2019 unsigned int arrayElement = mData.getUniformLocations()[location].element;
2020 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002021
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002022 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002023 GLfloat *target =
2024 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002025
Jamie Madill62d31cb2015-09-11 13:25:51 -04002026 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002027 {
2028 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2029 if (transpose == GL_FALSE)
2030 {
Jamie Madill334d6152015-10-22 14:00:28 -04002031 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2032 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002033 }
2034 else
2035 {
Jamie Madill334d6152015-10-22 14:00:28 -04002036 targetUniform->dirty =
2037 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002038 }
2039 target += targetMatrixStride;
2040 value += cols * rows;
2041 }
2042}
2043
Jamie Madill4a3c2342015-10-08 12:58:45 -04002044size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002045{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002046 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002047
Jamie Madill62d31cb2015-09-11 13:25:51 -04002048 // define member uniforms
2049 sh::Std140BlockEncoder std140Encoder;
2050 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2051 sh::BlockLayoutEncoder *encoder = nullptr;
2052
2053 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002054 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002055 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002056 }
2057 else
2058 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002059 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002060 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002061
Jamie Madill39046162016-02-08 15:05:17 -05002062 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2063 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002064
2065 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002066}
2067
Jamie Madill62d31cb2015-09-11 13:25:51 -04002068void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002069{
Olli Etuaho96963162016-03-21 11:54:33 +02002070 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002071 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002072 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002073 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002074 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002075 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002076 }
2077}
2078
Olli Etuaho96963162016-03-21 11:54:33 +02002079void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002080{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002081 ASSERT(d3dUniform->isSampler());
Olli Etuaho96963162016-03-21 11:54:33 +02002082 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader());
2083 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader());
2084 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) || fragmentShaderD3D->hasUniform(d3dUniform));
2085 if (vertexShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002086 {
Olli Etuaho96963162016-03-21 11:54:33 +02002087 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2088 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002089 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2090 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002091 }
Olli Etuaho96963162016-03-21 11:54:33 +02002092 if (fragmentShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002093 {
Olli Etuaho96963162016-03-21 11:54:33 +02002094 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2095 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002096 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2097 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002098 }
2099}
2100
Jamie Madill62d31cb2015-09-11 13:25:51 -04002101// static
2102void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002103 GLenum samplerType,
2104 unsigned int samplerCount,
2105 std::vector<Sampler> &outSamplers,
2106 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002107{
2108 unsigned int samplerIndex = startSamplerIndex;
2109
2110 do
2111 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002112 ASSERT(samplerIndex < outSamplers.size());
2113 Sampler *sampler = &outSamplers[samplerIndex];
2114 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002115 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002116 sampler->logicalTextureUnit = 0;
2117 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002118 samplerIndex++;
2119 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002120}
2121
Brandon Jonesc9610c52014-08-25 17:02:59 -07002122void ProgramD3D::reset()
2123{
Brandon Joneseb994362014-09-24 10:27:28 -07002124 SafeDeleteContainer(mVertexExecutables);
2125 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002126
2127 for (auto &element : mGeometryExecutables)
2128 {
2129 SafeDelete(element);
2130 }
Brandon Joneseb994362014-09-24 10:27:28 -07002131
Brandon Jones22502d52014-08-29 16:58:36 -07002132 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002133 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002134
2135 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002136 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002137 mUsesFragDepth = false;
2138 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002139 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002140 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002141
Jamie Madill62d31cb2015-09-11 13:25:51 -04002142 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002143 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002144
Brandon Jonesc9610c52014-08-25 17:02:59 -07002145 SafeDelete(mVertexUniformStorage);
2146 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002147
2148 mSamplersPS.clear();
2149 mSamplersVS.clear();
2150
2151 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002152 mUsedPixelSamplerRange = 0;
2153 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002154
Jamie Madill8047c0d2016-03-07 13:02:12 -05002155 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002156
Jamie Madill9fc36822015-11-18 13:08:07 -05002157 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002158
2159 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002160}
2161
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162unsigned int ProgramD3D::getSerial() const
2163{
2164 return mSerial;
2165}
2166
2167unsigned int ProgramD3D::issueSerial()
2168{
2169 return mCurrentSerial++;
2170}
2171
Jamie Madill8047c0d2016-03-07 13:02:12 -05002172void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002173{
2174 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2175 ASSERT(vertexShader != nullptr);
2176
2177 // Init semantic index
2178 for (const sh::Attribute &attribute : mData.getAttributes())
2179 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002180 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2181 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002182
Jamie Madill8047c0d2016-03-07 13:02:12 -05002183 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002184 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002185 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002186 }
2187 }
Jamie Madill437d2662014-12-05 14:23:35 -05002188}
2189
Jamie Madill63805b42015-08-25 13:17:39 -04002190void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002191{
Jamie Madillbd136f92015-08-10 14:51:37 -04002192 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002193 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002194
Jamie Madill8047c0d2016-03-07 13:02:12 -05002195 for (unsigned int locationIndex : angle::IterateBitSet(mData.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002196 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002197 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002198
Jamie Madill8047c0d2016-03-07 13:02:12 -05002199 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002200 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002201 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002202 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002203 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002204 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002205 mCachedInputLayout[d3dSemantic] =
2206 GetVertexFormatType(vertexAttributes[locationIndex],
2207 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002208 }
2209 }
2210}
2211
Jamie Madill9fc36822015-11-18 13:08:07 -05002212void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002213{
Jamie Madill9fc36822015-11-18 13:08:07 -05002214 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2215
2216 const std::string &varyingSemantic =
2217 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2218
Jamie Madillccdf74b2015-08-18 10:46:12 -04002219 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002220 mStreamOutVaryings.clear();
2221
2222 const auto &tfVaryingNames = mData.getTransformFeedbackVaryingNames();
2223 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2224 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002225 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002226 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2227 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002228 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002229 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002230 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002231 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2232 builtins.glPosition.index, 4, outputSlot));
2233 }
2234 }
2235 else if (tfVaryingName == "gl_FragCoord")
2236 {
2237 if (builtins.glFragCoord.enabled)
2238 {
2239 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2240 builtins.glFragCoord.index, 4, outputSlot));
2241 }
2242 }
2243 else if (tfVaryingName == "gl_PointSize")
2244 {
2245 if (builtins.glPointSize.enabled)
2246 {
2247 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2248 }
2249 }
2250 else
2251 {
2252 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2253 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002254 const auto &varying = *registerInfo.packedVarying->varying;
2255 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002256 int componentCount = gl::VariableColumnCount(transposedType);
2257 ASSERT(!varying.isBuiltIn());
2258
Jamie Madill55c25d02015-11-18 13:08:08 -05002259 // Transform feedback for varying structs is underspecified.
2260 // See Khronos bug 9856.
2261 // TODO(jmadill): Figure out how to be spec-compliant here.
2262 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2263 continue;
2264
Jamie Madill9fc36822015-11-18 13:08:07 -05002265 // There can be more than one register assigned to a particular varying, and each
2266 // register needs its own stream out entry.
2267 if (tfVaryingName == varying.name)
2268 {
2269 mStreamOutVaryings.push_back(D3DVarying(
2270 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2271 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002272 }
2273 }
2274 }
2275}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002276
2277D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2278{
2279 return mD3DUniforms[mData.getUniformLocations()[location].index];
2280}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002281
2282bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2283{
2284 std::string baseName = blockName;
2285 gl::ParseAndStripArrayIndex(&baseName);
2286
2287 auto sizeIter = mBlockDataSizes.find(baseName);
2288 if (sizeIter == mBlockDataSizes.end())
2289 {
2290 *sizeOut = 0;
2291 return false;
2292 }
2293
2294 *sizeOut = sizeIter->second;
2295 return true;
2296}
2297
2298bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2299 sh::BlockMemberInfo *memberInfoOut) const
2300{
2301 auto infoIter = mBlockInfo.find(memberUniformName);
2302 if (infoIter == mBlockInfo.end())
2303 {
2304 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2305 return false;
2306 }
2307
2308 *memberInfoOut = infoIter->second;
2309 return true;
2310}
Jamie Madill8047c0d2016-03-07 13:02:12 -05002311} // namespace rx