blob: 2deda678ce6fcca2bdec08e6a337ca3ae8e71396 [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 {
Jamie Madill6a6b09c2017-01-12 21:52:29 +00001191 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1192 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1193 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001194 }
Brandon Jones22502d52014-08-29 16:58:36 -07001195
Geoff Langb543aff2014-09-30 14:52:54 -04001196 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001197 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001198}
1199
Jamie Madilld3dfda22015-07-06 08:28:49 -04001200gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001201 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001202 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001203{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001204 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001205
1206 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1207 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001208 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001209 {
Geoff Langb543aff2014-09-30 14:52:54 -04001210 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001211 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001212 }
1213 }
1214
Brandon Jones22502d52014-08-29 16:58:36 -07001215 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001216 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001217 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001218
1219 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001220 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001221
1222 gl::InfoLog tempInfoLog;
1223 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1224
Jamie Madill01074252016-11-28 15:55:51 -05001225 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001226 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001227 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001228 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001229
Jamie Madill97399232014-12-23 12:31:15 -05001230 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001231 {
Xinghua Caob1239382016-12-13 15:07:05 +08001232 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
1233 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001234 }
Jamie Madill97399232014-12-23 12:31:15 -05001235 else if (!infoLog)
1236 {
Jamie Madill6a6b09c2017-01-12 21:52:29 +00001237 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1238 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1239 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001240 }
Brandon Jones22502d52014-08-29 16:58:36 -07001241
Geoff Langb543aff2014-09-30 14:52:54 -04001242 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001243 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001244}
1245
Jamie Madill9082b982016-04-27 15:21:51 -04001246gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001247 GLenum drawMode,
1248 ShaderExecutableD3D **outExecutable,
1249 gl::InfoLog *infoLog)
1250{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001251 if (outExecutable)
1252 {
1253 *outExecutable = nullptr;
1254 }
1255
Austin Kinross88829e82016-01-12 13:04:41 -08001256 // Return a null shader if the current rendering doesn't use a geometry shader
1257 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001258 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001259 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001260 }
1261
Jamie Madill4e31ad52015-10-29 10:32:57 -04001262 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1263
Xinghua Caob1239382016-12-13 15:07:05 +08001264 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001265 {
1266 if (outExecutable)
1267 {
Xinghua Caob1239382016-12-13 15:07:05 +08001268 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001269 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001270 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001271 }
1272
1273 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001274 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001275 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001276
1277 gl::InfoLog tempInfoLog;
1278 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1279
Xinghua Caob1239382016-12-13 15:07:05 +08001280 ShaderExecutableD3D *geometryExecutable = nullptr;
1281 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001282 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001283 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001284 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001285
1286 if (!infoLog && error.isError())
1287 {
Jamie Madill6a6b09c2017-01-12 21:52:29 +00001288 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1289 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1290 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001291 }
1292
Xinghua Caob1239382016-12-13 15:07:05 +08001293 if (geometryExecutable != nullptr)
1294 {
1295 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1296 }
1297
Jamie Madill4e31ad52015-10-29 10:32:57 -04001298 if (outExecutable)
1299 {
Xinghua Caob1239382016-12-13 15:07:05 +08001300 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001301 }
1302 return error;
1303}
1304
Jamie Madill01074252016-11-28 15:55:51 -05001305class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001306{
Jamie Madill01074252016-11-28 15:55:51 -05001307 public:
1308 GetExecutableTask(ProgramD3D *program)
1309 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001310 {
Brandon Joneseb994362014-09-24 10:27:28 -07001311 }
1312
Jamie Madill01074252016-11-28 15:55:51 -05001313 virtual gl::Error run() = 0;
1314
1315 void operator()() override { mError = run(); }
1316
1317 const gl::Error &getError() const { return mError; }
1318 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1319 ShaderExecutableD3D *getResult() { return mResult; }
1320
1321 protected:
1322 ProgramD3D *mProgram;
1323 gl::Error mError;
1324 gl::InfoLog mInfoLog;
1325 ShaderExecutableD3D *mResult;
1326};
1327
1328class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1329{
1330 public:
1331 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1332 gl::Error run() override
1333 {
1334 const auto &defaultInputLayout =
1335 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1336
1337 ANGLE_TRY(
1338 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1339
1340 return gl::NoError();
1341 }
1342};
1343
1344class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1345{
1346 public:
1347 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1348 gl::Error run() override
1349 {
1350 const auto &defaultPixelOutput =
1351 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1352
1353 ANGLE_TRY(
1354 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1355
1356 return gl::NoError();
1357 }
1358};
1359
1360class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1361{
1362 public:
1363 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1364 : GetExecutableTask(program), mContextState(contextState)
1365 {
1366 }
1367
1368 gl::Error run() override
1369 {
1370 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1371 // D3D11.
1372 if (mProgram->usesGeometryShader(GL_POINTS))
1373 {
1374 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1375 &mResult, &mInfoLog));
1376 }
1377
1378 return gl::NoError();
1379 }
1380
1381 private:
1382 const gl::ContextState &mContextState;
1383};
1384
1385LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1386 gl::InfoLog &infoLog)
1387{
1388 // Ensure the compiler is initialized to avoid race conditions.
1389 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1390
1391 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1392
1393 GetVertexExecutableTask vertexTask(this);
1394 GetPixelExecutableTask pixelTask(this);
1395 GetGeometryExecutableTask geometryTask(this, contextState);
1396
1397 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1398 workerPool->postWorkerTask(&pixelTask),
1399 workerPool->postWorkerTask(&geometryTask)}};
1400
1401 WaitableEvent::WaitMany(&waitEvents);
1402
1403 infoLog << vertexTask.getInfoLog().str();
1404 infoLog << pixelTask.getInfoLog().str();
1405 infoLog << geometryTask.getInfoLog().str();
1406
1407 ANGLE_TRY(vertexTask.getError());
1408 ANGLE_TRY(pixelTask.getError());
1409 ANGLE_TRY(geometryTask.getError());
1410
1411 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1412 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1413 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1414
Jamie Madill48ef11b2016-04-27 15:21:52 -04001415 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001416
1417 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001418 {
Jamie Madill334d6152015-10-22 14:00:28 -04001419 // Geometry shaders are currently only used internally, so there is no corresponding shader
1420 // object at the interface level. For now the geometry shader debug info is prepended to
1421 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001422 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001423 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001424 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1425 }
1426
1427 if (defaultVertexExecutable)
1428 {
1429 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1430 }
1431
1432 if (defaultPixelExecutable)
1433 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001434 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001435 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001436 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1437 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001438
Jamie Madillb0a838b2016-11-13 20:02:12 -05001439 return (defaultVertexExecutable && defaultPixelExecutable &&
1440 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001441}
1442
Xinghua Caob1239382016-12-13 15:07:05 +08001443LinkResult ProgramD3D::compileComputeExecutable(gl::InfoLog &infoLog)
1444{
1445 // Ensure the compiler is initialized to avoid race conditions.
1446 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1447
1448 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(mState);
1449
1450 ShaderExecutableD3D *computeExecutable = nullptr;
1451 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, SHADER_COMPUTE,
1452 std::vector<D3DVarying>(), false,
1453 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1454
1455 if (computeExecutable == nullptr)
1456 {
1457 ERR("Error compiling dynamic compute executable:\n%s\n", infoLog.str().c_str());
1458 }
1459 else
1460 {
1461 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1462 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1463 mComputeExecutable.reset(computeExecutable);
1464 }
1465
1466 return mComputeExecutable.get() != nullptr;
1467}
1468
Jamie Madill192745a2016-12-22 15:58:21 -05001469LinkResult ProgramD3D::link(const gl::ContextState &data,
1470 const gl::VaryingPacking &packing,
1471 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001472{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001473 reset();
1474
Xinghua Caob1239382016-12-13 15:07:05 +08001475 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1476 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001477 {
Xinghua Caob1239382016-12-13 15:07:05 +08001478 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1479
1480 defineUniformsAndAssignRegisters();
1481
1482 LinkResult result = compileComputeExecutable(infoLog);
1483 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001484 {
Xinghua Caob1239382016-12-13 15:07:05 +08001485 infoLog << result.getError().getMessage();
1486 return result;
1487 }
1488 else if (!result.getResult())
1489 {
1490 infoLog << "Failed to create D3D compute shader.";
1491 return result;
1492 }
1493
1494 initUniformBlockInfo(computeShader);
1495 }
1496 else
1497 {
1498 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1499 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1500
1501 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1502 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1503
1504 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1505 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1506
1507 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1508 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1509
1510 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1511 {
1512 if (fragmentShaderD3D->usesFrontFacing())
1513 {
1514 infoLog << "The current renderer doesn't support gl_FrontFacing";
1515 return false;
1516 }
1517 }
1518
1519 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1520 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1521 // intelligently, but D3D9 assumes one semantic per register.
1522 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1523 packing.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
1524 {
1525 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001526 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001527 }
Xinghua Caob1239382016-12-13 15:07:05 +08001528
1529 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
1530 BuiltinVaryingsD3D builtins(metadata, packing);
1531
1532 mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, packing, builtins, &mPixelHLSL,
1533 &mVertexHLSL);
1534
1535 mUsesPointSize = vertexShaderD3D->usesPointSize();
1536 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1537 mUsesFragDepth = metadata.usesFragDepth();
1538
1539 // Cache if we use flat shading
1540 mUsesFlatInterpolation = (FindFlatInterpolationVarying(fragmentShader->getVaryings()) ||
1541 FindFlatInterpolationVarying(vertexShader->getVaryings()));
1542
1543 if (mRenderer->getMajorShaderModel() >= 4)
1544 {
1545 mGeometryShaderPreamble =
1546 mDynamicHLSL->generateGeometryShaderPreamble(packing, builtins);
1547 }
1548
1549 initAttribLocationsToD3DSemantic();
1550
1551 defineUniformsAndAssignRegisters();
1552
1553 gatherTransformFeedbackVaryings(packing, builtins[SHADER_VERTEX]);
1554
1555 LinkResult result = compileProgramExecutables(data, infoLog);
1556 if (result.isError())
1557 {
1558 infoLog << result.getError().getMessage();
1559 return result;
1560 }
1561 else if (!result.getResult())
1562 {
1563 infoLog << "Failed to create D3D shaders.";
1564 return result;
1565 }
1566
1567 initUniformBlockInfo(vertexShader);
1568 initUniformBlockInfo(fragmentShader);
Austin Kinross02df7962015-07-01 10:03:42 -07001569 }
1570
Jamie Madillb0a838b2016-11-13 20:02:12 -05001571 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001572}
1573
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001574GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001575{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001576 // TODO(jmadill): Do something useful here?
1577 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001578}
1579
Xinghua Caob1239382016-12-13 15:07:05 +08001580void ProgramD3D::initUniformBlockInfo(const gl::Shader *shader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001581{
Xinghua Caob1239382016-12-13 15:07:05 +08001582 for (const sh::InterfaceBlock &interfaceBlock : shader->getInterfaceBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001583 {
Xinghua Caob1239382016-12-13 15:07:05 +08001584 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001585 continue;
1586
Xinghua Caob1239382016-12-13 15:07:05 +08001587 if (mBlockDataSizes.count(interfaceBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001588 continue;
1589
Xinghua Caob1239382016-12-13 15:07:05 +08001590 size_t dataSize = getUniformBlockInfo(interfaceBlock);
1591 mBlockDataSizes[interfaceBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001592 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001593}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001594
Jamie Madill51f522f2016-12-21 15:10:55 -05001595void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001596{
Jamie Madill51f522f2016-12-21 15:10:55 -05001597 // Lazy init.
1598 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1599 {
1600 return;
1601 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001602
1603 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001604 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1605 const ShaderD3D *fragmentShaderD3D =
1606 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1607 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001608
Jamie Madill48ef11b2016-04-27 15:21:52 -04001609 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001610 {
1611 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1612
Jamie Madill4a3c2342015-10-08 12:58:45 -04001613 D3DUniformBlock d3dUniformBlock;
1614
Jamie Madill62d31cb2015-09-11 13:25:51 -04001615 if (uniformBlock.vertexStaticUse)
1616 {
Xinghua Caob1239382016-12-13 15:07:05 +08001617 ASSERT(vertexShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001618 unsigned int baseRegister =
1619 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001620 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001621 }
1622
1623 if (uniformBlock.fragmentStaticUse)
1624 {
Xinghua Caob1239382016-12-13 15:07:05 +08001625 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001626 unsigned int baseRegister =
1627 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001628 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001629 }
1630
Xinghua Caob1239382016-12-13 15:07:05 +08001631 if (uniformBlock.computeStaticUse)
1632 {
1633 ASSERT(computeShaderD3D != nullptr);
1634 unsigned int baseRegister =
1635 computeShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
1636 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1637 }
1638
Jamie Madill4a3c2342015-10-08 12:58:45 -04001639 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001640 }
1641}
1642
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001643void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001644{
1645 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001646 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001647 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001648 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001649 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001650 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001651 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001652 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001653 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001654 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001655 vertexRegisters = std::max(vertexRegisters,
1656 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001657 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001658 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001659 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001660 fragmentRegisters = std::max(
1661 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001662 }
Xinghua Caob1239382016-12-13 15:07:05 +08001663 if (d3dUniform->isReferencedByComputeShader())
1664 {
1665 computeRegisters = std::max(
1666 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1667 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001668 }
1669 }
1670
Xinghua Caob1239382016-12-13 15:07:05 +08001671 mVertexUniformStorage =
1672 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1673 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1674 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1675 mComputeUniformStorage =
1676 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Brandon Jonesc9610c52014-08-25 17:02:59 -07001677}
1678
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001679gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001680{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001681 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001682
Jamie Madill01074252016-11-28 15:55:51 -05001683 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001684
Jamie Madill62d31cb2015-09-11 13:25:51 -04001685 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001686 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001687 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001688 }
1689
Jamie Madill51f522f2016-12-21 15:10:55 -05001690 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001691}
1692
Jamie Madill9082b982016-04-27 15:21:51 -04001693gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001694{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001695 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001696 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001697 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001698 }
1699
Jamie Madill51f522f2016-12-21 15:10:55 -05001700 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001701
Jamie Madill03260fa2015-06-22 13:57:22 -04001702 mVertexUBOCache.clear();
1703 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001704
1705 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1706 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1707
Jamie Madill4a3c2342015-10-08 12:58:45 -04001708 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001709 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001710 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001711 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001712 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001713
Brandon Jones18bd4102014-09-22 14:21:44 -07001714 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001715 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001716 {
1717 continue;
1718 }
1719
Jamie Madill4a3c2342015-10-08 12:58:45 -04001720 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001721 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001722 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001723 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001724
Jamie Madill969194d2015-07-20 14:36:56 -04001725 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001726 {
1727 mVertexUBOCache.resize(registerIndex + 1, -1);
1728 }
1729
1730 ASSERT(mVertexUBOCache[registerIndex] == -1);
1731 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001732 }
1733
Jamie Madill4a3c2342015-10-08 12:58:45 -04001734 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001735 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001736 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001737 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001738
1739 if (mFragmentUBOCache.size() <= registerIndex)
1740 {
1741 mFragmentUBOCache.resize(registerIndex + 1, -1);
1742 }
1743
1744 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1745 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001746 }
1747 }
1748
Jamie Madill03260fa2015-06-22 13:57:22 -04001749 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001750}
1751
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001752void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001753{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001754 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001755 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001756 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001757 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001758}
1759
Jamie Madill334d6152015-10-22 14:00:28 -04001760void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001761{
1762 setUniform(location, count, v, GL_FLOAT);
1763}
1764
1765void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1766{
1767 setUniform(location, count, v, GL_FLOAT_VEC2);
1768}
1769
1770void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1771{
1772 setUniform(location, count, v, GL_FLOAT_VEC3);
1773}
1774
1775void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1776{
1777 setUniform(location, count, v, GL_FLOAT_VEC4);
1778}
1779
Jamie Madill334d6152015-10-22 14:00:28 -04001780void ProgramD3D::setUniformMatrix2fv(GLint location,
1781 GLsizei count,
1782 GLboolean transpose,
1783 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001784{
1785 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1786}
1787
Jamie Madill334d6152015-10-22 14:00:28 -04001788void ProgramD3D::setUniformMatrix3fv(GLint location,
1789 GLsizei count,
1790 GLboolean transpose,
1791 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001792{
1793 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1794}
1795
Jamie Madill334d6152015-10-22 14:00:28 -04001796void ProgramD3D::setUniformMatrix4fv(GLint location,
1797 GLsizei count,
1798 GLboolean transpose,
1799 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001800{
1801 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1802}
1803
Jamie Madill334d6152015-10-22 14:00:28 -04001804void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1805 GLsizei count,
1806 GLboolean transpose,
1807 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001808{
1809 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1810}
1811
Jamie Madill334d6152015-10-22 14:00:28 -04001812void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1813 GLsizei count,
1814 GLboolean transpose,
1815 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001816{
1817 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1818}
1819
Jamie Madill334d6152015-10-22 14:00:28 -04001820void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1821 GLsizei count,
1822 GLboolean transpose,
1823 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001824{
1825 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1826}
1827
Jamie Madill334d6152015-10-22 14:00:28 -04001828void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1829 GLsizei count,
1830 GLboolean transpose,
1831 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001832{
1833 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1834}
1835
Jamie Madill334d6152015-10-22 14:00:28 -04001836void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1837 GLsizei count,
1838 GLboolean transpose,
1839 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001840{
1841 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1842}
1843
Jamie Madill334d6152015-10-22 14:00:28 -04001844void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1845 GLsizei count,
1846 GLboolean transpose,
1847 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001848{
1849 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1850}
1851
1852void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1853{
1854 setUniform(location, count, v, GL_INT);
1855}
1856
1857void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1858{
1859 setUniform(location, count, v, GL_INT_VEC2);
1860}
1861
1862void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1863{
1864 setUniform(location, count, v, GL_INT_VEC3);
1865}
1866
1867void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1868{
1869 setUniform(location, count, v, GL_INT_VEC4);
1870}
1871
1872void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1873{
1874 setUniform(location, count, v, GL_UNSIGNED_INT);
1875}
1876
1877void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1878{
1879 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1880}
1881
1882void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1883{
1884 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1885}
1886
1887void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1888{
1889 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1890}
1891
Jamie Madill4a3c2342015-10-08 12:58:45 -04001892void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1893 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001894{
1895}
1896
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001897void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001898{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001899 D3DUniformMap uniformMap;
Xinghua Caob1239382016-12-13 15:07:05 +08001900 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1901 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05001902 {
Xinghua Caob1239382016-12-13 15:07:05 +08001903 for (const sh::Uniform &computeUniform : computeShader->getUniforms())
1904
Jamie Madillfb536032015-09-11 13:19:49 -04001905 {
Xinghua Caob1239382016-12-13 15:07:05 +08001906 if (computeUniform.staticUse)
1907 {
1908 defineUniformBase(computeShader, computeUniform, &uniformMap);
1909 }
Jamie Madillfb536032015-09-11 13:19:49 -04001910 }
1911 }
Xinghua Caob1239382016-12-13 15:07:05 +08001912 else
Jamie Madillfb536032015-09-11 13:19:49 -04001913 {
Xinghua Caob1239382016-12-13 15:07:05 +08001914 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1915 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
1916
Jamie Madillfb536032015-09-11 13:19:49 -04001917 {
Xinghua Caob1239382016-12-13 15:07:05 +08001918 if (vertexUniform.staticUse)
1919 {
1920 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
1921 }
1922 }
1923
1924 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1925 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
1926 {
1927 if (fragmentUniform.staticUse)
1928 {
1929 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
1930 }
Jamie Madillfb536032015-09-11 13:19:49 -04001931 }
1932 }
1933
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001934 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001935 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001936 {
1937 if (!glUniform.isInDefaultBlock())
1938 continue;
1939
1940 auto mapEntry = uniformMap.find(glUniform.name);
1941 ASSERT(mapEntry != uniformMap.end());
1942 mD3DUniforms.push_back(mapEntry->second);
1943 }
1944
Jamie Madill62d31cb2015-09-11 13:25:51 -04001945 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001946 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001947}
1948
Jamie Madill91445bc2015-09-23 16:47:53 -04001949void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001950 const sh::Uniform &uniform,
1951 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001952{
Olli Etuaho96963162016-03-21 11:54:33 +02001953 // Samplers get their registers assigned in assignAllSamplerRegisters.
1954 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001955 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001956 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001957 return;
1958 }
1959
Jamie Madill91445bc2015-09-23 16:47:53 -04001960 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1961
1962 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1963 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001965 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001966
Jamie Madill91445bc2015-09-23 16:47:53 -04001967 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001968}
1969
Jamie Madill62d31cb2015-09-11 13:25:51 -04001970D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1971{
1972 for (D3DUniform *d3dUniform : mD3DUniforms)
1973 {
1974 if (d3dUniform->name == name)
1975 {
1976 return d3dUniform;
1977 }
1978 }
1979
1980 return nullptr;
1981}
1982
Jamie Madill91445bc2015-09-23 16:47:53 -04001983void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001984 const sh::ShaderVariable &uniform,
1985 const std::string &fullName,
1986 sh::HLSLBlockEncoder *encoder,
1987 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001988{
1989 if (uniform.isStruct())
1990 {
1991 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1992 {
1993 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1994
Jamie Madill55def582015-05-04 11:24:57 -04001995 if (encoder)
1996 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001997
1998 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1999 {
Jamie Madill334d6152015-10-22 14:00:28 -04002000 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002001 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2002
Olli Etuaho96963162016-03-21 11:54:33 +02002003 // Samplers get their registers assigned in assignAllSamplerRegisters.
2004 // Also they couldn't use the same encoder as the rest of the struct, since they are
2005 // extracted out of the struct by the shader translator.
2006 if (gl::IsSamplerType(field.type))
2007 {
2008 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2009 }
2010 else
2011 {
2012 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2013 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002014 }
2015
Jamie Madill55def582015-05-04 11:24:57 -04002016 if (encoder)
2017 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002020 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002021
2022 // Not a struct. Arrays are treated as aggregate types.
2023 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002024 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002025 encoder->enterAggregateType();
2026 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002027
Jamie Madill62d31cb2015-09-11 13:25:51 -04002028 // Advance the uniform offset, to track registers allocation for structs
2029 sh::BlockMemberInfo blockInfo =
2030 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2031 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002032
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002033 auto uniformMapEntry = uniformMap->find(fullName);
2034 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002035
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002036 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002037 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002038 d3dUniform = uniformMapEntry->second;
2039 }
2040 else
2041 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002042 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002043 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002044 }
2045
2046 if (encoder)
2047 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002048 d3dUniform->registerElement =
2049 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002050 unsigned int reg =
2051 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002052 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002053 {
2054 d3dUniform->psRegisterIndex = reg;
2055 }
Xinghua Caob1239382016-12-13 15:07:05 +08002056 else if (shaderType == GL_VERTEX_SHADER)
2057 {
2058 d3dUniform->vsRegisterIndex = reg;
2059 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002060 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002061 {
Xinghua Caob1239382016-12-13 15:07:05 +08002062 ASSERT(shaderType == GL_COMPUTE_SHADER);
2063 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002064 }
Jamie Madillfb536032015-09-11 13:19:49 -04002065
2066 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002067 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002068 {
2069 encoder->exitAggregateType();
2070 }
2071 }
2072}
2073
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002074template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002075void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002076{
Jamie Madill334d6152015-10-22 14:00:28 -04002077 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002078 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2079
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002081
Jamie Madill62d31cb2015-09-11 13:25:51 -04002082 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002083 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002084 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002085
2086 if (targetUniform->type == targetUniformType)
2087 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002089
Jamie Madill62d31cb2015-09-11 13:25:51 -04002090 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002091 {
Jamie Madill334d6152015-10-22 14:00:28 -04002092 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002093 const T *source = v + (i * components);
2094
2095 for (int c = 0; c < components; c++)
2096 {
2097 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2098 }
2099 for (int c = components; c < 4; c++)
2100 {
2101 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2102 }
2103 }
2104 }
2105 else if (targetUniform->type == targetBoolType)
2106 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002107 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108
Jamie Madill62d31cb2015-09-11 13:25:51 -04002109 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002110 {
Jamie Madill334d6152015-10-22 14:00:28 -04002111 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002112 const T *source = v + (i * components);
2113
2114 for (int c = 0; c < components; c++)
2115 {
Jamie Madill334d6152015-10-22 14:00:28 -04002116 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2117 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002118 }
2119 for (int c = components; c < 4; c++)
2120 {
2121 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2122 }
2123 }
2124 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002125 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002126 {
2127 ASSERT(targetUniformType == GL_INT);
2128
Jamie Madill62d31cb2015-09-11 13:25:51 -04002129 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002130
2131 bool wasDirty = targetUniform->dirty;
2132
Jamie Madill62d31cb2015-09-11 13:25:51 -04002133 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002134 {
Jamie Madill334d6152015-10-22 14:00:28 -04002135 GLint *dest = target + (i * 4);
2136 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002137
2138 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2139 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2140 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2141 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2142 }
2143
2144 if (!wasDirty && targetUniform->dirty)
2145 {
2146 mDirtySamplerMapping = true;
2147 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002148 }
Jamie Madill334d6152015-10-22 14:00:28 -04002149 else
2150 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002151}
2152
2153template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002154void ProgramD3D::setUniformMatrixfv(GLint location,
2155 GLsizei countIn,
2156 GLboolean transpose,
2157 const GLfloat *value,
2158 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002159{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002160 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002161
Jamie Madill62d31cb2015-09-11 13:25:51 -04002162 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002163 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002164 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002165
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002166 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002167 GLfloat *target =
2168 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002169
Jamie Madill62d31cb2015-09-11 13:25:51 -04002170 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002171 {
2172 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2173 if (transpose == GL_FALSE)
2174 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002175 targetUniform->dirty =
2176 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002177 }
2178 else
2179 {
Jamie Madill334d6152015-10-22 14:00:28 -04002180 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002181 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002182 }
2183 target += targetMatrixStride;
2184 value += cols * rows;
2185 }
2186}
2187
Jamie Madill4a3c2342015-10-08 12:58:45 -04002188size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002189{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002190 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002191
Jamie Madill62d31cb2015-09-11 13:25:51 -04002192 // define member uniforms
2193 sh::Std140BlockEncoder std140Encoder;
2194 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2195 sh::BlockLayoutEncoder *encoder = nullptr;
2196
2197 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002198 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002199 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002200 }
2201 else
2202 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002203 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002204 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002205
Jamie Madill39046162016-02-08 15:05:17 -05002206 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2207 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002208
2209 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002210}
2211
Jamie Madill62d31cb2015-09-11 13:25:51 -04002212void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002213{
Olli Etuaho96963162016-03-21 11:54:33 +02002214 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002215 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002216 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002217 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002218 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002219 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002220 }
2221}
2222
Olli Etuaho96963162016-03-21 11:54:33 +02002223void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002224{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002225 ASSERT(d3dUniform->isSampler());
Xinghua Caob1239382016-12-13 15:07:05 +08002226 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2227 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002228 {
Xinghua Caob1239382016-12-13 15:07:05 +08002229 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2230 ASSERT(computeShaderD3D->hasUniform(d3dUniform));
2231 d3dUniform->csRegisterIndex = computeShaderD3D->getUniformRegister(d3dUniform->name);
2232 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2233 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2234 mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002235 }
Xinghua Caob1239382016-12-13 15:07:05 +08002236 else
Jamie Madillfb536032015-09-11 13:19:49 -04002237 {
Xinghua Caob1239382016-12-13 15:07:05 +08002238 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2239 const ShaderD3D *fragmentShaderD3D =
2240 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2241 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) ||
2242 fragmentShaderD3D->hasUniform(d3dUniform));
2243 if (vertexShaderD3D->hasUniform(d3dUniform))
2244 {
2245 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2246 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
2247 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2248 mSamplersVS, &mUsedVertexSamplerRange);
2249 }
2250 if (fragmentShaderD3D->hasUniform(d3dUniform))
2251 {
2252 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2253 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
2254 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2255 mSamplersPS, &mUsedPixelSamplerRange);
2256 }
Jamie Madillfb536032015-09-11 13:19:49 -04002257 }
2258}
2259
Jamie Madill62d31cb2015-09-11 13:25:51 -04002260// static
2261void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002262 GLenum samplerType,
2263 unsigned int samplerCount,
2264 std::vector<Sampler> &outSamplers,
2265 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002266{
2267 unsigned int samplerIndex = startSamplerIndex;
2268
2269 do
2270 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002271 ASSERT(samplerIndex < outSamplers.size());
2272 Sampler *sampler = &outSamplers[samplerIndex];
2273 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002274 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002275 sampler->logicalTextureUnit = 0;
2276 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002277 samplerIndex++;
2278 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002279}
2280
Brandon Jonesc9610c52014-08-25 17:02:59 -07002281void ProgramD3D::reset()
2282{
Xinghua Caob1239382016-12-13 15:07:05 +08002283 mVertexExecutables.clear();
2284 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002285
Xinghua Caob1239382016-12-13 15:07:05 +08002286 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002287 {
Xinghua Caob1239382016-12-13 15:07:05 +08002288 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002289 }
Brandon Joneseb994362014-09-24 10:27:28 -07002290
Xinghua Caob1239382016-12-13 15:07:05 +08002291 mComputeExecutable.reset(nullptr);
2292
Brandon Jones22502d52014-08-29 16:58:36 -07002293 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002294 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002295
2296 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002297 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002298 mUsesFragDepth = false;
2299 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002300 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002301 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002302
Jamie Madill62d31cb2015-09-11 13:25:51 -04002303 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002304 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002305
Xinghua Caob1239382016-12-13 15:07:05 +08002306 mVertexUniformStorage.reset(nullptr);
2307 mFragmentUniformStorage.reset(nullptr);
2308 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002309
2310 mSamplersPS.clear();
2311 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002312 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002313
2314 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002315 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002316 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002317 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002318
Jamie Madill8047c0d2016-03-07 13:02:12 -05002319 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002320
Jamie Madill9fc36822015-11-18 13:08:07 -05002321 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002322
2323 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002324}
2325
Geoff Lang7dd2e102014-11-10 15:19:26 -05002326unsigned int ProgramD3D::getSerial() const
2327{
2328 return mSerial;
2329}
2330
2331unsigned int ProgramD3D::issueSerial()
2332{
2333 return mCurrentSerial++;
2334}
2335
Jamie Madill8047c0d2016-03-07 13:02:12 -05002336void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002337{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002338 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002339 ASSERT(vertexShader != nullptr);
2340
2341 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002342 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002343 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002344 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2345 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002346
Jamie Madill8047c0d2016-03-07 13:02:12 -05002347 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002348 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002349 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002350 }
2351 }
Jamie Madill437d2662014-12-05 14:23:35 -05002352}
2353
Jamie Madill63805b42015-08-25 13:17:39 -04002354void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002355{
Jamie Madillbd136f92015-08-10 14:51:37 -04002356 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002357 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002358
Jamie Madill01074252016-11-28 15:55:51 -05002359 for (unsigned int locationIndex : IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002360 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002361 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002362
Jamie Madill8047c0d2016-03-07 13:02:12 -05002363 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002364 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002365 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002366 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002367 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002368 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002369 mCachedInputLayout[d3dSemantic] =
2370 GetVertexFormatType(vertexAttributes[locationIndex],
2371 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002372 }
2373 }
2374}
2375
Jamie Madill192745a2016-12-22 15:58:21 -05002376void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2377 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002378{
Jamie Madill9fc36822015-11-18 13:08:07 -05002379 const std::string &varyingSemantic =
2380 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2381
Jamie Madillccdf74b2015-08-18 10:46:12 -04002382 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002383 mStreamOutVaryings.clear();
2384
Jamie Madill48ef11b2016-04-27 15:21:52 -04002385 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002386 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2387 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002388 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002389 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2390 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002391 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002392 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002393 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002394 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2395 builtins.glPosition.index, 4, outputSlot));
2396 }
2397 }
2398 else if (tfVaryingName == "gl_FragCoord")
2399 {
2400 if (builtins.glFragCoord.enabled)
2401 {
2402 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2403 builtins.glFragCoord.index, 4, outputSlot));
2404 }
2405 }
2406 else if (tfVaryingName == "gl_PointSize")
2407 {
2408 if (builtins.glPointSize.enabled)
2409 {
2410 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2411 }
2412 }
2413 else
2414 {
Jamie Madill192745a2016-12-22 15:58:21 -05002415 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002416 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002417 const auto &varying = *registerInfo.packedVarying->varying;
2418 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002419 int componentCount = gl::VariableColumnCount(transposedType);
2420 ASSERT(!varying.isBuiltIn());
2421
Jamie Madill55c25d02015-11-18 13:08:08 -05002422 // Transform feedback for varying structs is underspecified.
2423 // See Khronos bug 9856.
2424 // TODO(jmadill): Figure out how to be spec-compliant here.
2425 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2426 continue;
2427
Jamie Madill9fc36822015-11-18 13:08:07 -05002428 // There can be more than one register assigned to a particular varying, and each
2429 // register needs its own stream out entry.
2430 if (tfVaryingName == varying.name)
2431 {
2432 mStreamOutVaryings.push_back(D3DVarying(
2433 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2434 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002435 }
2436 }
2437 }
2438}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002439
2440D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2441{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002442 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002443}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002444
2445bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2446{
2447 std::string baseName = blockName;
2448 gl::ParseAndStripArrayIndex(&baseName);
2449
2450 auto sizeIter = mBlockDataSizes.find(baseName);
2451 if (sizeIter == mBlockDataSizes.end())
2452 {
2453 *sizeOut = 0;
2454 return false;
2455 }
2456
2457 *sizeOut = sizeIter->second;
2458 return true;
2459}
2460
2461bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2462 sh::BlockMemberInfo *memberInfoOut) const
2463{
2464 auto infoIter = mBlockInfo.find(memberUniformName);
2465 if (infoIter == mBlockInfo.end())
2466 {
2467 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2468 return false;
2469 }
2470
2471 *memberInfoOut = infoIter->second;
2472 return true;
2473}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002474
2475void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2476 GLenum genMode,
2477 GLint components,
2478 const GLfloat *coeffs)
2479{
2480 UNREACHABLE();
2481}
2482
Jamie Madill8047c0d2016-03-07 13:02:12 -05002483} // namespace rx