blob: 1a282980b63040c10ec51458f023e90a602645bd [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 Madill53ea9cc2016-05-17 10:12:52 -040016#include "libANGLE/Uniform.h"
Jamie Madill192745a2016-12-22 15:58:21 -050017#include "libANGLE/VaryingPacking.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040018#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000019#include "libANGLE/features.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050020#include "libANGLE/renderer/ContextImpl.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050021#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050022#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050023#include "libANGLE/renderer/d3d/RendererD3D.h"
24#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050025#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050026#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050027
Jamie Madill01074252016-11-28 15:55:51 -050028using namespace angle;
29
Brandon Jonesc9610c52014-08-25 17:02:59 -070030namespace rx
31{
32
Brandon Joneseb994362014-09-24 10:27:28 -070033namespace
34{
35
Jamie Madillf8dd7b12015-08-05 13:50:08 -040036gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070037{
Jamie Madillbd136f92015-08-10 14:51:37 -040038 gl::InputLayout defaultLayout;
39 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070040 {
Brandon Joneseb994362014-09-24 10:27:28 -070041 if (shaderAttr.type != GL_NONE)
42 {
43 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
44
Jamie Madilld3dfda22015-07-06 08:28:49 -040045 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040046 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070047 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040048 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040049 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040050 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040051 gl::VertexFormatType defaultType =
52 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070053
Jamie Madillbd136f92015-08-10 14:51:37 -040054 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070055 }
56 }
57 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040058
59 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070060}
61
Jamie Madill334d6152015-10-22 14:00:28 -040062std::vector<GLenum> GetDefaultOutputLayoutFromShader(
63 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070064{
Jamie Madillb4463142014-12-19 14:56:54 -050065 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070066
Jamie Madillb4463142014-12-19 14:56:54 -050067 if (!shaderOutputVars.empty())
68 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070069 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
70 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050071 }
Brandon Joneseb994362014-09-24 10:27:28 -070072
73 return defaultPixelOutput;
74}
75
Brandon Jones1a8a7e32014-10-01 12:49:30 -070076bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
77{
78 return var.isRowMajorLayout;
79}
80
81bool IsRowMajorLayout(const sh::ShaderVariable &var)
82{
83 return false;
84}
85
Jamie Madill62d31cb2015-09-11 13:25:51 -040086template <typename VarT>
87void GetUniformBlockInfo(const std::vector<VarT> &fields,
88 const std::string &prefix,
89 sh::BlockLayoutEncoder *encoder,
90 bool inRowMajorLayout,
91 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
92{
93 for (const VarT &field : fields)
94 {
95 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
96
97 if (field.isStruct())
98 {
99 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
100
101 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
102 {
103 encoder->enterAggregateType();
104
105 const std::string uniformElementName =
106 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
107 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
108 blockInfoOut);
109
110 encoder->exitAggregateType();
111 }
112 }
113 else
114 {
115 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
116 (*blockInfoOut)[fieldName] =
117 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
118 }
119 }
120}
121
Jamie Madill334d6152015-10-22 14:00:28 -0400122template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100123static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
124{
125 ASSERT(dest != NULL);
126 ASSERT(dirtyFlag != NULL);
127
128 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
129 *dest = source;
130}
131
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800132template <typename T, int cols, int rows>
133bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400134{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800135 constexpr int targetWidth = 4;
136 constexpr int targetHeight = rows;
137 constexpr int srcWidth = rows;
138 constexpr int srcHeight = cols;
139
140 constexpr int copyWidth = std::min(targetHeight, srcWidth);
141 constexpr int copyHeight = std::min(targetWidth, srcHeight);
142
143 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400144
145 for (int x = 0; x < copyWidth; x++)
146 {
147 for (int y = 0; y < copyHeight; y++)
148 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800149 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400150 }
151 }
152
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800153 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
154 {
155 return false;
156 }
157
158 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
159 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400160}
161
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800162template <typename T, int cols, int rows>
163bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400164{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800165 constexpr int targetWidth = 4;
166 constexpr int targetHeight = rows;
167 constexpr int srcWidth = cols;
168 constexpr int srcHeight = rows;
169
170 constexpr int copyWidth = std::min(targetWidth, srcWidth);
171 constexpr int copyHeight = std::min(targetHeight, srcHeight);
172
173 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400174
175 for (int y = 0; y < copyHeight; y++)
176 {
177 for (int x = 0; x < copyWidth; x++)
178 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800179 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400180 }
181 }
182
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800183 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
184 {
185 return false;
186 }
187
188 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
189 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400190}
191
Jamie Madill4e31ad52015-10-29 10:32:57 -0400192gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
193{
194 switch (drawMode)
195 {
196 // Uses the point sprite geometry shader.
197 case GL_POINTS:
198 return gl::PRIMITIVE_POINTS;
199
200 // All line drawing uses the same geometry shader.
201 case GL_LINES:
202 case GL_LINE_STRIP:
203 case GL_LINE_LOOP:
204 return gl::PRIMITIVE_LINES;
205
206 // The triangle fan primitive is emulated with strips in D3D11.
207 case GL_TRIANGLES:
208 case GL_TRIANGLE_FAN:
209 return gl::PRIMITIVE_TRIANGLES;
210
211 // Special case for triangle strips.
212 case GL_TRIANGLE_STRIP:
213 return gl::PRIMITIVE_TRIANGLE_STRIP;
214
215 default:
216 UNREACHABLE();
217 return gl::PRIMITIVE_TYPE_MAX;
218 }
219}
220
Jamie Madill192745a2016-12-22 15:58:21 -0500221bool FindFlatInterpolationVarying(const std::vector<sh::Varying> &varyings)
222{
223 // Note: this assumes nested structs can only be packed with one interpolation.
224 for (const auto &varying : varyings)
225 {
226 if (varying.interpolation == sh::INTERPOLATION_FLAT)
227 {
228 return true;
229 }
230 }
231
232 return false;
233}
234
Jamie Madillada9ecc2015-08-17 12:53:37 -0400235} // anonymous namespace
236
Jamie Madill28afae52015-11-09 15:07:57 -0500237// D3DUniform Implementation
238
Jamie Madill62d31cb2015-09-11 13:25:51 -0400239D3DUniform::D3DUniform(GLenum typeIn,
240 const std::string &nameIn,
241 unsigned int arraySizeIn,
242 bool defaultBlock)
243 : type(typeIn),
244 name(nameIn),
245 arraySize(arraySizeIn),
246 data(nullptr),
247 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400248 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400249 psRegisterIndex(GL_INVALID_INDEX),
Xinghua Caob1239382016-12-13 15:07:05 +0800250 csRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400251 registerCount(0),
252 registerElement(0)
253{
254 // We use data storage for default block uniforms to cache values that are sent to D3D during
255 // rendering
256 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
257 if (defaultBlock)
258 {
259 size_t bytes = gl::VariableInternalSize(type) * elementCount();
260 data = new uint8_t[bytes];
261 memset(data, 0, bytes);
262
Jamie Madillcc2ed612017-03-14 15:59:00 -0400263 // Use the row count as register count, will work for non-square matrices.
Jamie Madill62d31cb2015-09-11 13:25:51 -0400264 registerCount = gl::VariableRowCount(type) * elementCount();
265 }
266}
267
268D3DUniform::~D3DUniform()
269{
270 SafeDeleteArray(data);
271}
272
273bool D3DUniform::isSampler() const
274{
275 return gl::IsSamplerType(type);
276}
277
278bool D3DUniform::isReferencedByVertexShader() const
279{
280 return vsRegisterIndex != GL_INVALID_INDEX;
281}
282
283bool D3DUniform::isReferencedByFragmentShader() const
284{
285 return psRegisterIndex != GL_INVALID_INDEX;
286}
287
Xinghua Caob1239382016-12-13 15:07:05 +0800288bool D3DUniform::isReferencedByComputeShader() const
289{
290 return csRegisterIndex != GL_INVALID_INDEX;
291}
292
Jamie Madill28afae52015-11-09 15:07:57 -0500293// D3DVarying Implementation
294
Jamie Madill9fc36822015-11-18 13:08:07 -0500295D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500296{
297}
298
Jamie Madill9fc36822015-11-18 13:08:07 -0500299D3DVarying::D3DVarying(const std::string &semanticNameIn,
300 unsigned int semanticIndexIn,
301 unsigned int componentCountIn,
302 unsigned int outputSlotIn)
303 : semanticName(semanticNameIn),
304 semanticIndex(semanticIndexIn),
305 componentCount(componentCountIn),
306 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500307{
308}
309
Jamie Madille39a3f02015-11-17 20:42:15 -0500310// ProgramD3DMetadata Implementation
311
Jamie Madillc9bde922016-07-24 17:58:50 -0400312ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500313 const ShaderD3D *vertexShader,
314 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400315 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
316 mShaderModelSuffix(renderer->getShaderModelSuffix()),
317 mUsesInstancedPointSpriteEmulation(
318 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
319 mUsesViewScale(renderer->presentPathFastEnabled()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500320 mVertexShader(vertexShader),
321 mFragmentShader(fragmentShader)
322{
323}
324
325int ProgramD3DMetadata::getRendererMajorShaderModel() const
326{
327 return mRendererMajorShaderModel;
328}
329
Jamie Madill9082b982016-04-27 15:21:51 -0400330bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500331{
Martin Radev1be913c2016-07-11 17:59:16 +0300332 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500333}
334
Jamie Madill48ef11b2016-04-27 15:21:52 -0400335bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500336{
Jamie Madill63286672015-11-24 13:00:08 -0500337 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500338}
339
340bool ProgramD3DMetadata::usesPointCoord() const
341{
342 return mFragmentShader->usesPointCoord();
343}
344
345bool ProgramD3DMetadata::usesFragCoord() const
346{
347 return mFragmentShader->usesFragCoord();
348}
349
350bool ProgramD3DMetadata::usesPointSize() const
351{
352 return mVertexShader->usesPointSize();
353}
354
355bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
356{
Jamie Madillc9bde922016-07-24 17:58:50 -0400357 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
358 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500359}
360
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800361bool ProgramD3DMetadata::usesViewScale() const
362{
363 return mUsesViewScale;
364}
365
Jamie Madille39a3f02015-11-17 20:42:15 -0500366bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
367{
Jamie Madillc9bde922016-07-24 17:58:50 -0400368 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500369 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400370 // Even with a geometry shader, the app can render triangles or lines and reference
371 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
372 // simplicity, we always add this to the vertex shader when the fragment shader
373 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500374 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
375 usesInsertedPointCoordValue();
376}
377
378bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
379{
380 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
381 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
382 // the vertex shader in this case. This saves us 1 output vector.
383 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
384}
385
386bool ProgramD3DMetadata::usesSystemValuePointSize() const
387{
388 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
389}
390
391bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
392{
393 return mFragmentShader->usesMultipleRenderTargets();
394}
395
396GLint ProgramD3DMetadata::getMajorShaderVersion() const
397{
398 return mVertexShader->getData().getShaderVersion();
399}
400
401const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
402{
403 return mFragmentShader;
404}
405
Jamie Madill28afae52015-11-09 15:07:57 -0500406// ProgramD3D Implementation
407
Jamie Madilld3dfda22015-07-06 08:28:49 -0400408ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
409 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500410 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400411 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700412{
Brandon Joneseb994362014-09-24 10:27:28 -0700413}
414
415ProgramD3D::VertexExecutable::~VertexExecutable()
416{
417 SafeDelete(mShaderExecutable);
418}
419
Jamie Madilld3dfda22015-07-06 08:28:49 -0400420// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500421ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
422 GLenum type)
423{
424 switch (type)
425 {
426 case GL_INT:
427 return HLSLAttribType::SIGNED_INT;
428 case GL_UNSIGNED_INT:
429 return HLSLAttribType::UNSIGNED_INT;
430 case GL_SIGNED_NORMALIZED:
431 case GL_UNSIGNED_NORMALIZED:
432 case GL_FLOAT:
433 return HLSLAttribType::FLOAT;
434 default:
435 UNREACHABLE();
436 return HLSLAttribType::FLOAT;
437 }
438}
439
440// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400441void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
442 const gl::InputLayout &inputLayout,
443 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700444{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500445 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400446
447 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700448 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400449 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500450 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
451 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400452
Jamie Madillbdec2f42016-03-02 16:35:32 -0500453 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
454 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
455 continue;
456
457 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
458 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700459 }
Brandon Joneseb994362014-09-24 10:27:28 -0700460}
461
Jamie Madilld3dfda22015-07-06 08:28:49 -0400462bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
463{
Jamie Madillbd136f92015-08-10 14:51:37 -0400464 size_t limit = std::max(mSignature.size(), signature.size());
465 for (size_t index = 0; index < limit; ++index)
466 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500467 // treat undefined indexes as FLOAT
468 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
469 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400470 if (a != b)
471 return false;
472 }
473
474 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400475}
476
477ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
478 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400479 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700480{
481}
482
483ProgramD3D::PixelExecutable::~PixelExecutable()
484{
485 SafeDelete(mShaderExecutable);
486}
487
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700488ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
489{
490}
491
Geoff Lang7dd2e102014-11-10 15:19:26 -0500492unsigned int ProgramD3D::mCurrentSerial = 1;
493
Jamie Madill48ef11b2016-04-27 15:21:52 -0400494ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
495 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700496 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800497 mDynamicHLSL(nullptr),
498 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
499 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700500 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400501 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800502 mVertexUniformStorage(nullptr),
503 mFragmentUniformStorage(nullptr),
504 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700505 mUsedVertexSamplerRange(0),
506 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800507 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700508 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500509 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700510{
Brandon Joneseb994362014-09-24 10:27:28 -0700511 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700512}
513
514ProgramD3D::~ProgramD3D()
515{
516 reset();
517 SafeDelete(mDynamicHLSL);
518}
519
Brandon Jones44151a92014-09-10 11:32:25 -0700520bool ProgramD3D::usesPointSpriteEmulation() const
521{
522 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
523}
524
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400525bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700526{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400527 if (drawMode != GL_POINTS)
528 {
529 return mUsesFlatInterpolation;
530 }
531
Cooper Partine6664f02015-01-09 16:22:24 -0800532 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
533}
534
535bool ProgramD3D::usesInstancedPointSpriteEmulation() const
536{
537 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700538}
539
Jamie Madill334d6152015-10-22 14:00:28 -0400540GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
541 unsigned int samplerIndex,
542 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700543{
544 GLint logicalTextureUnit = -1;
545
546 switch (type)
547 {
Jamie Madill334d6152015-10-22 14:00:28 -0400548 case gl::SAMPLER_PIXEL:
549 ASSERT(samplerIndex < caps.maxTextureImageUnits);
550 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
551 {
552 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
553 }
554 break;
555 case gl::SAMPLER_VERTEX:
556 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
557 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
558 {
559 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
560 }
561 break;
Xinghua Caob1239382016-12-13 15:07:05 +0800562 case gl::SAMPLER_COMPUTE:
563 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
564 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
565 {
566 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
567 }
568 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400569 default:
570 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700571 }
572
Jamie Madill334d6152015-10-22 14:00:28 -0400573 if (logicalTextureUnit >= 0 &&
574 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700575 {
576 return logicalTextureUnit;
577 }
578
579 return -1;
580}
581
582// Returns the texture type for a given Direct3D 9 sampler type and
583// index (0-15 for the pixel shader and 0-3 for the vertex shader).
584GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
585{
586 switch (type)
587 {
Jamie Madill334d6152015-10-22 14:00:28 -0400588 case gl::SAMPLER_PIXEL:
589 ASSERT(samplerIndex < mSamplersPS.size());
590 ASSERT(mSamplersPS[samplerIndex].active);
591 return mSamplersPS[samplerIndex].textureType;
592 case gl::SAMPLER_VERTEX:
593 ASSERT(samplerIndex < mSamplersVS.size());
594 ASSERT(mSamplersVS[samplerIndex].active);
595 return mSamplersVS[samplerIndex].textureType;
Xinghua Caob1239382016-12-13 15:07:05 +0800596 case gl::SAMPLER_COMPUTE:
597 ASSERT(samplerIndex < mSamplersCS.size());
598 ASSERT(mSamplersCS[samplerIndex].active);
599 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400600 default:
601 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700602 }
603
604 return GL_TEXTURE_2D;
605}
606
Olli Etuaho618bebc2016-01-15 16:40:00 +0200607GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700608{
609 switch (type)
610 {
Jamie Madill334d6152015-10-22 14:00:28 -0400611 case gl::SAMPLER_PIXEL:
612 return mUsedPixelSamplerRange;
613 case gl::SAMPLER_VERTEX:
614 return mUsedVertexSamplerRange;
Xinghua Caob1239382016-12-13 15:07:05 +0800615 case gl::SAMPLER_COMPUTE:
616 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400617 default:
618 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200619 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700620 }
621}
622
623void ProgramD3D::updateSamplerMapping()
624{
625 if (!mDirtySamplerMapping)
626 {
627 return;
628 }
629
630 mDirtySamplerMapping = false;
631
632 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400633 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700634 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400635 if (!d3dUniform->dirty)
636 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700637
Jamie Madill62d31cb2015-09-11 13:25:51 -0400638 if (!d3dUniform->isSampler())
639 continue;
640
641 int count = d3dUniform->elementCount();
642 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
643
644 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700645 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400646 unsigned int firstIndex = d3dUniform->psRegisterIndex;
647
648 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700649 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400650 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700651
Jamie Madill62d31cb2015-09-11 13:25:51 -0400652 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700653 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400654 ASSERT(mSamplersPS[samplerIndex].active);
655 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700656 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400657 }
658 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700659
Jamie Madill62d31cb2015-09-11 13:25:51 -0400660 if (d3dUniform->isReferencedByVertexShader())
661 {
662 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
663
664 for (int i = 0; i < count; i++)
665 {
666 unsigned int samplerIndex = firstIndex + i;
667
668 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700669 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400670 ASSERT(mSamplersVS[samplerIndex].active);
671 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700672 }
673 }
674 }
Xinghua Caob1239382016-12-13 15:07:05 +0800675
676 if (d3dUniform->isReferencedByComputeShader())
677 {
678 unsigned int firstIndex = d3dUniform->csRegisterIndex;
679
680 for (int i = 0; i < count; i++)
681 {
682 unsigned int samplerIndex = firstIndex + i;
683
684 if (samplerIndex < mSamplersCS.size())
685 {
686 ASSERT(mSamplersCS[samplerIndex].active);
687 mSamplersCS[samplerIndex].logicalTextureUnit = v[i][0];
688 }
689 }
690 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700691 }
692}
693
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500694LinkResult ProgramD3D::load(const ContextImpl *contextImpl,
695 gl::InfoLog &infoLog,
696 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700697{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500698 // TODO(jmadill): Use Renderer from contextImpl.
699
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700 reset();
701
Jamie Madill334d6152015-10-22 14:00:28 -0400702 DeviceIdentifier binaryDeviceIdentifier = {0};
703 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
704 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700705
706 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
707 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
708 {
709 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500710 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700711 }
712
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500713 int compileFlags = stream->readInt<int>();
714 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
715 {
Jamie Madillf6113162015-05-07 11:49:21 -0400716 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500717 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500718 }
719
Jamie Madill8047c0d2016-03-07 13:02:12 -0500720 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400721 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500722 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400723 }
724
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700725 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
726 for (unsigned int i = 0; i < psSamplerCount; ++i)
727 {
728 Sampler sampler;
729 stream->readBool(&sampler.active);
730 stream->readInt(&sampler.logicalTextureUnit);
731 stream->readInt(&sampler.textureType);
732 mSamplersPS.push_back(sampler);
733 }
734 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
735 for (unsigned int i = 0; i < vsSamplerCount; ++i)
736 {
737 Sampler sampler;
738 stream->readBool(&sampler.active);
739 stream->readInt(&sampler.logicalTextureUnit);
740 stream->readInt(&sampler.textureType);
741 mSamplersVS.push_back(sampler);
742 }
743
Xinghua Caob1239382016-12-13 15:07:05 +0800744 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
745 for (unsigned int i = 0; i < csSamplerCount; ++i)
746 {
747 Sampler sampler;
748 stream->readBool(&sampler.active);
749 stream->readInt(&sampler.logicalTextureUnit);
750 stream->readInt(&sampler.textureType);
751 mSamplersCS.push_back(sampler);
752 }
753
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700754 stream->readInt(&mUsedVertexSamplerRange);
755 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800756 stream->readInt(&mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700757
758 const unsigned int uniformCount = stream->readInt<unsigned int>();
759 if (stream->error())
760 {
Jamie Madillf6113162015-05-07 11:49:21 -0400761 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500762 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700763 }
764
Jamie Madill48ef11b2016-04-27 15:21:52 -0400765 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400766 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700767 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
768 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400769 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700770
Jamie Madill62d31cb2015-09-11 13:25:51 -0400771 D3DUniform *d3dUniform =
772 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
773 linkedUniform.isInDefaultBlock());
774 stream->readInt(&d3dUniform->psRegisterIndex);
775 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800776 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400777 stream->readInt(&d3dUniform->registerCount);
778 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700779
Jamie Madill62d31cb2015-09-11 13:25:51 -0400780 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700781 }
782
Jamie Madill4a3c2342015-10-08 12:58:45 -0400783 const unsigned int blockCount = stream->readInt<unsigned int>();
784 if (stream->error())
785 {
786 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500787 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400788 }
789
790 ASSERT(mD3DUniformBlocks.empty());
791 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
792 {
793 D3DUniformBlock uniformBlock;
794 stream->readInt(&uniformBlock.psRegisterIndex);
795 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800796 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400797 mD3DUniformBlocks.push_back(uniformBlock);
798 }
799
Jamie Madill9fc36822015-11-18 13:08:07 -0500800 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
801 mStreamOutVaryings.resize(streamOutVaryingCount);
802 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700803 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500804 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700805
Jamie Madill28afae52015-11-09 15:07:57 -0500806 stream->readString(&varying->semanticName);
807 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500808 stream->readInt(&varying->componentCount);
809 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700810 }
811
Brandon Jones22502d52014-08-29 16:58:36 -0700812 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400813 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500814 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700815 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400816 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500817 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700818 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700819 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400820 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700821
822 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
823 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400824 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
825 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700826 {
827 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
828 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
829 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
830 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
831 }
832
Jamie Madill4e31ad52015-10-29 10:32:57 -0400833 stream->readString(&mGeometryShaderPreamble);
834
Jamie Madill334d6152015-10-22 14:00:28 -0400835 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700836
Jamie Madillb0a838b2016-11-13 20:02:12 -0500837 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
838
Brandon Joneseb994362014-09-24 10:27:28 -0700839 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400840 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
841 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700842 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400843 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400844 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700845
Jamie Madilld3dfda22015-07-06 08:28:49 -0400846 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700847 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400848 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700849 }
850
Jamie Madill334d6152015-10-22 14:00:28 -0400851 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700852 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400853
Jamie Madillada9ecc2015-08-17 12:53:37 -0400854 ShaderExecutableD3D *shaderExecutable = nullptr;
855
Jamie Madillb0a838b2016-11-13 20:02:12 -0500856 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
857 mStreamOutVaryings, separateAttribs,
858 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400859
Brandon Joneseb994362014-09-24 10:27:28 -0700860 if (!shaderExecutable)
861 {
Jamie Madillf6113162015-05-07 11:49:21 -0400862 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500863 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700864 }
865
866 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400867 VertexExecutable::Signature signature;
868 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700869
870 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800871 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
872 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700873
874 stream->skip(vertexShaderSize);
875 }
876
877 const size_t pixelShaderCount = stream->readInt<unsigned int>();
878 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
879 {
880 const size_t outputCount = stream->readInt<unsigned int>();
881 std::vector<GLenum> outputs(outputCount);
882 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
883 {
884 stream->readInt(&outputs[outputIndex]);
885 }
886
Jamie Madill334d6152015-10-22 14:00:28 -0400887 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700888 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400889 ShaderExecutableD3D *shaderExecutable = nullptr;
890
Jamie Madillb0a838b2016-11-13 20:02:12 -0500891 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
892 mStreamOutVaryings, separateAttribs,
893 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700894
895 if (!shaderExecutable)
896 {
Jamie Madillf6113162015-05-07 11:49:21 -0400897 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500898 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700899 }
900
901 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800902 mPixelExecutables.push_back(
903 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700904
905 stream->skip(pixelShaderSize);
906 }
907
Jamie Madill4e31ad52015-10-29 10:32:57 -0400908 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
909 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700910 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400911 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
912 if (geometryShaderSize == 0)
913 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400914 continue;
915 }
916
Brandon Joneseb994362014-09-24 10:27:28 -0700917 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400918
Xinghua Caob1239382016-12-13 15:07:05 +0800919 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -0500920 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
921 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
Xinghua Caob1239382016-12-13 15:07:05 +0800922 &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700923
Xinghua Caob1239382016-12-13 15:07:05 +0800924 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700925 {
Jamie Madillf6113162015-05-07 11:49:21 -0400926 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500927 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700928 }
Xinghua Caob1239382016-12-13 15:07:05 +0800929
930 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
931
Brandon Joneseb994362014-09-24 10:27:28 -0700932 stream->skip(geometryShaderSize);
933 }
934
Xinghua Caob1239382016-12-13 15:07:05 +0800935 unsigned int computeShaderSize = stream->readInt<unsigned int>();
936 if (computeShaderSize > 0)
937 {
938 const unsigned char *computeShaderFunction = binary + stream->offset();
939
940 ShaderExecutableD3D *computeExecutable = nullptr;
941 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
942 SHADER_COMPUTE, std::vector<D3DVarying>(), false,
943 &computeExecutable));
944
945 if (!computeExecutable)
946 {
947 infoLog << "Could not create compute shader.";
948 return false;
949 }
950
951 mComputeExecutable.reset(computeExecutable);
952 }
953
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700954 initializeUniformStorage();
955
Jamie Madillb0a838b2016-11-13 20:02:12 -0500956 return true;
Brandon Jones22502d52014-08-29 16:58:36 -0700957}
958
Geoff Langb543aff2014-09-30 14:52:54 -0400959gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700960{
Austin Kinross137b1512015-06-17 16:14:53 -0700961 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400962 // When we load the binary again later, we can validate the device identifier before trying to
963 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700964 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400965 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
966 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700967
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500968 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
969
Jamie Madill8047c0d2016-03-07 13:02:12 -0500970 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400971 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500972 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400973 }
974
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700975 stream->writeInt(mSamplersPS.size());
976 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
977 {
978 stream->writeInt(mSamplersPS[i].active);
979 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
980 stream->writeInt(mSamplersPS[i].textureType);
981 }
982
983 stream->writeInt(mSamplersVS.size());
984 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
985 {
986 stream->writeInt(mSamplersVS[i].active);
987 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
988 stream->writeInt(mSamplersVS[i].textureType);
989 }
990
Xinghua Caob1239382016-12-13 15:07:05 +0800991 stream->writeInt(mSamplersCS.size());
992 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
993 {
994 stream->writeInt(mSamplersCS[i].active);
995 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
996 stream->writeInt(mSamplersCS[i].textureType);
997 }
998
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700999 stream->writeInt(mUsedVertexSamplerRange);
1000 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001001 stream->writeInt(mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001002
Jamie Madill62d31cb2015-09-11 13:25:51 -04001003 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001004 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001005 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001006 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001007 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1008 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001009 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001010 stream->writeInt(uniform->registerCount);
1011 stream->writeInt(uniform->registerElement);
1012 }
1013
Jamie Madill51f522f2016-12-21 15:10:55 -05001014 // Ensure we init the uniform block structure data if we should.
1015 // http://anglebug.com/1637
1016 ensureUniformBlocksInitialized();
1017
Jamie Madill4a3c2342015-10-08 12:58:45 -04001018 stream->writeInt(mD3DUniformBlocks.size());
1019 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1020 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001021 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1022 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001023 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001024 }
1025
Jamie Madill9fc36822015-11-18 13:08:07 -05001026 stream->writeInt(mStreamOutVaryings.size());
1027 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001028 {
Brandon Joneseb994362014-09-24 10:27:28 -07001029 stream->writeString(varying.semanticName);
1030 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001031 stream->writeInt(varying.componentCount);
1032 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001033 }
1034
Brandon Jones22502d52014-08-29 16:58:36 -07001035 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001036 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001037 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001038 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001039 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001040 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001041 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001042 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001043 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001044
Brandon Joneseb994362014-09-24 10:27:28 -07001045 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001046 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001047 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1048 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001049 {
Brandon Joneseb994362014-09-24 10:27:28 -07001050 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001051 stream->writeInt(variable.type);
1052 stream->writeString(variable.name);
1053 stream->writeString(variable.source);
1054 stream->writeInt(variable.outputIndex);
1055 }
1056
Jamie Madill4e31ad52015-10-29 10:32:57 -04001057 stream->writeString(mGeometryShaderPreamble);
1058
Brandon Joneseb994362014-09-24 10:27:28 -07001059 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001060 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1061 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001062 {
Xinghua Caob1239382016-12-13 15:07:05 +08001063 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001064
Jamie Madilld3dfda22015-07-06 08:28:49 -04001065 const auto &inputLayout = vertexExecutable->inputs();
1066 stream->writeInt(inputLayout.size());
1067
1068 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001069 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001070 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001071 }
1072
1073 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1074 stream->writeInt(vertexShaderSize);
1075
1076 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1077 stream->writeBytes(vertexBlob, vertexShaderSize);
1078 }
1079
1080 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001081 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1082 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001083 {
Xinghua Caob1239382016-12-13 15:07:05 +08001084 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001085
1086 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1087 stream->writeInt(outputs.size());
1088 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1089 {
1090 stream->writeInt(outputs[outputIndex]);
1091 }
1092
1093 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1094 stream->writeInt(pixelShaderSize);
1095
1096 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1097 stream->writeBytes(pixelBlob, pixelShaderSize);
1098 }
1099
Xinghua Caob1239382016-12-13 15:07:05 +08001100 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001101 {
Xinghua Caob1239382016-12-13 15:07:05 +08001102 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001103 {
1104 stream->writeInt(0);
1105 continue;
1106 }
1107
Xinghua Caob1239382016-12-13 15:07:05 +08001108 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001109 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001110 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1111 }
1112
1113 if (mComputeExecutable)
1114 {
1115 size_t computeShaderSize = mComputeExecutable->getLength();
1116 stream->writeInt(computeShaderSize);
1117 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1118 }
1119 else
1120 {
1121 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001122 }
1123
Jamie Madill51f522f2016-12-21 15:10:55 -05001124 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001125}
1126
Geoff Langc5629752015-12-07 16:29:04 -05001127void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1128{
1129}
1130
Jamie Madill334d6152015-10-22 14:00:28 -04001131gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1132 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001133{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001134 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001135
Jamie Madill85a18042015-03-05 15:41:41 -05001136 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001137 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001138
1139 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1140 {
1141 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1142
1143 if (colorbuffer)
1144 {
Jamie Madill334d6152015-10-22 14:00:28 -04001145 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1146 ? GL_COLOR_ATTACHMENT0
1147 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001148 }
1149 else
1150 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001151 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001152 }
1153 }
1154
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001155 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001156}
1157
Jamie Madill97399232014-12-23 12:31:15 -05001158gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001159 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001160 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001161{
1162 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1163 {
1164 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1165 {
Geoff Langb543aff2014-09-30 14:52:54 -04001166 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001167 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001168 }
1169 }
1170
Jamie Madill334d6152015-10-22 14:00:28 -04001171 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1172 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001173
1174 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001175 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001176
1177 gl::InfoLog tempInfoLog;
1178 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1179
Jamie Madill01074252016-11-28 15:55:51 -05001180 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001181 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001182 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001183 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001184
Jamie Madill97399232014-12-23 12:31:15 -05001185 if (pixelExecutable)
1186 {
Xinghua Caob1239382016-12-13 15:07:05 +08001187 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
1188 new PixelExecutable(outputSignature, pixelExecutable)));
Jamie Madill97399232014-12-23 12:31:15 -05001189 }
1190 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001191 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001192 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1193 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001194 }
Brandon Jones22502d52014-08-29 16:58:36 -07001195
Geoff Langb543aff2014-09-30 14:52:54 -04001196 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001197 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001198}
1199
Jamie Madilld3dfda22015-07-06 08:28:49 -04001200gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001201 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001202 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001203{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001204 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001205
1206 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1207 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001208 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001209 {
Geoff Langb543aff2014-09-30 14:52:54 -04001210 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001211 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001212 }
1213 }
1214
Brandon Jones22502d52014-08-29 16:58:36 -07001215 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001216 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001217 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001218
1219 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001220 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001221
1222 gl::InfoLog tempInfoLog;
1223 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1224
Jamie Madill01074252016-11-28 15:55:51 -05001225 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001226 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001227 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001228 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001229
Jamie Madill97399232014-12-23 12:31:15 -05001230 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001231 {
Xinghua Caob1239382016-12-13 15:07:05 +08001232 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
1233 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001234 }
Jamie Madill97399232014-12-23 12:31:15 -05001235 else if (!infoLog)
1236 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001237 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1238 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001239 }
Brandon Jones22502d52014-08-29 16:58:36 -07001240
Geoff Langb543aff2014-09-30 14:52:54 -04001241 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001242 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001243}
1244
Jamie Madill9082b982016-04-27 15:21:51 -04001245gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001246 GLenum drawMode,
1247 ShaderExecutableD3D **outExecutable,
1248 gl::InfoLog *infoLog)
1249{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001250 if (outExecutable)
1251 {
1252 *outExecutable = nullptr;
1253 }
1254
Austin Kinross88829e82016-01-12 13:04:41 -08001255 // Return a null shader if the current rendering doesn't use a geometry shader
1256 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001257 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001258 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001259 }
1260
Jamie Madill4e31ad52015-10-29 10:32:57 -04001261 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1262
Xinghua Caob1239382016-12-13 15:07:05 +08001263 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001264 {
1265 if (outExecutable)
1266 {
Xinghua Caob1239382016-12-13 15:07:05 +08001267 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001268 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001269 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001270 }
1271
1272 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001273 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001274 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001275
1276 gl::InfoLog tempInfoLog;
1277 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1278
Xinghua Caob1239382016-12-13 15:07:05 +08001279 ShaderExecutableD3D *geometryExecutable = nullptr;
1280 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001281 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001282 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001283 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001284
1285 if (!infoLog && error.isError())
1286 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001287 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1288 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001289 }
1290
Xinghua Caob1239382016-12-13 15:07:05 +08001291 if (geometryExecutable != nullptr)
1292 {
1293 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1294 }
1295
Jamie Madill4e31ad52015-10-29 10:32:57 -04001296 if (outExecutable)
1297 {
Xinghua Caob1239382016-12-13 15:07:05 +08001298 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001299 }
1300 return error;
1301}
1302
Jamie Madill01074252016-11-28 15:55:51 -05001303class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001304{
Jamie Madill01074252016-11-28 15:55:51 -05001305 public:
1306 GetExecutableTask(ProgramD3D *program)
1307 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001308 {
Brandon Joneseb994362014-09-24 10:27:28 -07001309 }
1310
Jamie Madill01074252016-11-28 15:55:51 -05001311 virtual gl::Error run() = 0;
1312
1313 void operator()() override { mError = run(); }
1314
1315 const gl::Error &getError() const { return mError; }
1316 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1317 ShaderExecutableD3D *getResult() { return mResult; }
1318
1319 protected:
1320 ProgramD3D *mProgram;
1321 gl::Error mError;
1322 gl::InfoLog mInfoLog;
1323 ShaderExecutableD3D *mResult;
1324};
1325
1326class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1327{
1328 public:
1329 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1330 gl::Error run() override
1331 {
1332 const auto &defaultInputLayout =
1333 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1334
1335 ANGLE_TRY(
1336 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1337
1338 return gl::NoError();
1339 }
1340};
1341
1342class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1343{
1344 public:
1345 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1346 gl::Error run() override
1347 {
1348 const auto &defaultPixelOutput =
1349 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1350
1351 ANGLE_TRY(
1352 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1353
1354 return gl::NoError();
1355 }
1356};
1357
1358class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1359{
1360 public:
1361 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1362 : GetExecutableTask(program), mContextState(contextState)
1363 {
1364 }
1365
1366 gl::Error run() override
1367 {
1368 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1369 // D3D11.
1370 if (mProgram->usesGeometryShader(GL_POINTS))
1371 {
1372 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1373 &mResult, &mInfoLog));
1374 }
1375
1376 return gl::NoError();
1377 }
1378
1379 private:
1380 const gl::ContextState &mContextState;
1381};
1382
1383LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1384 gl::InfoLog &infoLog)
1385{
1386 // Ensure the compiler is initialized to avoid race conditions.
1387 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1388
1389 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1390
1391 GetVertexExecutableTask vertexTask(this);
1392 GetPixelExecutableTask pixelTask(this);
1393 GetGeometryExecutableTask geometryTask(this, contextState);
1394
1395 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1396 workerPool->postWorkerTask(&pixelTask),
1397 workerPool->postWorkerTask(&geometryTask)}};
1398
1399 WaitableEvent::WaitMany(&waitEvents);
1400
1401 infoLog << vertexTask.getInfoLog().str();
1402 infoLog << pixelTask.getInfoLog().str();
1403 infoLog << geometryTask.getInfoLog().str();
1404
1405 ANGLE_TRY(vertexTask.getError());
1406 ANGLE_TRY(pixelTask.getError());
1407 ANGLE_TRY(geometryTask.getError());
1408
1409 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1410 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1411 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1412
Jamie Madill48ef11b2016-04-27 15:21:52 -04001413 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001414
1415 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001416 {
Jamie Madill334d6152015-10-22 14:00:28 -04001417 // Geometry shaders are currently only used internally, so there is no corresponding shader
1418 // object at the interface level. For now the geometry shader debug info is prepended to
1419 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001420 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001421 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001422 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1423 }
1424
1425 if (defaultVertexExecutable)
1426 {
1427 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1428 }
1429
1430 if (defaultPixelExecutable)
1431 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001432 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001433 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001434 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1435 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001436
Jamie Madillb0a838b2016-11-13 20:02:12 -05001437 return (defaultVertexExecutable && defaultPixelExecutable &&
1438 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001439}
1440
Xinghua Caob1239382016-12-13 15:07:05 +08001441LinkResult ProgramD3D::compileComputeExecutable(gl::InfoLog &infoLog)
1442{
1443 // Ensure the compiler is initialized to avoid race conditions.
1444 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1445
1446 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(mState);
1447
1448 ShaderExecutableD3D *computeExecutable = nullptr;
1449 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, SHADER_COMPUTE,
1450 std::vector<D3DVarying>(), false,
1451 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1452
1453 if (computeExecutable == nullptr)
1454 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001455 ERR() << "Error compiling dynamic compute executable:" << std::endl
1456 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001457 }
1458 else
1459 {
1460 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1461 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1462 mComputeExecutable.reset(computeExecutable);
1463 }
1464
1465 return mComputeExecutable.get() != nullptr;
1466}
1467
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001468LinkResult ProgramD3D::link(ContextImpl *contextImpl,
Jamie Madill192745a2016-12-22 15:58:21 -05001469 const gl::VaryingPacking &packing,
1470 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001471{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001472 const auto &data = contextImpl->getContextState();
1473
Jamie Madill62d31cb2015-09-11 13:25:51 -04001474 reset();
1475
Xinghua Caob1239382016-12-13 15:07:05 +08001476 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1477 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001478 {
Xinghua Caob1239382016-12-13 15:07:05 +08001479 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1480
1481 defineUniformsAndAssignRegisters();
1482
1483 LinkResult result = compileComputeExecutable(infoLog);
1484 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001485 {
Xinghua Caob1239382016-12-13 15:07:05 +08001486 infoLog << result.getError().getMessage();
1487 return result;
1488 }
1489 else if (!result.getResult())
1490 {
1491 infoLog << "Failed to create D3D compute shader.";
1492 return result;
1493 }
1494
1495 initUniformBlockInfo(computeShader);
1496 }
1497 else
1498 {
1499 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1500 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1501
1502 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1503 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1504
1505 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1506 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1507
1508 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1509 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1510
1511 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1512 {
1513 if (fragmentShaderD3D->usesFrontFacing())
1514 {
1515 infoLog << "The current renderer doesn't support gl_FrontFacing";
1516 return false;
1517 }
1518 }
1519
1520 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1521 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1522 // intelligently, but D3D9 assumes one semantic per register.
1523 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1524 packing.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
1525 {
1526 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001527 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001528 }
Xinghua Caob1239382016-12-13 15:07:05 +08001529
1530 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
1531 BuiltinVaryingsD3D builtins(metadata, packing);
1532
1533 mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, packing, builtins, &mPixelHLSL,
1534 &mVertexHLSL);
1535
1536 mUsesPointSize = vertexShaderD3D->usesPointSize();
1537 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1538 mUsesFragDepth = metadata.usesFragDepth();
1539
1540 // Cache if we use flat shading
1541 mUsesFlatInterpolation = (FindFlatInterpolationVarying(fragmentShader->getVaryings()) ||
1542 FindFlatInterpolationVarying(vertexShader->getVaryings()));
1543
1544 if (mRenderer->getMajorShaderModel() >= 4)
1545 {
1546 mGeometryShaderPreamble =
1547 mDynamicHLSL->generateGeometryShaderPreamble(packing, builtins);
1548 }
1549
1550 initAttribLocationsToD3DSemantic();
1551
1552 defineUniformsAndAssignRegisters();
1553
1554 gatherTransformFeedbackVaryings(packing, builtins[SHADER_VERTEX]);
1555
1556 LinkResult result = compileProgramExecutables(data, infoLog);
1557 if (result.isError())
1558 {
1559 infoLog << result.getError().getMessage();
1560 return result;
1561 }
1562 else if (!result.getResult())
1563 {
1564 infoLog << "Failed to create D3D shaders.";
1565 return result;
1566 }
1567
1568 initUniformBlockInfo(vertexShader);
1569 initUniformBlockInfo(fragmentShader);
Austin Kinross02df7962015-07-01 10:03:42 -07001570 }
1571
Jamie Madillb0a838b2016-11-13 20:02:12 -05001572 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001573}
1574
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001575GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001576{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001577 // TODO(jmadill): Do something useful here?
1578 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001579}
1580
Xinghua Caob1239382016-12-13 15:07:05 +08001581void ProgramD3D::initUniformBlockInfo(const gl::Shader *shader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001582{
Xinghua Caob1239382016-12-13 15:07:05 +08001583 for (const sh::InterfaceBlock &interfaceBlock : shader->getInterfaceBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001584 {
Xinghua Caob1239382016-12-13 15:07:05 +08001585 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001586 continue;
1587
Xinghua Caob1239382016-12-13 15:07:05 +08001588 if (mBlockDataSizes.count(interfaceBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001589 continue;
1590
Xinghua Caob1239382016-12-13 15:07:05 +08001591 size_t dataSize = getUniformBlockInfo(interfaceBlock);
1592 mBlockDataSizes[interfaceBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001593 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001594}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001595
Jamie Madill51f522f2016-12-21 15:10:55 -05001596void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001597{
Jamie Madill51f522f2016-12-21 15:10:55 -05001598 // Lazy init.
1599 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1600 {
1601 return;
1602 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001603
1604 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001605 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1606 const ShaderD3D *fragmentShaderD3D =
1607 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1608 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001609
Jamie Madill48ef11b2016-04-27 15:21:52 -04001610 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001611 {
1612 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1613
Jamie Madill4a3c2342015-10-08 12:58:45 -04001614 D3DUniformBlock d3dUniformBlock;
1615
Jamie Madill62d31cb2015-09-11 13:25:51 -04001616 if (uniformBlock.vertexStaticUse)
1617 {
Xinghua Caob1239382016-12-13 15:07:05 +08001618 ASSERT(vertexShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001619 unsigned int baseRegister =
1620 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001621 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001622 }
1623
1624 if (uniformBlock.fragmentStaticUse)
1625 {
Xinghua Caob1239382016-12-13 15:07:05 +08001626 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001627 unsigned int baseRegister =
1628 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001629 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001630 }
1631
Xinghua Caob1239382016-12-13 15:07:05 +08001632 if (uniformBlock.computeStaticUse)
1633 {
1634 ASSERT(computeShaderD3D != nullptr);
1635 unsigned int baseRegister =
1636 computeShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
1637 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1638 }
1639
Jamie Madill4a3c2342015-10-08 12:58:45 -04001640 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001641 }
1642}
1643
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001644void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001645{
1646 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001647 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001648 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001649 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001650 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001651 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001652 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001653 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001654 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001655 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001656 vertexRegisters = std::max(vertexRegisters,
1657 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001658 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001659 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001660 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001661 fragmentRegisters = std::max(
1662 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001663 }
Xinghua Caob1239382016-12-13 15:07:05 +08001664 if (d3dUniform->isReferencedByComputeShader())
1665 {
1666 computeRegisters = std::max(
1667 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1668 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001669 }
1670 }
1671
Xinghua Caob1239382016-12-13 15:07:05 +08001672 mVertexUniformStorage =
1673 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1674 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1675 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1676 mComputeUniformStorage =
1677 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Brandon Jonesc9610c52014-08-25 17:02:59 -07001678}
1679
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001680gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001681{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001682 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001683
Jamie Madill01074252016-11-28 15:55:51 -05001684 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001685
Jamie Madill62d31cb2015-09-11 13:25:51 -04001686 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001687 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001688 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001689 }
1690
Jamie Madill51f522f2016-12-21 15:10:55 -05001691 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001692}
1693
Jamie Madill9082b982016-04-27 15:21:51 -04001694gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001695{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001696 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001697 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001698 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001699 }
1700
Jamie Madill51f522f2016-12-21 15:10:55 -05001701 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001702
Jamie Madill03260fa2015-06-22 13:57:22 -04001703 mVertexUBOCache.clear();
1704 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001705
1706 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1707 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1708
Jamie Madill4a3c2342015-10-08 12:58:45 -04001709 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001710 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001711 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001712 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001713 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001714
Brandon Jones18bd4102014-09-22 14:21:44 -07001715 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001716 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001717 {
1718 continue;
1719 }
1720
Jamie Madill4a3c2342015-10-08 12:58:45 -04001721 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001722 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001723 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001724 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001725
Jamie Madill969194d2015-07-20 14:36:56 -04001726 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001727 {
1728 mVertexUBOCache.resize(registerIndex + 1, -1);
1729 }
1730
1731 ASSERT(mVertexUBOCache[registerIndex] == -1);
1732 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001733 }
1734
Jamie Madill4a3c2342015-10-08 12:58:45 -04001735 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001736 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001737 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001738 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001739
1740 if (mFragmentUBOCache.size() <= registerIndex)
1741 {
1742 mFragmentUBOCache.resize(registerIndex + 1, -1);
1743 }
1744
1745 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1746 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001747 }
1748 }
1749
Jamie Madill03260fa2015-06-22 13:57:22 -04001750 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001751}
1752
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001753void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001754{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001755 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001756 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001757 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001758 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001759}
1760
Jamie Madill334d6152015-10-22 14:00:28 -04001761void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001762{
1763 setUniform(location, count, v, GL_FLOAT);
1764}
1765
1766void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1767{
1768 setUniform(location, count, v, GL_FLOAT_VEC2);
1769}
1770
1771void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1772{
1773 setUniform(location, count, v, GL_FLOAT_VEC3);
1774}
1775
1776void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1777{
1778 setUniform(location, count, v, GL_FLOAT_VEC4);
1779}
1780
Jamie Madill334d6152015-10-22 14:00:28 -04001781void ProgramD3D::setUniformMatrix2fv(GLint location,
1782 GLsizei count,
1783 GLboolean transpose,
1784 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001785{
1786 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1787}
1788
Jamie Madill334d6152015-10-22 14:00:28 -04001789void ProgramD3D::setUniformMatrix3fv(GLint location,
1790 GLsizei count,
1791 GLboolean transpose,
1792 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001793{
1794 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1795}
1796
Jamie Madill334d6152015-10-22 14:00:28 -04001797void ProgramD3D::setUniformMatrix4fv(GLint location,
1798 GLsizei count,
1799 GLboolean transpose,
1800 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001801{
1802 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1803}
1804
Jamie Madill334d6152015-10-22 14:00:28 -04001805void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1806 GLsizei count,
1807 GLboolean transpose,
1808 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001809{
1810 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1811}
1812
Jamie Madill334d6152015-10-22 14:00:28 -04001813void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1814 GLsizei count,
1815 GLboolean transpose,
1816 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001817{
1818 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1819}
1820
Jamie Madill334d6152015-10-22 14:00:28 -04001821void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1822 GLsizei count,
1823 GLboolean transpose,
1824 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001825{
1826 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1827}
1828
Jamie Madill334d6152015-10-22 14:00:28 -04001829void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1830 GLsizei count,
1831 GLboolean transpose,
1832 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001833{
1834 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1835}
1836
Jamie Madill334d6152015-10-22 14:00:28 -04001837void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1838 GLsizei count,
1839 GLboolean transpose,
1840 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001841{
1842 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1843}
1844
Jamie Madill334d6152015-10-22 14:00:28 -04001845void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1846 GLsizei count,
1847 GLboolean transpose,
1848 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001849{
1850 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1851}
1852
1853void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1854{
1855 setUniform(location, count, v, GL_INT);
1856}
1857
1858void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1859{
1860 setUniform(location, count, v, GL_INT_VEC2);
1861}
1862
1863void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1864{
1865 setUniform(location, count, v, GL_INT_VEC3);
1866}
1867
1868void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1869{
1870 setUniform(location, count, v, GL_INT_VEC4);
1871}
1872
1873void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1874{
1875 setUniform(location, count, v, GL_UNSIGNED_INT);
1876}
1877
1878void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1879{
1880 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1881}
1882
1883void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1884{
1885 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1886}
1887
1888void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1889{
1890 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1891}
1892
Jamie Madill4a3c2342015-10-08 12:58:45 -04001893void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1894 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001895{
1896}
1897
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001898void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001899{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001900 D3DUniformMap uniformMap;
Xinghua Caob1239382016-12-13 15:07:05 +08001901 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1902 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05001903 {
Xinghua Caob1239382016-12-13 15:07:05 +08001904 for (const sh::Uniform &computeUniform : computeShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001905 {
Xinghua Caob1239382016-12-13 15:07:05 +08001906 if (computeUniform.staticUse)
1907 {
1908 defineUniformBase(computeShader, computeUniform, &uniformMap);
1909 }
Jamie Madillfb536032015-09-11 13:19:49 -04001910 }
1911 }
Xinghua Caob1239382016-12-13 15:07:05 +08001912 else
Jamie Madillfb536032015-09-11 13:19:49 -04001913 {
Xinghua Caob1239382016-12-13 15:07:05 +08001914 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1915 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001916 {
Xinghua Caob1239382016-12-13 15:07:05 +08001917 if (vertexUniform.staticUse)
1918 {
1919 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
1920 }
1921 }
1922
1923 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1924 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
1925 {
1926 if (fragmentUniform.staticUse)
1927 {
1928 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
1929 }
Jamie Madillfb536032015-09-11 13:19:49 -04001930 }
1931 }
1932
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001933 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001934 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001935 {
1936 if (!glUniform.isInDefaultBlock())
1937 continue;
1938
1939 auto mapEntry = uniformMap.find(glUniform.name);
1940 ASSERT(mapEntry != uniformMap.end());
1941 mD3DUniforms.push_back(mapEntry->second);
1942 }
1943
Jamie Madill62d31cb2015-09-11 13:25:51 -04001944 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001945 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001946}
1947
Jamie Madill91445bc2015-09-23 16:47:53 -04001948void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001949 const sh::Uniform &uniform,
1950 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001951{
Olli Etuaho96963162016-03-21 11:54:33 +02001952 // Samplers get their registers assigned in assignAllSamplerRegisters.
1953 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001954 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001955 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001956 return;
1957 }
1958
Jamie Madill91445bc2015-09-23 16:47:53 -04001959 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1960
1961 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1962 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04001963 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001964 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001965
Jamie Madill91445bc2015-09-23 16:47:53 -04001966 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001967}
1968
Jamie Madill62d31cb2015-09-11 13:25:51 -04001969D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1970{
1971 for (D3DUniform *d3dUniform : mD3DUniforms)
1972 {
1973 if (d3dUniform->name == name)
1974 {
1975 return d3dUniform;
1976 }
1977 }
1978
1979 return nullptr;
1980}
1981
Jamie Madill91445bc2015-09-23 16:47:53 -04001982void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001983 const sh::ShaderVariable &uniform,
1984 const std::string &fullName,
1985 sh::HLSLBlockEncoder *encoder,
1986 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001987{
1988 if (uniform.isStruct())
1989 {
1990 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1991 {
1992 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1993
Jamie Madill55def582015-05-04 11:24:57 -04001994 if (encoder)
1995 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001996
1997 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1998 {
Jamie Madill334d6152015-10-22 14:00:28 -04001999 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002000 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2001
Olli Etuaho96963162016-03-21 11:54:33 +02002002 // Samplers get their registers assigned in assignAllSamplerRegisters.
2003 // Also they couldn't use the same encoder as the rest of the struct, since they are
2004 // extracted out of the struct by the shader translator.
2005 if (gl::IsSamplerType(field.type))
2006 {
2007 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2008 }
2009 else
2010 {
2011 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2012 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002013 }
2014
Jamie Madill55def582015-05-04 11:24:57 -04002015 if (encoder)
2016 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002018 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002019 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002020
2021 // Not a struct. Arrays are treated as aggregate types.
2022 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002023 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002024 encoder->enterAggregateType();
2025 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002026
Jamie Madill62d31cb2015-09-11 13:25:51 -04002027 // Advance the uniform offset, to track registers allocation for structs
2028 sh::BlockMemberInfo blockInfo =
2029 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2030 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002032 auto uniformMapEntry = uniformMap->find(fullName);
2033 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002034
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002035 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002036 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002037 d3dUniform = uniformMapEntry->second;
2038 }
2039 else
2040 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002041 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002042 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002043 }
2044
2045 if (encoder)
2046 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002047 d3dUniform->registerElement =
2048 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002049 unsigned int reg =
2050 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002051 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002052 {
2053 d3dUniform->psRegisterIndex = reg;
2054 }
Xinghua Caob1239382016-12-13 15:07:05 +08002055 else if (shaderType == GL_VERTEX_SHADER)
2056 {
2057 d3dUniform->vsRegisterIndex = reg;
2058 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002059 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002060 {
Xinghua Caob1239382016-12-13 15:07:05 +08002061 ASSERT(shaderType == GL_COMPUTE_SHADER);
2062 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002063 }
Jamie Madillfb536032015-09-11 13:19:49 -04002064
2065 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002066 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002067 {
2068 encoder->exitAggregateType();
2069 }
2070 }
2071}
2072
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002073template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002074void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002075{
Jamie Madill334d6152015-10-22 14:00:28 -04002076 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002077 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2078
Jamie Madill62d31cb2015-09-11 13:25:51 -04002079 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002080
Jamie Madill62d31cb2015-09-11 13:25:51 -04002081 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002082 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002083 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002084
2085 if (targetUniform->type == targetUniformType)
2086 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002087 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002088
Jamie Madill62d31cb2015-09-11 13:25:51 -04002089 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002090 {
Jamie Madill334d6152015-10-22 14:00:28 -04002091 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002092 const T *source = v + (i * components);
2093
2094 for (int c = 0; c < components; c++)
2095 {
2096 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2097 }
2098 for (int c = components; c < 4; c++)
2099 {
2100 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2101 }
2102 }
2103 }
2104 else if (targetUniform->type == targetBoolType)
2105 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002106 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002107
Jamie Madill62d31cb2015-09-11 13:25:51 -04002108 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002109 {
Jamie Madill334d6152015-10-22 14:00:28 -04002110 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002111 const T *source = v + (i * components);
2112
2113 for (int c = 0; c < components; c++)
2114 {
Jamie Madill334d6152015-10-22 14:00:28 -04002115 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2116 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002117 }
2118 for (int c = components; c < 4; c++)
2119 {
2120 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2121 }
2122 }
2123 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002124 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002125 {
2126 ASSERT(targetUniformType == GL_INT);
2127
Jamie Madill62d31cb2015-09-11 13:25:51 -04002128 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002129
2130 bool wasDirty = targetUniform->dirty;
2131
Jamie Madill62d31cb2015-09-11 13:25:51 -04002132 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002133 {
Jamie Madill334d6152015-10-22 14:00:28 -04002134 GLint *dest = target + (i * 4);
2135 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002136
2137 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2138 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2139 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2140 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2141 }
2142
2143 if (!wasDirty && targetUniform->dirty)
2144 {
2145 mDirtySamplerMapping = true;
2146 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002147 }
Jamie Madill334d6152015-10-22 14:00:28 -04002148 else
2149 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002150}
2151
2152template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002153void ProgramD3D::setUniformMatrixfv(GLint location,
2154 GLsizei countIn,
2155 GLboolean transpose,
2156 const GLfloat *value,
2157 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002158{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002159 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002160
Jamie Madill62d31cb2015-09-11 13:25:51 -04002161 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002162 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002163 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002164
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002165 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002166 GLfloat *target =
2167 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002168
Jamie Madill62d31cb2015-09-11 13:25:51 -04002169 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002170 {
2171 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2172 if (transpose == GL_FALSE)
2173 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002174 targetUniform->dirty =
2175 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002176 }
2177 else
2178 {
Jamie Madill334d6152015-10-22 14:00:28 -04002179 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002180 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002181 }
2182 target += targetMatrixStride;
2183 value += cols * rows;
2184 }
2185}
2186
Jamie Madill4a3c2342015-10-08 12:58:45 -04002187size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002188{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002189 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002190
Jamie Madill62d31cb2015-09-11 13:25:51 -04002191 // define member uniforms
2192 sh::Std140BlockEncoder std140Encoder;
Jamie Madillcc2ed612017-03-14 15:59:00 -04002193 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002194 sh::BlockLayoutEncoder *encoder = nullptr;
2195
2196 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002197 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002198 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002199 }
2200 else
2201 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002202 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002203 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002204
Jamie Madill39046162016-02-08 15:05:17 -05002205 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2206 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002207
2208 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002209}
2210
Jamie Madill62d31cb2015-09-11 13:25:51 -04002211void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002212{
Olli Etuaho96963162016-03-21 11:54:33 +02002213 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002214 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002215 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002216 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002217 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002218 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002219 }
2220}
2221
Olli Etuaho96963162016-03-21 11:54:33 +02002222void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002223{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002224 ASSERT(d3dUniform->isSampler());
Xinghua Caob1239382016-12-13 15:07:05 +08002225 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2226 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002227 {
Xinghua Caob1239382016-12-13 15:07:05 +08002228 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2229 ASSERT(computeShaderD3D->hasUniform(d3dUniform));
2230 d3dUniform->csRegisterIndex = computeShaderD3D->getUniformRegister(d3dUniform->name);
2231 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2232 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2233 mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002234 }
Xinghua Caob1239382016-12-13 15:07:05 +08002235 else
Jamie Madillfb536032015-09-11 13:19:49 -04002236 {
Xinghua Caob1239382016-12-13 15:07:05 +08002237 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2238 const ShaderD3D *fragmentShaderD3D =
2239 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2240 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) ||
2241 fragmentShaderD3D->hasUniform(d3dUniform));
2242 if (vertexShaderD3D->hasUniform(d3dUniform))
2243 {
2244 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2245 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
2246 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2247 mSamplersVS, &mUsedVertexSamplerRange);
2248 }
2249 if (fragmentShaderD3D->hasUniform(d3dUniform))
2250 {
2251 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2252 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
2253 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2254 mSamplersPS, &mUsedPixelSamplerRange);
2255 }
Jamie Madillfb536032015-09-11 13:19:49 -04002256 }
2257}
2258
Jamie Madill62d31cb2015-09-11 13:25:51 -04002259// static
2260void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002261 GLenum samplerType,
2262 unsigned int samplerCount,
2263 std::vector<Sampler> &outSamplers,
2264 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002265{
2266 unsigned int samplerIndex = startSamplerIndex;
2267
2268 do
2269 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002270 ASSERT(samplerIndex < outSamplers.size());
2271 Sampler *sampler = &outSamplers[samplerIndex];
2272 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002273 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002274 sampler->logicalTextureUnit = 0;
2275 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002276 samplerIndex++;
2277 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002278}
2279
Brandon Jonesc9610c52014-08-25 17:02:59 -07002280void ProgramD3D::reset()
2281{
Xinghua Caob1239382016-12-13 15:07:05 +08002282 mVertexExecutables.clear();
2283 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002284
Xinghua Caob1239382016-12-13 15:07:05 +08002285 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002286 {
Xinghua Caob1239382016-12-13 15:07:05 +08002287 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002288 }
Brandon Joneseb994362014-09-24 10:27:28 -07002289
Xinghua Caob1239382016-12-13 15:07:05 +08002290 mComputeExecutable.reset(nullptr);
2291
Brandon Jones22502d52014-08-29 16:58:36 -07002292 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002293 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002294
2295 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002296 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002297 mUsesFragDepth = false;
2298 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002299 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002300 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002301
Jamie Madill62d31cb2015-09-11 13:25:51 -04002302 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002303 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002304
Xinghua Caob1239382016-12-13 15:07:05 +08002305 mVertexUniformStorage.reset(nullptr);
2306 mFragmentUniformStorage.reset(nullptr);
2307 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002308
2309 mSamplersPS.clear();
2310 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002311 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002312
2313 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002314 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002315 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002316 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002317
Jamie Madill8047c0d2016-03-07 13:02:12 -05002318 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002319
Jamie Madill9fc36822015-11-18 13:08:07 -05002320 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002321
2322 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002323}
2324
Geoff Lang7dd2e102014-11-10 15:19:26 -05002325unsigned int ProgramD3D::getSerial() const
2326{
2327 return mSerial;
2328}
2329
2330unsigned int ProgramD3D::issueSerial()
2331{
2332 return mCurrentSerial++;
2333}
2334
Jamie Madill8047c0d2016-03-07 13:02:12 -05002335void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002336{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002337 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002338 ASSERT(vertexShader != nullptr);
2339
2340 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002341 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002342 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002343 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2344 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002345
Jamie Madill8047c0d2016-03-07 13:02:12 -05002346 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002347 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002348 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002349 }
2350 }
Jamie Madill437d2662014-12-05 14:23:35 -05002351}
2352
Jamie Madill63805b42015-08-25 13:17:39 -04002353void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002354{
Jamie Madillbd136f92015-08-10 14:51:37 -04002355 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002356 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002357
Jamie Madill01074252016-11-28 15:55:51 -05002358 for (unsigned int locationIndex : IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002359 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002360 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002361
Jamie Madill8047c0d2016-03-07 13:02:12 -05002362 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002363 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002364 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002365 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002366 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002367 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002368 mCachedInputLayout[d3dSemantic] =
2369 GetVertexFormatType(vertexAttributes[locationIndex],
2370 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002371 }
2372 }
2373}
2374
Jamie Madill192745a2016-12-22 15:58:21 -05002375void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2376 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002377{
Jamie Madill9fc36822015-11-18 13:08:07 -05002378 const std::string &varyingSemantic =
2379 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2380
Jamie Madillccdf74b2015-08-18 10:46:12 -04002381 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002382 mStreamOutVaryings.clear();
2383
Jamie Madill48ef11b2016-04-27 15:21:52 -04002384 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002385 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2386 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002387 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002388 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2389 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002390 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002391 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002392 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002393 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2394 builtins.glPosition.index, 4, outputSlot));
2395 }
2396 }
2397 else if (tfVaryingName == "gl_FragCoord")
2398 {
2399 if (builtins.glFragCoord.enabled)
2400 {
2401 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2402 builtins.glFragCoord.index, 4, outputSlot));
2403 }
2404 }
2405 else if (tfVaryingName == "gl_PointSize")
2406 {
2407 if (builtins.glPointSize.enabled)
2408 {
2409 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2410 }
2411 }
2412 else
2413 {
Jamie Madill192745a2016-12-22 15:58:21 -05002414 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002415 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002416 const auto &varying = *registerInfo.packedVarying->varying;
2417 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002418 int componentCount = gl::VariableColumnCount(transposedType);
2419 ASSERT(!varying.isBuiltIn());
2420
Jamie Madill55c25d02015-11-18 13:08:08 -05002421 // Transform feedback for varying structs is underspecified.
2422 // See Khronos bug 9856.
2423 // TODO(jmadill): Figure out how to be spec-compliant here.
2424 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2425 continue;
2426
Jamie Madill9fc36822015-11-18 13:08:07 -05002427 // There can be more than one register assigned to a particular varying, and each
2428 // register needs its own stream out entry.
2429 if (tfVaryingName == varying.name)
2430 {
2431 mStreamOutVaryings.push_back(D3DVarying(
2432 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2433 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002434 }
2435 }
2436 }
2437}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002438
2439D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2440{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002441 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002442}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002443
2444bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2445{
2446 std::string baseName = blockName;
2447 gl::ParseAndStripArrayIndex(&baseName);
2448
2449 auto sizeIter = mBlockDataSizes.find(baseName);
2450 if (sizeIter == mBlockDataSizes.end())
2451 {
2452 *sizeOut = 0;
2453 return false;
2454 }
2455
2456 *sizeOut = sizeIter->second;
2457 return true;
2458}
2459
2460bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2461 sh::BlockMemberInfo *memberInfoOut) const
2462{
2463 auto infoIter = mBlockInfo.find(memberUniformName);
2464 if (infoIter == mBlockInfo.end())
2465 {
2466 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2467 return false;
2468 }
2469
2470 *memberInfoOut = infoIter->second;
2471 return true;
2472}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002473
2474void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2475 GLenum genMode,
2476 GLint components,
2477 const GLfloat *coeffs)
2478{
2479 UNREACHABLE();
2480}
2481
Jamie Madill8047c0d2016-03-07 13:02:12 -05002482} // namespace rx