blob: b8df596bdd12e1d44fb4b50f0147a300cdaee582 [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
Jamie Madill20e005b2017-04-07 14:19:22 -040011#include "common/bitset_utils.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{
Yunchao He4f285442017-04-21 12:15:49 +0800125 ASSERT(dest != nullptr);
126 ASSERT(dirtyFlag != nullptr);
Jacek Caban2302e692015-12-01 12:44:43 +0100127
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
Yunchao He61afff12017-03-14 15:34:03 +08001131void ProgramD3D::setSeparable(bool /* separable */)
1132{
1133}
1134
Jamie Madill334d6152015-10-22 14:00:28 -04001135gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1136 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001137{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001138 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001139
Jamie Madill85a18042015-03-05 15:41:41 -05001140 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001141 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001142
1143 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1144 {
1145 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1146
1147 if (colorbuffer)
1148 {
Jamie Madill334d6152015-10-22 14:00:28 -04001149 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1150 ? GL_COLOR_ATTACHMENT0
1151 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001152 }
1153 else
1154 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001155 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001156 }
1157 }
1158
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001159 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001160}
1161
Jamie Madill97399232014-12-23 12:31:15 -05001162gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001163 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001164 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001165{
1166 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1167 {
1168 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1169 {
Geoff Langb543aff2014-09-30 14:52:54 -04001170 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001171 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001172 }
1173 }
1174
Jamie Madill334d6152015-10-22 14:00:28 -04001175 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1176 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001177
1178 // Generate new pixel executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001179 ShaderExecutableD3D *pixelExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001180
1181 gl::InfoLog tempInfoLog;
1182 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1183
Jamie Madill01074252016-11-28 15:55:51 -05001184 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001185 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001186 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001187 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001188
Jamie Madill97399232014-12-23 12:31:15 -05001189 if (pixelExecutable)
1190 {
Xinghua Caob1239382016-12-13 15:07:05 +08001191 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
1192 new PixelExecutable(outputSignature, pixelExecutable)));
Jamie Madill97399232014-12-23 12:31:15 -05001193 }
1194 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001195 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001196 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1197 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001198 }
Brandon Jones22502d52014-08-29 16:58:36 -07001199
Geoff Langb543aff2014-09-30 14:52:54 -04001200 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001201 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001202}
1203
Jamie Madilld3dfda22015-07-06 08:28:49 -04001204gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001205 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001206 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001207{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001208 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001209
1210 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1211 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001212 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001213 {
Geoff Langb543aff2014-09-30 14:52:54 -04001214 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001215 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001216 }
1217 }
1218
Brandon Jones22502d52014-08-29 16:58:36 -07001219 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001220 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001221 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001222
1223 // Generate new vertex executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001224 ShaderExecutableD3D *vertexExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001225
1226 gl::InfoLog tempInfoLog;
1227 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1228
Jamie Madill01074252016-11-28 15:55:51 -05001229 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001230 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001231 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001232 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001233
Jamie Madill97399232014-12-23 12:31:15 -05001234 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001235 {
Xinghua Caob1239382016-12-13 15:07:05 +08001236 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
1237 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001238 }
Jamie Madill97399232014-12-23 12:31:15 -05001239 else if (!infoLog)
1240 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001241 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1242 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001243 }
Brandon Jones22502d52014-08-29 16:58:36 -07001244
Geoff Langb543aff2014-09-30 14:52:54 -04001245 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001246 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001247}
1248
Jamie Madill9082b982016-04-27 15:21:51 -04001249gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001250 GLenum drawMode,
1251 ShaderExecutableD3D **outExecutable,
1252 gl::InfoLog *infoLog)
1253{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001254 if (outExecutable)
1255 {
1256 *outExecutable = nullptr;
1257 }
1258
Austin Kinross88829e82016-01-12 13:04:41 -08001259 // Return a null shader if the current rendering doesn't use a geometry shader
1260 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001261 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001262 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001263 }
1264
Jamie Madill4e31ad52015-10-29 10:32:57 -04001265 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1266
Xinghua Caob1239382016-12-13 15:07:05 +08001267 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001268 {
1269 if (outExecutable)
1270 {
Xinghua Caob1239382016-12-13 15:07:05 +08001271 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001272 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001273 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001274 }
1275
1276 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001277 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001278 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001279
1280 gl::InfoLog tempInfoLog;
1281 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1282
Xinghua Caob1239382016-12-13 15:07:05 +08001283 ShaderExecutableD3D *geometryExecutable = nullptr;
1284 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001285 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001286 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001287 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001288
1289 if (!infoLog && error.isError())
1290 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001291 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1292 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001293 }
1294
Xinghua Caob1239382016-12-13 15:07:05 +08001295 if (geometryExecutable != nullptr)
1296 {
1297 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1298 }
1299
Jamie Madill4e31ad52015-10-29 10:32:57 -04001300 if (outExecutable)
1301 {
Xinghua Caob1239382016-12-13 15:07:05 +08001302 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001303 }
1304 return error;
1305}
1306
Jamie Madill01074252016-11-28 15:55:51 -05001307class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001308{
Jamie Madill01074252016-11-28 15:55:51 -05001309 public:
1310 GetExecutableTask(ProgramD3D *program)
1311 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001312 {
Brandon Joneseb994362014-09-24 10:27:28 -07001313 }
1314
Jamie Madill01074252016-11-28 15:55:51 -05001315 virtual gl::Error run() = 0;
1316
1317 void operator()() override { mError = run(); }
1318
1319 const gl::Error &getError() const { return mError; }
1320 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1321 ShaderExecutableD3D *getResult() { return mResult; }
1322
1323 protected:
1324 ProgramD3D *mProgram;
1325 gl::Error mError;
1326 gl::InfoLog mInfoLog;
1327 ShaderExecutableD3D *mResult;
1328};
1329
1330class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1331{
1332 public:
1333 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1334 gl::Error run() override
1335 {
1336 const auto &defaultInputLayout =
1337 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1338
1339 ANGLE_TRY(
1340 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1341
1342 return gl::NoError();
1343 }
1344};
1345
1346class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1347{
1348 public:
1349 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1350 gl::Error run() override
1351 {
1352 const auto &defaultPixelOutput =
1353 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1354
1355 ANGLE_TRY(
1356 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1357
1358 return gl::NoError();
1359 }
1360};
1361
1362class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1363{
1364 public:
1365 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1366 : GetExecutableTask(program), mContextState(contextState)
1367 {
1368 }
1369
1370 gl::Error run() override
1371 {
1372 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1373 // D3D11.
1374 if (mProgram->usesGeometryShader(GL_POINTS))
1375 {
1376 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1377 &mResult, &mInfoLog));
1378 }
1379
1380 return gl::NoError();
1381 }
1382
1383 private:
1384 const gl::ContextState &mContextState;
1385};
1386
Xinghua Cao73badc02017-03-29 19:14:53 +08001387gl::Error ProgramD3D::getComputeExecutable(ShaderExecutableD3D **outExecutable)
1388{
1389 if (outExecutable)
1390 {
1391 *outExecutable = mComputeExecutable.get();
1392 }
1393
1394 return gl::NoError();
1395}
1396
Jamie Madill01074252016-11-28 15:55:51 -05001397LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1398 gl::InfoLog &infoLog)
1399{
1400 // Ensure the compiler is initialized to avoid race conditions.
1401 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1402
1403 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1404
1405 GetVertexExecutableTask vertexTask(this);
1406 GetPixelExecutableTask pixelTask(this);
1407 GetGeometryExecutableTask geometryTask(this, contextState);
1408
1409 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1410 workerPool->postWorkerTask(&pixelTask),
1411 workerPool->postWorkerTask(&geometryTask)}};
1412
1413 WaitableEvent::WaitMany(&waitEvents);
1414
1415 infoLog << vertexTask.getInfoLog().str();
1416 infoLog << pixelTask.getInfoLog().str();
1417 infoLog << geometryTask.getInfoLog().str();
1418
1419 ANGLE_TRY(vertexTask.getError());
1420 ANGLE_TRY(pixelTask.getError());
1421 ANGLE_TRY(geometryTask.getError());
1422
1423 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1424 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1425 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1426
Jamie Madill48ef11b2016-04-27 15:21:52 -04001427 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001428
1429 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001430 {
Jamie Madill334d6152015-10-22 14:00:28 -04001431 // Geometry shaders are currently only used internally, so there is no corresponding shader
1432 // object at the interface level. For now the geometry shader debug info is prepended to
1433 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001434 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001435 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001436 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1437 }
1438
1439 if (defaultVertexExecutable)
1440 {
1441 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1442 }
1443
1444 if (defaultPixelExecutable)
1445 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001446 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001447 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001448 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1449 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001450
Jamie Madillb0a838b2016-11-13 20:02:12 -05001451 return (defaultVertexExecutable && defaultPixelExecutable &&
1452 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001453}
1454
Xinghua Caob1239382016-12-13 15:07:05 +08001455LinkResult ProgramD3D::compileComputeExecutable(gl::InfoLog &infoLog)
1456{
1457 // Ensure the compiler is initialized to avoid race conditions.
1458 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1459
1460 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(mState);
1461
1462 ShaderExecutableD3D *computeExecutable = nullptr;
1463 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, SHADER_COMPUTE,
1464 std::vector<D3DVarying>(), false,
1465 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1466
1467 if (computeExecutable == nullptr)
1468 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001469 ERR() << "Error compiling dynamic compute executable:" << std::endl
1470 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001471 }
1472 else
1473 {
1474 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1475 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1476 mComputeExecutable.reset(computeExecutable);
1477 }
1478
1479 return mComputeExecutable.get() != nullptr;
1480}
1481
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001482LinkResult ProgramD3D::link(ContextImpl *contextImpl,
Jamie Madill192745a2016-12-22 15:58:21 -05001483 const gl::VaryingPacking &packing,
1484 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001485{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001486 const auto &data = contextImpl->getContextState();
1487
Jamie Madill62d31cb2015-09-11 13:25:51 -04001488 reset();
1489
Xinghua Caob1239382016-12-13 15:07:05 +08001490 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1491 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001492 {
Xinghua Caob1239382016-12-13 15:07:05 +08001493 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1494
1495 defineUniformsAndAssignRegisters();
1496
1497 LinkResult result = compileComputeExecutable(infoLog);
1498 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001499 {
Xinghua Caob1239382016-12-13 15:07:05 +08001500 infoLog << result.getError().getMessage();
1501 return result;
1502 }
1503 else if (!result.getResult())
1504 {
1505 infoLog << "Failed to create D3D compute shader.";
1506 return result;
1507 }
1508
1509 initUniformBlockInfo(computeShader);
1510 }
1511 else
1512 {
1513 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1514 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1515
1516 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1517 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1518
1519 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1520 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1521
1522 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1523 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1524
1525 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1526 {
1527 if (fragmentShaderD3D->usesFrontFacing())
1528 {
1529 infoLog << "The current renderer doesn't support gl_FrontFacing";
1530 return false;
1531 }
1532 }
1533
1534 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1535 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1536 // intelligently, but D3D9 assumes one semantic per register.
1537 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1538 packing.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
1539 {
1540 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001541 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001542 }
Xinghua Caob1239382016-12-13 15:07:05 +08001543
1544 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
1545 BuiltinVaryingsD3D builtins(metadata, packing);
1546
1547 mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, packing, builtins, &mPixelHLSL,
1548 &mVertexHLSL);
1549
1550 mUsesPointSize = vertexShaderD3D->usesPointSize();
1551 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1552 mUsesFragDepth = metadata.usesFragDepth();
1553
1554 // Cache if we use flat shading
1555 mUsesFlatInterpolation = (FindFlatInterpolationVarying(fragmentShader->getVaryings()) ||
1556 FindFlatInterpolationVarying(vertexShader->getVaryings()));
1557
1558 if (mRenderer->getMajorShaderModel() >= 4)
1559 {
1560 mGeometryShaderPreamble =
1561 mDynamicHLSL->generateGeometryShaderPreamble(packing, builtins);
1562 }
1563
1564 initAttribLocationsToD3DSemantic();
1565
1566 defineUniformsAndAssignRegisters();
1567
1568 gatherTransformFeedbackVaryings(packing, builtins[SHADER_VERTEX]);
1569
1570 LinkResult result = compileProgramExecutables(data, infoLog);
1571 if (result.isError())
1572 {
1573 infoLog << result.getError().getMessage();
1574 return result;
1575 }
1576 else if (!result.getResult())
1577 {
1578 infoLog << "Failed to create D3D shaders.";
1579 return result;
1580 }
1581
1582 initUniformBlockInfo(vertexShader);
1583 initUniformBlockInfo(fragmentShader);
Austin Kinross02df7962015-07-01 10:03:42 -07001584 }
1585
Jamie Madillb0a838b2016-11-13 20:02:12 -05001586 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001587}
1588
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001589GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001590{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001591 // TODO(jmadill): Do something useful here?
1592 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001593}
1594
Xinghua Caob1239382016-12-13 15:07:05 +08001595void ProgramD3D::initUniformBlockInfo(const gl::Shader *shader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001596{
Xinghua Caob1239382016-12-13 15:07:05 +08001597 for (const sh::InterfaceBlock &interfaceBlock : shader->getInterfaceBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001598 {
Xinghua Caob1239382016-12-13 15:07:05 +08001599 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001600 continue;
1601
Xinghua Caob1239382016-12-13 15:07:05 +08001602 if (mBlockDataSizes.count(interfaceBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001603 continue;
1604
Xinghua Caob1239382016-12-13 15:07:05 +08001605 size_t dataSize = getUniformBlockInfo(interfaceBlock);
1606 mBlockDataSizes[interfaceBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001607 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001608}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001609
Jamie Madill51f522f2016-12-21 15:10:55 -05001610void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001611{
Jamie Madill51f522f2016-12-21 15:10:55 -05001612 // Lazy init.
1613 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1614 {
1615 return;
1616 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001617
1618 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001619 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1620 const ShaderD3D *fragmentShaderD3D =
1621 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1622 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001623
Jamie Madill48ef11b2016-04-27 15:21:52 -04001624 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001625 {
1626 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1627
Jamie Madill4a3c2342015-10-08 12:58:45 -04001628 D3DUniformBlock d3dUniformBlock;
1629
Jamie Madill62d31cb2015-09-11 13:25:51 -04001630 if (uniformBlock.vertexStaticUse)
1631 {
Xinghua Caob1239382016-12-13 15:07:05 +08001632 ASSERT(vertexShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001633 unsigned int baseRegister =
1634 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001635 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001636 }
1637
1638 if (uniformBlock.fragmentStaticUse)
1639 {
Xinghua Caob1239382016-12-13 15:07:05 +08001640 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001641 unsigned int baseRegister =
1642 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001643 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001644 }
1645
Xinghua Caob1239382016-12-13 15:07:05 +08001646 if (uniformBlock.computeStaticUse)
1647 {
1648 ASSERT(computeShaderD3D != nullptr);
1649 unsigned int baseRegister =
1650 computeShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
1651 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1652 }
1653
Jamie Madill4a3c2342015-10-08 12:58:45 -04001654 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001655 }
1656}
1657
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001658void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001659{
1660 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001661 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001662 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001663 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001664 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001665 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001666 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001667 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001668 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001669 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001670 vertexRegisters = std::max(vertexRegisters,
1671 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001672 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001673 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001674 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001675 fragmentRegisters = std::max(
1676 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001677 }
Xinghua Caob1239382016-12-13 15:07:05 +08001678 if (d3dUniform->isReferencedByComputeShader())
1679 {
1680 computeRegisters = std::max(
1681 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1682 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001683 }
1684 }
1685
Xinghua Caob1239382016-12-13 15:07:05 +08001686 mVertexUniformStorage =
1687 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1688 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1689 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1690 mComputeUniformStorage =
1691 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Brandon Jonesc9610c52014-08-25 17:02:59 -07001692}
1693
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001694gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001695{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001696 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001697
Jamie Madill01074252016-11-28 15:55:51 -05001698 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001699
Jamie Madill62d31cb2015-09-11 13:25:51 -04001700 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001701 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001702 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001703 }
1704
Jamie Madill51f522f2016-12-21 15:10:55 -05001705 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001706}
1707
Xinghua Cao73badc02017-03-29 19:14:53 +08001708gl::Error ProgramD3D::applyComputeUniforms()
1709{
1710 ASSERT(!mDirtySamplerMapping);
1711 ANGLE_TRY(mRenderer->applyComputeUniforms(*this, mD3DUniforms));
1712
1713 for (D3DUniform *d3dUniform : mD3DUniforms)
1714 {
1715 d3dUniform->dirty = false;
1716 }
1717
1718 return gl::NoError();
1719}
1720
Jamie Madill9082b982016-04-27 15:21:51 -04001721gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001722{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001723 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001724 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001725 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001726 }
1727
Jamie Madill51f522f2016-12-21 15:10:55 -05001728 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001729
Jamie Madill03260fa2015-06-22 13:57:22 -04001730 mVertexUBOCache.clear();
1731 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001732
1733 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1734 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1735
Jamie Madill4a3c2342015-10-08 12:58:45 -04001736 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001737 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001738 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001739 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001740 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001741
Brandon Jones18bd4102014-09-22 14:21:44 -07001742 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001743 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001744 {
1745 continue;
1746 }
1747
Jamie Madill4a3c2342015-10-08 12:58:45 -04001748 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001749 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001750 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001751 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001752
Jamie Madill969194d2015-07-20 14:36:56 -04001753 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001754 {
1755 mVertexUBOCache.resize(registerIndex + 1, -1);
1756 }
1757
1758 ASSERT(mVertexUBOCache[registerIndex] == -1);
1759 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001760 }
1761
Jamie Madill4a3c2342015-10-08 12:58:45 -04001762 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001763 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001764 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001765 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001766
1767 if (mFragmentUBOCache.size() <= registerIndex)
1768 {
1769 mFragmentUBOCache.resize(registerIndex + 1, -1);
1770 }
1771
1772 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1773 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001774 }
1775 }
1776
Jamie Madill03260fa2015-06-22 13:57:22 -04001777 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001778}
1779
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001780void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001781{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001782 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001783 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001784 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001785 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001786}
1787
Jamie Madill334d6152015-10-22 14:00:28 -04001788void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001789{
1790 setUniform(location, count, v, GL_FLOAT);
1791}
1792
1793void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1794{
1795 setUniform(location, count, v, GL_FLOAT_VEC2);
1796}
1797
1798void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1799{
1800 setUniform(location, count, v, GL_FLOAT_VEC3);
1801}
1802
1803void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1804{
1805 setUniform(location, count, v, GL_FLOAT_VEC4);
1806}
1807
Jamie Madill334d6152015-10-22 14:00:28 -04001808void ProgramD3D::setUniformMatrix2fv(GLint location,
1809 GLsizei count,
1810 GLboolean transpose,
1811 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001812{
1813 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1814}
1815
Jamie Madill334d6152015-10-22 14:00:28 -04001816void ProgramD3D::setUniformMatrix3fv(GLint location,
1817 GLsizei count,
1818 GLboolean transpose,
1819 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001820{
1821 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1822}
1823
Jamie Madill334d6152015-10-22 14:00:28 -04001824void ProgramD3D::setUniformMatrix4fv(GLint location,
1825 GLsizei count,
1826 GLboolean transpose,
1827 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001828{
1829 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1830}
1831
Jamie Madill334d6152015-10-22 14:00:28 -04001832void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1833 GLsizei count,
1834 GLboolean transpose,
1835 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001836{
1837 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1838}
1839
Jamie Madill334d6152015-10-22 14:00:28 -04001840void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1841 GLsizei count,
1842 GLboolean transpose,
1843 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001844{
1845 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1846}
1847
Jamie Madill334d6152015-10-22 14:00:28 -04001848void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1849 GLsizei count,
1850 GLboolean transpose,
1851 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001852{
1853 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1854}
1855
Jamie Madill334d6152015-10-22 14:00:28 -04001856void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1857 GLsizei count,
1858 GLboolean transpose,
1859 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001860{
1861 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1862}
1863
Jamie Madill334d6152015-10-22 14:00:28 -04001864void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1865 GLsizei count,
1866 GLboolean transpose,
1867 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001868{
1869 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1870}
1871
Jamie Madill334d6152015-10-22 14:00:28 -04001872void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1873 GLsizei count,
1874 GLboolean transpose,
1875 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001876{
1877 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1878}
1879
1880void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1881{
1882 setUniform(location, count, v, GL_INT);
1883}
1884
1885void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1886{
1887 setUniform(location, count, v, GL_INT_VEC2);
1888}
1889
1890void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1891{
1892 setUniform(location, count, v, GL_INT_VEC3);
1893}
1894
1895void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1896{
1897 setUniform(location, count, v, GL_INT_VEC4);
1898}
1899
1900void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1901{
1902 setUniform(location, count, v, GL_UNSIGNED_INT);
1903}
1904
1905void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1906{
1907 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1908}
1909
1910void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1911{
1912 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1913}
1914
1915void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1916{
1917 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1918}
1919
Jamie Madill4a3c2342015-10-08 12:58:45 -04001920void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1921 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001922{
1923}
1924
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001925void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001926{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001927 D3DUniformMap uniformMap;
Xinghua Caob1239382016-12-13 15:07:05 +08001928 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1929 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05001930 {
Xinghua Caob1239382016-12-13 15:07:05 +08001931 for (const sh::Uniform &computeUniform : computeShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001932 {
Xinghua Caob1239382016-12-13 15:07:05 +08001933 if (computeUniform.staticUse)
1934 {
1935 defineUniformBase(computeShader, computeUniform, &uniformMap);
1936 }
Jamie Madillfb536032015-09-11 13:19:49 -04001937 }
1938 }
Xinghua Caob1239382016-12-13 15:07:05 +08001939 else
Jamie Madillfb536032015-09-11 13:19:49 -04001940 {
Xinghua Caob1239382016-12-13 15:07:05 +08001941 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1942 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001943 {
Xinghua Caob1239382016-12-13 15:07:05 +08001944 if (vertexUniform.staticUse)
1945 {
1946 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
1947 }
1948 }
1949
1950 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1951 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
1952 {
1953 if (fragmentUniform.staticUse)
1954 {
1955 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
1956 }
Jamie Madillfb536032015-09-11 13:19:49 -04001957 }
1958 }
1959
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001960 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001961 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001962 {
1963 if (!glUniform.isInDefaultBlock())
1964 continue;
1965
1966 auto mapEntry = uniformMap.find(glUniform.name);
1967 ASSERT(mapEntry != uniformMap.end());
1968 mD3DUniforms.push_back(mapEntry->second);
1969 }
1970
Jamie Madill62d31cb2015-09-11 13:25:51 -04001971 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001972 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001973}
1974
Jamie Madill91445bc2015-09-23 16:47:53 -04001975void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001976 const sh::Uniform &uniform,
1977 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001978{
Olli Etuaho96963162016-03-21 11:54:33 +02001979 // Samplers get their registers assigned in assignAllSamplerRegisters.
1980 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001981 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001982 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001983 return;
1984 }
1985
Jamie Madill91445bc2015-09-23 16:47:53 -04001986 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1987
1988 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1989 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04001990 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001991 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001992
Jamie Madill91445bc2015-09-23 16:47:53 -04001993 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001994}
1995
Jamie Madill62d31cb2015-09-11 13:25:51 -04001996D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1997{
1998 for (D3DUniform *d3dUniform : mD3DUniforms)
1999 {
2000 if (d3dUniform->name == name)
2001 {
2002 return d3dUniform;
2003 }
2004 }
2005
2006 return nullptr;
2007}
2008
Jamie Madill91445bc2015-09-23 16:47:53 -04002009void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002010 const sh::ShaderVariable &uniform,
2011 const std::string &fullName,
2012 sh::HLSLBlockEncoder *encoder,
2013 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002014{
2015 if (uniform.isStruct())
2016 {
2017 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2018 {
2019 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2020
Jamie Madill55def582015-05-04 11:24:57 -04002021 if (encoder)
2022 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002023
2024 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2025 {
Jamie Madill334d6152015-10-22 14:00:28 -04002026 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002027 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2028
Olli Etuaho96963162016-03-21 11:54:33 +02002029 // Samplers get their registers assigned in assignAllSamplerRegisters.
2030 // Also they couldn't use the same encoder as the rest of the struct, since they are
2031 // extracted out of the struct by the shader translator.
2032 if (gl::IsSamplerType(field.type))
2033 {
2034 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2035 }
2036 else
2037 {
2038 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2039 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002040 }
2041
Jamie Madill55def582015-05-04 11:24:57 -04002042 if (encoder)
2043 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002044 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002045 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002046 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002047
2048 // Not a struct. Arrays are treated as aggregate types.
2049 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002050 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002051 encoder->enterAggregateType();
2052 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002053
Jamie Madill62d31cb2015-09-11 13:25:51 -04002054 // Advance the uniform offset, to track registers allocation for structs
2055 sh::BlockMemberInfo blockInfo =
2056 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2057 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002058
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002059 auto uniformMapEntry = uniformMap->find(fullName);
2060 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002061
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002062 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002063 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002064 d3dUniform = uniformMapEntry->second;
2065 }
2066 else
2067 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002068 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002069 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002070 }
2071
2072 if (encoder)
2073 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002074 d3dUniform->registerElement =
2075 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002076 unsigned int reg =
2077 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002078 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002079 {
2080 d3dUniform->psRegisterIndex = reg;
2081 }
Xinghua Caob1239382016-12-13 15:07:05 +08002082 else if (shaderType == GL_VERTEX_SHADER)
2083 {
2084 d3dUniform->vsRegisterIndex = reg;
2085 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002086 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002087 {
Xinghua Caob1239382016-12-13 15:07:05 +08002088 ASSERT(shaderType == GL_COMPUTE_SHADER);
2089 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002090 }
Jamie Madillfb536032015-09-11 13:19:49 -04002091
2092 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002093 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002094 {
2095 encoder->exitAggregateType();
2096 }
2097 }
2098}
2099
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002100template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002101void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002102{
Jamie Madill334d6152015-10-22 14:00:28 -04002103 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002104 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2105
Jamie Madill62d31cb2015-09-11 13:25:51 -04002106 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002107
Jamie Madill62d31cb2015-09-11 13:25:51 -04002108 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002109 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002110 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002111
2112 if (targetUniform->type == targetUniformType)
2113 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002114 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002115
Jamie Madill62d31cb2015-09-11 13:25:51 -04002116 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002117 {
Jamie Madill334d6152015-10-22 14:00:28 -04002118 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002119 const T *source = v + (i * components);
2120
2121 for (int c = 0; c < components; c++)
2122 {
2123 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2124 }
2125 for (int c = components; c < 4; c++)
2126 {
2127 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2128 }
2129 }
2130 }
2131 else if (targetUniform->type == targetBoolType)
2132 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002133 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002134
Jamie Madill62d31cb2015-09-11 13:25:51 -04002135 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002136 {
Jamie Madill334d6152015-10-22 14:00:28 -04002137 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002138 const T *source = v + (i * components);
2139
2140 for (int c = 0; c < components; c++)
2141 {
Jamie Madill334d6152015-10-22 14:00:28 -04002142 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2143 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002144 }
2145 for (int c = components; c < 4; c++)
2146 {
2147 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2148 }
2149 }
2150 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002151 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002152 {
2153 ASSERT(targetUniformType == GL_INT);
2154
Jamie Madill62d31cb2015-09-11 13:25:51 -04002155 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002156
2157 bool wasDirty = targetUniform->dirty;
2158
Jamie Madill62d31cb2015-09-11 13:25:51 -04002159 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002160 {
Jamie Madill334d6152015-10-22 14:00:28 -04002161 GLint *dest = target + (i * 4);
2162 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002163
2164 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2165 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2166 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2167 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2168 }
2169
2170 if (!wasDirty && targetUniform->dirty)
2171 {
2172 mDirtySamplerMapping = true;
2173 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002174 }
Jamie Madill334d6152015-10-22 14:00:28 -04002175 else
2176 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002177}
2178
2179template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002180void ProgramD3D::setUniformMatrixfv(GLint location,
2181 GLsizei countIn,
2182 GLboolean transpose,
2183 const GLfloat *value,
2184 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002185{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002186 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002187
Jamie Madill62d31cb2015-09-11 13:25:51 -04002188 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002189 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002190 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002191
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002192 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002193 GLfloat *target =
2194 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002195
Jamie Madill62d31cb2015-09-11 13:25:51 -04002196 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002197 {
2198 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2199 if (transpose == GL_FALSE)
2200 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002201 targetUniform->dirty =
2202 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002203 }
2204 else
2205 {
Jamie Madill334d6152015-10-22 14:00:28 -04002206 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002207 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002208 }
2209 target += targetMatrixStride;
2210 value += cols * rows;
2211 }
2212}
2213
Jamie Madill4a3c2342015-10-08 12:58:45 -04002214size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002215{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002216 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002217
Jamie Madill62d31cb2015-09-11 13:25:51 -04002218 // define member uniforms
2219 sh::Std140BlockEncoder std140Encoder;
Jamie Madillcc2ed612017-03-14 15:59:00 -04002220 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002221 sh::BlockLayoutEncoder *encoder = nullptr;
2222
2223 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002224 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002225 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002226 }
2227 else
2228 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002229 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002230 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002231
Jamie Madill39046162016-02-08 15:05:17 -05002232 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2233 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002234
2235 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002236}
2237
Jamie Madill62d31cb2015-09-11 13:25:51 -04002238void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002239{
Olli Etuaho96963162016-03-21 11:54:33 +02002240 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002241 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002242 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002243 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002244 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002245 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002246 }
2247}
2248
Olli Etuaho96963162016-03-21 11:54:33 +02002249void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002250{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002251 ASSERT(d3dUniform->isSampler());
Xinghua Caob1239382016-12-13 15:07:05 +08002252 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2253 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002254 {
Xinghua Caob1239382016-12-13 15:07:05 +08002255 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2256 ASSERT(computeShaderD3D->hasUniform(d3dUniform));
2257 d3dUniform->csRegisterIndex = computeShaderD3D->getUniformRegister(d3dUniform->name);
2258 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2259 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2260 mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002261 }
Xinghua Caob1239382016-12-13 15:07:05 +08002262 else
Jamie Madillfb536032015-09-11 13:19:49 -04002263 {
Xinghua Caob1239382016-12-13 15:07:05 +08002264 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2265 const ShaderD3D *fragmentShaderD3D =
2266 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2267 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) ||
2268 fragmentShaderD3D->hasUniform(d3dUniform));
2269 if (vertexShaderD3D->hasUniform(d3dUniform))
2270 {
2271 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2272 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
2273 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2274 mSamplersVS, &mUsedVertexSamplerRange);
2275 }
2276 if (fragmentShaderD3D->hasUniform(d3dUniform))
2277 {
2278 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2279 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
2280 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2281 mSamplersPS, &mUsedPixelSamplerRange);
2282 }
Jamie Madillfb536032015-09-11 13:19:49 -04002283 }
2284}
2285
Jamie Madill62d31cb2015-09-11 13:25:51 -04002286// static
2287void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002288 GLenum samplerType,
2289 unsigned int samplerCount,
2290 std::vector<Sampler> &outSamplers,
2291 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002292{
2293 unsigned int samplerIndex = startSamplerIndex;
2294
2295 do
2296 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002297 ASSERT(samplerIndex < outSamplers.size());
2298 Sampler *sampler = &outSamplers[samplerIndex];
2299 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002300 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002301 sampler->logicalTextureUnit = 0;
2302 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002303 samplerIndex++;
2304 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002305}
2306
Brandon Jonesc9610c52014-08-25 17:02:59 -07002307void ProgramD3D::reset()
2308{
Xinghua Caob1239382016-12-13 15:07:05 +08002309 mVertexExecutables.clear();
2310 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002311
Xinghua Caob1239382016-12-13 15:07:05 +08002312 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002313 {
Xinghua Caob1239382016-12-13 15:07:05 +08002314 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002315 }
Brandon Joneseb994362014-09-24 10:27:28 -07002316
Xinghua Caob1239382016-12-13 15:07:05 +08002317 mComputeExecutable.reset(nullptr);
2318
Brandon Jones22502d52014-08-29 16:58:36 -07002319 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002320 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002321
2322 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002323 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002324 mUsesFragDepth = false;
2325 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002326 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002327 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002328
Jamie Madill62d31cb2015-09-11 13:25:51 -04002329 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002330 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002331
Xinghua Caob1239382016-12-13 15:07:05 +08002332 mVertexUniformStorage.reset(nullptr);
2333 mFragmentUniformStorage.reset(nullptr);
2334 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002335
2336 mSamplersPS.clear();
2337 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002338 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002339
2340 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002341 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002342 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002343 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002344
Jamie Madill8047c0d2016-03-07 13:02:12 -05002345 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002346
Jamie Madill9fc36822015-11-18 13:08:07 -05002347 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002348
2349 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002350}
2351
Geoff Lang7dd2e102014-11-10 15:19:26 -05002352unsigned int ProgramD3D::getSerial() const
2353{
2354 return mSerial;
2355}
2356
2357unsigned int ProgramD3D::issueSerial()
2358{
2359 return mCurrentSerial++;
2360}
2361
Jamie Madill8047c0d2016-03-07 13:02:12 -05002362void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002363{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002364 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002365 ASSERT(vertexShader != nullptr);
2366
2367 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002368 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002369 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002370 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2371 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002372
Jamie Madill8047c0d2016-03-07 13:02:12 -05002373 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002374 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002375 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002376 }
2377 }
Jamie Madill437d2662014-12-05 14:23:35 -05002378}
2379
Jamie Madill63805b42015-08-25 13:17:39 -04002380void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002381{
Jamie Madillbd136f92015-08-10 14:51:37 -04002382 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002383 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002384
Jamie Madill6de51852017-04-12 09:53:01 -04002385 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002386 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002387 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002388
Jamie Madill8047c0d2016-03-07 13:02:12 -05002389 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002390 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002391 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002392 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002393 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002394 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002395 mCachedInputLayout[d3dSemantic] =
2396 GetVertexFormatType(vertexAttributes[locationIndex],
2397 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002398 }
2399 }
2400}
2401
Jamie Madill192745a2016-12-22 15:58:21 -05002402void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2403 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002404{
Jamie Madill9fc36822015-11-18 13:08:07 -05002405 const std::string &varyingSemantic =
2406 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2407
Jamie Madillccdf74b2015-08-18 10:46:12 -04002408 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002409 mStreamOutVaryings.clear();
2410
Jamie Madill48ef11b2016-04-27 15:21:52 -04002411 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002412 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2413 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002414 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002415 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2416 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002417 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002418 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002419 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002420 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2421 builtins.glPosition.index, 4, outputSlot));
2422 }
2423 }
2424 else if (tfVaryingName == "gl_FragCoord")
2425 {
2426 if (builtins.glFragCoord.enabled)
2427 {
2428 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2429 builtins.glFragCoord.index, 4, outputSlot));
2430 }
2431 }
2432 else if (tfVaryingName == "gl_PointSize")
2433 {
2434 if (builtins.glPointSize.enabled)
2435 {
2436 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2437 }
2438 }
2439 else
2440 {
jchen10a9042d32017-03-17 08:50:45 +08002441 size_t subscript = GL_INVALID_INDEX;
2442 std::string baseName = gl::ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002443 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002444 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002445 const auto &varying = *registerInfo.packedVarying->varying;
2446 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002447 int componentCount = gl::VariableColumnCount(transposedType);
2448 ASSERT(!varying.isBuiltIn());
2449
Jamie Madill55c25d02015-11-18 13:08:08 -05002450 // Transform feedback for varying structs is underspecified.
2451 // See Khronos bug 9856.
2452 // TODO(jmadill): Figure out how to be spec-compliant here.
2453 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2454 continue;
2455
Jamie Madill9fc36822015-11-18 13:08:07 -05002456 // There can be more than one register assigned to a particular varying, and each
2457 // register needs its own stream out entry.
jchen10a9042d32017-03-17 08:50:45 +08002458 if (baseName == registerInfo.packedVarying->varying->name &&
2459 (subscript == GL_INVALID_INDEX || subscript == registerInfo.varyingArrayIndex))
Jamie Madill9fc36822015-11-18 13:08:07 -05002460 {
2461 mStreamOutVaryings.push_back(D3DVarying(
2462 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2463 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002464 }
2465 }
2466 }
2467}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002468
2469D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2470{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002471 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002472}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002473
2474bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2475{
2476 std::string baseName = blockName;
2477 gl::ParseAndStripArrayIndex(&baseName);
2478
2479 auto sizeIter = mBlockDataSizes.find(baseName);
2480 if (sizeIter == mBlockDataSizes.end())
2481 {
2482 *sizeOut = 0;
2483 return false;
2484 }
2485
2486 *sizeOut = sizeIter->second;
2487 return true;
2488}
2489
2490bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2491 sh::BlockMemberInfo *memberInfoOut) const
2492{
2493 auto infoIter = mBlockInfo.find(memberUniformName);
2494 if (infoIter == mBlockInfo.end())
2495 {
2496 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2497 return false;
2498 }
2499
2500 *memberInfoOut = infoIter->second;
2501 return true;
2502}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002503
2504void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2505 GLenum genMode,
2506 GLint components,
2507 const GLfloat *coeffs)
2508{
2509 UNREACHABLE();
2510}
2511
Jamie Madill8047c0d2016-03-07 13:02:12 -05002512} // namespace rx