blob: 07db179e732335ab6935b6e95d660c44943c7857 [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Dian Xianga4928832015-09-15 10:11:17 -070011#include "common/BitSetIterator.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Framebuffer.h"
14#include "libANGLE/FramebufferAttachment.h"
15#include "libANGLE/Program.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040016#include "libANGLE/Uniform.h"
Jamie Madill192745a2016-12-22 15:58:21 -050017#include "libANGLE/VaryingPacking.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040018#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000019#include "libANGLE/features.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050020#include "libANGLE/renderer/ContextImpl.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050021#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050022#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050023#include "libANGLE/renderer/d3d/RendererD3D.h"
24#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050025#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050026#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050027
Jamie Madill01074252016-11-28 15:55:51 -050028using namespace angle;
29
Brandon Jonesc9610c52014-08-25 17:02:59 -070030namespace rx
31{
32
Brandon Joneseb994362014-09-24 10:27:28 -070033namespace
34{
35
Jamie Madillf8dd7b12015-08-05 13:50:08 -040036gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070037{
Jamie Madillbd136f92015-08-10 14:51:37 -040038 gl::InputLayout defaultLayout;
39 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070040 {
Brandon Joneseb994362014-09-24 10:27:28 -070041 if (shaderAttr.type != GL_NONE)
42 {
43 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
44
Jamie Madilld3dfda22015-07-06 08:28:49 -040045 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040046 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070047 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040048 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040049 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040050 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040051 gl::VertexFormatType defaultType =
52 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070053
Jamie Madillbd136f92015-08-10 14:51:37 -040054 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070055 }
56 }
57 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040058
59 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070060}
61
Jamie Madill334d6152015-10-22 14:00:28 -040062std::vector<GLenum> GetDefaultOutputLayoutFromShader(
63 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070064{
Jamie Madillb4463142014-12-19 14:56:54 -050065 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070066
Jamie Madillb4463142014-12-19 14:56:54 -050067 if (!shaderOutputVars.empty())
68 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070069 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
70 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050071 }
Brandon Joneseb994362014-09-24 10:27:28 -070072
73 return defaultPixelOutput;
74}
75
Brandon Jones1a8a7e32014-10-01 12:49:30 -070076bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
77{
78 return var.isRowMajorLayout;
79}
80
81bool IsRowMajorLayout(const sh::ShaderVariable &var)
82{
83 return false;
84}
85
Jamie Madill62d31cb2015-09-11 13:25:51 -040086template <typename VarT>
87void GetUniformBlockInfo(const std::vector<VarT> &fields,
88 const std::string &prefix,
89 sh::BlockLayoutEncoder *encoder,
90 bool inRowMajorLayout,
91 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
92{
93 for (const VarT &field : fields)
94 {
95 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
96
97 if (field.isStruct())
98 {
99 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
100
101 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
102 {
103 encoder->enterAggregateType();
104
105 const std::string uniformElementName =
106 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
107 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
108 blockInfoOut);
109
110 encoder->exitAggregateType();
111 }
112 }
113 else
114 {
115 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
116 (*blockInfoOut)[fieldName] =
117 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
118 }
119 }
120}
121
Jamie Madill334d6152015-10-22 14:00:28 -0400122template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100123static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
124{
125 ASSERT(dest != NULL);
126 ASSERT(dirtyFlag != NULL);
127
128 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
129 *dest = source;
130}
131
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800132template <typename T, int cols, int rows>
133bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400134{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800135 constexpr int targetWidth = 4;
136 constexpr int targetHeight = rows;
137 constexpr int srcWidth = rows;
138 constexpr int srcHeight = cols;
139
140 constexpr int copyWidth = std::min(targetHeight, srcWidth);
141 constexpr int copyHeight = std::min(targetWidth, srcHeight);
142
143 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400144
145 for (int x = 0; x < copyWidth; x++)
146 {
147 for (int y = 0; y < copyHeight; y++)
148 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800149 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400150 }
151 }
152
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800153 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
154 {
155 return false;
156 }
157
158 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
159 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400160}
161
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800162template <typename T, int cols, int rows>
163bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400164{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800165 constexpr int targetWidth = 4;
166 constexpr int targetHeight = rows;
167 constexpr int srcWidth = cols;
168 constexpr int srcHeight = rows;
169
170 constexpr int copyWidth = std::min(targetWidth, srcWidth);
171 constexpr int copyHeight = std::min(targetHeight, srcHeight);
172
173 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400174
175 for (int y = 0; y < copyHeight; y++)
176 {
177 for (int x = 0; x < copyWidth; x++)
178 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800179 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400180 }
181 }
182
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800183 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
184 {
185 return false;
186 }
187
188 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
189 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400190}
191
Jamie Madill4e31ad52015-10-29 10:32:57 -0400192gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
193{
194 switch (drawMode)
195 {
196 // Uses the point sprite geometry shader.
197 case GL_POINTS:
198 return gl::PRIMITIVE_POINTS;
199
200 // All line drawing uses the same geometry shader.
201 case GL_LINES:
202 case GL_LINE_STRIP:
203 case GL_LINE_LOOP:
204 return gl::PRIMITIVE_LINES;
205
206 // The triangle fan primitive is emulated with strips in D3D11.
207 case GL_TRIANGLES:
208 case GL_TRIANGLE_FAN:
209 return gl::PRIMITIVE_TRIANGLES;
210
211 // Special case for triangle strips.
212 case GL_TRIANGLE_STRIP:
213 return gl::PRIMITIVE_TRIANGLE_STRIP;
214
215 default:
216 UNREACHABLE();
217 return gl::PRIMITIVE_TYPE_MAX;
218 }
219}
220
Jamie Madill192745a2016-12-22 15:58:21 -0500221bool FindFlatInterpolationVarying(const std::vector<sh::Varying> &varyings)
222{
223 // Note: this assumes nested structs can only be packed with one interpolation.
224 for (const auto &varying : varyings)
225 {
226 if (varying.interpolation == sh::INTERPOLATION_FLAT)
227 {
228 return true;
229 }
230 }
231
232 return false;
233}
234
Jamie Madillada9ecc2015-08-17 12:53:37 -0400235} // anonymous namespace
236
Jamie Madill28afae52015-11-09 15:07:57 -0500237// D3DUniform Implementation
238
Jamie Madill62d31cb2015-09-11 13:25:51 -0400239D3DUniform::D3DUniform(GLenum typeIn,
240 const std::string &nameIn,
241 unsigned int arraySizeIn,
242 bool defaultBlock)
243 : type(typeIn),
244 name(nameIn),
245 arraySize(arraySizeIn),
246 data(nullptr),
247 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400248 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400249 psRegisterIndex(GL_INVALID_INDEX),
Xinghua Caob1239382016-12-13 15:07:05 +0800250 csRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400251 registerCount(0),
252 registerElement(0)
253{
254 // We use data storage for default block uniforms to cache values that are sent to D3D during
255 // rendering
256 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
257 if (defaultBlock)
258 {
259 size_t bytes = gl::VariableInternalSize(type) * elementCount();
260 data = new uint8_t[bytes];
261 memset(data, 0, bytes);
262
Jamie Madillcc2ed612017-03-14 15:59:00 -0400263 // Use the row count as register count, will work for non-square matrices.
Jamie Madill62d31cb2015-09-11 13:25:51 -0400264 registerCount = gl::VariableRowCount(type) * elementCount();
265 }
266}
267
268D3DUniform::~D3DUniform()
269{
270 SafeDeleteArray(data);
271}
272
273bool D3DUniform::isSampler() const
274{
275 return gl::IsSamplerType(type);
276}
277
278bool D3DUniform::isReferencedByVertexShader() const
279{
280 return vsRegisterIndex != GL_INVALID_INDEX;
281}
282
283bool D3DUniform::isReferencedByFragmentShader() const
284{
285 return psRegisterIndex != GL_INVALID_INDEX;
286}
287
Xinghua Caob1239382016-12-13 15:07:05 +0800288bool D3DUniform::isReferencedByComputeShader() const
289{
290 return csRegisterIndex != GL_INVALID_INDEX;
291}
292
Jamie Madill28afae52015-11-09 15:07:57 -0500293// D3DVarying Implementation
294
Jamie Madill9fc36822015-11-18 13:08:07 -0500295D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500296{
297}
298
Jamie Madill9fc36822015-11-18 13:08:07 -0500299D3DVarying::D3DVarying(const std::string &semanticNameIn,
300 unsigned int semanticIndexIn,
301 unsigned int componentCountIn,
302 unsigned int outputSlotIn)
303 : semanticName(semanticNameIn),
304 semanticIndex(semanticIndexIn),
305 componentCount(componentCountIn),
306 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500307{
308}
309
Jamie Madille39a3f02015-11-17 20:42:15 -0500310// ProgramD3DMetadata Implementation
311
Jamie Madillc9bde922016-07-24 17:58:50 -0400312ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500313 const ShaderD3D *vertexShader,
314 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400315 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
316 mShaderModelSuffix(renderer->getShaderModelSuffix()),
317 mUsesInstancedPointSpriteEmulation(
318 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
319 mUsesViewScale(renderer->presentPathFastEnabled()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500320 mVertexShader(vertexShader),
321 mFragmentShader(fragmentShader)
322{
323}
324
325int ProgramD3DMetadata::getRendererMajorShaderModel() const
326{
327 return mRendererMajorShaderModel;
328}
329
Jamie Madill9082b982016-04-27 15:21:51 -0400330bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500331{
Martin Radev1be913c2016-07-11 17:59:16 +0300332 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500333}
334
Jamie Madill48ef11b2016-04-27 15:21:52 -0400335bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500336{
Jamie Madill63286672015-11-24 13:00:08 -0500337 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500338}
339
340bool ProgramD3DMetadata::usesPointCoord() const
341{
342 return mFragmentShader->usesPointCoord();
343}
344
345bool ProgramD3DMetadata::usesFragCoord() const
346{
347 return mFragmentShader->usesFragCoord();
348}
349
350bool ProgramD3DMetadata::usesPointSize() const
351{
352 return mVertexShader->usesPointSize();
353}
354
355bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
356{
Jamie Madillc9bde922016-07-24 17:58:50 -0400357 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
358 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500359}
360
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800361bool ProgramD3DMetadata::usesViewScale() const
362{
363 return mUsesViewScale;
364}
365
Jamie Madille39a3f02015-11-17 20:42:15 -0500366bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
367{
Jamie Madillc9bde922016-07-24 17:58:50 -0400368 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500369 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400370 // Even with a geometry shader, the app can render triangles or lines and reference
371 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
372 // simplicity, we always add this to the vertex shader when the fragment shader
373 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500374 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
375 usesInsertedPointCoordValue();
376}
377
378bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
379{
380 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
381 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
382 // the vertex shader in this case. This saves us 1 output vector.
383 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
384}
385
386bool ProgramD3DMetadata::usesSystemValuePointSize() const
387{
388 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
389}
390
391bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
392{
393 return mFragmentShader->usesMultipleRenderTargets();
394}
395
396GLint ProgramD3DMetadata::getMajorShaderVersion() const
397{
398 return mVertexShader->getData().getShaderVersion();
399}
400
401const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
402{
403 return mFragmentShader;
404}
405
Jamie Madill28afae52015-11-09 15:07:57 -0500406// ProgramD3D Implementation
407
Jamie Madilld3dfda22015-07-06 08:28:49 -0400408ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
409 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500410 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400411 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700412{
Brandon Joneseb994362014-09-24 10:27:28 -0700413}
414
415ProgramD3D::VertexExecutable::~VertexExecutable()
416{
417 SafeDelete(mShaderExecutable);
418}
419
Jamie Madilld3dfda22015-07-06 08:28:49 -0400420// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500421ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
422 GLenum type)
423{
424 switch (type)
425 {
426 case GL_INT:
427 return HLSLAttribType::SIGNED_INT;
428 case GL_UNSIGNED_INT:
429 return HLSLAttribType::UNSIGNED_INT;
430 case GL_SIGNED_NORMALIZED:
431 case GL_UNSIGNED_NORMALIZED:
432 case GL_FLOAT:
433 return HLSLAttribType::FLOAT;
434 default:
435 UNREACHABLE();
436 return HLSLAttribType::FLOAT;
437 }
438}
439
440// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400441void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
442 const gl::InputLayout &inputLayout,
443 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700444{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500445 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400446
447 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700448 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400449 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500450 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
451 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400452
Jamie Madillbdec2f42016-03-02 16:35:32 -0500453 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
454 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
455 continue;
456
457 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
458 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700459 }
Brandon Joneseb994362014-09-24 10:27:28 -0700460}
461
Jamie Madilld3dfda22015-07-06 08:28:49 -0400462bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
463{
Jamie Madillbd136f92015-08-10 14:51:37 -0400464 size_t limit = std::max(mSignature.size(), signature.size());
465 for (size_t index = 0; index < limit; ++index)
466 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500467 // treat undefined indexes as FLOAT
468 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
469 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400470 if (a != b)
471 return false;
472 }
473
474 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400475}
476
477ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
478 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400479 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700480{
481}
482
483ProgramD3D::PixelExecutable::~PixelExecutable()
484{
485 SafeDelete(mShaderExecutable);
486}
487
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700488ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
489{
490}
491
Geoff Lang7dd2e102014-11-10 15:19:26 -0500492unsigned int ProgramD3D::mCurrentSerial = 1;
493
Jamie Madill48ef11b2016-04-27 15:21:52 -0400494ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
495 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700496 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800497 mDynamicHLSL(nullptr),
498 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
499 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700500 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400501 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800502 mVertexUniformStorage(nullptr),
503 mFragmentUniformStorage(nullptr),
504 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700505 mUsedVertexSamplerRange(0),
506 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800507 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700508 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500509 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700510{
Brandon Joneseb994362014-09-24 10:27:28 -0700511 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700512}
513
514ProgramD3D::~ProgramD3D()
515{
516 reset();
517 SafeDelete(mDynamicHLSL);
518}
519
Brandon Jones44151a92014-09-10 11:32:25 -0700520bool ProgramD3D::usesPointSpriteEmulation() const
521{
522 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
523}
524
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400525bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700526{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400527 if (drawMode != GL_POINTS)
528 {
529 return mUsesFlatInterpolation;
530 }
531
Cooper Partine6664f02015-01-09 16:22:24 -0800532 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
533}
534
535bool ProgramD3D::usesInstancedPointSpriteEmulation() const
536{
537 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700538}
539
Jamie Madill334d6152015-10-22 14:00:28 -0400540GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
541 unsigned int samplerIndex,
542 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700543{
544 GLint logicalTextureUnit = -1;
545
546 switch (type)
547 {
Jamie Madill334d6152015-10-22 14:00:28 -0400548 case gl::SAMPLER_PIXEL:
549 ASSERT(samplerIndex < caps.maxTextureImageUnits);
550 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
551 {
552 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
553 }
554 break;
555 case gl::SAMPLER_VERTEX:
556 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
557 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
558 {
559 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
560 }
561 break;
Xinghua Caob1239382016-12-13 15:07:05 +0800562 case gl::SAMPLER_COMPUTE:
563 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
564 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
565 {
566 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
567 }
568 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400569 default:
570 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700571 }
572
Jamie Madill334d6152015-10-22 14:00:28 -0400573 if (logicalTextureUnit >= 0 &&
574 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700575 {
576 return logicalTextureUnit;
577 }
578
579 return -1;
580}
581
582// Returns the texture type for a given Direct3D 9 sampler type and
583// index (0-15 for the pixel shader and 0-3 for the vertex shader).
584GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
585{
586 switch (type)
587 {
Jamie Madill334d6152015-10-22 14:00:28 -0400588 case gl::SAMPLER_PIXEL:
589 ASSERT(samplerIndex < mSamplersPS.size());
590 ASSERT(mSamplersPS[samplerIndex].active);
591 return mSamplersPS[samplerIndex].textureType;
592 case gl::SAMPLER_VERTEX:
593 ASSERT(samplerIndex < mSamplersVS.size());
594 ASSERT(mSamplersVS[samplerIndex].active);
595 return mSamplersVS[samplerIndex].textureType;
Xinghua Caob1239382016-12-13 15:07:05 +0800596 case gl::SAMPLER_COMPUTE:
597 ASSERT(samplerIndex < mSamplersCS.size());
598 ASSERT(mSamplersCS[samplerIndex].active);
599 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400600 default:
601 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700602 }
603
604 return GL_TEXTURE_2D;
605}
606
Olli Etuaho618bebc2016-01-15 16:40:00 +0200607GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700608{
609 switch (type)
610 {
Jamie Madill334d6152015-10-22 14:00:28 -0400611 case gl::SAMPLER_PIXEL:
612 return mUsedPixelSamplerRange;
613 case gl::SAMPLER_VERTEX:
614 return mUsedVertexSamplerRange;
Xinghua Caob1239382016-12-13 15:07:05 +0800615 case gl::SAMPLER_COMPUTE:
616 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400617 default:
618 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200619 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700620 }
621}
622
623void ProgramD3D::updateSamplerMapping()
624{
625 if (!mDirtySamplerMapping)
626 {
627 return;
628 }
629
630 mDirtySamplerMapping = false;
631
632 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400633 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700634 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400635 if (!d3dUniform->dirty)
636 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700637
Jamie Madill62d31cb2015-09-11 13:25:51 -0400638 if (!d3dUniform->isSampler())
639 continue;
640
641 int count = d3dUniform->elementCount();
642 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
643
644 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700645 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400646 unsigned int firstIndex = d3dUniform->psRegisterIndex;
647
648 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700649 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400650 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700651
Jamie Madill62d31cb2015-09-11 13:25:51 -0400652 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700653 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400654 ASSERT(mSamplersPS[samplerIndex].active);
655 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700656 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400657 }
658 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700659
Jamie Madill62d31cb2015-09-11 13:25:51 -0400660 if (d3dUniform->isReferencedByVertexShader())
661 {
662 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
663
664 for (int i = 0; i < count; i++)
665 {
666 unsigned int samplerIndex = firstIndex + i;
667
668 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700669 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400670 ASSERT(mSamplersVS[samplerIndex].active);
671 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700672 }
673 }
674 }
Xinghua Caob1239382016-12-13 15:07:05 +0800675
676 if (d3dUniform->isReferencedByComputeShader())
677 {
678 unsigned int firstIndex = d3dUniform->csRegisterIndex;
679
680 for (int i = 0; i < count; i++)
681 {
682 unsigned int samplerIndex = firstIndex + i;
683
684 if (samplerIndex < mSamplersCS.size())
685 {
686 ASSERT(mSamplersCS[samplerIndex].active);
687 mSamplersCS[samplerIndex].logicalTextureUnit = v[i][0];
688 }
689 }
690 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700691 }
692}
693
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500694LinkResult ProgramD3D::load(const ContextImpl *contextImpl,
695 gl::InfoLog &infoLog,
696 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700697{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500698 // TODO(jmadill): Use Renderer from contextImpl.
699
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700 reset();
701
Jamie Madill334d6152015-10-22 14:00:28 -0400702 DeviceIdentifier binaryDeviceIdentifier = {0};
703 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
704 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700705
706 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
707 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
708 {
709 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500710 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700711 }
712
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500713 int compileFlags = stream->readInt<int>();
714 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
715 {
Jamie Madillf6113162015-05-07 11:49:21 -0400716 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500717 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500718 }
719
Jamie Madill8047c0d2016-03-07 13:02:12 -0500720 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400721 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500722 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400723 }
724
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700725 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
726 for (unsigned int i = 0; i < psSamplerCount; ++i)
727 {
728 Sampler sampler;
729 stream->readBool(&sampler.active);
730 stream->readInt(&sampler.logicalTextureUnit);
731 stream->readInt(&sampler.textureType);
732 mSamplersPS.push_back(sampler);
733 }
734 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
735 for (unsigned int i = 0; i < vsSamplerCount; ++i)
736 {
737 Sampler sampler;
738 stream->readBool(&sampler.active);
739 stream->readInt(&sampler.logicalTextureUnit);
740 stream->readInt(&sampler.textureType);
741 mSamplersVS.push_back(sampler);
742 }
743
Xinghua Caob1239382016-12-13 15:07:05 +0800744 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
745 for (unsigned int i = 0; i < csSamplerCount; ++i)
746 {
747 Sampler sampler;
748 stream->readBool(&sampler.active);
749 stream->readInt(&sampler.logicalTextureUnit);
750 stream->readInt(&sampler.textureType);
751 mSamplersCS.push_back(sampler);
752 }
753
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700754 stream->readInt(&mUsedVertexSamplerRange);
755 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800756 stream->readInt(&mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700757
758 const unsigned int uniformCount = stream->readInt<unsigned int>();
759 if (stream->error())
760 {
Jamie Madillf6113162015-05-07 11:49:21 -0400761 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500762 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700763 }
764
Jamie Madill48ef11b2016-04-27 15:21:52 -0400765 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400766 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700767 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
768 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400769 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700770
Jamie Madill62d31cb2015-09-11 13:25:51 -0400771 D3DUniform *d3dUniform =
772 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
773 linkedUniform.isInDefaultBlock());
774 stream->readInt(&d3dUniform->psRegisterIndex);
775 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800776 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400777 stream->readInt(&d3dUniform->registerCount);
778 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700779
Jamie Madill62d31cb2015-09-11 13:25:51 -0400780 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700781 }
782
Jamie Madill4a3c2342015-10-08 12:58:45 -0400783 const unsigned int blockCount = stream->readInt<unsigned int>();
784 if (stream->error())
785 {
786 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500787 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400788 }
789
790 ASSERT(mD3DUniformBlocks.empty());
791 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
792 {
793 D3DUniformBlock uniformBlock;
794 stream->readInt(&uniformBlock.psRegisterIndex);
795 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800796 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400797 mD3DUniformBlocks.push_back(uniformBlock);
798 }
799
Jamie Madill9fc36822015-11-18 13:08:07 -0500800 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
801 mStreamOutVaryings.resize(streamOutVaryingCount);
802 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700803 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500804 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700805
Jamie Madill28afae52015-11-09 15:07:57 -0500806 stream->readString(&varying->semanticName);
807 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500808 stream->readInt(&varying->componentCount);
809 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700810 }
811
Brandon Jones22502d52014-08-29 16:58:36 -0700812 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400813 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500814 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700815 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400816 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500817 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700818 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700819 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400820 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700821
822 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
823 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400824 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
825 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700826 {
827 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
828 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
829 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
830 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
831 }
832
Jamie Madill4e31ad52015-10-29 10:32:57 -0400833 stream->readString(&mGeometryShaderPreamble);
834
Jamie Madill334d6152015-10-22 14:00:28 -0400835 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700836
Jamie Madillb0a838b2016-11-13 20:02:12 -0500837 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
838
Brandon Joneseb994362014-09-24 10:27:28 -0700839 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400840 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
841 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700842 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400843 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400844 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700845
Jamie Madilld3dfda22015-07-06 08:28:49 -0400846 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700847 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400848 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700849 }
850
Jamie Madill334d6152015-10-22 14:00:28 -0400851 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700852 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400853
Jamie Madillada9ecc2015-08-17 12:53:37 -0400854 ShaderExecutableD3D *shaderExecutable = nullptr;
855
Jamie Madillb0a838b2016-11-13 20:02:12 -0500856 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
857 mStreamOutVaryings, separateAttribs,
858 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400859
Brandon Joneseb994362014-09-24 10:27:28 -0700860 if (!shaderExecutable)
861 {
Jamie Madillf6113162015-05-07 11:49:21 -0400862 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500863 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700864 }
865
866 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400867 VertexExecutable::Signature signature;
868 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700869
870 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800871 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
872 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700873
874 stream->skip(vertexShaderSize);
875 }
876
877 const size_t pixelShaderCount = stream->readInt<unsigned int>();
878 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
879 {
880 const size_t outputCount = stream->readInt<unsigned int>();
881 std::vector<GLenum> outputs(outputCount);
882 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
883 {
884 stream->readInt(&outputs[outputIndex]);
885 }
886
Jamie Madill334d6152015-10-22 14:00:28 -0400887 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700888 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400889 ShaderExecutableD3D *shaderExecutable = nullptr;
890
Jamie Madillb0a838b2016-11-13 20:02:12 -0500891 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
892 mStreamOutVaryings, separateAttribs,
893 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700894
895 if (!shaderExecutable)
896 {
Jamie Madillf6113162015-05-07 11:49:21 -0400897 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500898 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700899 }
900
901 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800902 mPixelExecutables.push_back(
903 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700904
905 stream->skip(pixelShaderSize);
906 }
907
Jamie Madill4e31ad52015-10-29 10:32:57 -0400908 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
909 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700910 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400911 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
912 if (geometryShaderSize == 0)
913 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400914 continue;
915 }
916
Brandon Joneseb994362014-09-24 10:27:28 -0700917 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400918
Xinghua Caob1239382016-12-13 15:07:05 +0800919 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -0500920 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
921 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
Xinghua Caob1239382016-12-13 15:07:05 +0800922 &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700923
Xinghua Caob1239382016-12-13 15:07:05 +0800924 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700925 {
Jamie Madillf6113162015-05-07 11:49:21 -0400926 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500927 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700928 }
Xinghua Caob1239382016-12-13 15:07:05 +0800929
930 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
931
Brandon Joneseb994362014-09-24 10:27:28 -0700932 stream->skip(geometryShaderSize);
933 }
934
Xinghua Caob1239382016-12-13 15:07:05 +0800935 unsigned int computeShaderSize = stream->readInt<unsigned int>();
936 if (computeShaderSize > 0)
937 {
938 const unsigned char *computeShaderFunction = binary + stream->offset();
939
940 ShaderExecutableD3D *computeExecutable = nullptr;
941 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
942 SHADER_COMPUTE, std::vector<D3DVarying>(), false,
943 &computeExecutable));
944
945 if (!computeExecutable)
946 {
947 infoLog << "Could not create compute shader.";
948 return false;
949 }
950
951 mComputeExecutable.reset(computeExecutable);
952 }
953
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700954 initializeUniformStorage();
955
Jamie Madillb0a838b2016-11-13 20:02:12 -0500956 return true;
Brandon Jones22502d52014-08-29 16:58:36 -0700957}
958
Geoff Langb543aff2014-09-30 14:52:54 -0400959gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700960{
Austin Kinross137b1512015-06-17 16:14:53 -0700961 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400962 // When we load the binary again later, we can validate the device identifier before trying to
963 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700964 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400965 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
966 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700967
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500968 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
969
Jamie Madill8047c0d2016-03-07 13:02:12 -0500970 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400971 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500972 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400973 }
974
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700975 stream->writeInt(mSamplersPS.size());
976 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
977 {
978 stream->writeInt(mSamplersPS[i].active);
979 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
980 stream->writeInt(mSamplersPS[i].textureType);
981 }
982
983 stream->writeInt(mSamplersVS.size());
984 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
985 {
986 stream->writeInt(mSamplersVS[i].active);
987 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
988 stream->writeInt(mSamplersVS[i].textureType);
989 }
990
Xinghua Caob1239382016-12-13 15:07:05 +0800991 stream->writeInt(mSamplersCS.size());
992 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
993 {
994 stream->writeInt(mSamplersCS[i].active);
995 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
996 stream->writeInt(mSamplersCS[i].textureType);
997 }
998
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700999 stream->writeInt(mUsedVertexSamplerRange);
1000 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001001 stream->writeInt(mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001002
Jamie Madill62d31cb2015-09-11 13:25:51 -04001003 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001004 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001005 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001006 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001007 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1008 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001009 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001010 stream->writeInt(uniform->registerCount);
1011 stream->writeInt(uniform->registerElement);
1012 }
1013
Jamie Madill51f522f2016-12-21 15:10:55 -05001014 // Ensure we init the uniform block structure data if we should.
1015 // http://anglebug.com/1637
1016 ensureUniformBlocksInitialized();
1017
Jamie Madill4a3c2342015-10-08 12:58:45 -04001018 stream->writeInt(mD3DUniformBlocks.size());
1019 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1020 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001021 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1022 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001023 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001024 }
1025
Jamie Madill9fc36822015-11-18 13:08:07 -05001026 stream->writeInt(mStreamOutVaryings.size());
1027 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001028 {
Brandon Joneseb994362014-09-24 10:27:28 -07001029 stream->writeString(varying.semanticName);
1030 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001031 stream->writeInt(varying.componentCount);
1032 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001033 }
1034
Brandon Jones22502d52014-08-29 16:58:36 -07001035 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001036 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001037 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001038 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001039 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001040 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001041 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001042 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001043 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001044
Brandon Joneseb994362014-09-24 10:27:28 -07001045 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001046 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001047 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1048 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001049 {
Brandon Joneseb994362014-09-24 10:27:28 -07001050 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001051 stream->writeInt(variable.type);
1052 stream->writeString(variable.name);
1053 stream->writeString(variable.source);
1054 stream->writeInt(variable.outputIndex);
1055 }
1056
Jamie Madill4e31ad52015-10-29 10:32:57 -04001057 stream->writeString(mGeometryShaderPreamble);
1058
Brandon Joneseb994362014-09-24 10:27:28 -07001059 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001060 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1061 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001062 {
Xinghua Caob1239382016-12-13 15:07:05 +08001063 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001064
Jamie Madilld3dfda22015-07-06 08:28:49 -04001065 const auto &inputLayout = vertexExecutable->inputs();
1066 stream->writeInt(inputLayout.size());
1067
1068 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001069 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001070 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001071 }
1072
1073 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1074 stream->writeInt(vertexShaderSize);
1075
1076 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1077 stream->writeBytes(vertexBlob, vertexShaderSize);
1078 }
1079
1080 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001081 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1082 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001083 {
Xinghua Caob1239382016-12-13 15:07:05 +08001084 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001085
1086 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1087 stream->writeInt(outputs.size());
1088 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1089 {
1090 stream->writeInt(outputs[outputIndex]);
1091 }
1092
1093 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1094 stream->writeInt(pixelShaderSize);
1095
1096 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1097 stream->writeBytes(pixelBlob, pixelShaderSize);
1098 }
1099
Xinghua Caob1239382016-12-13 15:07:05 +08001100 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001101 {
Xinghua Caob1239382016-12-13 15:07:05 +08001102 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001103 {
1104 stream->writeInt(0);
1105 continue;
1106 }
1107
Xinghua Caob1239382016-12-13 15:07:05 +08001108 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001109 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001110 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1111 }
1112
1113 if (mComputeExecutable)
1114 {
1115 size_t computeShaderSize = mComputeExecutable->getLength();
1116 stream->writeInt(computeShaderSize);
1117 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1118 }
1119 else
1120 {
1121 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001122 }
1123
Jamie Madill51f522f2016-12-21 15:10:55 -05001124 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001125}
1126
Geoff Langc5629752015-12-07 16:29:04 -05001127void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1128{
1129}
1130
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
Geoff Lang359ef262015-01-05 14:42:29 -05001179 ShaderExecutableD3D *pixelExecutable = NULL;
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
Geoff Lang359ef262015-01-05 14:42:29 -05001224 ShaderExecutableD3D *vertexExecutable = NULL;
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
1387LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1388 gl::InfoLog &infoLog)
1389{
1390 // Ensure the compiler is initialized to avoid race conditions.
1391 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1392
1393 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1394
1395 GetVertexExecutableTask vertexTask(this);
1396 GetPixelExecutableTask pixelTask(this);
1397 GetGeometryExecutableTask geometryTask(this, contextState);
1398
1399 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1400 workerPool->postWorkerTask(&pixelTask),
1401 workerPool->postWorkerTask(&geometryTask)}};
1402
1403 WaitableEvent::WaitMany(&waitEvents);
1404
1405 infoLog << vertexTask.getInfoLog().str();
1406 infoLog << pixelTask.getInfoLog().str();
1407 infoLog << geometryTask.getInfoLog().str();
1408
1409 ANGLE_TRY(vertexTask.getError());
1410 ANGLE_TRY(pixelTask.getError());
1411 ANGLE_TRY(geometryTask.getError());
1412
1413 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1414 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1415 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1416
Jamie Madill48ef11b2016-04-27 15:21:52 -04001417 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001418
1419 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001420 {
Jamie Madill334d6152015-10-22 14:00:28 -04001421 // Geometry shaders are currently only used internally, so there is no corresponding shader
1422 // object at the interface level. For now the geometry shader debug info is prepended to
1423 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001424 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001425 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001426 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1427 }
1428
1429 if (defaultVertexExecutable)
1430 {
1431 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1432 }
1433
1434 if (defaultPixelExecutable)
1435 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001436 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001437 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001438 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1439 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001440
Jamie Madillb0a838b2016-11-13 20:02:12 -05001441 return (defaultVertexExecutable && defaultPixelExecutable &&
1442 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001443}
1444
Xinghua Caob1239382016-12-13 15:07:05 +08001445LinkResult ProgramD3D::compileComputeExecutable(gl::InfoLog &infoLog)
1446{
1447 // Ensure the compiler is initialized to avoid race conditions.
1448 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1449
1450 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(mState);
1451
1452 ShaderExecutableD3D *computeExecutable = nullptr;
1453 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, SHADER_COMPUTE,
1454 std::vector<D3DVarying>(), false,
1455 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1456
1457 if (computeExecutable == nullptr)
1458 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001459 ERR() << "Error compiling dynamic compute executable:" << std::endl
1460 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001461 }
1462 else
1463 {
1464 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1465 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1466 mComputeExecutable.reset(computeExecutable);
1467 }
1468
1469 return mComputeExecutable.get() != nullptr;
1470}
1471
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001472LinkResult ProgramD3D::link(ContextImpl *contextImpl,
Jamie Madill192745a2016-12-22 15:58:21 -05001473 const gl::VaryingPacking &packing,
1474 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001475{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001476 const auto &data = contextImpl->getContextState();
1477
Jamie Madill62d31cb2015-09-11 13:25:51 -04001478 reset();
1479
Xinghua Caob1239382016-12-13 15:07:05 +08001480 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1481 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001482 {
Xinghua Caob1239382016-12-13 15:07:05 +08001483 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1484
1485 defineUniformsAndAssignRegisters();
1486
1487 LinkResult result = compileComputeExecutable(infoLog);
1488 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001489 {
Xinghua Caob1239382016-12-13 15:07:05 +08001490 infoLog << result.getError().getMessage();
1491 return result;
1492 }
1493 else if (!result.getResult())
1494 {
1495 infoLog << "Failed to create D3D compute shader.";
1496 return result;
1497 }
1498
1499 initUniformBlockInfo(computeShader);
1500 }
1501 else
1502 {
1503 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1504 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1505
1506 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1507 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1508
1509 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1510 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1511
1512 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1513 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1514
1515 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1516 {
1517 if (fragmentShaderD3D->usesFrontFacing())
1518 {
1519 infoLog << "The current renderer doesn't support gl_FrontFacing";
1520 return false;
1521 }
1522 }
1523
1524 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1525 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1526 // intelligently, but D3D9 assumes one semantic per register.
1527 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1528 packing.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
1529 {
1530 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001531 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001532 }
Xinghua Caob1239382016-12-13 15:07:05 +08001533
1534 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
1535 BuiltinVaryingsD3D builtins(metadata, packing);
1536
1537 mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, packing, builtins, &mPixelHLSL,
1538 &mVertexHLSL);
1539
1540 mUsesPointSize = vertexShaderD3D->usesPointSize();
1541 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1542 mUsesFragDepth = metadata.usesFragDepth();
1543
1544 // Cache if we use flat shading
1545 mUsesFlatInterpolation = (FindFlatInterpolationVarying(fragmentShader->getVaryings()) ||
1546 FindFlatInterpolationVarying(vertexShader->getVaryings()));
1547
1548 if (mRenderer->getMajorShaderModel() >= 4)
1549 {
1550 mGeometryShaderPreamble =
1551 mDynamicHLSL->generateGeometryShaderPreamble(packing, builtins);
1552 }
1553
1554 initAttribLocationsToD3DSemantic();
1555
1556 defineUniformsAndAssignRegisters();
1557
1558 gatherTransformFeedbackVaryings(packing, builtins[SHADER_VERTEX]);
1559
1560 LinkResult result = compileProgramExecutables(data, infoLog);
1561 if (result.isError())
1562 {
1563 infoLog << result.getError().getMessage();
1564 return result;
1565 }
1566 else if (!result.getResult())
1567 {
1568 infoLog << "Failed to create D3D shaders.";
1569 return result;
1570 }
1571
1572 initUniformBlockInfo(vertexShader);
1573 initUniformBlockInfo(fragmentShader);
Austin Kinross02df7962015-07-01 10:03:42 -07001574 }
1575
Jamie Madillb0a838b2016-11-13 20:02:12 -05001576 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001577}
1578
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001579GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001580{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001581 // TODO(jmadill): Do something useful here?
1582 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001583}
1584
Xinghua Caob1239382016-12-13 15:07:05 +08001585void ProgramD3D::initUniformBlockInfo(const gl::Shader *shader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001586{
Xinghua Caob1239382016-12-13 15:07:05 +08001587 for (const sh::InterfaceBlock &interfaceBlock : shader->getInterfaceBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001588 {
Xinghua Caob1239382016-12-13 15:07:05 +08001589 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001590 continue;
1591
Xinghua Caob1239382016-12-13 15:07:05 +08001592 if (mBlockDataSizes.count(interfaceBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001593 continue;
1594
Xinghua Caob1239382016-12-13 15:07:05 +08001595 size_t dataSize = getUniformBlockInfo(interfaceBlock);
1596 mBlockDataSizes[interfaceBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001597 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001598}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001599
Jamie Madill51f522f2016-12-21 15:10:55 -05001600void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001601{
Jamie Madill51f522f2016-12-21 15:10:55 -05001602 // Lazy init.
1603 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1604 {
1605 return;
1606 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001607
1608 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001609 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1610 const ShaderD3D *fragmentShaderD3D =
1611 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1612 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001613
Jamie Madill48ef11b2016-04-27 15:21:52 -04001614 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001615 {
1616 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1617
Jamie Madill4a3c2342015-10-08 12:58:45 -04001618 D3DUniformBlock d3dUniformBlock;
1619
Jamie Madill62d31cb2015-09-11 13:25:51 -04001620 if (uniformBlock.vertexStaticUse)
1621 {
Xinghua Caob1239382016-12-13 15:07:05 +08001622 ASSERT(vertexShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001623 unsigned int baseRegister =
1624 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001625 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001626 }
1627
1628 if (uniformBlock.fragmentStaticUse)
1629 {
Xinghua Caob1239382016-12-13 15:07:05 +08001630 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001631 unsigned int baseRegister =
1632 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001633 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001634 }
1635
Xinghua Caob1239382016-12-13 15:07:05 +08001636 if (uniformBlock.computeStaticUse)
1637 {
1638 ASSERT(computeShaderD3D != nullptr);
1639 unsigned int baseRegister =
1640 computeShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
1641 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1642 }
1643
Jamie Madill4a3c2342015-10-08 12:58:45 -04001644 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001645 }
1646}
1647
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001648void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001649{
1650 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001651 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001652 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001653 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001654 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001655 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001656 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001657 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001658 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001659 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001660 vertexRegisters = std::max(vertexRegisters,
1661 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001662 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001663 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001664 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001665 fragmentRegisters = std::max(
1666 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001667 }
Xinghua Caob1239382016-12-13 15:07:05 +08001668 if (d3dUniform->isReferencedByComputeShader())
1669 {
1670 computeRegisters = std::max(
1671 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1672 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001673 }
1674 }
1675
Xinghua Caob1239382016-12-13 15:07:05 +08001676 mVertexUniformStorage =
1677 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1678 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1679 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1680 mComputeUniformStorage =
1681 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Brandon Jonesc9610c52014-08-25 17:02:59 -07001682}
1683
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001684gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001685{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001686 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001687
Jamie Madill01074252016-11-28 15:55:51 -05001688 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001689
Jamie Madill62d31cb2015-09-11 13:25:51 -04001690 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001691 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001692 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001693 }
1694
Jamie Madill51f522f2016-12-21 15:10:55 -05001695 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001696}
1697
Jamie Madill9082b982016-04-27 15:21:51 -04001698gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001699{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001700 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001701 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001702 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001703 }
1704
Jamie Madill51f522f2016-12-21 15:10:55 -05001705 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001706
Jamie Madill03260fa2015-06-22 13:57:22 -04001707 mVertexUBOCache.clear();
1708 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001709
1710 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1711 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1712
Jamie Madill4a3c2342015-10-08 12:58:45 -04001713 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001714 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001715 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001716 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001717 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001718
Brandon Jones18bd4102014-09-22 14:21:44 -07001719 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001720 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001721 {
1722 continue;
1723 }
1724
Jamie Madill4a3c2342015-10-08 12:58:45 -04001725 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001726 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001727 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001728 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001729
Jamie Madill969194d2015-07-20 14:36:56 -04001730 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001731 {
1732 mVertexUBOCache.resize(registerIndex + 1, -1);
1733 }
1734
1735 ASSERT(mVertexUBOCache[registerIndex] == -1);
1736 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001737 }
1738
Jamie Madill4a3c2342015-10-08 12:58:45 -04001739 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001740 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001741 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001742 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001743
1744 if (mFragmentUBOCache.size() <= registerIndex)
1745 {
1746 mFragmentUBOCache.resize(registerIndex + 1, -1);
1747 }
1748
1749 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1750 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001751 }
1752 }
1753
Jamie Madill03260fa2015-06-22 13:57:22 -04001754 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001755}
1756
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001757void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001758{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001759 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001760 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001761 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001762 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001763}
1764
Jamie Madill334d6152015-10-22 14:00:28 -04001765void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001766{
1767 setUniform(location, count, v, GL_FLOAT);
1768}
1769
1770void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1771{
1772 setUniform(location, count, v, GL_FLOAT_VEC2);
1773}
1774
1775void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1776{
1777 setUniform(location, count, v, GL_FLOAT_VEC3);
1778}
1779
1780void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1781{
1782 setUniform(location, count, v, GL_FLOAT_VEC4);
1783}
1784
Jamie Madill334d6152015-10-22 14:00:28 -04001785void ProgramD3D::setUniformMatrix2fv(GLint location,
1786 GLsizei count,
1787 GLboolean transpose,
1788 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001789{
1790 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1791}
1792
Jamie Madill334d6152015-10-22 14:00:28 -04001793void ProgramD3D::setUniformMatrix3fv(GLint location,
1794 GLsizei count,
1795 GLboolean transpose,
1796 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001797{
1798 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1799}
1800
Jamie Madill334d6152015-10-22 14:00:28 -04001801void ProgramD3D::setUniformMatrix4fv(GLint location,
1802 GLsizei count,
1803 GLboolean transpose,
1804 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001805{
1806 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1807}
1808
Jamie Madill334d6152015-10-22 14:00:28 -04001809void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1810 GLsizei count,
1811 GLboolean transpose,
1812 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001813{
1814 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1815}
1816
Jamie Madill334d6152015-10-22 14:00:28 -04001817void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1818 GLsizei count,
1819 GLboolean transpose,
1820 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001821{
1822 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1823}
1824
Jamie Madill334d6152015-10-22 14:00:28 -04001825void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1826 GLsizei count,
1827 GLboolean transpose,
1828 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001829{
1830 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1831}
1832
Jamie Madill334d6152015-10-22 14:00:28 -04001833void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1834 GLsizei count,
1835 GLboolean transpose,
1836 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001837{
1838 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1839}
1840
Jamie Madill334d6152015-10-22 14:00:28 -04001841void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1842 GLsizei count,
1843 GLboolean transpose,
1844 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001845{
1846 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1847}
1848
Jamie Madill334d6152015-10-22 14:00:28 -04001849void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1850 GLsizei count,
1851 GLboolean transpose,
1852 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001853{
1854 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1855}
1856
1857void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1858{
1859 setUniform(location, count, v, GL_INT);
1860}
1861
1862void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1863{
1864 setUniform(location, count, v, GL_INT_VEC2);
1865}
1866
1867void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1868{
1869 setUniform(location, count, v, GL_INT_VEC3);
1870}
1871
1872void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1873{
1874 setUniform(location, count, v, GL_INT_VEC4);
1875}
1876
1877void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1878{
1879 setUniform(location, count, v, GL_UNSIGNED_INT);
1880}
1881
1882void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1883{
1884 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1885}
1886
1887void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1888{
1889 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1890}
1891
1892void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1893{
1894 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1895}
1896
Jamie Madill4a3c2342015-10-08 12:58:45 -04001897void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1898 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001899{
1900}
1901
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001902void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001903{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001904 D3DUniformMap uniformMap;
Xinghua Caob1239382016-12-13 15:07:05 +08001905 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1906 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05001907 {
Xinghua Caob1239382016-12-13 15:07:05 +08001908 for (const sh::Uniform &computeUniform : computeShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001909 {
Xinghua Caob1239382016-12-13 15:07:05 +08001910 if (computeUniform.staticUse)
1911 {
1912 defineUniformBase(computeShader, computeUniform, &uniformMap);
1913 }
Jamie Madillfb536032015-09-11 13:19:49 -04001914 }
1915 }
Xinghua Caob1239382016-12-13 15:07:05 +08001916 else
Jamie Madillfb536032015-09-11 13:19:49 -04001917 {
Xinghua Caob1239382016-12-13 15:07:05 +08001918 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1919 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001920 {
Xinghua Caob1239382016-12-13 15:07:05 +08001921 if (vertexUniform.staticUse)
1922 {
1923 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
1924 }
1925 }
1926
1927 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1928 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
1929 {
1930 if (fragmentUniform.staticUse)
1931 {
1932 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
1933 }
Jamie Madillfb536032015-09-11 13:19:49 -04001934 }
1935 }
1936
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001937 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001938 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001939 {
1940 if (!glUniform.isInDefaultBlock())
1941 continue;
1942
1943 auto mapEntry = uniformMap.find(glUniform.name);
1944 ASSERT(mapEntry != uniformMap.end());
1945 mD3DUniforms.push_back(mapEntry->second);
1946 }
1947
Jamie Madill62d31cb2015-09-11 13:25:51 -04001948 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001949 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001950}
1951
Jamie Madill91445bc2015-09-23 16:47:53 -04001952void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001953 const sh::Uniform &uniform,
1954 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001955{
Olli Etuaho96963162016-03-21 11:54:33 +02001956 // Samplers get their registers assigned in assignAllSamplerRegisters.
1957 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001958 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001959 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001960 return;
1961 }
1962
Jamie Madill91445bc2015-09-23 16:47:53 -04001963 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1964
1965 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1966 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04001967 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001968 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001969
Jamie Madill91445bc2015-09-23 16:47:53 -04001970 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001971}
1972
Jamie Madill62d31cb2015-09-11 13:25:51 -04001973D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1974{
1975 for (D3DUniform *d3dUniform : mD3DUniforms)
1976 {
1977 if (d3dUniform->name == name)
1978 {
1979 return d3dUniform;
1980 }
1981 }
1982
1983 return nullptr;
1984}
1985
Jamie Madill91445bc2015-09-23 16:47:53 -04001986void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001987 const sh::ShaderVariable &uniform,
1988 const std::string &fullName,
1989 sh::HLSLBlockEncoder *encoder,
1990 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001991{
1992 if (uniform.isStruct())
1993 {
1994 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1995 {
1996 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1997
Jamie Madill55def582015-05-04 11:24:57 -04001998 if (encoder)
1999 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002000
2001 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2002 {
Jamie Madill334d6152015-10-22 14:00:28 -04002003 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002004 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2005
Olli Etuaho96963162016-03-21 11:54:33 +02002006 // Samplers get their registers assigned in assignAllSamplerRegisters.
2007 // Also they couldn't use the same encoder as the rest of the struct, since they are
2008 // extracted out of the struct by the shader translator.
2009 if (gl::IsSamplerType(field.type))
2010 {
2011 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2012 }
2013 else
2014 {
2015 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2016 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017 }
2018
Jamie Madill55def582015-05-04 11:24:57 -04002019 if (encoder)
2020 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002021 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002022 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002023 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002024
2025 // Not a struct. Arrays are treated as aggregate types.
2026 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002027 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002028 encoder->enterAggregateType();
2029 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002030
Jamie Madill62d31cb2015-09-11 13:25:51 -04002031 // Advance the uniform offset, to track registers allocation for structs
2032 sh::BlockMemberInfo blockInfo =
2033 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2034 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002035
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002036 auto uniformMapEntry = uniformMap->find(fullName);
2037 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002038
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002039 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002040 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002041 d3dUniform = uniformMapEntry->second;
2042 }
2043 else
2044 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002045 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002046 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002047 }
2048
2049 if (encoder)
2050 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002051 d3dUniform->registerElement =
2052 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002053 unsigned int reg =
2054 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002055 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002056 {
2057 d3dUniform->psRegisterIndex = reg;
2058 }
Xinghua Caob1239382016-12-13 15:07:05 +08002059 else if (shaderType == GL_VERTEX_SHADER)
2060 {
2061 d3dUniform->vsRegisterIndex = reg;
2062 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002063 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002064 {
Xinghua Caob1239382016-12-13 15:07:05 +08002065 ASSERT(shaderType == GL_COMPUTE_SHADER);
2066 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002067 }
Jamie Madillfb536032015-09-11 13:19:49 -04002068
2069 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002070 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002071 {
2072 encoder->exitAggregateType();
2073 }
2074 }
2075}
2076
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002077template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002078void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002079{
Jamie Madill334d6152015-10-22 14:00:28 -04002080 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002081 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2082
Jamie Madill62d31cb2015-09-11 13:25:51 -04002083 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002084
Jamie Madill62d31cb2015-09-11 13:25:51 -04002085 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002086 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002087 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002088
2089 if (targetUniform->type == targetUniformType)
2090 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002091 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002092
Jamie Madill62d31cb2015-09-11 13:25:51 -04002093 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002094 {
Jamie Madill334d6152015-10-22 14:00:28 -04002095 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002096 const T *source = v + (i * components);
2097
2098 for (int c = 0; c < components; c++)
2099 {
2100 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2101 }
2102 for (int c = components; c < 4; c++)
2103 {
2104 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2105 }
2106 }
2107 }
2108 else if (targetUniform->type == targetBoolType)
2109 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002110 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002111
Jamie Madill62d31cb2015-09-11 13:25:51 -04002112 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002113 {
Jamie Madill334d6152015-10-22 14:00:28 -04002114 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002115 const T *source = v + (i * components);
2116
2117 for (int c = 0; c < components; c++)
2118 {
Jamie Madill334d6152015-10-22 14:00:28 -04002119 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2120 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002121 }
2122 for (int c = components; c < 4; c++)
2123 {
2124 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2125 }
2126 }
2127 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002128 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002129 {
2130 ASSERT(targetUniformType == GL_INT);
2131
Jamie Madill62d31cb2015-09-11 13:25:51 -04002132 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002133
2134 bool wasDirty = targetUniform->dirty;
2135
Jamie Madill62d31cb2015-09-11 13:25:51 -04002136 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002137 {
Jamie Madill334d6152015-10-22 14:00:28 -04002138 GLint *dest = target + (i * 4);
2139 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002140
2141 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2142 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2143 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2144 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2145 }
2146
2147 if (!wasDirty && targetUniform->dirty)
2148 {
2149 mDirtySamplerMapping = true;
2150 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002151 }
Jamie Madill334d6152015-10-22 14:00:28 -04002152 else
2153 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002154}
2155
2156template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002157void ProgramD3D::setUniformMatrixfv(GLint location,
2158 GLsizei countIn,
2159 GLboolean transpose,
2160 const GLfloat *value,
2161 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002162{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002163 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002164
Jamie Madill62d31cb2015-09-11 13:25:51 -04002165 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002166 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002167 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002168
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002169 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002170 GLfloat *target =
2171 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002172
Jamie Madill62d31cb2015-09-11 13:25:51 -04002173 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002174 {
2175 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2176 if (transpose == GL_FALSE)
2177 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002178 targetUniform->dirty =
2179 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002180 }
2181 else
2182 {
Jamie Madill334d6152015-10-22 14:00:28 -04002183 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002184 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002185 }
2186 target += targetMatrixStride;
2187 value += cols * rows;
2188 }
2189}
2190
Jamie Madill4a3c2342015-10-08 12:58:45 -04002191size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002192{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002193 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002194
Jamie Madill62d31cb2015-09-11 13:25:51 -04002195 // define member uniforms
2196 sh::Std140BlockEncoder std140Encoder;
Jamie Madillcc2ed612017-03-14 15:59:00 -04002197 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002198 sh::BlockLayoutEncoder *encoder = nullptr;
2199
2200 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002201 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002202 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002203 }
2204 else
2205 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002206 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002207 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002208
Jamie Madill39046162016-02-08 15:05:17 -05002209 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2210 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002211
2212 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002213}
2214
Jamie Madill62d31cb2015-09-11 13:25:51 -04002215void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002216{
Olli Etuaho96963162016-03-21 11:54:33 +02002217 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002218 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002219 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002220 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002221 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002222 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002223 }
2224}
2225
Olli Etuaho96963162016-03-21 11:54:33 +02002226void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002227{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002228 ASSERT(d3dUniform->isSampler());
Xinghua Caob1239382016-12-13 15:07:05 +08002229 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2230 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002231 {
Xinghua Caob1239382016-12-13 15:07:05 +08002232 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2233 ASSERT(computeShaderD3D->hasUniform(d3dUniform));
2234 d3dUniform->csRegisterIndex = computeShaderD3D->getUniformRegister(d3dUniform->name);
2235 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2236 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2237 mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002238 }
Xinghua Caob1239382016-12-13 15:07:05 +08002239 else
Jamie Madillfb536032015-09-11 13:19:49 -04002240 {
Xinghua Caob1239382016-12-13 15:07:05 +08002241 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2242 const ShaderD3D *fragmentShaderD3D =
2243 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2244 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) ||
2245 fragmentShaderD3D->hasUniform(d3dUniform));
2246 if (vertexShaderD3D->hasUniform(d3dUniform))
2247 {
2248 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2249 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
2250 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2251 mSamplersVS, &mUsedVertexSamplerRange);
2252 }
2253 if (fragmentShaderD3D->hasUniform(d3dUniform))
2254 {
2255 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2256 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
2257 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2258 mSamplersPS, &mUsedPixelSamplerRange);
2259 }
Jamie Madillfb536032015-09-11 13:19:49 -04002260 }
2261}
2262
Jamie Madill62d31cb2015-09-11 13:25:51 -04002263// static
2264void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002265 GLenum samplerType,
2266 unsigned int samplerCount,
2267 std::vector<Sampler> &outSamplers,
2268 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002269{
2270 unsigned int samplerIndex = startSamplerIndex;
2271
2272 do
2273 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002274 ASSERT(samplerIndex < outSamplers.size());
2275 Sampler *sampler = &outSamplers[samplerIndex];
2276 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002277 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002278 sampler->logicalTextureUnit = 0;
2279 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002280 samplerIndex++;
2281 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002282}
2283
Brandon Jonesc9610c52014-08-25 17:02:59 -07002284void ProgramD3D::reset()
2285{
Xinghua Caob1239382016-12-13 15:07:05 +08002286 mVertexExecutables.clear();
2287 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002288
Xinghua Caob1239382016-12-13 15:07:05 +08002289 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002290 {
Xinghua Caob1239382016-12-13 15:07:05 +08002291 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002292 }
Brandon Joneseb994362014-09-24 10:27:28 -07002293
Xinghua Caob1239382016-12-13 15:07:05 +08002294 mComputeExecutable.reset(nullptr);
2295
Brandon Jones22502d52014-08-29 16:58:36 -07002296 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002297 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002298
2299 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002300 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002301 mUsesFragDepth = false;
2302 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002303 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002304 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002305
Jamie Madill62d31cb2015-09-11 13:25:51 -04002306 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002307 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002308
Xinghua Caob1239382016-12-13 15:07:05 +08002309 mVertexUniformStorage.reset(nullptr);
2310 mFragmentUniformStorage.reset(nullptr);
2311 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002312
2313 mSamplersPS.clear();
2314 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002315 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002316
2317 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002318 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002319 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002320 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002321
Jamie Madill8047c0d2016-03-07 13:02:12 -05002322 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002323
Jamie Madill9fc36822015-11-18 13:08:07 -05002324 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002325
2326 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002327}
2328
Geoff Lang7dd2e102014-11-10 15:19:26 -05002329unsigned int ProgramD3D::getSerial() const
2330{
2331 return mSerial;
2332}
2333
2334unsigned int ProgramD3D::issueSerial()
2335{
2336 return mCurrentSerial++;
2337}
2338
Jamie Madill8047c0d2016-03-07 13:02:12 -05002339void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002340{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002341 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002342 ASSERT(vertexShader != nullptr);
2343
2344 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002345 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002346 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002347 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2348 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002349
Jamie Madill8047c0d2016-03-07 13:02:12 -05002350 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002351 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002352 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002353 }
2354 }
Jamie Madill437d2662014-12-05 14:23:35 -05002355}
2356
Jamie Madill63805b42015-08-25 13:17:39 -04002357void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002358{
Jamie Madillbd136f92015-08-10 14:51:37 -04002359 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002360 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002361
Jamie Madill01074252016-11-28 15:55:51 -05002362 for (unsigned int locationIndex : IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002363 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002364 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002365
Jamie Madill8047c0d2016-03-07 13:02:12 -05002366 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002367 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002368 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002369 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002370 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002371 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002372 mCachedInputLayout[d3dSemantic] =
2373 GetVertexFormatType(vertexAttributes[locationIndex],
2374 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002375 }
2376 }
2377}
2378
Jamie Madill192745a2016-12-22 15:58:21 -05002379void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2380 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002381{
Jamie Madill9fc36822015-11-18 13:08:07 -05002382 const std::string &varyingSemantic =
2383 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2384
Jamie Madillccdf74b2015-08-18 10:46:12 -04002385 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002386 mStreamOutVaryings.clear();
2387
Jamie Madill48ef11b2016-04-27 15:21:52 -04002388 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002389 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2390 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002391 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002392 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2393 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002394 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002395 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002396 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002397 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2398 builtins.glPosition.index, 4, outputSlot));
2399 }
2400 }
2401 else if (tfVaryingName == "gl_FragCoord")
2402 {
2403 if (builtins.glFragCoord.enabled)
2404 {
2405 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2406 builtins.glFragCoord.index, 4, outputSlot));
2407 }
2408 }
2409 else if (tfVaryingName == "gl_PointSize")
2410 {
2411 if (builtins.glPointSize.enabled)
2412 {
2413 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2414 }
2415 }
2416 else
2417 {
Jamie Madill192745a2016-12-22 15:58:21 -05002418 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002419 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002420 const auto &varying = *registerInfo.packedVarying->varying;
2421 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002422 int componentCount = gl::VariableColumnCount(transposedType);
2423 ASSERT(!varying.isBuiltIn());
2424
Jamie Madill55c25d02015-11-18 13:08:08 -05002425 // Transform feedback for varying structs is underspecified.
2426 // See Khronos bug 9856.
2427 // TODO(jmadill): Figure out how to be spec-compliant here.
2428 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2429 continue;
2430
Jamie Madill9fc36822015-11-18 13:08:07 -05002431 // There can be more than one register assigned to a particular varying, and each
2432 // register needs its own stream out entry.
2433 if (tfVaryingName == varying.name)
2434 {
2435 mStreamOutVaryings.push_back(D3DVarying(
2436 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2437 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002438 }
2439 }
2440 }
2441}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002442
2443D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2444{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002445 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002446}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002447
2448bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2449{
2450 std::string baseName = blockName;
2451 gl::ParseAndStripArrayIndex(&baseName);
2452
2453 auto sizeIter = mBlockDataSizes.find(baseName);
2454 if (sizeIter == mBlockDataSizes.end())
2455 {
2456 *sizeOut = 0;
2457 return false;
2458 }
2459
2460 *sizeOut = sizeIter->second;
2461 return true;
2462}
2463
2464bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2465 sh::BlockMemberInfo *memberInfoOut) const
2466{
2467 auto infoIter = mBlockInfo.find(memberUniformName);
2468 if (infoIter == mBlockInfo.end())
2469 {
2470 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2471 return false;
2472 }
2473
2474 *memberInfoOut = infoIter->second;
2475 return true;
2476}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002477
2478void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2479 GLenum genMode,
2480 GLint components,
2481 const GLfloat *coeffs)
2482{
2483 UNREACHABLE();
2484}
2485
Jamie Madill8047c0d2016-03-07 13:02:12 -05002486} // namespace rx