blob: 420d8ee9b13ac7cb1febdad01038a14e06234242 [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"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050021#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050022#include "libANGLE/renderer/d3d/RendererD3D.h"
23#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050024#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050025#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050026
Jamie Madill01074252016-11-28 15:55:51 -050027using namespace angle;
28
Brandon Jonesc9610c52014-08-25 17:02:59 -070029namespace rx
30{
31
Brandon Joneseb994362014-09-24 10:27:28 -070032namespace
33{
34
Jamie Madillf8dd7b12015-08-05 13:50:08 -040035gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070036{
Jamie Madillbd136f92015-08-10 14:51:37 -040037 gl::InputLayout defaultLayout;
38 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070039 {
Brandon Joneseb994362014-09-24 10:27:28 -070040 if (shaderAttr.type != GL_NONE)
41 {
42 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
43
Jamie Madilld3dfda22015-07-06 08:28:49 -040044 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040045 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070046 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040047 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040048 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040049 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040050 gl::VertexFormatType defaultType =
51 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070052
Jamie Madillbd136f92015-08-10 14:51:37 -040053 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070054 }
55 }
56 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040057
58 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070059}
60
Jamie Madill334d6152015-10-22 14:00:28 -040061std::vector<GLenum> GetDefaultOutputLayoutFromShader(
62 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070063{
Jamie Madillb4463142014-12-19 14:56:54 -050064 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070065
Jamie Madillb4463142014-12-19 14:56:54 -050066 if (!shaderOutputVars.empty())
67 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070068 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
69 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050070 }
Brandon Joneseb994362014-09-24 10:27:28 -070071
72 return defaultPixelOutput;
73}
74
Brandon Jones1a8a7e32014-10-01 12:49:30 -070075bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
76{
77 return var.isRowMajorLayout;
78}
79
80bool IsRowMajorLayout(const sh::ShaderVariable &var)
81{
82 return false;
83}
84
Jamie Madill62d31cb2015-09-11 13:25:51 -040085template <typename VarT>
86void GetUniformBlockInfo(const std::vector<VarT> &fields,
87 const std::string &prefix,
88 sh::BlockLayoutEncoder *encoder,
89 bool inRowMajorLayout,
90 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
91{
92 for (const VarT &field : fields)
93 {
94 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
95
96 if (field.isStruct())
97 {
98 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
99
100 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
101 {
102 encoder->enterAggregateType();
103
104 const std::string uniformElementName =
105 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
106 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
107 blockInfoOut);
108
109 encoder->exitAggregateType();
110 }
111 }
112 else
113 {
114 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
115 (*blockInfoOut)[fieldName] =
116 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
117 }
118 }
119}
120
Jamie Madill334d6152015-10-22 14:00:28 -0400121template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100122static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
123{
124 ASSERT(dest != NULL);
125 ASSERT(dirtyFlag != NULL);
126
127 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
128 *dest = source;
129}
130
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800131template <typename T, int cols, int rows>
132bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400133{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800134 constexpr int targetWidth = 4;
135 constexpr int targetHeight = rows;
136 constexpr int srcWidth = rows;
137 constexpr int srcHeight = cols;
138
139 constexpr int copyWidth = std::min(targetHeight, srcWidth);
140 constexpr int copyHeight = std::min(targetWidth, srcHeight);
141
142 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400143
144 for (int x = 0; x < copyWidth; x++)
145 {
146 for (int y = 0; y < copyHeight; y++)
147 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800148 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400149 }
150 }
151
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800152 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
153 {
154 return false;
155 }
156
157 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
158 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400159}
160
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800161template <typename T, int cols, int rows>
162bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400163{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800164 constexpr int targetWidth = 4;
165 constexpr int targetHeight = rows;
166 constexpr int srcWidth = cols;
167 constexpr int srcHeight = rows;
168
169 constexpr int copyWidth = std::min(targetWidth, srcWidth);
170 constexpr int copyHeight = std::min(targetHeight, srcHeight);
171
172 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400173
174 for (int y = 0; y < copyHeight; y++)
175 {
176 for (int x = 0; x < copyWidth; x++)
177 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800178 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400179 }
180 }
181
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800182 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
183 {
184 return false;
185 }
186
187 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
188 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400189}
190
Jamie Madill4e31ad52015-10-29 10:32:57 -0400191gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
192{
193 switch (drawMode)
194 {
195 // Uses the point sprite geometry shader.
196 case GL_POINTS:
197 return gl::PRIMITIVE_POINTS;
198
199 // All line drawing uses the same geometry shader.
200 case GL_LINES:
201 case GL_LINE_STRIP:
202 case GL_LINE_LOOP:
203 return gl::PRIMITIVE_LINES;
204
205 // The triangle fan primitive is emulated with strips in D3D11.
206 case GL_TRIANGLES:
207 case GL_TRIANGLE_FAN:
208 return gl::PRIMITIVE_TRIANGLES;
209
210 // Special case for triangle strips.
211 case GL_TRIANGLE_STRIP:
212 return gl::PRIMITIVE_TRIANGLE_STRIP;
213
214 default:
215 UNREACHABLE();
216 return gl::PRIMITIVE_TYPE_MAX;
217 }
218}
219
Jamie Madill192745a2016-12-22 15:58:21 -0500220bool FindFlatInterpolationVarying(const std::vector<sh::Varying> &varyings)
221{
222 // Note: this assumes nested structs can only be packed with one interpolation.
223 for (const auto &varying : varyings)
224 {
225 if (varying.interpolation == sh::INTERPOLATION_FLAT)
226 {
227 return true;
228 }
229 }
230
231 return false;
232}
233
Jamie Madillada9ecc2015-08-17 12:53:37 -0400234} // anonymous namespace
235
Jamie Madill28afae52015-11-09 15:07:57 -0500236// D3DUniform Implementation
237
Jamie Madill62d31cb2015-09-11 13:25:51 -0400238D3DUniform::D3DUniform(GLenum typeIn,
239 const std::string &nameIn,
240 unsigned int arraySizeIn,
241 bool defaultBlock)
242 : type(typeIn),
243 name(nameIn),
244 arraySize(arraySizeIn),
245 data(nullptr),
246 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400247 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400248 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400249 registerCount(0),
250 registerElement(0)
251{
252 // We use data storage for default block uniforms to cache values that are sent to D3D during
253 // rendering
254 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
255 if (defaultBlock)
256 {
257 size_t bytes = gl::VariableInternalSize(type) * elementCount();
258 data = new uint8_t[bytes];
259 memset(data, 0, bytes);
260
261 // TODO(jmadill): is this correct with non-square matrices?
262 registerCount = gl::VariableRowCount(type) * elementCount();
263 }
264}
265
266D3DUniform::~D3DUniform()
267{
268 SafeDeleteArray(data);
269}
270
271bool D3DUniform::isSampler() const
272{
273 return gl::IsSamplerType(type);
274}
275
276bool D3DUniform::isReferencedByVertexShader() const
277{
278 return vsRegisterIndex != GL_INVALID_INDEX;
279}
280
281bool D3DUniform::isReferencedByFragmentShader() const
282{
283 return psRegisterIndex != GL_INVALID_INDEX;
284}
285
Jamie Madill28afae52015-11-09 15:07:57 -0500286// D3DVarying Implementation
287
Jamie Madill9fc36822015-11-18 13:08:07 -0500288D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500289{
290}
291
Jamie Madill9fc36822015-11-18 13:08:07 -0500292D3DVarying::D3DVarying(const std::string &semanticNameIn,
293 unsigned int semanticIndexIn,
294 unsigned int componentCountIn,
295 unsigned int outputSlotIn)
296 : semanticName(semanticNameIn),
297 semanticIndex(semanticIndexIn),
298 componentCount(componentCountIn),
299 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500300{
301}
302
Jamie Madille39a3f02015-11-17 20:42:15 -0500303// ProgramD3DMetadata Implementation
304
Jamie Madillc9bde922016-07-24 17:58:50 -0400305ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500306 const ShaderD3D *vertexShader,
307 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400308 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
309 mShaderModelSuffix(renderer->getShaderModelSuffix()),
310 mUsesInstancedPointSpriteEmulation(
311 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
312 mUsesViewScale(renderer->presentPathFastEnabled()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500313 mVertexShader(vertexShader),
314 mFragmentShader(fragmentShader)
315{
316}
317
318int ProgramD3DMetadata::getRendererMajorShaderModel() const
319{
320 return mRendererMajorShaderModel;
321}
322
Jamie Madill9082b982016-04-27 15:21:51 -0400323bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500324{
Martin Radev1be913c2016-07-11 17:59:16 +0300325 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500326}
327
Jamie Madill48ef11b2016-04-27 15:21:52 -0400328bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500329{
Jamie Madill63286672015-11-24 13:00:08 -0500330 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500331}
332
333bool ProgramD3DMetadata::usesPointCoord() const
334{
335 return mFragmentShader->usesPointCoord();
336}
337
338bool ProgramD3DMetadata::usesFragCoord() const
339{
340 return mFragmentShader->usesFragCoord();
341}
342
343bool ProgramD3DMetadata::usesPointSize() const
344{
345 return mVertexShader->usesPointSize();
346}
347
348bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
349{
Jamie Madillc9bde922016-07-24 17:58:50 -0400350 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
351 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500352}
353
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800354bool ProgramD3DMetadata::usesViewScale() const
355{
356 return mUsesViewScale;
357}
358
Jamie Madille39a3f02015-11-17 20:42:15 -0500359bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
360{
Jamie Madillc9bde922016-07-24 17:58:50 -0400361 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500362 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400363 // Even with a geometry shader, the app can render triangles or lines and reference
364 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
365 // simplicity, we always add this to the vertex shader when the fragment shader
366 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500367 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
368 usesInsertedPointCoordValue();
369}
370
371bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
372{
373 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
374 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
375 // the vertex shader in this case. This saves us 1 output vector.
376 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
377}
378
379bool ProgramD3DMetadata::usesSystemValuePointSize() const
380{
381 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
382}
383
384bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
385{
386 return mFragmentShader->usesMultipleRenderTargets();
387}
388
389GLint ProgramD3DMetadata::getMajorShaderVersion() const
390{
391 return mVertexShader->getData().getShaderVersion();
392}
393
394const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
395{
396 return mFragmentShader;
397}
398
Jamie Madill28afae52015-11-09 15:07:57 -0500399// ProgramD3D Implementation
400
Jamie Madilld3dfda22015-07-06 08:28:49 -0400401ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
402 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500403 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400404 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700405{
Brandon Joneseb994362014-09-24 10:27:28 -0700406}
407
408ProgramD3D::VertexExecutable::~VertexExecutable()
409{
410 SafeDelete(mShaderExecutable);
411}
412
Jamie Madilld3dfda22015-07-06 08:28:49 -0400413// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500414ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
415 GLenum type)
416{
417 switch (type)
418 {
419 case GL_INT:
420 return HLSLAttribType::SIGNED_INT;
421 case GL_UNSIGNED_INT:
422 return HLSLAttribType::UNSIGNED_INT;
423 case GL_SIGNED_NORMALIZED:
424 case GL_UNSIGNED_NORMALIZED:
425 case GL_FLOAT:
426 return HLSLAttribType::FLOAT;
427 default:
428 UNREACHABLE();
429 return HLSLAttribType::FLOAT;
430 }
431}
432
433// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400434void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
435 const gl::InputLayout &inputLayout,
436 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700437{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500438 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400439
440 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700441 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400442 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500443 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
444 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400445
Jamie Madillbdec2f42016-03-02 16:35:32 -0500446 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
447 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
448 continue;
449
450 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
451 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700452 }
Brandon Joneseb994362014-09-24 10:27:28 -0700453}
454
Jamie Madilld3dfda22015-07-06 08:28:49 -0400455bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
456{
Jamie Madillbd136f92015-08-10 14:51:37 -0400457 size_t limit = std::max(mSignature.size(), signature.size());
458 for (size_t index = 0; index < limit; ++index)
459 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500460 // treat undefined indexes as FLOAT
461 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
462 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400463 if (a != b)
464 return false;
465 }
466
467 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400468}
469
470ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
471 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400472 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700473{
474}
475
476ProgramD3D::PixelExecutable::~PixelExecutable()
477{
478 SafeDelete(mShaderExecutable);
479}
480
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700481ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
482{
483}
484
Geoff Lang7dd2e102014-11-10 15:19:26 -0500485unsigned int ProgramD3D::mCurrentSerial = 1;
486
Jamie Madill48ef11b2016-04-27 15:21:52 -0400487ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
488 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700489 mRenderer(renderer),
Jamie Madill417df922017-01-12 09:23:07 -0500490 mDynamicHLSL(NULL),
491 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700492 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400493 mUsesFlatInterpolation(false),
Jamie Madill417df922017-01-12 09:23:07 -0500494 mVertexUniformStorage(NULL),
495 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700496 mUsedVertexSamplerRange(0),
497 mUsedPixelSamplerRange(0),
498 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500499 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700500{
Brandon Joneseb994362014-09-24 10:27:28 -0700501 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700502}
503
504ProgramD3D::~ProgramD3D()
505{
506 reset();
507 SafeDelete(mDynamicHLSL);
508}
509
Brandon Jones44151a92014-09-10 11:32:25 -0700510bool ProgramD3D::usesPointSpriteEmulation() const
511{
512 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
513}
514
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400515bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700516{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400517 if (drawMode != GL_POINTS)
518 {
519 return mUsesFlatInterpolation;
520 }
521
Cooper Partine6664f02015-01-09 16:22:24 -0800522 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
523}
524
525bool ProgramD3D::usesInstancedPointSpriteEmulation() const
526{
527 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700528}
529
Jamie Madill334d6152015-10-22 14:00:28 -0400530GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
531 unsigned int samplerIndex,
532 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700533{
534 GLint logicalTextureUnit = -1;
535
536 switch (type)
537 {
Jamie Madill334d6152015-10-22 14:00:28 -0400538 case gl::SAMPLER_PIXEL:
539 ASSERT(samplerIndex < caps.maxTextureImageUnits);
540 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
541 {
542 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
543 }
544 break;
545 case gl::SAMPLER_VERTEX:
546 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
547 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
548 {
549 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
550 }
551 break;
552 default:
553 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700554 }
555
Jamie Madill334d6152015-10-22 14:00:28 -0400556 if (logicalTextureUnit >= 0 &&
557 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700558 {
559 return logicalTextureUnit;
560 }
561
562 return -1;
563}
564
565// Returns the texture type for a given Direct3D 9 sampler type and
566// index (0-15 for the pixel shader and 0-3 for the vertex shader).
567GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
568{
569 switch (type)
570 {
Jamie Madill334d6152015-10-22 14:00:28 -0400571 case gl::SAMPLER_PIXEL:
572 ASSERT(samplerIndex < mSamplersPS.size());
573 ASSERT(mSamplersPS[samplerIndex].active);
574 return mSamplersPS[samplerIndex].textureType;
575 case gl::SAMPLER_VERTEX:
576 ASSERT(samplerIndex < mSamplersVS.size());
577 ASSERT(mSamplersVS[samplerIndex].active);
578 return mSamplersVS[samplerIndex].textureType;
579 default:
580 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700581 }
582
583 return GL_TEXTURE_2D;
584}
585
Olli Etuaho618bebc2016-01-15 16:40:00 +0200586GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700587{
588 switch (type)
589 {
Jamie Madill334d6152015-10-22 14:00:28 -0400590 case gl::SAMPLER_PIXEL:
591 return mUsedPixelSamplerRange;
592 case gl::SAMPLER_VERTEX:
593 return mUsedVertexSamplerRange;
594 default:
595 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200596 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700597 }
598}
599
600void ProgramD3D::updateSamplerMapping()
601{
602 if (!mDirtySamplerMapping)
603 {
604 return;
605 }
606
607 mDirtySamplerMapping = false;
608
609 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400610 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700611 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400612 if (!d3dUniform->dirty)
613 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700614
Jamie Madill62d31cb2015-09-11 13:25:51 -0400615 if (!d3dUniform->isSampler())
616 continue;
617
618 int count = d3dUniform->elementCount();
619 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
620
621 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700622 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400623 unsigned int firstIndex = d3dUniform->psRegisterIndex;
624
625 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700626 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400627 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700628
Jamie Madill62d31cb2015-09-11 13:25:51 -0400629 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700630 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400631 ASSERT(mSamplersPS[samplerIndex].active);
632 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700633 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400634 }
635 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700636
Jamie Madill62d31cb2015-09-11 13:25:51 -0400637 if (d3dUniform->isReferencedByVertexShader())
638 {
639 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
640
641 for (int i = 0; i < count; i++)
642 {
643 unsigned int samplerIndex = firstIndex + i;
644
645 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700646 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400647 ASSERT(mSamplersVS[samplerIndex].active);
648 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700649 }
650 }
651 }
652 }
653}
654
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500655LinkResult ProgramD3D::load(const ContextImpl *contextImpl,
656 gl::InfoLog &infoLog,
657 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700658{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500659 // TODO(jmadill): Use Renderer from contextImpl.
660
Jamie Madill62d31cb2015-09-11 13:25:51 -0400661 reset();
662
Jamie Madill334d6152015-10-22 14:00:28 -0400663 DeviceIdentifier binaryDeviceIdentifier = {0};
664 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
665 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700666
667 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
668 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
669 {
670 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500671 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700672 }
673
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500674 int compileFlags = stream->readInt<int>();
675 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
676 {
Jamie Madillf6113162015-05-07 11:49:21 -0400677 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500678 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500679 }
680
Jamie Madill8047c0d2016-03-07 13:02:12 -0500681 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400682 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500683 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400684 }
685
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700686 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
687 for (unsigned int i = 0; i < psSamplerCount; ++i)
688 {
689 Sampler sampler;
690 stream->readBool(&sampler.active);
691 stream->readInt(&sampler.logicalTextureUnit);
692 stream->readInt(&sampler.textureType);
693 mSamplersPS.push_back(sampler);
694 }
695 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
696 for (unsigned int i = 0; i < vsSamplerCount; ++i)
697 {
698 Sampler sampler;
699 stream->readBool(&sampler.active);
700 stream->readInt(&sampler.logicalTextureUnit);
701 stream->readInt(&sampler.textureType);
702 mSamplersVS.push_back(sampler);
703 }
704
705 stream->readInt(&mUsedVertexSamplerRange);
706 stream->readInt(&mUsedPixelSamplerRange);
707
708 const unsigned int uniformCount = stream->readInt<unsigned int>();
709 if (stream->error())
710 {
Jamie Madillf6113162015-05-07 11:49:21 -0400711 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500712 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700713 }
714
Jamie Madill48ef11b2016-04-27 15:21:52 -0400715 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400716 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700717 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
718 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400719 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700720
Jamie Madill62d31cb2015-09-11 13:25:51 -0400721 D3DUniform *d3dUniform =
722 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
723 linkedUniform.isInDefaultBlock());
724 stream->readInt(&d3dUniform->psRegisterIndex);
725 stream->readInt(&d3dUniform->vsRegisterIndex);
726 stream->readInt(&d3dUniform->registerCount);
727 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700728
Jamie Madill62d31cb2015-09-11 13:25:51 -0400729 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700730 }
731
Jamie Madill4a3c2342015-10-08 12:58:45 -0400732 const unsigned int blockCount = stream->readInt<unsigned int>();
733 if (stream->error())
734 {
735 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500736 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400737 }
738
739 ASSERT(mD3DUniformBlocks.empty());
740 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
741 {
742 D3DUniformBlock uniformBlock;
743 stream->readInt(&uniformBlock.psRegisterIndex);
744 stream->readInt(&uniformBlock.vsRegisterIndex);
745 mD3DUniformBlocks.push_back(uniformBlock);
746 }
747
Jamie Madill9fc36822015-11-18 13:08:07 -0500748 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
749 mStreamOutVaryings.resize(streamOutVaryingCount);
750 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700751 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500752 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700753
Jamie Madill28afae52015-11-09 15:07:57 -0500754 stream->readString(&varying->semanticName);
755 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500756 stream->readInt(&varying->componentCount);
757 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700758 }
759
Brandon Jones22502d52014-08-29 16:58:36 -0700760 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400761 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500762 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700763 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400764 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500765 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700766 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700767 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400768 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700769
770 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
771 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400772 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
773 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700774 {
775 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
776 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
777 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
778 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
779 }
780
Jamie Madill4e31ad52015-10-29 10:32:57 -0400781 stream->readString(&mGeometryShaderPreamble);
782
Jamie Madill334d6152015-10-22 14:00:28 -0400783 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700784
Jamie Madillb0a838b2016-11-13 20:02:12 -0500785 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
786
Brandon Joneseb994362014-09-24 10:27:28 -0700787 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400788 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
789 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700790 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400791 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400792 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700793
Jamie Madilld3dfda22015-07-06 08:28:49 -0400794 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700795 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400796 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700797 }
798
Jamie Madill334d6152015-10-22 14:00:28 -0400799 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700800 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400801
Jamie Madillada9ecc2015-08-17 12:53:37 -0400802 ShaderExecutableD3D *shaderExecutable = nullptr;
803
Jamie Madillb0a838b2016-11-13 20:02:12 -0500804 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
805 mStreamOutVaryings, separateAttribs,
806 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400807
Brandon Joneseb994362014-09-24 10:27:28 -0700808 if (!shaderExecutable)
809 {
Jamie Madillf6113162015-05-07 11:49:21 -0400810 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500811 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700812 }
813
814 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400815 VertexExecutable::Signature signature;
816 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700817
818 // add new binary
Jamie Madill417df922017-01-12 09:23:07 -0500819 mVertexExecutables.push_back(
820 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700821
822 stream->skip(vertexShaderSize);
823 }
824
825 const size_t pixelShaderCount = stream->readInt<unsigned int>();
826 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
827 {
828 const size_t outputCount = stream->readInt<unsigned int>();
829 std::vector<GLenum> outputs(outputCount);
830 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
831 {
832 stream->readInt(&outputs[outputIndex]);
833 }
834
Jamie Madill334d6152015-10-22 14:00:28 -0400835 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700836 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400837 ShaderExecutableD3D *shaderExecutable = nullptr;
838
Jamie Madillb0a838b2016-11-13 20:02:12 -0500839 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
840 mStreamOutVaryings, separateAttribs,
841 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700842
843 if (!shaderExecutable)
844 {
Jamie Madillf6113162015-05-07 11:49:21 -0400845 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500846 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700847 }
848
849 // add new binary
Jamie Madill417df922017-01-12 09:23:07 -0500850 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700851
852 stream->skip(pixelShaderSize);
853 }
854
Jamie Madill4e31ad52015-10-29 10:32:57 -0400855 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
856 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700857 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400858 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
859 if (geometryShaderSize == 0)
860 {
Jamie Madill417df922017-01-12 09:23:07 -0500861 mGeometryExecutables[geometryExeIndex] = nullptr;
Jamie Madill4e31ad52015-10-29 10:32:57 -0400862 continue;
863 }
864
Brandon Joneseb994362014-09-24 10:27:28 -0700865 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400866
Jamie Madillb0a838b2016-11-13 20:02:12 -0500867 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
868 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
Jamie Madill417df922017-01-12 09:23:07 -0500869 &mGeometryExecutables[geometryExeIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -0700870
Jamie Madill417df922017-01-12 09:23:07 -0500871 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700872 {
Jamie Madillf6113162015-05-07 11:49:21 -0400873 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500874 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700875 }
876 stream->skip(geometryShaderSize);
877 }
878
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700879 initializeUniformStorage();
880
Jamie Madillb0a838b2016-11-13 20:02:12 -0500881 return true;
Brandon Jones22502d52014-08-29 16:58:36 -0700882}
883
Geoff Langb543aff2014-09-30 14:52:54 -0400884gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700885{
Austin Kinross137b1512015-06-17 16:14:53 -0700886 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400887 // When we load the binary again later, we can validate the device identifier before trying to
888 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700889 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400890 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
891 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700892
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500893 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
894
Jamie Madill8047c0d2016-03-07 13:02:12 -0500895 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400896 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500897 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400898 }
899
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700900 stream->writeInt(mSamplersPS.size());
901 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
902 {
903 stream->writeInt(mSamplersPS[i].active);
904 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
905 stream->writeInt(mSamplersPS[i].textureType);
906 }
907
908 stream->writeInt(mSamplersVS.size());
909 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
910 {
911 stream->writeInt(mSamplersVS[i].active);
912 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
913 stream->writeInt(mSamplersVS[i].textureType);
914 }
915
916 stream->writeInt(mUsedVertexSamplerRange);
917 stream->writeInt(mUsedPixelSamplerRange);
918
Jamie Madill62d31cb2015-09-11 13:25:51 -0400919 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -0400920 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700921 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400922 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -0400923 stream->writeIntOrNegOne(uniform->psRegisterIndex);
924 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400925 stream->writeInt(uniform->registerCount);
926 stream->writeInt(uniform->registerElement);
927 }
928
Jamie Madill51f522f2016-12-21 15:10:55 -0500929 // Ensure we init the uniform block structure data if we should.
930 // http://anglebug.com/1637
931 ensureUniformBlocksInitialized();
932
Jamie Madill4a3c2342015-10-08 12:58:45 -0400933 stream->writeInt(mD3DUniformBlocks.size());
934 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
935 {
Jamie Madille2e406c2016-06-02 13:04:10 -0400936 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
937 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700938 }
939
Jamie Madill9fc36822015-11-18 13:08:07 -0500940 stream->writeInt(mStreamOutVaryings.size());
941 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -0700942 {
Brandon Joneseb994362014-09-24 10:27:28 -0700943 stream->writeString(varying.semanticName);
944 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500945 stream->writeInt(varying.componentCount);
946 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700947 }
948
Brandon Jones22502d52014-08-29 16:58:36 -0700949 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400950 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500951 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700952 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400953 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500954 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700955 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700956 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400957 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700958
Brandon Joneseb994362014-09-24 10:27:28 -0700959 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700960 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -0400961 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
962 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700963 {
Brandon Joneseb994362014-09-24 10:27:28 -0700964 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -0700965 stream->writeInt(variable.type);
966 stream->writeString(variable.name);
967 stream->writeString(variable.source);
968 stream->writeInt(variable.outputIndex);
969 }
970
Jamie Madill4e31ad52015-10-29 10:32:57 -0400971 stream->writeString(mGeometryShaderPreamble);
972
Brandon Joneseb994362014-09-24 10:27:28 -0700973 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -0400974 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
975 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700976 {
Jamie Madill417df922017-01-12 09:23:07 -0500977 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700978
Jamie Madilld3dfda22015-07-06 08:28:49 -0400979 const auto &inputLayout = vertexExecutable->inputs();
980 stream->writeInt(inputLayout.size());
981
982 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700983 {
Jamie Madille2e406c2016-06-02 13:04:10 -0400984 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -0700985 }
986
987 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
988 stream->writeInt(vertexShaderSize);
989
990 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
991 stream->writeBytes(vertexBlob, vertexShaderSize);
992 }
993
994 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -0400995 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
996 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700997 {
Jamie Madill417df922017-01-12 09:23:07 -0500998 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700999
1000 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1001 stream->writeInt(outputs.size());
1002 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1003 {
1004 stream->writeInt(outputs[outputIndex]);
1005 }
1006
1007 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1008 stream->writeInt(pixelShaderSize);
1009
1010 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1011 stream->writeBytes(pixelBlob, pixelShaderSize);
1012 }
1013
Jamie Madill417df922017-01-12 09:23:07 -05001014 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001015 {
Jamie Madill417df922017-01-12 09:23:07 -05001016 if (geometryExe == nullptr)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001017 {
1018 stream->writeInt(0);
1019 continue;
1020 }
1021
Jamie Madill417df922017-01-12 09:23:07 -05001022 size_t geometryShaderSize = geometryExe->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001023 stream->writeInt(geometryShaderSize);
Jamie Madill417df922017-01-12 09:23:07 -05001024 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001025 }
1026
Jamie Madill51f522f2016-12-21 15:10:55 -05001027 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001028}
1029
Geoff Langc5629752015-12-07 16:29:04 -05001030void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1031{
1032}
1033
Jamie Madill334d6152015-10-22 14:00:28 -04001034gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1035 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001036{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001037 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001038
Jamie Madill85a18042015-03-05 15:41:41 -05001039 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001040 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001041
1042 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1043 {
1044 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1045
1046 if (colorbuffer)
1047 {
Jamie Madill334d6152015-10-22 14:00:28 -04001048 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1049 ? GL_COLOR_ATTACHMENT0
1050 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001051 }
1052 else
1053 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001054 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001055 }
1056 }
1057
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001058 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001059}
1060
Jamie Madill97399232014-12-23 12:31:15 -05001061gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001062 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001063 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001064{
1065 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1066 {
1067 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1068 {
Geoff Langb543aff2014-09-30 14:52:54 -04001069 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001070 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001071 }
1072 }
1073
Jamie Madill334d6152015-10-22 14:00:28 -04001074 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1075 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001076
1077 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001078 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001079
1080 gl::InfoLog tempInfoLog;
1081 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1082
Jamie Madill01074252016-11-28 15:55:51 -05001083 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001084 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001085 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001086 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001087
Jamie Madill97399232014-12-23 12:31:15 -05001088 if (pixelExecutable)
1089 {
Jamie Madill417df922017-01-12 09:23:07 -05001090 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
Jamie Madill97399232014-12-23 12:31:15 -05001091 }
1092 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001093 {
Yuly Novikovafcc41c2016-12-13 12:59:39 -05001094 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1095 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001096 }
Brandon Jones22502d52014-08-29 16:58:36 -07001097
Geoff Langb543aff2014-09-30 14:52:54 -04001098 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001099 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001100}
1101
Jamie Madilld3dfda22015-07-06 08:28:49 -04001102gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001103 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001104 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001105{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001106 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001107
1108 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1109 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001110 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001111 {
Geoff Langb543aff2014-09-30 14:52:54 -04001112 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001113 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001114 }
1115 }
1116
Brandon Jones22502d52014-08-29 16:58:36 -07001117 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001118 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001119 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001120
1121 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001122 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001123
1124 gl::InfoLog tempInfoLog;
1125 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1126
Jamie Madill01074252016-11-28 15:55:51 -05001127 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001128 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001129 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001130 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001131
Jamie Madill97399232014-12-23 12:31:15 -05001132 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001133 {
Jamie Madill417df922017-01-12 09:23:07 -05001134 mVertexExecutables.push_back(
1135 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001136 }
Jamie Madill97399232014-12-23 12:31:15 -05001137 else if (!infoLog)
1138 {
Yuly Novikovafcc41c2016-12-13 12:59:39 -05001139 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1140 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001141 }
Brandon Jones22502d52014-08-29 16:58:36 -07001142
Geoff Langb543aff2014-09-30 14:52:54 -04001143 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001144 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001145}
1146
Jamie Madill9082b982016-04-27 15:21:51 -04001147gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001148 GLenum drawMode,
1149 ShaderExecutableD3D **outExecutable,
1150 gl::InfoLog *infoLog)
1151{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001152 if (outExecutable)
1153 {
1154 *outExecutable = nullptr;
1155 }
1156
Austin Kinross88829e82016-01-12 13:04:41 -08001157 // Return a null shader if the current rendering doesn't use a geometry shader
1158 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001159 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001160 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001161 }
1162
Jamie Madill4e31ad52015-10-29 10:32:57 -04001163 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1164
Jamie Madill417df922017-01-12 09:23:07 -05001165 if (mGeometryExecutables[geometryShaderType] != nullptr)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001166 {
1167 if (outExecutable)
1168 {
Jamie Madill417df922017-01-12 09:23:07 -05001169 *outExecutable = mGeometryExecutables[geometryShaderType];
Jamie Madill4e31ad52015-10-29 10:32:57 -04001170 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001171 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001172 }
1173
1174 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001175 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001176 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001177
1178 gl::InfoLog tempInfoLog;
1179 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1180
Jamie Madill417df922017-01-12 09:23:07 -05001181 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001182 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001183 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Jamie Madill417df922017-01-12 09:23:07 -05001184 angle::CompilerWorkaroundsD3D(), &mGeometryExecutables[geometryShaderType]);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001185
1186 if (!infoLog && error.isError())
1187 {
Yuly Novikovafcc41c2016-12-13 12:59:39 -05001188 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1189 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001190 }
1191
1192 if (outExecutable)
1193 {
Jamie Madill417df922017-01-12 09:23:07 -05001194 *outExecutable = mGeometryExecutables[geometryShaderType];
Jamie Madill4e31ad52015-10-29 10:32:57 -04001195 }
1196 return error;
1197}
1198
Jamie Madill01074252016-11-28 15:55:51 -05001199class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001200{
Jamie Madill01074252016-11-28 15:55:51 -05001201 public:
1202 GetExecutableTask(ProgramD3D *program)
1203 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001204 {
Brandon Joneseb994362014-09-24 10:27:28 -07001205 }
1206
Jamie Madill01074252016-11-28 15:55:51 -05001207 virtual gl::Error run() = 0;
1208
1209 void operator()() override { mError = run(); }
1210
1211 const gl::Error &getError() const { return mError; }
1212 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1213 ShaderExecutableD3D *getResult() { return mResult; }
1214
1215 protected:
1216 ProgramD3D *mProgram;
1217 gl::Error mError;
1218 gl::InfoLog mInfoLog;
1219 ShaderExecutableD3D *mResult;
1220};
1221
1222class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1223{
1224 public:
1225 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1226 gl::Error run() override
1227 {
1228 const auto &defaultInputLayout =
1229 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1230
1231 ANGLE_TRY(
1232 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1233
1234 return gl::NoError();
1235 }
1236};
1237
1238class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1239{
1240 public:
1241 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1242 gl::Error run() override
1243 {
1244 const auto &defaultPixelOutput =
1245 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1246
1247 ANGLE_TRY(
1248 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1249
1250 return gl::NoError();
1251 }
1252};
1253
1254class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1255{
1256 public:
1257 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1258 : GetExecutableTask(program), mContextState(contextState)
1259 {
1260 }
1261
1262 gl::Error run() override
1263 {
1264 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1265 // D3D11.
1266 if (mProgram->usesGeometryShader(GL_POINTS))
1267 {
1268 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1269 &mResult, &mInfoLog));
1270 }
1271
1272 return gl::NoError();
1273 }
1274
1275 private:
1276 const gl::ContextState &mContextState;
1277};
1278
1279LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1280 gl::InfoLog &infoLog)
1281{
1282 // Ensure the compiler is initialized to avoid race conditions.
1283 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1284
1285 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1286
1287 GetVertexExecutableTask vertexTask(this);
1288 GetPixelExecutableTask pixelTask(this);
1289 GetGeometryExecutableTask geometryTask(this, contextState);
1290
1291 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1292 workerPool->postWorkerTask(&pixelTask),
1293 workerPool->postWorkerTask(&geometryTask)}};
1294
1295 WaitableEvent::WaitMany(&waitEvents);
1296
1297 infoLog << vertexTask.getInfoLog().str();
1298 infoLog << pixelTask.getInfoLog().str();
1299 infoLog << geometryTask.getInfoLog().str();
1300
1301 ANGLE_TRY(vertexTask.getError());
1302 ANGLE_TRY(pixelTask.getError());
1303 ANGLE_TRY(geometryTask.getError());
1304
1305 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1306 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1307 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1308
Jamie Madill48ef11b2016-04-27 15:21:52 -04001309 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001310
1311 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001312 {
Jamie Madill334d6152015-10-22 14:00:28 -04001313 // Geometry shaders are currently only used internally, so there is no corresponding shader
1314 // object at the interface level. For now the geometry shader debug info is prepended to
1315 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001316 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001317 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001318 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1319 }
1320
1321 if (defaultVertexExecutable)
1322 {
1323 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1324 }
1325
1326 if (defaultPixelExecutable)
1327 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001328 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001329 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001330 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1331 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001332
Jamie Madillb0a838b2016-11-13 20:02:12 -05001333 return (defaultVertexExecutable && defaultPixelExecutable &&
1334 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001335}
1336
Jamie Madill192745a2016-12-22 15:58:21 -05001337LinkResult ProgramD3D::link(const gl::ContextState &data,
1338 const gl::VaryingPacking &packing,
1339 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001340{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001341 reset();
1342
Jamie Madill417df922017-01-12 09:23:07 -05001343 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1344 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1345
1346 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1347 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1348
1349 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1350 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1351
1352 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1353 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1354
1355 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
Austin Kinross02df7962015-07-01 10:03:42 -07001356 {
Jamie Madill417df922017-01-12 09:23:07 -05001357 if (fragmentShaderD3D->usesFrontFacing())
Austin Kinross02df7962015-07-01 10:03:42 -07001358 {
Jamie Madill417df922017-01-12 09:23:07 -05001359 infoLog << "The current renderer doesn't support gl_FrontFacing";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001360 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001361 }
1362 }
1363
Jamie Madill417df922017-01-12 09:23:07 -05001364 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1365 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1366 // intelligently, but D3D9 assumes one semantic per register.
1367 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1368 packing.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
1369 {
1370 infoLog << "Cannot pack these varyings on D3D9.";
1371 return false;
1372 }
1373
1374 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
1375 BuiltinVaryingsD3D builtins(metadata, packing);
1376
1377 mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, packing, builtins, &mPixelHLSL,
1378 &mVertexHLSL);
1379
1380 mUsesPointSize = vertexShaderD3D->usesPointSize();
1381 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1382 mUsesFragDepth = metadata.usesFragDepth();
1383
1384 // Cache if we use flat shading
1385 mUsesFlatInterpolation = (FindFlatInterpolationVarying(fragmentShader->getVaryings()) ||
1386 FindFlatInterpolationVarying(vertexShader->getVaryings()));
1387
1388 if (mRenderer->getMajorShaderModel() >= 4)
1389 {
1390 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(packing, builtins);
1391 }
1392
1393 initAttribLocationsToD3DSemantic();
1394
1395 defineUniformsAndAssignRegisters();
1396
1397 gatherTransformFeedbackVaryings(packing, builtins[SHADER_VERTEX]);
1398
1399 LinkResult result = compileProgramExecutables(data, infoLog);
1400 if (result.isError())
1401 {
1402 infoLog << result.getError().getMessage();
1403 return result;
1404 }
1405 else if (!result.getResult())
1406 {
1407 infoLog << "Failed to create D3D shaders.";
1408 return result;
1409 }
1410
1411 initUniformBlockInfo();
1412
Jamie Madillb0a838b2016-11-13 20:02:12 -05001413 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001414}
1415
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001416GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001417{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001418 // TODO(jmadill): Do something useful here?
1419 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001420}
1421
Jamie Madill417df922017-01-12 09:23:07 -05001422void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001423{
Jamie Madill417df922017-01-12 09:23:07 -05001424 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1425
1426 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001427 {
Jamie Madill417df922017-01-12 09:23:07 -05001428 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001429 continue;
1430
Jamie Madill417df922017-01-12 09:23:07 -05001431 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001432 continue;
1433
Jamie Madill417df922017-01-12 09:23:07 -05001434 size_t dataSize = getUniformBlockInfo(vertexBlock);
1435 mBlockDataSizes[vertexBlock.name] = dataSize;
1436 }
1437
1438 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1439
1440 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1441 {
1442 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1443 continue;
1444
1445 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
1446 continue;
1447
1448 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1449 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001450 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001451}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001452
Jamie Madill51f522f2016-12-21 15:10:55 -05001453void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001454{
Jamie Madill51f522f2016-12-21 15:10:55 -05001455 // Lazy init.
1456 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1457 {
1458 return;
1459 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001460
1461 // Assign registers and update sizes.
Jamie Madill417df922017-01-12 09:23:07 -05001462 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1463 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001464
Jamie Madill48ef11b2016-04-27 15:21:52 -04001465 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001466 {
1467 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1468
Jamie Madill4a3c2342015-10-08 12:58:45 -04001469 D3DUniformBlock d3dUniformBlock;
1470
Jamie Madill62d31cb2015-09-11 13:25:51 -04001471 if (uniformBlock.vertexStaticUse)
1472 {
1473 unsigned int baseRegister =
1474 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001475 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001476 }
1477
1478 if (uniformBlock.fragmentStaticUse)
1479 {
1480 unsigned int baseRegister =
1481 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001482 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001483 }
1484
Jamie Madill4a3c2342015-10-08 12:58:45 -04001485 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001486 }
1487}
1488
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001489void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001490{
1491 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001492 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001493 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001494 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001495 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001496 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001497 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001498 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001499 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001500 vertexRegisters = std::max(vertexRegisters,
1501 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001502 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001504 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001505 fragmentRegisters = std::max(
1506 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001507 }
1508 }
1509 }
1510
Jamie Madill417df922017-01-12 09:23:07 -05001511 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
1512 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001513}
1514
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001515gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001516{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001517 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001518
Jamie Madill01074252016-11-28 15:55:51 -05001519 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001520
Jamie Madill62d31cb2015-09-11 13:25:51 -04001521 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001522 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001523 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001524 }
1525
Jamie Madill51f522f2016-12-21 15:10:55 -05001526 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001527}
1528
Jamie Madill9082b982016-04-27 15:21:51 -04001529gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001530{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001531 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001532 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001533 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001534 }
1535
Jamie Madill51f522f2016-12-21 15:10:55 -05001536 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001537
Jamie Madill03260fa2015-06-22 13:57:22 -04001538 mVertexUBOCache.clear();
1539 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001540
1541 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1542 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1543
Jamie Madill4a3c2342015-10-08 12:58:45 -04001544 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001545 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001546 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001547 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001548 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001549
Brandon Jones18bd4102014-09-22 14:21:44 -07001550 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001551 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001552 {
1553 continue;
1554 }
1555
Jamie Madill4a3c2342015-10-08 12:58:45 -04001556 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001557 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001558 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001559 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001560
Jamie Madill969194d2015-07-20 14:36:56 -04001561 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001562 {
1563 mVertexUBOCache.resize(registerIndex + 1, -1);
1564 }
1565
1566 ASSERT(mVertexUBOCache[registerIndex] == -1);
1567 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001568 }
1569
Jamie Madill4a3c2342015-10-08 12:58:45 -04001570 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001571 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001572 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001573 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001574
1575 if (mFragmentUBOCache.size() <= registerIndex)
1576 {
1577 mFragmentUBOCache.resize(registerIndex + 1, -1);
1578 }
1579
1580 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1581 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001582 }
1583 }
1584
Jamie Madill03260fa2015-06-22 13:57:22 -04001585 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001586}
1587
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001588void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001589{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001590 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001591 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001592 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001593 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001594}
1595
Jamie Madill334d6152015-10-22 14:00:28 -04001596void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001597{
1598 setUniform(location, count, v, GL_FLOAT);
1599}
1600
1601void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1602{
1603 setUniform(location, count, v, GL_FLOAT_VEC2);
1604}
1605
1606void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1607{
1608 setUniform(location, count, v, GL_FLOAT_VEC3);
1609}
1610
1611void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1612{
1613 setUniform(location, count, v, GL_FLOAT_VEC4);
1614}
1615
Jamie Madill334d6152015-10-22 14:00:28 -04001616void ProgramD3D::setUniformMatrix2fv(GLint location,
1617 GLsizei count,
1618 GLboolean transpose,
1619 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001620{
1621 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1622}
1623
Jamie Madill334d6152015-10-22 14:00:28 -04001624void ProgramD3D::setUniformMatrix3fv(GLint location,
1625 GLsizei count,
1626 GLboolean transpose,
1627 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001628{
1629 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1630}
1631
Jamie Madill334d6152015-10-22 14:00:28 -04001632void ProgramD3D::setUniformMatrix4fv(GLint location,
1633 GLsizei count,
1634 GLboolean transpose,
1635 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001636{
1637 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1638}
1639
Jamie Madill334d6152015-10-22 14:00:28 -04001640void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1641 GLsizei count,
1642 GLboolean transpose,
1643 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001644{
1645 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1646}
1647
Jamie Madill334d6152015-10-22 14:00:28 -04001648void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1649 GLsizei count,
1650 GLboolean transpose,
1651 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001652{
1653 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1654}
1655
Jamie Madill334d6152015-10-22 14:00:28 -04001656void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1657 GLsizei count,
1658 GLboolean transpose,
1659 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001660{
1661 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1662}
1663
Jamie Madill334d6152015-10-22 14:00:28 -04001664void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1665 GLsizei count,
1666 GLboolean transpose,
1667 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001668{
1669 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1670}
1671
Jamie Madill334d6152015-10-22 14:00:28 -04001672void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1673 GLsizei count,
1674 GLboolean transpose,
1675 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001676{
1677 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1678}
1679
Jamie Madill334d6152015-10-22 14:00:28 -04001680void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1681 GLsizei count,
1682 GLboolean transpose,
1683 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001684{
1685 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1686}
1687
1688void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1689{
1690 setUniform(location, count, v, GL_INT);
1691}
1692
1693void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1694{
1695 setUniform(location, count, v, GL_INT_VEC2);
1696}
1697
1698void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1699{
1700 setUniform(location, count, v, GL_INT_VEC3);
1701}
1702
1703void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1704{
1705 setUniform(location, count, v, GL_INT_VEC4);
1706}
1707
1708void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1709{
1710 setUniform(location, count, v, GL_UNSIGNED_INT);
1711}
1712
1713void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1714{
1715 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1716}
1717
1718void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1719{
1720 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1721}
1722
1723void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1724{
1725 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1726}
1727
Jamie Madill4a3c2342015-10-08 12:58:45 -04001728void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1729 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001730{
1731}
1732
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001733void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001734{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001735 D3DUniformMap uniformMap;
Jamie Madill417df922017-01-12 09:23:07 -05001736 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1737 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Xinghua Cao2cd9d7e2016-12-13 15:07:05 +08001738
Jamie Madill417df922017-01-12 09:23:07 -05001739 {
1740 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001741 {
Jamie Madill417df922017-01-12 09:23:07 -05001742 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001743 }
1744 }
Jamie Madill417df922017-01-12 09:23:07 -05001745
1746 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1747 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001748 {
Jamie Madill417df922017-01-12 09:23:07 -05001749 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001750 {
Jamie Madill417df922017-01-12 09:23:07 -05001751 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001752 }
1753 }
1754
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001755 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001756 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001757 {
1758 if (!glUniform.isInDefaultBlock())
1759 continue;
1760
1761 auto mapEntry = uniformMap.find(glUniform.name);
1762 ASSERT(mapEntry != uniformMap.end());
1763 mD3DUniforms.push_back(mapEntry->second);
1764 }
1765
Jamie Madill62d31cb2015-09-11 13:25:51 -04001766 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001767 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001768}
1769
Jamie Madill91445bc2015-09-23 16:47:53 -04001770void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001771 const sh::Uniform &uniform,
1772 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001773{
Olli Etuaho96963162016-03-21 11:54:33 +02001774 // Samplers get their registers assigned in assignAllSamplerRegisters.
1775 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001776 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001777 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001778 return;
1779 }
1780
Jamie Madill91445bc2015-09-23 16:47:53 -04001781 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1782
1783 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1784 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001785 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001786 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001787
Jamie Madill91445bc2015-09-23 16:47:53 -04001788 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001789}
1790
Jamie Madill62d31cb2015-09-11 13:25:51 -04001791D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1792{
1793 for (D3DUniform *d3dUniform : mD3DUniforms)
1794 {
1795 if (d3dUniform->name == name)
1796 {
1797 return d3dUniform;
1798 }
1799 }
1800
1801 return nullptr;
1802}
1803
Jamie Madill91445bc2015-09-23 16:47:53 -04001804void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001805 const sh::ShaderVariable &uniform,
1806 const std::string &fullName,
1807 sh::HLSLBlockEncoder *encoder,
1808 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001809{
1810 if (uniform.isStruct())
1811 {
1812 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1813 {
1814 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1815
Jamie Madill55def582015-05-04 11:24:57 -04001816 if (encoder)
1817 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001818
1819 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1820 {
Jamie Madill334d6152015-10-22 14:00:28 -04001821 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001822 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1823
Olli Etuaho96963162016-03-21 11:54:33 +02001824 // Samplers get their registers assigned in assignAllSamplerRegisters.
1825 // Also they couldn't use the same encoder as the rest of the struct, since they are
1826 // extracted out of the struct by the shader translator.
1827 if (gl::IsSamplerType(field.type))
1828 {
1829 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
1830 }
1831 else
1832 {
1833 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
1834 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001835 }
1836
Jamie Madill55def582015-05-04 11:24:57 -04001837 if (encoder)
1838 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001839 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001840 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001841 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001842
1843 // Not a struct. Arrays are treated as aggregate types.
1844 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001845 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001846 encoder->enterAggregateType();
1847 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001848
Jamie Madill62d31cb2015-09-11 13:25:51 -04001849 // Advance the uniform offset, to track registers allocation for structs
1850 sh::BlockMemberInfo blockInfo =
1851 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1852 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001853
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001854 auto uniformMapEntry = uniformMap->find(fullName);
1855 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001856
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001857 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001858 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001859 d3dUniform = uniformMapEntry->second;
1860 }
1861 else
1862 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001863 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001864 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001865 }
1866
1867 if (encoder)
1868 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001869 d3dUniform->registerElement =
1870 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001871 unsigned int reg =
1872 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04001873 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001874 {
1875 d3dUniform->psRegisterIndex = reg;
1876 }
Jamie Madill91445bc2015-09-23 16:47:53 -04001877 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04001878 {
Jamie Madill417df922017-01-12 09:23:07 -05001879 ASSERT(shaderType == GL_VERTEX_SHADER);
1880 d3dUniform->vsRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001881 }
Jamie Madillfb536032015-09-11 13:19:49 -04001882
1883 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04001884 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04001885 {
1886 encoder->exitAggregateType();
1887 }
1888 }
1889}
1890
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001891template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001892void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001893{
Jamie Madill334d6152015-10-22 14:00:28 -04001894 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001895 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
1896
Jamie Madill62d31cb2015-09-11 13:25:51 -04001897 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001898
Jamie Madill62d31cb2015-09-11 13:25:51 -04001899 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001900 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001901 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001902
1903 if (targetUniform->type == targetUniformType)
1904 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001905 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001906
Jamie Madill62d31cb2015-09-11 13:25:51 -04001907 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001908 {
Jamie Madill334d6152015-10-22 14:00:28 -04001909 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001910 const T *source = v + (i * components);
1911
1912 for (int c = 0; c < components; c++)
1913 {
1914 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
1915 }
1916 for (int c = components; c < 4; c++)
1917 {
1918 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
1919 }
1920 }
1921 }
1922 else if (targetUniform->type == targetBoolType)
1923 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001924 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001925
Jamie Madill62d31cb2015-09-11 13:25:51 -04001926 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927 {
Jamie Madill334d6152015-10-22 14:00:28 -04001928 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001929 const T *source = v + (i * components);
1930
1931 for (int c = 0; c < components; c++)
1932 {
Jamie Madill334d6152015-10-22 14:00:28 -04001933 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
1934 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001935 }
1936 for (int c = components; c < 4; c++)
1937 {
1938 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
1939 }
1940 }
1941 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001942 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943 {
1944 ASSERT(targetUniformType == GL_INT);
1945
Jamie Madill62d31cb2015-09-11 13:25:51 -04001946 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001947
1948 bool wasDirty = targetUniform->dirty;
1949
Jamie Madill62d31cb2015-09-11 13:25:51 -04001950 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001951 {
Jamie Madill334d6152015-10-22 14:00:28 -04001952 GLint *dest = target + (i * 4);
1953 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001954
1955 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
1956 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
1957 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
1958 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
1959 }
1960
1961 if (!wasDirty && targetUniform->dirty)
1962 {
1963 mDirtySamplerMapping = true;
1964 }
Brandon Jones18bd4102014-09-22 14:21:44 -07001965 }
Jamie Madill334d6152015-10-22 14:00:28 -04001966 else
1967 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001968}
1969
1970template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04001971void ProgramD3D::setUniformMatrixfv(GLint location,
1972 GLsizei countIn,
1973 GLboolean transpose,
1974 const GLfloat *value,
1975 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001976{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001977 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001978
Jamie Madill62d31cb2015-09-11 13:25:51 -04001979 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001980 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001981 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001982
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001983 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001984 GLfloat *target =
1985 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986
Jamie Madill62d31cb2015-09-11 13:25:51 -04001987 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001988 {
1989 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
1990 if (transpose == GL_FALSE)
1991 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08001992 targetUniform->dirty =
1993 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001994 }
1995 else
1996 {
Jamie Madill334d6152015-10-22 14:00:28 -04001997 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08001998 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001999 }
2000 target += targetMatrixStride;
2001 value += cols * rows;
2002 }
2003}
2004
Jamie Madill4a3c2342015-10-08 12:58:45 -04002005size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002006{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002007 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002008
Jamie Madill62d31cb2015-09-11 13:25:51 -04002009 // define member uniforms
2010 sh::Std140BlockEncoder std140Encoder;
2011 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2012 sh::BlockLayoutEncoder *encoder = nullptr;
2013
2014 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002015 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002016 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017 }
2018 else
2019 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002020 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002021 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002022
Jamie Madill39046162016-02-08 15:05:17 -05002023 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2024 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002025
2026 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002027}
2028
Jamie Madill62d31cb2015-09-11 13:25:51 -04002029void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002030{
Olli Etuaho96963162016-03-21 11:54:33 +02002031 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002032 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002033 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002034 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002035 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002036 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002037 }
2038}
2039
Olli Etuaho96963162016-03-21 11:54:33 +02002040void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002041{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002042 ASSERT(d3dUniform->isSampler());
Jamie Madill417df922017-01-12 09:23:07 -05002043 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2044 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2045 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) || fragmentShaderD3D->hasUniform(d3dUniform));
2046 if (vertexShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002047 {
Jamie Madill417df922017-01-12 09:23:07 -05002048 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2049 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
2050 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2051 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002052 }
Jamie Madill417df922017-01-12 09:23:07 -05002053 if (fragmentShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002054 {
Jamie Madill417df922017-01-12 09:23:07 -05002055 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2056 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
2057 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2058 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002059 }
2060}
2061
Jamie Madill62d31cb2015-09-11 13:25:51 -04002062// static
2063void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002064 GLenum samplerType,
2065 unsigned int samplerCount,
2066 std::vector<Sampler> &outSamplers,
2067 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002068{
2069 unsigned int samplerIndex = startSamplerIndex;
2070
2071 do
2072 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002073 ASSERT(samplerIndex < outSamplers.size());
2074 Sampler *sampler = &outSamplers[samplerIndex];
2075 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002076 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002077 sampler->logicalTextureUnit = 0;
2078 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002079 samplerIndex++;
2080 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002081}
2082
Brandon Jonesc9610c52014-08-25 17:02:59 -07002083void ProgramD3D::reset()
2084{
Jamie Madill417df922017-01-12 09:23:07 -05002085 SafeDeleteContainer(mVertexExecutables);
2086 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002087
Jamie Madill417df922017-01-12 09:23:07 -05002088 for (auto &element : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002089 {
Jamie Madill417df922017-01-12 09:23:07 -05002090 SafeDelete(element);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002091 }
Brandon Joneseb994362014-09-24 10:27:28 -07002092
Brandon Jones22502d52014-08-29 16:58:36 -07002093 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002094 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002095
2096 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002097 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002098 mUsesFragDepth = false;
2099 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002100 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002101 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002102
Jamie Madill62d31cb2015-09-11 13:25:51 -04002103 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002104 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002105
Jamie Madill417df922017-01-12 09:23:07 -05002106 SafeDelete(mVertexUniformStorage);
2107 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108
2109 mSamplersPS.clear();
2110 mSamplersVS.clear();
2111
2112 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002113 mUsedPixelSamplerRange = 0;
2114 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002115
Jamie Madill8047c0d2016-03-07 13:02:12 -05002116 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002117
Jamie Madill9fc36822015-11-18 13:08:07 -05002118 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002119
2120 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002121}
2122
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123unsigned int ProgramD3D::getSerial() const
2124{
2125 return mSerial;
2126}
2127
2128unsigned int ProgramD3D::issueSerial()
2129{
2130 return mCurrentSerial++;
2131}
2132
Jamie Madill8047c0d2016-03-07 13:02:12 -05002133void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002134{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002135 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002136 ASSERT(vertexShader != nullptr);
2137
2138 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002139 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002140 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002141 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2142 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002143
Jamie Madill8047c0d2016-03-07 13:02:12 -05002144 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002145 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002146 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002147 }
2148 }
Jamie Madill437d2662014-12-05 14:23:35 -05002149}
2150
Jamie Madill63805b42015-08-25 13:17:39 -04002151void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002152{
Jamie Madillbd136f92015-08-10 14:51:37 -04002153 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002154 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002155
Jamie Madill01074252016-11-28 15:55:51 -05002156 for (unsigned int locationIndex : IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002157 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002158 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002159
Jamie Madill8047c0d2016-03-07 13:02:12 -05002160 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002161 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002162 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002163 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002164 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002165 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002166 mCachedInputLayout[d3dSemantic] =
2167 GetVertexFormatType(vertexAttributes[locationIndex],
2168 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002169 }
2170 }
2171}
2172
Jamie Madill192745a2016-12-22 15:58:21 -05002173void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2174 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002175{
Jamie Madill9fc36822015-11-18 13:08:07 -05002176 const std::string &varyingSemantic =
2177 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2178
Jamie Madillccdf74b2015-08-18 10:46:12 -04002179 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002180 mStreamOutVaryings.clear();
2181
Jamie Madill48ef11b2016-04-27 15:21:52 -04002182 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002183 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2184 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002185 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002186 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2187 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002188 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002189 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002190 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002191 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2192 builtins.glPosition.index, 4, outputSlot));
2193 }
2194 }
2195 else if (tfVaryingName == "gl_FragCoord")
2196 {
2197 if (builtins.glFragCoord.enabled)
2198 {
2199 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2200 builtins.glFragCoord.index, 4, outputSlot));
2201 }
2202 }
2203 else if (tfVaryingName == "gl_PointSize")
2204 {
2205 if (builtins.glPointSize.enabled)
2206 {
2207 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2208 }
2209 }
2210 else
2211 {
Jamie Madill192745a2016-12-22 15:58:21 -05002212 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002213 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002214 const auto &varying = *registerInfo.packedVarying->varying;
2215 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002216 int componentCount = gl::VariableColumnCount(transposedType);
2217 ASSERT(!varying.isBuiltIn());
2218
Jamie Madill55c25d02015-11-18 13:08:08 -05002219 // Transform feedback for varying structs is underspecified.
2220 // See Khronos bug 9856.
2221 // TODO(jmadill): Figure out how to be spec-compliant here.
2222 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2223 continue;
2224
Jamie Madill9fc36822015-11-18 13:08:07 -05002225 // There can be more than one register assigned to a particular varying, and each
2226 // register needs its own stream out entry.
2227 if (tfVaryingName == varying.name)
2228 {
2229 mStreamOutVaryings.push_back(D3DVarying(
2230 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2231 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002232 }
2233 }
2234 }
2235}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002236
2237D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2238{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002239 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002240}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002241
2242bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2243{
2244 std::string baseName = blockName;
2245 gl::ParseAndStripArrayIndex(&baseName);
2246
2247 auto sizeIter = mBlockDataSizes.find(baseName);
2248 if (sizeIter == mBlockDataSizes.end())
2249 {
2250 *sizeOut = 0;
2251 return false;
2252 }
2253
2254 *sizeOut = sizeIter->second;
2255 return true;
2256}
2257
2258bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2259 sh::BlockMemberInfo *memberInfoOut) const
2260{
2261 auto infoIter = mBlockInfo.find(memberUniformName);
2262 if (infoIter == mBlockInfo.end())
2263 {
2264 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2265 return false;
2266 }
2267
2268 *memberInfoOut = infoIter->second;
2269 return true;
2270}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002271
2272void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2273 GLenum genMode,
2274 GLint components,
2275 const GLfloat *coeffs)
2276{
2277 UNREACHABLE();
2278}
2279
Jamie Madill8047c0d2016-03-07 13:02:12 -05002280} // namespace rx