blob: 85acde7ffdb3a05649d6d9a59d2770929ad303ef [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),
Xinghua Caob1239382016-12-13 15:07:05 +0800249 csRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400250 registerCount(0),
251 registerElement(0)
252{
253 // We use data storage for default block uniforms to cache values that are sent to D3D during
254 // rendering
255 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
256 if (defaultBlock)
257 {
258 size_t bytes = gl::VariableInternalSize(type) * elementCount();
259 data = new uint8_t[bytes];
260 memset(data, 0, bytes);
261
262 // TODO(jmadill): is this correct with non-square matrices?
263 registerCount = gl::VariableRowCount(type) * elementCount();
264 }
265}
266
267D3DUniform::~D3DUniform()
268{
269 SafeDeleteArray(data);
270}
271
272bool D3DUniform::isSampler() const
273{
274 return gl::IsSamplerType(type);
275}
276
277bool D3DUniform::isReferencedByVertexShader() const
278{
279 return vsRegisterIndex != GL_INVALID_INDEX;
280}
281
282bool D3DUniform::isReferencedByFragmentShader() const
283{
284 return psRegisterIndex != GL_INVALID_INDEX;
285}
286
Xinghua Caob1239382016-12-13 15:07:05 +0800287bool D3DUniform::isReferencedByComputeShader() const
288{
289 return csRegisterIndex != GL_INVALID_INDEX;
290}
291
Jamie Madill28afae52015-11-09 15:07:57 -0500292// D3DVarying Implementation
293
Jamie Madill9fc36822015-11-18 13:08:07 -0500294D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500295{
296}
297
Jamie Madill9fc36822015-11-18 13:08:07 -0500298D3DVarying::D3DVarying(const std::string &semanticNameIn,
299 unsigned int semanticIndexIn,
300 unsigned int componentCountIn,
301 unsigned int outputSlotIn)
302 : semanticName(semanticNameIn),
303 semanticIndex(semanticIndexIn),
304 componentCount(componentCountIn),
305 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500306{
307}
308
Jamie Madille39a3f02015-11-17 20:42:15 -0500309// ProgramD3DMetadata Implementation
310
Jamie Madillc9bde922016-07-24 17:58:50 -0400311ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500312 const ShaderD3D *vertexShader,
313 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400314 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
315 mShaderModelSuffix(renderer->getShaderModelSuffix()),
316 mUsesInstancedPointSpriteEmulation(
317 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
318 mUsesViewScale(renderer->presentPathFastEnabled()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500319 mVertexShader(vertexShader),
320 mFragmentShader(fragmentShader)
321{
322}
323
324int ProgramD3DMetadata::getRendererMajorShaderModel() const
325{
326 return mRendererMajorShaderModel;
327}
328
Jamie Madill9082b982016-04-27 15:21:51 -0400329bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500330{
Martin Radev1be913c2016-07-11 17:59:16 +0300331 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500332}
333
Jamie Madill48ef11b2016-04-27 15:21:52 -0400334bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500335{
Jamie Madill63286672015-11-24 13:00:08 -0500336 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500337}
338
339bool ProgramD3DMetadata::usesPointCoord() const
340{
341 return mFragmentShader->usesPointCoord();
342}
343
344bool ProgramD3DMetadata::usesFragCoord() const
345{
346 return mFragmentShader->usesFragCoord();
347}
348
349bool ProgramD3DMetadata::usesPointSize() const
350{
351 return mVertexShader->usesPointSize();
352}
353
354bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
355{
Jamie Madillc9bde922016-07-24 17:58:50 -0400356 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
357 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500358}
359
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800360bool ProgramD3DMetadata::usesViewScale() const
361{
362 return mUsesViewScale;
363}
364
Jamie Madille39a3f02015-11-17 20:42:15 -0500365bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
366{
Jamie Madillc9bde922016-07-24 17:58:50 -0400367 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500368 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400369 // Even with a geometry shader, the app can render triangles or lines and reference
370 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
371 // simplicity, we always add this to the vertex shader when the fragment shader
372 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500373 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
374 usesInsertedPointCoordValue();
375}
376
377bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
378{
379 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
380 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
381 // the vertex shader in this case. This saves us 1 output vector.
382 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
383}
384
385bool ProgramD3DMetadata::usesSystemValuePointSize() const
386{
387 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
388}
389
390bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
391{
392 return mFragmentShader->usesMultipleRenderTargets();
393}
394
395GLint ProgramD3DMetadata::getMajorShaderVersion() const
396{
397 return mVertexShader->getData().getShaderVersion();
398}
399
400const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
401{
402 return mFragmentShader;
403}
404
Jamie Madill28afae52015-11-09 15:07:57 -0500405// ProgramD3D Implementation
406
Jamie Madilld3dfda22015-07-06 08:28:49 -0400407ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
408 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500409 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400410 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700411{
Brandon Joneseb994362014-09-24 10:27:28 -0700412}
413
414ProgramD3D::VertexExecutable::~VertexExecutable()
415{
416 SafeDelete(mShaderExecutable);
417}
418
Jamie Madilld3dfda22015-07-06 08:28:49 -0400419// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500420ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
421 GLenum type)
422{
423 switch (type)
424 {
425 case GL_INT:
426 return HLSLAttribType::SIGNED_INT;
427 case GL_UNSIGNED_INT:
428 return HLSLAttribType::UNSIGNED_INT;
429 case GL_SIGNED_NORMALIZED:
430 case GL_UNSIGNED_NORMALIZED:
431 case GL_FLOAT:
432 return HLSLAttribType::FLOAT;
433 default:
434 UNREACHABLE();
435 return HLSLAttribType::FLOAT;
436 }
437}
438
439// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400440void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
441 const gl::InputLayout &inputLayout,
442 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700443{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500444 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400445
446 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700447 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400448 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500449 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
450 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400451
Jamie Madillbdec2f42016-03-02 16:35:32 -0500452 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
453 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
454 continue;
455
456 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
457 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700458 }
Brandon Joneseb994362014-09-24 10:27:28 -0700459}
460
Jamie Madilld3dfda22015-07-06 08:28:49 -0400461bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
462{
Jamie Madillbd136f92015-08-10 14:51:37 -0400463 size_t limit = std::max(mSignature.size(), signature.size());
464 for (size_t index = 0; index < limit; ++index)
465 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500466 // treat undefined indexes as FLOAT
467 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
468 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400469 if (a != b)
470 return false;
471 }
472
473 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400474}
475
476ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
477 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400478 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700479{
480}
481
482ProgramD3D::PixelExecutable::~PixelExecutable()
483{
484 SafeDelete(mShaderExecutable);
485}
486
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700487ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
488{
489}
490
Geoff Lang7dd2e102014-11-10 15:19:26 -0500491unsigned int ProgramD3D::mCurrentSerial = 1;
492
Jamie Madill48ef11b2016-04-27 15:21:52 -0400493ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
494 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700495 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800496 mDynamicHLSL(nullptr),
497 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
498 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700499 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400500 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800501 mVertexUniformStorage(nullptr),
502 mFragmentUniformStorage(nullptr),
503 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700504 mUsedVertexSamplerRange(0),
505 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800506 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700507 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500508 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700509{
Brandon Joneseb994362014-09-24 10:27:28 -0700510 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700511}
512
513ProgramD3D::~ProgramD3D()
514{
515 reset();
516 SafeDelete(mDynamicHLSL);
517}
518
Brandon Jones44151a92014-09-10 11:32:25 -0700519bool ProgramD3D::usesPointSpriteEmulation() const
520{
521 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
522}
523
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400524bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700525{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400526 if (drawMode != GL_POINTS)
527 {
528 return mUsesFlatInterpolation;
529 }
530
Cooper Partine6664f02015-01-09 16:22:24 -0800531 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
532}
533
534bool ProgramD3D::usesInstancedPointSpriteEmulation() const
535{
536 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700537}
538
Jamie Madill334d6152015-10-22 14:00:28 -0400539GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
540 unsigned int samplerIndex,
541 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700542{
543 GLint logicalTextureUnit = -1;
544
545 switch (type)
546 {
Jamie Madill334d6152015-10-22 14:00:28 -0400547 case gl::SAMPLER_PIXEL:
548 ASSERT(samplerIndex < caps.maxTextureImageUnits);
549 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
550 {
551 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
552 }
553 break;
554 case gl::SAMPLER_VERTEX:
555 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
556 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
557 {
558 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
559 }
560 break;
Xinghua Caob1239382016-12-13 15:07:05 +0800561 case gl::SAMPLER_COMPUTE:
562 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
563 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
564 {
565 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
566 }
567 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400568 default:
569 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700570 }
571
Jamie Madill334d6152015-10-22 14:00:28 -0400572 if (logicalTextureUnit >= 0 &&
573 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700574 {
575 return logicalTextureUnit;
576 }
577
578 return -1;
579}
580
581// Returns the texture type for a given Direct3D 9 sampler type and
582// index (0-15 for the pixel shader and 0-3 for the vertex shader).
583GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
584{
585 switch (type)
586 {
Jamie Madill334d6152015-10-22 14:00:28 -0400587 case gl::SAMPLER_PIXEL:
588 ASSERT(samplerIndex < mSamplersPS.size());
589 ASSERT(mSamplersPS[samplerIndex].active);
590 return mSamplersPS[samplerIndex].textureType;
591 case gl::SAMPLER_VERTEX:
592 ASSERT(samplerIndex < mSamplersVS.size());
593 ASSERT(mSamplersVS[samplerIndex].active);
594 return mSamplersVS[samplerIndex].textureType;
Xinghua Caob1239382016-12-13 15:07:05 +0800595 case gl::SAMPLER_COMPUTE:
596 ASSERT(samplerIndex < mSamplersCS.size());
597 ASSERT(mSamplersCS[samplerIndex].active);
598 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400599 default:
600 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700601 }
602
603 return GL_TEXTURE_2D;
604}
605
Olli Etuaho618bebc2016-01-15 16:40:00 +0200606GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700607{
608 switch (type)
609 {
Jamie Madill334d6152015-10-22 14:00:28 -0400610 case gl::SAMPLER_PIXEL:
611 return mUsedPixelSamplerRange;
612 case gl::SAMPLER_VERTEX:
613 return mUsedVertexSamplerRange;
Xinghua Caob1239382016-12-13 15:07:05 +0800614 case gl::SAMPLER_COMPUTE:
615 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400616 default:
617 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200618 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700619 }
620}
621
622void ProgramD3D::updateSamplerMapping()
623{
624 if (!mDirtySamplerMapping)
625 {
626 return;
627 }
628
629 mDirtySamplerMapping = false;
630
631 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400632 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700633 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400634 if (!d3dUniform->dirty)
635 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700636
Jamie Madill62d31cb2015-09-11 13:25:51 -0400637 if (!d3dUniform->isSampler())
638 continue;
639
640 int count = d3dUniform->elementCount();
641 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
642
643 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700644 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400645 unsigned int firstIndex = d3dUniform->psRegisterIndex;
646
647 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700648 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400649 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700650
Jamie Madill62d31cb2015-09-11 13:25:51 -0400651 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700652 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400653 ASSERT(mSamplersPS[samplerIndex].active);
654 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700655 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400656 }
657 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700658
Jamie Madill62d31cb2015-09-11 13:25:51 -0400659 if (d3dUniform->isReferencedByVertexShader())
660 {
661 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
662
663 for (int i = 0; i < count; i++)
664 {
665 unsigned int samplerIndex = firstIndex + i;
666
667 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700668 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400669 ASSERT(mSamplersVS[samplerIndex].active);
670 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700671 }
672 }
673 }
Xinghua Caob1239382016-12-13 15:07:05 +0800674
675 if (d3dUniform->isReferencedByComputeShader())
676 {
677 unsigned int firstIndex = d3dUniform->csRegisterIndex;
678
679 for (int i = 0; i < count; i++)
680 {
681 unsigned int samplerIndex = firstIndex + i;
682
683 if (samplerIndex < mSamplersCS.size())
684 {
685 ASSERT(mSamplersCS[samplerIndex].active);
686 mSamplersCS[samplerIndex].logicalTextureUnit = v[i][0];
687 }
688 }
689 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700690 }
691}
692
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500693LinkResult ProgramD3D::load(const ContextImpl *contextImpl,
694 gl::InfoLog &infoLog,
695 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700696{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500697 // TODO(jmadill): Use Renderer from contextImpl.
698
Jamie Madill62d31cb2015-09-11 13:25:51 -0400699 reset();
700
Jamie Madill334d6152015-10-22 14:00:28 -0400701 DeviceIdentifier binaryDeviceIdentifier = {0};
702 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
703 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700704
705 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
706 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
707 {
708 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500709 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700710 }
711
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500712 int compileFlags = stream->readInt<int>();
713 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
714 {
Jamie Madillf6113162015-05-07 11:49:21 -0400715 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500716 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500717 }
718
Jamie Madill8047c0d2016-03-07 13:02:12 -0500719 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400720 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500721 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400722 }
723
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700724 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
725 for (unsigned int i = 0; i < psSamplerCount; ++i)
726 {
727 Sampler sampler;
728 stream->readBool(&sampler.active);
729 stream->readInt(&sampler.logicalTextureUnit);
730 stream->readInt(&sampler.textureType);
731 mSamplersPS.push_back(sampler);
732 }
733 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
734 for (unsigned int i = 0; i < vsSamplerCount; ++i)
735 {
736 Sampler sampler;
737 stream->readBool(&sampler.active);
738 stream->readInt(&sampler.logicalTextureUnit);
739 stream->readInt(&sampler.textureType);
740 mSamplersVS.push_back(sampler);
741 }
742
Xinghua Caob1239382016-12-13 15:07:05 +0800743 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
744 for (unsigned int i = 0; i < csSamplerCount; ++i)
745 {
746 Sampler sampler;
747 stream->readBool(&sampler.active);
748 stream->readInt(&sampler.logicalTextureUnit);
749 stream->readInt(&sampler.textureType);
750 mSamplersCS.push_back(sampler);
751 }
752
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700753 stream->readInt(&mUsedVertexSamplerRange);
754 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800755 stream->readInt(&mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700756
757 const unsigned int uniformCount = stream->readInt<unsigned int>();
758 if (stream->error())
759 {
Jamie Madillf6113162015-05-07 11:49:21 -0400760 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500761 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700762 }
763
Jamie Madill48ef11b2016-04-27 15:21:52 -0400764 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400765 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700766 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
767 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400768 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700769
Jamie Madill62d31cb2015-09-11 13:25:51 -0400770 D3DUniform *d3dUniform =
771 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
772 linkedUniform.isInDefaultBlock());
773 stream->readInt(&d3dUniform->psRegisterIndex);
774 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800775 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400776 stream->readInt(&d3dUniform->registerCount);
777 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700778
Jamie Madill62d31cb2015-09-11 13:25:51 -0400779 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700780 }
781
Jamie Madill4a3c2342015-10-08 12:58:45 -0400782 const unsigned int blockCount = stream->readInt<unsigned int>();
783 if (stream->error())
784 {
785 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500786 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400787 }
788
789 ASSERT(mD3DUniformBlocks.empty());
790 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
791 {
792 D3DUniformBlock uniformBlock;
793 stream->readInt(&uniformBlock.psRegisterIndex);
794 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800795 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400796 mD3DUniformBlocks.push_back(uniformBlock);
797 }
798
Jamie Madill9fc36822015-11-18 13:08:07 -0500799 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
800 mStreamOutVaryings.resize(streamOutVaryingCount);
801 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700802 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500803 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700804
Jamie Madill28afae52015-11-09 15:07:57 -0500805 stream->readString(&varying->semanticName);
806 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500807 stream->readInt(&varying->componentCount);
808 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700809 }
810
Brandon Jones22502d52014-08-29 16:58:36 -0700811 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400812 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500813 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700814 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400815 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500816 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700817 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700818 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400819 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700820
821 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
822 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400823 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
824 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700825 {
826 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
827 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
828 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
829 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
830 }
831
Jamie Madill4e31ad52015-10-29 10:32:57 -0400832 stream->readString(&mGeometryShaderPreamble);
833
Jamie Madill334d6152015-10-22 14:00:28 -0400834 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700835
Jamie Madillb0a838b2016-11-13 20:02:12 -0500836 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
837
Brandon Joneseb994362014-09-24 10:27:28 -0700838 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400839 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
840 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700841 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400842 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400843 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700844
Jamie Madilld3dfda22015-07-06 08:28:49 -0400845 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700846 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400847 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700848 }
849
Jamie Madill334d6152015-10-22 14:00:28 -0400850 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700851 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400852
Jamie Madillada9ecc2015-08-17 12:53:37 -0400853 ShaderExecutableD3D *shaderExecutable = nullptr;
854
Jamie Madillb0a838b2016-11-13 20:02:12 -0500855 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
856 mStreamOutVaryings, separateAttribs,
857 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400858
Brandon Joneseb994362014-09-24 10:27:28 -0700859 if (!shaderExecutable)
860 {
Jamie Madillf6113162015-05-07 11:49:21 -0400861 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500862 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700863 }
864
865 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400866 VertexExecutable::Signature signature;
867 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700868
869 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800870 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
871 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700872
873 stream->skip(vertexShaderSize);
874 }
875
876 const size_t pixelShaderCount = stream->readInt<unsigned int>();
877 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
878 {
879 const size_t outputCount = stream->readInt<unsigned int>();
880 std::vector<GLenum> outputs(outputCount);
881 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
882 {
883 stream->readInt(&outputs[outputIndex]);
884 }
885
Jamie Madill334d6152015-10-22 14:00:28 -0400886 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700887 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400888 ShaderExecutableD3D *shaderExecutable = nullptr;
889
Jamie Madillb0a838b2016-11-13 20:02:12 -0500890 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
891 mStreamOutVaryings, separateAttribs,
892 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700893
894 if (!shaderExecutable)
895 {
Jamie Madillf6113162015-05-07 11:49:21 -0400896 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500897 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700898 }
899
900 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800901 mPixelExecutables.push_back(
902 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700903
904 stream->skip(pixelShaderSize);
905 }
906
Jamie Madill4e31ad52015-10-29 10:32:57 -0400907 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
908 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700909 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400910 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
911 if (geometryShaderSize == 0)
912 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400913 continue;
914 }
915
Brandon Joneseb994362014-09-24 10:27:28 -0700916 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400917
Xinghua Caob1239382016-12-13 15:07:05 +0800918 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -0500919 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
920 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
Xinghua Caob1239382016-12-13 15:07:05 +0800921 &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700922
Xinghua Caob1239382016-12-13 15:07:05 +0800923 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700924 {
Jamie Madillf6113162015-05-07 11:49:21 -0400925 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500926 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700927 }
Xinghua Caob1239382016-12-13 15:07:05 +0800928
929 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
930
Brandon Joneseb994362014-09-24 10:27:28 -0700931 stream->skip(geometryShaderSize);
932 }
933
Xinghua Caob1239382016-12-13 15:07:05 +0800934 unsigned int computeShaderSize = stream->readInt<unsigned int>();
935 if (computeShaderSize > 0)
936 {
937 const unsigned char *computeShaderFunction = binary + stream->offset();
938
939 ShaderExecutableD3D *computeExecutable = nullptr;
940 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
941 SHADER_COMPUTE, std::vector<D3DVarying>(), false,
942 &computeExecutable));
943
944 if (!computeExecutable)
945 {
946 infoLog << "Could not create compute shader.";
947 return false;
948 }
949
950 mComputeExecutable.reset(computeExecutable);
951 }
952
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700953 initializeUniformStorage();
954
Jamie Madillb0a838b2016-11-13 20:02:12 -0500955 return true;
Brandon Jones22502d52014-08-29 16:58:36 -0700956}
957
Geoff Langb543aff2014-09-30 14:52:54 -0400958gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700959{
Austin Kinross137b1512015-06-17 16:14:53 -0700960 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400961 // When we load the binary again later, we can validate the device identifier before trying to
962 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700963 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400964 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
965 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700966
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500967 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
968
Jamie Madill8047c0d2016-03-07 13:02:12 -0500969 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400970 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500971 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400972 }
973
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700974 stream->writeInt(mSamplersPS.size());
975 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
976 {
977 stream->writeInt(mSamplersPS[i].active);
978 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
979 stream->writeInt(mSamplersPS[i].textureType);
980 }
981
982 stream->writeInt(mSamplersVS.size());
983 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
984 {
985 stream->writeInt(mSamplersVS[i].active);
986 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
987 stream->writeInt(mSamplersVS[i].textureType);
988 }
989
Xinghua Caob1239382016-12-13 15:07:05 +0800990 stream->writeInt(mSamplersCS.size());
991 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
992 {
993 stream->writeInt(mSamplersCS[i].active);
994 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
995 stream->writeInt(mSamplersCS[i].textureType);
996 }
997
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700998 stream->writeInt(mUsedVertexSamplerRange);
999 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001000 stream->writeInt(mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001001
Jamie Madill62d31cb2015-09-11 13:25:51 -04001002 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001003 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001004 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001005 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001006 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1007 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001008 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001009 stream->writeInt(uniform->registerCount);
1010 stream->writeInt(uniform->registerElement);
1011 }
1012
Jamie Madill51f522f2016-12-21 15:10:55 -05001013 // Ensure we init the uniform block structure data if we should.
1014 // http://anglebug.com/1637
1015 ensureUniformBlocksInitialized();
1016
Jamie Madill4a3c2342015-10-08 12:58:45 -04001017 stream->writeInt(mD3DUniformBlocks.size());
1018 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1019 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001020 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1021 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001022 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001023 }
1024
Jamie Madill9fc36822015-11-18 13:08:07 -05001025 stream->writeInt(mStreamOutVaryings.size());
1026 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001027 {
Brandon Joneseb994362014-09-24 10:27:28 -07001028 stream->writeString(varying.semanticName);
1029 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001030 stream->writeInt(varying.componentCount);
1031 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001032 }
1033
Brandon Jones22502d52014-08-29 16:58:36 -07001034 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001035 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001036 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001037 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001038 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001039 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001040 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001041 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001042 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001043
Brandon Joneseb994362014-09-24 10:27:28 -07001044 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001045 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001046 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1047 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001048 {
Brandon Joneseb994362014-09-24 10:27:28 -07001049 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001050 stream->writeInt(variable.type);
1051 stream->writeString(variable.name);
1052 stream->writeString(variable.source);
1053 stream->writeInt(variable.outputIndex);
1054 }
1055
Jamie Madill4e31ad52015-10-29 10:32:57 -04001056 stream->writeString(mGeometryShaderPreamble);
1057
Brandon Joneseb994362014-09-24 10:27:28 -07001058 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001059 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1060 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001061 {
Xinghua Caob1239382016-12-13 15:07:05 +08001062 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001063
Jamie Madilld3dfda22015-07-06 08:28:49 -04001064 const auto &inputLayout = vertexExecutable->inputs();
1065 stream->writeInt(inputLayout.size());
1066
1067 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001068 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001069 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001070 }
1071
1072 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1073 stream->writeInt(vertexShaderSize);
1074
1075 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1076 stream->writeBytes(vertexBlob, vertexShaderSize);
1077 }
1078
1079 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001080 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1081 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001082 {
Xinghua Caob1239382016-12-13 15:07:05 +08001083 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001084
1085 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1086 stream->writeInt(outputs.size());
1087 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1088 {
1089 stream->writeInt(outputs[outputIndex]);
1090 }
1091
1092 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1093 stream->writeInt(pixelShaderSize);
1094
1095 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1096 stream->writeBytes(pixelBlob, pixelShaderSize);
1097 }
1098
Xinghua Caob1239382016-12-13 15:07:05 +08001099 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001100 {
Xinghua Caob1239382016-12-13 15:07:05 +08001101 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001102 {
1103 stream->writeInt(0);
1104 continue;
1105 }
1106
Xinghua Caob1239382016-12-13 15:07:05 +08001107 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001108 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001109 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1110 }
1111
1112 if (mComputeExecutable)
1113 {
1114 size_t computeShaderSize = mComputeExecutable->getLength();
1115 stream->writeInt(computeShaderSize);
1116 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1117 }
1118 else
1119 {
1120 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001121 }
1122
Jamie Madill51f522f2016-12-21 15:10:55 -05001123 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001124}
1125
Geoff Langc5629752015-12-07 16:29:04 -05001126void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1127{
1128}
1129
Jamie Madill334d6152015-10-22 14:00:28 -04001130gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1131 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001132{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001133 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001134
Jamie Madill85a18042015-03-05 15:41:41 -05001135 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001136 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001137
1138 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1139 {
1140 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1141
1142 if (colorbuffer)
1143 {
Jamie Madill334d6152015-10-22 14:00:28 -04001144 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1145 ? GL_COLOR_ATTACHMENT0
1146 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001147 }
1148 else
1149 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001150 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001151 }
1152 }
1153
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001154 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001155}
1156
Jamie Madill97399232014-12-23 12:31:15 -05001157gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001158 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001159 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001160{
1161 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1162 {
1163 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1164 {
Geoff Langb543aff2014-09-30 14:52:54 -04001165 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001166 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001167 }
1168 }
1169
Jamie Madill334d6152015-10-22 14:00:28 -04001170 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1171 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001172
1173 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001174 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001175
1176 gl::InfoLog tempInfoLog;
1177 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1178
Jamie Madill01074252016-11-28 15:55:51 -05001179 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001180 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001181 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001182 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001183
Jamie Madill97399232014-12-23 12:31:15 -05001184 if (pixelExecutable)
1185 {
Xinghua Caob1239382016-12-13 15:07:05 +08001186 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
1187 new PixelExecutable(outputSignature, pixelExecutable)));
Jamie Madill97399232014-12-23 12:31:15 -05001188 }
1189 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001190 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001191 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1192 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001193 }
Brandon Jones22502d52014-08-29 16:58:36 -07001194
Geoff Langb543aff2014-09-30 14:52:54 -04001195 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001196 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001197}
1198
Jamie Madilld3dfda22015-07-06 08:28:49 -04001199gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001200 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001201 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001202{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001203 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001204
1205 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1206 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001207 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001208 {
Geoff Langb543aff2014-09-30 14:52:54 -04001209 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001210 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001211 }
1212 }
1213
Brandon Jones22502d52014-08-29 16:58:36 -07001214 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001215 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001216 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001217
1218 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001219 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001220
1221 gl::InfoLog tempInfoLog;
1222 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1223
Jamie Madill01074252016-11-28 15:55:51 -05001224 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001225 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001226 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001227 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001228
Jamie Madill97399232014-12-23 12:31:15 -05001229 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001230 {
Xinghua Caob1239382016-12-13 15:07:05 +08001231 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
1232 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001233 }
Jamie Madill97399232014-12-23 12:31:15 -05001234 else if (!infoLog)
1235 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001236 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1237 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001238 }
Brandon Jones22502d52014-08-29 16:58:36 -07001239
Geoff Langb543aff2014-09-30 14:52:54 -04001240 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001241 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001242}
1243
Jamie Madill9082b982016-04-27 15:21:51 -04001244gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001245 GLenum drawMode,
1246 ShaderExecutableD3D **outExecutable,
1247 gl::InfoLog *infoLog)
1248{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001249 if (outExecutable)
1250 {
1251 *outExecutable = nullptr;
1252 }
1253
Austin Kinross88829e82016-01-12 13:04:41 -08001254 // Return a null shader if the current rendering doesn't use a geometry shader
1255 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001256 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001257 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001258 }
1259
Jamie Madill4e31ad52015-10-29 10:32:57 -04001260 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1261
Xinghua Caob1239382016-12-13 15:07:05 +08001262 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001263 {
1264 if (outExecutable)
1265 {
Xinghua Caob1239382016-12-13 15:07:05 +08001266 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001267 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001268 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001269 }
1270
1271 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001272 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001273 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001274
1275 gl::InfoLog tempInfoLog;
1276 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1277
Xinghua Caob1239382016-12-13 15:07:05 +08001278 ShaderExecutableD3D *geometryExecutable = nullptr;
1279 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001280 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001281 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001282 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001283
1284 if (!infoLog && error.isError())
1285 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001286 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1287 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001288 }
1289
Xinghua Caob1239382016-12-13 15:07:05 +08001290 if (geometryExecutable != nullptr)
1291 {
1292 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1293 }
1294
Jamie Madill4e31ad52015-10-29 10:32:57 -04001295 if (outExecutable)
1296 {
Xinghua Caob1239382016-12-13 15:07:05 +08001297 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001298 }
1299 return error;
1300}
1301
Jamie Madill01074252016-11-28 15:55:51 -05001302class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001303{
Jamie Madill01074252016-11-28 15:55:51 -05001304 public:
1305 GetExecutableTask(ProgramD3D *program)
1306 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001307 {
Brandon Joneseb994362014-09-24 10:27:28 -07001308 }
1309
Jamie Madill01074252016-11-28 15:55:51 -05001310 virtual gl::Error run() = 0;
1311
1312 void operator()() override { mError = run(); }
1313
1314 const gl::Error &getError() const { return mError; }
1315 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1316 ShaderExecutableD3D *getResult() { return mResult; }
1317
1318 protected:
1319 ProgramD3D *mProgram;
1320 gl::Error mError;
1321 gl::InfoLog mInfoLog;
1322 ShaderExecutableD3D *mResult;
1323};
1324
1325class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1326{
1327 public:
1328 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1329 gl::Error run() override
1330 {
1331 const auto &defaultInputLayout =
1332 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1333
1334 ANGLE_TRY(
1335 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1336
1337 return gl::NoError();
1338 }
1339};
1340
1341class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1342{
1343 public:
1344 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1345 gl::Error run() override
1346 {
1347 const auto &defaultPixelOutput =
1348 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1349
1350 ANGLE_TRY(
1351 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1352
1353 return gl::NoError();
1354 }
1355};
1356
1357class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1358{
1359 public:
1360 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1361 : GetExecutableTask(program), mContextState(contextState)
1362 {
1363 }
1364
1365 gl::Error run() override
1366 {
1367 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1368 // D3D11.
1369 if (mProgram->usesGeometryShader(GL_POINTS))
1370 {
1371 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1372 &mResult, &mInfoLog));
1373 }
1374
1375 return gl::NoError();
1376 }
1377
1378 private:
1379 const gl::ContextState &mContextState;
1380};
1381
1382LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1383 gl::InfoLog &infoLog)
1384{
1385 // Ensure the compiler is initialized to avoid race conditions.
1386 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1387
1388 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1389
1390 GetVertexExecutableTask vertexTask(this);
1391 GetPixelExecutableTask pixelTask(this);
1392 GetGeometryExecutableTask geometryTask(this, contextState);
1393
1394 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1395 workerPool->postWorkerTask(&pixelTask),
1396 workerPool->postWorkerTask(&geometryTask)}};
1397
1398 WaitableEvent::WaitMany(&waitEvents);
1399
1400 infoLog << vertexTask.getInfoLog().str();
1401 infoLog << pixelTask.getInfoLog().str();
1402 infoLog << geometryTask.getInfoLog().str();
1403
1404 ANGLE_TRY(vertexTask.getError());
1405 ANGLE_TRY(pixelTask.getError());
1406 ANGLE_TRY(geometryTask.getError());
1407
1408 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1409 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1410 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1411
Jamie Madill48ef11b2016-04-27 15:21:52 -04001412 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001413
1414 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001415 {
Jamie Madill334d6152015-10-22 14:00:28 -04001416 // Geometry shaders are currently only used internally, so there is no corresponding shader
1417 // object at the interface level. For now the geometry shader debug info is prepended to
1418 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001419 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001420 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001421 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1422 }
1423
1424 if (defaultVertexExecutable)
1425 {
1426 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1427 }
1428
1429 if (defaultPixelExecutable)
1430 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001431 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001432 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001433 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1434 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001435
Jamie Madillb0a838b2016-11-13 20:02:12 -05001436 return (defaultVertexExecutable && defaultPixelExecutable &&
1437 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001438}
1439
Xinghua Caob1239382016-12-13 15:07:05 +08001440LinkResult ProgramD3D::compileComputeExecutable(gl::InfoLog &infoLog)
1441{
1442 // Ensure the compiler is initialized to avoid race conditions.
1443 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1444
1445 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(mState);
1446
1447 ShaderExecutableD3D *computeExecutable = nullptr;
1448 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, SHADER_COMPUTE,
1449 std::vector<D3DVarying>(), false,
1450 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1451
1452 if (computeExecutable == nullptr)
1453 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001454 ERR() << "Error compiling dynamic compute executable:" << std::endl
1455 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001456 }
1457 else
1458 {
1459 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1460 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1461 mComputeExecutable.reset(computeExecutable);
1462 }
1463
1464 return mComputeExecutable.get() != nullptr;
1465}
1466
Jamie Madill192745a2016-12-22 15:58:21 -05001467LinkResult ProgramD3D::link(const gl::ContextState &data,
1468 const gl::VaryingPacking &packing,
1469 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001470{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001471 reset();
1472
Xinghua Caob1239382016-12-13 15:07:05 +08001473 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1474 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001475 {
Xinghua Caob1239382016-12-13 15:07:05 +08001476 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1477
1478 defineUniformsAndAssignRegisters();
1479
1480 LinkResult result = compileComputeExecutable(infoLog);
1481 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001482 {
Xinghua Caob1239382016-12-13 15:07:05 +08001483 infoLog << result.getError().getMessage();
1484 return result;
1485 }
1486 else if (!result.getResult())
1487 {
1488 infoLog << "Failed to create D3D compute shader.";
1489 return result;
1490 }
1491
1492 initUniformBlockInfo(computeShader);
1493 }
1494 else
1495 {
1496 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1497 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1498
1499 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1500 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1501
1502 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1503 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1504
1505 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1506 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1507
1508 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1509 {
1510 if (fragmentShaderD3D->usesFrontFacing())
1511 {
1512 infoLog << "The current renderer doesn't support gl_FrontFacing";
1513 return false;
1514 }
1515 }
1516
1517 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1518 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1519 // intelligently, but D3D9 assumes one semantic per register.
1520 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1521 packing.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
1522 {
1523 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001524 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001525 }
Xinghua Caob1239382016-12-13 15:07:05 +08001526
1527 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
1528 BuiltinVaryingsD3D builtins(metadata, packing);
1529
1530 mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, packing, builtins, &mPixelHLSL,
1531 &mVertexHLSL);
1532
1533 mUsesPointSize = vertexShaderD3D->usesPointSize();
1534 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1535 mUsesFragDepth = metadata.usesFragDepth();
1536
1537 // Cache if we use flat shading
1538 mUsesFlatInterpolation = (FindFlatInterpolationVarying(fragmentShader->getVaryings()) ||
1539 FindFlatInterpolationVarying(vertexShader->getVaryings()));
1540
1541 if (mRenderer->getMajorShaderModel() >= 4)
1542 {
1543 mGeometryShaderPreamble =
1544 mDynamicHLSL->generateGeometryShaderPreamble(packing, builtins);
1545 }
1546
1547 initAttribLocationsToD3DSemantic();
1548
1549 defineUniformsAndAssignRegisters();
1550
1551 gatherTransformFeedbackVaryings(packing, builtins[SHADER_VERTEX]);
1552
1553 LinkResult result = compileProgramExecutables(data, infoLog);
1554 if (result.isError())
1555 {
1556 infoLog << result.getError().getMessage();
1557 return result;
1558 }
1559 else if (!result.getResult())
1560 {
1561 infoLog << "Failed to create D3D shaders.";
1562 return result;
1563 }
1564
1565 initUniformBlockInfo(vertexShader);
1566 initUniformBlockInfo(fragmentShader);
Austin Kinross02df7962015-07-01 10:03:42 -07001567 }
1568
Jamie Madillb0a838b2016-11-13 20:02:12 -05001569 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001570}
1571
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001572GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001573{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001574 // TODO(jmadill): Do something useful here?
1575 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001576}
1577
Xinghua Caob1239382016-12-13 15:07:05 +08001578void ProgramD3D::initUniformBlockInfo(const gl::Shader *shader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001579{
Xinghua Caob1239382016-12-13 15:07:05 +08001580 for (const sh::InterfaceBlock &interfaceBlock : shader->getInterfaceBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001581 {
Xinghua Caob1239382016-12-13 15:07:05 +08001582 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001583 continue;
1584
Xinghua Caob1239382016-12-13 15:07:05 +08001585 if (mBlockDataSizes.count(interfaceBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001586 continue;
1587
Xinghua Caob1239382016-12-13 15:07:05 +08001588 size_t dataSize = getUniformBlockInfo(interfaceBlock);
1589 mBlockDataSizes[interfaceBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001590 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001591}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001592
Jamie Madill51f522f2016-12-21 15:10:55 -05001593void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001594{
Jamie Madill51f522f2016-12-21 15:10:55 -05001595 // Lazy init.
1596 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1597 {
1598 return;
1599 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001600
1601 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001602 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1603 const ShaderD3D *fragmentShaderD3D =
1604 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1605 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001606
Jamie Madill48ef11b2016-04-27 15:21:52 -04001607 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001608 {
1609 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1610
Jamie Madill4a3c2342015-10-08 12:58:45 -04001611 D3DUniformBlock d3dUniformBlock;
1612
Jamie Madill62d31cb2015-09-11 13:25:51 -04001613 if (uniformBlock.vertexStaticUse)
1614 {
Xinghua Caob1239382016-12-13 15:07:05 +08001615 ASSERT(vertexShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001616 unsigned int baseRegister =
1617 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001618 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001619 }
1620
1621 if (uniformBlock.fragmentStaticUse)
1622 {
Xinghua Caob1239382016-12-13 15:07:05 +08001623 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001624 unsigned int baseRegister =
1625 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001626 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001627 }
1628
Xinghua Caob1239382016-12-13 15:07:05 +08001629 if (uniformBlock.computeStaticUse)
1630 {
1631 ASSERT(computeShaderD3D != nullptr);
1632 unsigned int baseRegister =
1633 computeShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
1634 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1635 }
1636
Jamie Madill4a3c2342015-10-08 12:58:45 -04001637 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001638 }
1639}
1640
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001641void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001642{
1643 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001644 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001645 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001646 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001647 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001648 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001649 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001650 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001651 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001652 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001653 vertexRegisters = std::max(vertexRegisters,
1654 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001655 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001656 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001657 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001658 fragmentRegisters = std::max(
1659 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001660 }
Xinghua Caob1239382016-12-13 15:07:05 +08001661 if (d3dUniform->isReferencedByComputeShader())
1662 {
1663 computeRegisters = std::max(
1664 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1665 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001666 }
1667 }
1668
Xinghua Caob1239382016-12-13 15:07:05 +08001669 mVertexUniformStorage =
1670 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1671 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1672 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1673 mComputeUniformStorage =
1674 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Brandon Jonesc9610c52014-08-25 17:02:59 -07001675}
1676
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001677gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001678{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001679 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001680
Jamie Madill01074252016-11-28 15:55:51 -05001681 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001682
Jamie Madill62d31cb2015-09-11 13:25:51 -04001683 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001684 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001685 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001686 }
1687
Jamie Madill51f522f2016-12-21 15:10:55 -05001688 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001689}
1690
Jamie Madill9082b982016-04-27 15:21:51 -04001691gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001692{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001693 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001694 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001695 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001696 }
1697
Jamie Madill51f522f2016-12-21 15:10:55 -05001698 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001699
Jamie Madill03260fa2015-06-22 13:57:22 -04001700 mVertexUBOCache.clear();
1701 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001702
1703 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1704 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1705
Jamie Madill4a3c2342015-10-08 12:58:45 -04001706 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001707 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001708 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001709 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001710 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001711
Brandon Jones18bd4102014-09-22 14:21:44 -07001712 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001713 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001714 {
1715 continue;
1716 }
1717
Jamie Madill4a3c2342015-10-08 12:58:45 -04001718 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001719 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001720 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001721 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001722
Jamie Madill969194d2015-07-20 14:36:56 -04001723 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001724 {
1725 mVertexUBOCache.resize(registerIndex + 1, -1);
1726 }
1727
1728 ASSERT(mVertexUBOCache[registerIndex] == -1);
1729 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001730 }
1731
Jamie Madill4a3c2342015-10-08 12:58:45 -04001732 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001733 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001734 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001735 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001736
1737 if (mFragmentUBOCache.size() <= registerIndex)
1738 {
1739 mFragmentUBOCache.resize(registerIndex + 1, -1);
1740 }
1741
1742 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1743 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001744 }
1745 }
1746
Jamie Madill03260fa2015-06-22 13:57:22 -04001747 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001748}
1749
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001750void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001751{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001752 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001753 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001754 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001755 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001756}
1757
Jamie Madill334d6152015-10-22 14:00:28 -04001758void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001759{
1760 setUniform(location, count, v, GL_FLOAT);
1761}
1762
1763void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1764{
1765 setUniform(location, count, v, GL_FLOAT_VEC2);
1766}
1767
1768void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1769{
1770 setUniform(location, count, v, GL_FLOAT_VEC3);
1771}
1772
1773void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1774{
1775 setUniform(location, count, v, GL_FLOAT_VEC4);
1776}
1777
Jamie Madill334d6152015-10-22 14:00:28 -04001778void ProgramD3D::setUniformMatrix2fv(GLint location,
1779 GLsizei count,
1780 GLboolean transpose,
1781 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001782{
1783 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1784}
1785
Jamie Madill334d6152015-10-22 14:00:28 -04001786void ProgramD3D::setUniformMatrix3fv(GLint location,
1787 GLsizei count,
1788 GLboolean transpose,
1789 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001790{
1791 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1792}
1793
Jamie Madill334d6152015-10-22 14:00:28 -04001794void ProgramD3D::setUniformMatrix4fv(GLint location,
1795 GLsizei count,
1796 GLboolean transpose,
1797 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001798{
1799 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1800}
1801
Jamie Madill334d6152015-10-22 14:00:28 -04001802void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1803 GLsizei count,
1804 GLboolean transpose,
1805 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001806{
1807 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1808}
1809
Jamie Madill334d6152015-10-22 14:00:28 -04001810void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1811 GLsizei count,
1812 GLboolean transpose,
1813 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001814{
1815 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1816}
1817
Jamie Madill334d6152015-10-22 14:00:28 -04001818void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1819 GLsizei count,
1820 GLboolean transpose,
1821 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001822{
1823 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1824}
1825
Jamie Madill334d6152015-10-22 14:00:28 -04001826void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1827 GLsizei count,
1828 GLboolean transpose,
1829 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001830{
1831 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1832}
1833
Jamie Madill334d6152015-10-22 14:00:28 -04001834void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1835 GLsizei count,
1836 GLboolean transpose,
1837 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001838{
1839 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1840}
1841
Jamie Madill334d6152015-10-22 14:00:28 -04001842void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1843 GLsizei count,
1844 GLboolean transpose,
1845 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001846{
1847 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1848}
1849
1850void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1851{
1852 setUniform(location, count, v, GL_INT);
1853}
1854
1855void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1856{
1857 setUniform(location, count, v, GL_INT_VEC2);
1858}
1859
1860void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1861{
1862 setUniform(location, count, v, GL_INT_VEC3);
1863}
1864
1865void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1866{
1867 setUniform(location, count, v, GL_INT_VEC4);
1868}
1869
1870void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1871{
1872 setUniform(location, count, v, GL_UNSIGNED_INT);
1873}
1874
1875void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1876{
1877 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1878}
1879
1880void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1881{
1882 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1883}
1884
1885void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1886{
1887 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1888}
1889
Jamie Madill4a3c2342015-10-08 12:58:45 -04001890void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1891 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001892{
1893}
1894
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001895void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001896{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001897 D3DUniformMap uniformMap;
Xinghua Caob1239382016-12-13 15:07:05 +08001898 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1899 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05001900 {
Xinghua Caob1239382016-12-13 15:07:05 +08001901 for (const sh::Uniform &computeUniform : computeShader->getUniforms())
1902
Jamie Madillfb536032015-09-11 13:19:49 -04001903 {
Xinghua Caob1239382016-12-13 15:07:05 +08001904 if (computeUniform.staticUse)
1905 {
1906 defineUniformBase(computeShader, computeUniform, &uniformMap);
1907 }
Jamie Madillfb536032015-09-11 13:19:49 -04001908 }
1909 }
Xinghua Caob1239382016-12-13 15:07:05 +08001910 else
Jamie Madillfb536032015-09-11 13:19:49 -04001911 {
Xinghua Caob1239382016-12-13 15:07:05 +08001912 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1913 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
1914
Jamie Madillfb536032015-09-11 13:19:49 -04001915 {
Xinghua Caob1239382016-12-13 15:07:05 +08001916 if (vertexUniform.staticUse)
1917 {
1918 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
1919 }
1920 }
1921
1922 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1923 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
1924 {
1925 if (fragmentUniform.staticUse)
1926 {
1927 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
1928 }
Jamie Madillfb536032015-09-11 13:19:49 -04001929 }
1930 }
1931
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001932 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001933 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001934 {
1935 if (!glUniform.isInDefaultBlock())
1936 continue;
1937
1938 auto mapEntry = uniformMap.find(glUniform.name);
1939 ASSERT(mapEntry != uniformMap.end());
1940 mD3DUniforms.push_back(mapEntry->second);
1941 }
1942
Jamie Madill62d31cb2015-09-11 13:25:51 -04001943 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001944 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001945}
1946
Jamie Madill91445bc2015-09-23 16:47:53 -04001947void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001948 const sh::Uniform &uniform,
1949 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001950{
Olli Etuaho96963162016-03-21 11:54:33 +02001951 // Samplers get their registers assigned in assignAllSamplerRegisters.
1952 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001953 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001954 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001955 return;
1956 }
1957
Jamie Madill91445bc2015-09-23 16:47:53 -04001958 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1959
1960 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1961 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001962 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001963 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964
Jamie Madill91445bc2015-09-23 16:47:53 -04001965 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001966}
1967
Jamie Madill62d31cb2015-09-11 13:25:51 -04001968D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1969{
1970 for (D3DUniform *d3dUniform : mD3DUniforms)
1971 {
1972 if (d3dUniform->name == name)
1973 {
1974 return d3dUniform;
1975 }
1976 }
1977
1978 return nullptr;
1979}
1980
Jamie Madill91445bc2015-09-23 16:47:53 -04001981void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001982 const sh::ShaderVariable &uniform,
1983 const std::string &fullName,
1984 sh::HLSLBlockEncoder *encoder,
1985 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986{
1987 if (uniform.isStruct())
1988 {
1989 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1990 {
1991 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1992
Jamie Madill55def582015-05-04 11:24:57 -04001993 if (encoder)
1994 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001995
1996 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1997 {
Jamie Madill334d6152015-10-22 14:00:28 -04001998 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001999 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2000
Olli Etuaho96963162016-03-21 11:54:33 +02002001 // Samplers get their registers assigned in assignAllSamplerRegisters.
2002 // Also they couldn't use the same encoder as the rest of the struct, since they are
2003 // extracted out of the struct by the shader translator.
2004 if (gl::IsSamplerType(field.type))
2005 {
2006 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2007 }
2008 else
2009 {
2010 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2011 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002012 }
2013
Jamie Madill55def582015-05-04 11:24:57 -04002014 if (encoder)
2015 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002016 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002017 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019
2020 // Not a struct. Arrays are treated as aggregate types.
2021 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002022 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002023 encoder->enterAggregateType();
2024 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002025
Jamie Madill62d31cb2015-09-11 13:25:51 -04002026 // Advance the uniform offset, to track registers allocation for structs
2027 sh::BlockMemberInfo blockInfo =
2028 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2029 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002030
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002031 auto uniformMapEntry = uniformMap->find(fullName);
2032 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002033
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002034 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002035 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002036 d3dUniform = uniformMapEntry->second;
2037 }
2038 else
2039 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002040 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002041 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002042 }
2043
2044 if (encoder)
2045 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002046 d3dUniform->registerElement =
2047 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002048 unsigned int reg =
2049 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002050 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002051 {
2052 d3dUniform->psRegisterIndex = reg;
2053 }
Xinghua Caob1239382016-12-13 15:07:05 +08002054 else if (shaderType == GL_VERTEX_SHADER)
2055 {
2056 d3dUniform->vsRegisterIndex = reg;
2057 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002058 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002059 {
Xinghua Caob1239382016-12-13 15:07:05 +08002060 ASSERT(shaderType == GL_COMPUTE_SHADER);
2061 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002062 }
Jamie Madillfb536032015-09-11 13:19:49 -04002063
2064 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002065 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002066 {
2067 encoder->exitAggregateType();
2068 }
2069 }
2070}
2071
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002072template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002073void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002074{
Jamie Madill334d6152015-10-22 14:00:28 -04002075 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002076 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2077
Jamie Madill62d31cb2015-09-11 13:25:51 -04002078 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002079
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002081 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002082 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002083
2084 if (targetUniform->type == targetUniformType)
2085 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002086 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002087
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002089 {
Jamie Madill334d6152015-10-22 14:00:28 -04002090 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002091 const T *source = v + (i * components);
2092
2093 for (int c = 0; c < components; c++)
2094 {
2095 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2096 }
2097 for (int c = components; c < 4; c++)
2098 {
2099 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2100 }
2101 }
2102 }
2103 else if (targetUniform->type == targetBoolType)
2104 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002105 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002106
Jamie Madill62d31cb2015-09-11 13:25:51 -04002107 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108 {
Jamie Madill334d6152015-10-22 14:00:28 -04002109 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002110 const T *source = v + (i * components);
2111
2112 for (int c = 0; c < components; c++)
2113 {
Jamie Madill334d6152015-10-22 14:00:28 -04002114 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2115 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002116 }
2117 for (int c = components; c < 4; c++)
2118 {
2119 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2120 }
2121 }
2122 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002123 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002124 {
2125 ASSERT(targetUniformType == GL_INT);
2126
Jamie Madill62d31cb2015-09-11 13:25:51 -04002127 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002128
2129 bool wasDirty = targetUniform->dirty;
2130
Jamie Madill62d31cb2015-09-11 13:25:51 -04002131 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002132 {
Jamie Madill334d6152015-10-22 14:00:28 -04002133 GLint *dest = target + (i * 4);
2134 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002135
2136 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2137 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2138 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2139 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2140 }
2141
2142 if (!wasDirty && targetUniform->dirty)
2143 {
2144 mDirtySamplerMapping = true;
2145 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002146 }
Jamie Madill334d6152015-10-22 14:00:28 -04002147 else
2148 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002149}
2150
2151template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002152void ProgramD3D::setUniformMatrixfv(GLint location,
2153 GLsizei countIn,
2154 GLboolean transpose,
2155 const GLfloat *value,
2156 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002157{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002158 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002159
Jamie Madill62d31cb2015-09-11 13:25:51 -04002160 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002161 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002162 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002163
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002164 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002165 GLfloat *target =
2166 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002167
Jamie Madill62d31cb2015-09-11 13:25:51 -04002168 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002169 {
2170 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2171 if (transpose == GL_FALSE)
2172 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002173 targetUniform->dirty =
2174 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002175 }
2176 else
2177 {
Jamie Madill334d6152015-10-22 14:00:28 -04002178 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002179 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002180 }
2181 target += targetMatrixStride;
2182 value += cols * rows;
2183 }
2184}
2185
Jamie Madill4a3c2342015-10-08 12:58:45 -04002186size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002187{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002188 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002189
Jamie Madill62d31cb2015-09-11 13:25:51 -04002190 // define member uniforms
2191 sh::Std140BlockEncoder std140Encoder;
2192 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2193 sh::BlockLayoutEncoder *encoder = nullptr;
2194
2195 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002196 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002197 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002198 }
2199 else
2200 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002201 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002202 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002203
Jamie Madill39046162016-02-08 15:05:17 -05002204 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2205 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002206
2207 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002208}
2209
Jamie Madill62d31cb2015-09-11 13:25:51 -04002210void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002211{
Olli Etuaho96963162016-03-21 11:54:33 +02002212 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002213 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002214 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002215 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002216 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002217 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002218 }
2219}
2220
Olli Etuaho96963162016-03-21 11:54:33 +02002221void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002222{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002223 ASSERT(d3dUniform->isSampler());
Xinghua Caob1239382016-12-13 15:07:05 +08002224 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2225 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002226 {
Xinghua Caob1239382016-12-13 15:07:05 +08002227 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2228 ASSERT(computeShaderD3D->hasUniform(d3dUniform));
2229 d3dUniform->csRegisterIndex = computeShaderD3D->getUniformRegister(d3dUniform->name);
2230 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2231 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2232 mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002233 }
Xinghua Caob1239382016-12-13 15:07:05 +08002234 else
Jamie Madillfb536032015-09-11 13:19:49 -04002235 {
Xinghua Caob1239382016-12-13 15:07:05 +08002236 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2237 const ShaderD3D *fragmentShaderD3D =
2238 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2239 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) ||
2240 fragmentShaderD3D->hasUniform(d3dUniform));
2241 if (vertexShaderD3D->hasUniform(d3dUniform))
2242 {
2243 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2244 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
2245 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2246 mSamplersVS, &mUsedVertexSamplerRange);
2247 }
2248 if (fragmentShaderD3D->hasUniform(d3dUniform))
2249 {
2250 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2251 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
2252 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2253 mSamplersPS, &mUsedPixelSamplerRange);
2254 }
Jamie Madillfb536032015-09-11 13:19:49 -04002255 }
2256}
2257
Jamie Madill62d31cb2015-09-11 13:25:51 -04002258// static
2259void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002260 GLenum samplerType,
2261 unsigned int samplerCount,
2262 std::vector<Sampler> &outSamplers,
2263 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002264{
2265 unsigned int samplerIndex = startSamplerIndex;
2266
2267 do
2268 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002269 ASSERT(samplerIndex < outSamplers.size());
2270 Sampler *sampler = &outSamplers[samplerIndex];
2271 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002272 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002273 sampler->logicalTextureUnit = 0;
2274 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002275 samplerIndex++;
2276 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002277}
2278
Brandon Jonesc9610c52014-08-25 17:02:59 -07002279void ProgramD3D::reset()
2280{
Xinghua Caob1239382016-12-13 15:07:05 +08002281 mVertexExecutables.clear();
2282 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002283
Xinghua Caob1239382016-12-13 15:07:05 +08002284 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002285 {
Xinghua Caob1239382016-12-13 15:07:05 +08002286 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002287 }
Brandon Joneseb994362014-09-24 10:27:28 -07002288
Xinghua Caob1239382016-12-13 15:07:05 +08002289 mComputeExecutable.reset(nullptr);
2290
Brandon Jones22502d52014-08-29 16:58:36 -07002291 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002292 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002293
2294 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002295 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002296 mUsesFragDepth = false;
2297 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002298 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002299 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002300
Jamie Madill62d31cb2015-09-11 13:25:51 -04002301 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002302 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002303
Xinghua Caob1239382016-12-13 15:07:05 +08002304 mVertexUniformStorage.reset(nullptr);
2305 mFragmentUniformStorage.reset(nullptr);
2306 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002307
2308 mSamplersPS.clear();
2309 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002310 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002311
2312 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002313 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002314 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002315 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002316
Jamie Madill8047c0d2016-03-07 13:02:12 -05002317 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002318
Jamie Madill9fc36822015-11-18 13:08:07 -05002319 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002320
2321 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002322}
2323
Geoff Lang7dd2e102014-11-10 15:19:26 -05002324unsigned int ProgramD3D::getSerial() const
2325{
2326 return mSerial;
2327}
2328
2329unsigned int ProgramD3D::issueSerial()
2330{
2331 return mCurrentSerial++;
2332}
2333
Jamie Madill8047c0d2016-03-07 13:02:12 -05002334void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002335{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002336 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002337 ASSERT(vertexShader != nullptr);
2338
2339 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002340 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002341 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002342 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2343 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002344
Jamie Madill8047c0d2016-03-07 13:02:12 -05002345 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002346 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002347 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002348 }
2349 }
Jamie Madill437d2662014-12-05 14:23:35 -05002350}
2351
Jamie Madill63805b42015-08-25 13:17:39 -04002352void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002353{
Jamie Madillbd136f92015-08-10 14:51:37 -04002354 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002355 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002356
Jamie Madill01074252016-11-28 15:55:51 -05002357 for (unsigned int locationIndex : IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002358 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002359 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002360
Jamie Madill8047c0d2016-03-07 13:02:12 -05002361 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002362 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002363 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002364 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002365 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002366 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002367 mCachedInputLayout[d3dSemantic] =
2368 GetVertexFormatType(vertexAttributes[locationIndex],
2369 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002370 }
2371 }
2372}
2373
Jamie Madill192745a2016-12-22 15:58:21 -05002374void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2375 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002376{
Jamie Madill9fc36822015-11-18 13:08:07 -05002377 const std::string &varyingSemantic =
2378 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2379
Jamie Madillccdf74b2015-08-18 10:46:12 -04002380 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002381 mStreamOutVaryings.clear();
2382
Jamie Madill48ef11b2016-04-27 15:21:52 -04002383 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002384 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2385 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002386 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002387 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2388 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002389 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002390 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002391 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002392 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2393 builtins.glPosition.index, 4, outputSlot));
2394 }
2395 }
2396 else if (tfVaryingName == "gl_FragCoord")
2397 {
2398 if (builtins.glFragCoord.enabled)
2399 {
2400 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2401 builtins.glFragCoord.index, 4, outputSlot));
2402 }
2403 }
2404 else if (tfVaryingName == "gl_PointSize")
2405 {
2406 if (builtins.glPointSize.enabled)
2407 {
2408 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2409 }
2410 }
2411 else
2412 {
Jamie Madill192745a2016-12-22 15:58:21 -05002413 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002414 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002415 const auto &varying = *registerInfo.packedVarying->varying;
2416 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002417 int componentCount = gl::VariableColumnCount(transposedType);
2418 ASSERT(!varying.isBuiltIn());
2419
Jamie Madill55c25d02015-11-18 13:08:08 -05002420 // Transform feedback for varying structs is underspecified.
2421 // See Khronos bug 9856.
2422 // TODO(jmadill): Figure out how to be spec-compliant here.
2423 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2424 continue;
2425
Jamie Madill9fc36822015-11-18 13:08:07 -05002426 // There can be more than one register assigned to a particular varying, and each
2427 // register needs its own stream out entry.
2428 if (tfVaryingName == varying.name)
2429 {
2430 mStreamOutVaryings.push_back(D3DVarying(
2431 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2432 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002433 }
2434 }
2435 }
2436}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002437
2438D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2439{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002440 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002441}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002442
2443bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2444{
2445 std::string baseName = blockName;
2446 gl::ParseAndStripArrayIndex(&baseName);
2447
2448 auto sizeIter = mBlockDataSizes.find(baseName);
2449 if (sizeIter == mBlockDataSizes.end())
2450 {
2451 *sizeOut = 0;
2452 return false;
2453 }
2454
2455 *sizeOut = sizeIter->second;
2456 return true;
2457}
2458
2459bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2460 sh::BlockMemberInfo *memberInfoOut) const
2461{
2462 auto infoIter = mBlockInfo.find(memberUniformName);
2463 if (infoIter == mBlockInfo.end())
2464 {
2465 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2466 return false;
2467 }
2468
2469 *memberInfoOut = infoIter->second;
2470 return true;
2471}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002472
2473void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2474 GLenum genMode,
2475 GLint components,
2476 const GLfloat *coeffs)
2477{
2478 UNREACHABLE();
2479}
2480
Jamie Madill8047c0d2016-03-07 13:02:12 -05002481} // namespace rx