blob: 11ed561fa9c1027188a85faba4d048e17a1295e5 [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Jamie Madill20e005b2017-04-07 14:19:22 -040011#include "common/bitset_utils.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Jamie Madillc564c072017-06-01 12:45:42 -040013#include "libANGLE/Context.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Framebuffer.h"
15#include "libANGLE/FramebufferAttachment.h"
16#include "libANGLE/Program.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040017#include "libANGLE/Uniform.h"
Jamie Madill192745a2016-12-22 15:58:21 -050018#include "libANGLE/VaryingPacking.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040019#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000020#include "libANGLE/features.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050021#include "libANGLE/renderer/ContextImpl.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050022#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050023#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050024#include "libANGLE/renderer/d3d/RendererD3D.h"
25#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050026#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050027#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050028
Jamie Madill01074252016-11-28 15:55:51 -050029using namespace angle;
30
Brandon Jonesc9610c52014-08-25 17:02:59 -070031namespace rx
32{
33
Brandon Joneseb994362014-09-24 10:27:28 -070034namespace
35{
36
Jamie Madillf8dd7b12015-08-05 13:50:08 -040037gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070038{
Jamie Madillbd136f92015-08-10 14:51:37 -040039 gl::InputLayout defaultLayout;
40 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070041 {
Brandon Joneseb994362014-09-24 10:27:28 -070042 if (shaderAttr.type != GL_NONE)
43 {
44 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
45
Jamie Madilld3dfda22015-07-06 08:28:49 -040046 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040047 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070048 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040049 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040050 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040051 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040052 gl::VertexFormatType defaultType =
53 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070054
Jamie Madillbd136f92015-08-10 14:51:37 -040055 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070056 }
57 }
58 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040059
60 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070061}
62
Jamie Madill334d6152015-10-22 14:00:28 -040063std::vector<GLenum> GetDefaultOutputLayoutFromShader(
64 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070065{
Jamie Madillb4463142014-12-19 14:56:54 -050066 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070067
Jamie Madillb4463142014-12-19 14:56:54 -050068 if (!shaderOutputVars.empty())
69 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070070 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
71 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050072 }
Brandon Joneseb994362014-09-24 10:27:28 -070073
74 return defaultPixelOutput;
75}
76
Brandon Jones1a8a7e32014-10-01 12:49:30 -070077bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
78{
79 return var.isRowMajorLayout;
80}
81
82bool IsRowMajorLayout(const sh::ShaderVariable &var)
83{
84 return false;
85}
86
Jamie Madill62d31cb2015-09-11 13:25:51 -040087template <typename VarT>
88void GetUniformBlockInfo(const std::vector<VarT> &fields,
89 const std::string &prefix,
90 sh::BlockLayoutEncoder *encoder,
91 bool inRowMajorLayout,
92 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
93{
94 for (const VarT &field : fields)
95 {
96 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
97
98 if (field.isStruct())
99 {
100 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
101
102 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
103 {
104 encoder->enterAggregateType();
105
106 const std::string uniformElementName =
107 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
108 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
109 blockInfoOut);
110
111 encoder->exitAggregateType();
112 }
113 }
114 else
115 {
116 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
117 (*blockInfoOut)[fieldName] =
118 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
119 }
120 }
121}
122
Jamie Madill334d6152015-10-22 14:00:28 -0400123template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100124static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
125{
Yunchao He4f285442017-04-21 12:15:49 +0800126 ASSERT(dest != nullptr);
127 ASSERT(dirtyFlag != nullptr);
Jacek Caban2302e692015-12-01 12:44:43 +0100128
129 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
130 *dest = source;
131}
132
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800133template <typename T, int cols, int rows>
134bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400135{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800136 constexpr int targetWidth = 4;
137 constexpr int targetHeight = rows;
138 constexpr int srcWidth = rows;
139 constexpr int srcHeight = cols;
140
141 constexpr int copyWidth = std::min(targetHeight, srcWidth);
142 constexpr int copyHeight = std::min(targetWidth, srcHeight);
143
144 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400145
146 for (int x = 0; x < copyWidth; x++)
147 {
148 for (int y = 0; y < copyHeight; y++)
149 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800150 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400151 }
152 }
153
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800154 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
155 {
156 return false;
157 }
158
159 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
160 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400161}
162
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800163template <typename T, int cols, int rows>
164bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400165{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800166 constexpr int targetWidth = 4;
167 constexpr int targetHeight = rows;
168 constexpr int srcWidth = cols;
169 constexpr int srcHeight = rows;
170
171 constexpr int copyWidth = std::min(targetWidth, srcWidth);
172 constexpr int copyHeight = std::min(targetHeight, srcHeight);
173
174 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400175
176 for (int y = 0; y < copyHeight; y++)
177 {
178 for (int x = 0; x < copyWidth; x++)
179 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800180 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400181 }
182 }
183
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800184 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
185 {
186 return false;
187 }
188
189 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
190 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400191}
192
Jamie Madill4e31ad52015-10-29 10:32:57 -0400193gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
194{
195 switch (drawMode)
196 {
197 // Uses the point sprite geometry shader.
198 case GL_POINTS:
199 return gl::PRIMITIVE_POINTS;
200
201 // All line drawing uses the same geometry shader.
202 case GL_LINES:
203 case GL_LINE_STRIP:
204 case GL_LINE_LOOP:
205 return gl::PRIMITIVE_LINES;
206
207 // The triangle fan primitive is emulated with strips in D3D11.
208 case GL_TRIANGLES:
209 case GL_TRIANGLE_FAN:
210 return gl::PRIMITIVE_TRIANGLES;
211
212 // Special case for triangle strips.
213 case GL_TRIANGLE_STRIP:
214 return gl::PRIMITIVE_TRIANGLE_STRIP;
215
216 default:
217 UNREACHABLE();
218 return gl::PRIMITIVE_TYPE_MAX;
219 }
220}
221
Jamie Madill192745a2016-12-22 15:58:21 -0500222bool FindFlatInterpolationVarying(const std::vector<sh::Varying> &varyings)
223{
224 // Note: this assumes nested structs can only be packed with one interpolation.
225 for (const auto &varying : varyings)
226 {
227 if (varying.interpolation == sh::INTERPOLATION_FLAT)
228 {
229 return true;
230 }
231 }
232
233 return false;
234}
235
Jamie Madillada9ecc2015-08-17 12:53:37 -0400236} // anonymous namespace
237
Jamie Madill28afae52015-11-09 15:07:57 -0500238// D3DUniform Implementation
239
Jamie Madill62d31cb2015-09-11 13:25:51 -0400240D3DUniform::D3DUniform(GLenum typeIn,
241 const std::string &nameIn,
242 unsigned int arraySizeIn,
243 bool defaultBlock)
244 : type(typeIn),
245 name(nameIn),
246 arraySize(arraySizeIn),
247 data(nullptr),
248 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400249 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400250 psRegisterIndex(GL_INVALID_INDEX),
Xinghua Caob1239382016-12-13 15:07:05 +0800251 csRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400252 registerCount(0),
253 registerElement(0)
254{
255 // We use data storage for default block uniforms to cache values that are sent to D3D during
256 // rendering
257 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
258 if (defaultBlock)
259 {
260 size_t bytes = gl::VariableInternalSize(type) * elementCount();
261 data = new uint8_t[bytes];
262 memset(data, 0, bytes);
263
Jamie Madillcc2ed612017-03-14 15:59:00 -0400264 // Use the row count as register count, will work for non-square matrices.
Jamie Madill62d31cb2015-09-11 13:25:51 -0400265 registerCount = gl::VariableRowCount(type) * elementCount();
266 }
267}
268
269D3DUniform::~D3DUniform()
270{
271 SafeDeleteArray(data);
272}
273
274bool D3DUniform::isSampler() const
275{
276 return gl::IsSamplerType(type);
277}
278
279bool D3DUniform::isReferencedByVertexShader() const
280{
281 return vsRegisterIndex != GL_INVALID_INDEX;
282}
283
284bool D3DUniform::isReferencedByFragmentShader() const
285{
286 return psRegisterIndex != GL_INVALID_INDEX;
287}
288
Xinghua Caob1239382016-12-13 15:07:05 +0800289bool D3DUniform::isReferencedByComputeShader() const
290{
291 return csRegisterIndex != GL_INVALID_INDEX;
292}
293
Jamie Madill28afae52015-11-09 15:07:57 -0500294// D3DVarying Implementation
295
Jamie Madill9fc36822015-11-18 13:08:07 -0500296D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500297{
298}
299
Jamie Madill9fc36822015-11-18 13:08:07 -0500300D3DVarying::D3DVarying(const std::string &semanticNameIn,
301 unsigned int semanticIndexIn,
302 unsigned int componentCountIn,
303 unsigned int outputSlotIn)
304 : semanticName(semanticNameIn),
305 semanticIndex(semanticIndexIn),
306 componentCount(componentCountIn),
307 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500308{
309}
310
Jamie Madille39a3f02015-11-17 20:42:15 -0500311// ProgramD3DMetadata Implementation
312
Jamie Madillc9bde922016-07-24 17:58:50 -0400313ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500314 const ShaderD3D *vertexShader,
315 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400316 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
317 mShaderModelSuffix(renderer->getShaderModelSuffix()),
318 mUsesInstancedPointSpriteEmulation(
319 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
320 mUsesViewScale(renderer->presentPathFastEnabled()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500321 mVertexShader(vertexShader),
322 mFragmentShader(fragmentShader)
323{
324}
325
326int ProgramD3DMetadata::getRendererMajorShaderModel() const
327{
328 return mRendererMajorShaderModel;
329}
330
Jamie Madill9082b982016-04-27 15:21:51 -0400331bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500332{
Martin Radev1be913c2016-07-11 17:59:16 +0300333 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500334}
335
Jamie Madill48ef11b2016-04-27 15:21:52 -0400336bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500337{
Jamie Madill63286672015-11-24 13:00:08 -0500338 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500339}
340
341bool ProgramD3DMetadata::usesPointCoord() const
342{
343 return mFragmentShader->usesPointCoord();
344}
345
346bool ProgramD3DMetadata::usesFragCoord() const
347{
348 return mFragmentShader->usesFragCoord();
349}
350
351bool ProgramD3DMetadata::usesPointSize() const
352{
353 return mVertexShader->usesPointSize();
354}
355
356bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
357{
Jamie Madillc9bde922016-07-24 17:58:50 -0400358 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
359 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500360}
361
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800362bool ProgramD3DMetadata::usesViewScale() const
363{
364 return mUsesViewScale;
365}
366
Jamie Madille39a3f02015-11-17 20:42:15 -0500367bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
368{
Jamie Madillc9bde922016-07-24 17:58:50 -0400369 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500370 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400371 // Even with a geometry shader, the app can render triangles or lines and reference
372 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
373 // simplicity, we always add this to the vertex shader when the fragment shader
374 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500375 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
376 usesInsertedPointCoordValue();
377}
378
379bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
380{
381 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
382 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
383 // the vertex shader in this case. This saves us 1 output vector.
384 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
385}
386
387bool ProgramD3DMetadata::usesSystemValuePointSize() const
388{
389 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
390}
391
392bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
393{
394 return mFragmentShader->usesMultipleRenderTargets();
395}
396
397GLint ProgramD3DMetadata::getMajorShaderVersion() const
398{
399 return mVertexShader->getData().getShaderVersion();
400}
401
402const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
403{
404 return mFragmentShader;
405}
406
Jamie Madill28afae52015-11-09 15:07:57 -0500407// ProgramD3D Implementation
408
Jamie Madilld3dfda22015-07-06 08:28:49 -0400409ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
410 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500411 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400412 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700413{
Brandon Joneseb994362014-09-24 10:27:28 -0700414}
415
416ProgramD3D::VertexExecutable::~VertexExecutable()
417{
418 SafeDelete(mShaderExecutable);
419}
420
Jamie Madilld3dfda22015-07-06 08:28:49 -0400421// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500422ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
423 GLenum type)
424{
425 switch (type)
426 {
427 case GL_INT:
428 return HLSLAttribType::SIGNED_INT;
429 case GL_UNSIGNED_INT:
430 return HLSLAttribType::UNSIGNED_INT;
431 case GL_SIGNED_NORMALIZED:
432 case GL_UNSIGNED_NORMALIZED:
433 case GL_FLOAT:
434 return HLSLAttribType::FLOAT;
435 default:
436 UNREACHABLE();
437 return HLSLAttribType::FLOAT;
438 }
439}
440
441// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400442void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
443 const gl::InputLayout &inputLayout,
444 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700445{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500446 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400447
448 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700449 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400450 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500451 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
452 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400453
Jamie Madillbdec2f42016-03-02 16:35:32 -0500454 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
455 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
456 continue;
457
458 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
459 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700460 }
Brandon Joneseb994362014-09-24 10:27:28 -0700461}
462
Jamie Madilld3dfda22015-07-06 08:28:49 -0400463bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
464{
Jamie Madillbd136f92015-08-10 14:51:37 -0400465 size_t limit = std::max(mSignature.size(), signature.size());
466 for (size_t index = 0; index < limit; ++index)
467 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500468 // treat undefined indexes as FLOAT
469 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
470 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400471 if (a != b)
472 return false;
473 }
474
475 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400476}
477
478ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
479 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400480 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700481{
482}
483
484ProgramD3D::PixelExecutable::~PixelExecutable()
485{
486 SafeDelete(mShaderExecutable);
487}
488
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700489ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
490{
491}
492
Geoff Lang7dd2e102014-11-10 15:19:26 -0500493unsigned int ProgramD3D::mCurrentSerial = 1;
494
Jamie Madill48ef11b2016-04-27 15:21:52 -0400495ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
496 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700497 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800498 mDynamicHLSL(nullptr),
499 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
500 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700501 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400502 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800503 mVertexUniformStorage(nullptr),
504 mFragmentUniformStorage(nullptr),
505 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700506 mUsedVertexSamplerRange(0),
507 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800508 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700509 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500510 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700511{
Brandon Joneseb994362014-09-24 10:27:28 -0700512 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700513}
514
515ProgramD3D::~ProgramD3D()
516{
517 reset();
518 SafeDelete(mDynamicHLSL);
519}
520
Brandon Jones44151a92014-09-10 11:32:25 -0700521bool ProgramD3D::usesPointSpriteEmulation() const
522{
523 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
524}
525
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400526bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700527{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400528 if (drawMode != GL_POINTS)
529 {
530 return mUsesFlatInterpolation;
531 }
532
Cooper Partine6664f02015-01-09 16:22:24 -0800533 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
534}
535
536bool ProgramD3D::usesInstancedPointSpriteEmulation() const
537{
538 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700539}
540
Jamie Madill334d6152015-10-22 14:00:28 -0400541GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
542 unsigned int samplerIndex,
543 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700544{
545 GLint logicalTextureUnit = -1;
546
547 switch (type)
548 {
Jamie Madill334d6152015-10-22 14:00:28 -0400549 case gl::SAMPLER_PIXEL:
550 ASSERT(samplerIndex < caps.maxTextureImageUnits);
551 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
552 {
553 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
554 }
555 break;
556 case gl::SAMPLER_VERTEX:
557 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
558 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
559 {
560 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
561 }
562 break;
Xinghua Caob1239382016-12-13 15:07:05 +0800563 case gl::SAMPLER_COMPUTE:
564 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
565 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
566 {
567 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
568 }
569 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400570 default:
571 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700572 }
573
Jamie Madill334d6152015-10-22 14:00:28 -0400574 if (logicalTextureUnit >= 0 &&
575 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700576 {
577 return logicalTextureUnit;
578 }
579
580 return -1;
581}
582
583// Returns the texture type for a given Direct3D 9 sampler type and
584// index (0-15 for the pixel shader and 0-3 for the vertex shader).
585GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
586{
587 switch (type)
588 {
Jamie Madill334d6152015-10-22 14:00:28 -0400589 case gl::SAMPLER_PIXEL:
590 ASSERT(samplerIndex < mSamplersPS.size());
591 ASSERT(mSamplersPS[samplerIndex].active);
592 return mSamplersPS[samplerIndex].textureType;
593 case gl::SAMPLER_VERTEX:
594 ASSERT(samplerIndex < mSamplersVS.size());
595 ASSERT(mSamplersVS[samplerIndex].active);
596 return mSamplersVS[samplerIndex].textureType;
Xinghua Caob1239382016-12-13 15:07:05 +0800597 case gl::SAMPLER_COMPUTE:
598 ASSERT(samplerIndex < mSamplersCS.size());
599 ASSERT(mSamplersCS[samplerIndex].active);
600 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400601 default:
602 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700603 }
604
605 return GL_TEXTURE_2D;
606}
607
Olli Etuaho618bebc2016-01-15 16:40:00 +0200608GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700609{
610 switch (type)
611 {
Jamie Madill334d6152015-10-22 14:00:28 -0400612 case gl::SAMPLER_PIXEL:
613 return mUsedPixelSamplerRange;
614 case gl::SAMPLER_VERTEX:
615 return mUsedVertexSamplerRange;
Xinghua Caob1239382016-12-13 15:07:05 +0800616 case gl::SAMPLER_COMPUTE:
617 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400618 default:
619 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200620 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700621 }
622}
623
624void ProgramD3D::updateSamplerMapping()
625{
626 if (!mDirtySamplerMapping)
627 {
628 return;
629 }
630
631 mDirtySamplerMapping = false;
632
633 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400634 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700635 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400636 if (!d3dUniform->dirty)
637 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700638
Jamie Madill62d31cb2015-09-11 13:25:51 -0400639 if (!d3dUniform->isSampler())
640 continue;
641
642 int count = d3dUniform->elementCount();
643 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
644
645 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700646 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400647 unsigned int firstIndex = d3dUniform->psRegisterIndex;
648
649 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700650 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400651 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700652
Jamie Madill62d31cb2015-09-11 13:25:51 -0400653 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700654 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400655 ASSERT(mSamplersPS[samplerIndex].active);
656 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700657 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400658 }
659 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700660
Jamie Madill62d31cb2015-09-11 13:25:51 -0400661 if (d3dUniform->isReferencedByVertexShader())
662 {
663 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
664
665 for (int i = 0; i < count; i++)
666 {
667 unsigned int samplerIndex = firstIndex + i;
668
669 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700670 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400671 ASSERT(mSamplersVS[samplerIndex].active);
672 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700673 }
674 }
675 }
Xinghua Caob1239382016-12-13 15:07:05 +0800676
677 if (d3dUniform->isReferencedByComputeShader())
678 {
679 unsigned int firstIndex = d3dUniform->csRegisterIndex;
680
681 for (int i = 0; i < count; i++)
682 {
683 unsigned int samplerIndex = firstIndex + i;
684
685 if (samplerIndex < mSamplersCS.size())
686 {
687 ASSERT(mSamplersCS[samplerIndex].active);
688 mSamplersCS[samplerIndex].logicalTextureUnit = v[i][0];
689 }
690 }
691 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700692 }
693}
694
Jamie Madillc564c072017-06-01 12:45:42 -0400695LinkResult ProgramD3D::load(const gl::Context *context,
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500696 gl::InfoLog &infoLog,
697 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700698{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500699 // TODO(jmadill): Use Renderer from contextImpl.
700
Jamie Madill62d31cb2015-09-11 13:25:51 -0400701 reset();
702
Jamie Madill334d6152015-10-22 14:00:28 -0400703 DeviceIdentifier binaryDeviceIdentifier = {0};
704 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
705 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700706
707 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
708 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
709 {
710 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500711 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700712 }
713
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500714 int compileFlags = stream->readInt<int>();
715 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
716 {
Jamie Madillf6113162015-05-07 11:49:21 -0400717 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500718 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500719 }
720
Jamie Madill8047c0d2016-03-07 13:02:12 -0500721 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400722 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500723 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400724 }
725
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700726 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
727 for (unsigned int i = 0; i < psSamplerCount; ++i)
728 {
729 Sampler sampler;
730 stream->readBool(&sampler.active);
731 stream->readInt(&sampler.logicalTextureUnit);
732 stream->readInt(&sampler.textureType);
733 mSamplersPS.push_back(sampler);
734 }
735 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
736 for (unsigned int i = 0; i < vsSamplerCount; ++i)
737 {
738 Sampler sampler;
739 stream->readBool(&sampler.active);
740 stream->readInt(&sampler.logicalTextureUnit);
741 stream->readInt(&sampler.textureType);
742 mSamplersVS.push_back(sampler);
743 }
744
Xinghua Caob1239382016-12-13 15:07:05 +0800745 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
746 for (unsigned int i = 0; i < csSamplerCount; ++i)
747 {
748 Sampler sampler;
749 stream->readBool(&sampler.active);
750 stream->readInt(&sampler.logicalTextureUnit);
751 stream->readInt(&sampler.textureType);
752 mSamplersCS.push_back(sampler);
753 }
754
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700755 stream->readInt(&mUsedVertexSamplerRange);
756 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800757 stream->readInt(&mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700758
759 const unsigned int uniformCount = stream->readInt<unsigned int>();
760 if (stream->error())
761 {
Jamie Madillf6113162015-05-07 11:49:21 -0400762 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500763 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700764 }
765
Jamie Madill48ef11b2016-04-27 15:21:52 -0400766 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400767 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700768 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
769 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400770 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700771
Jamie Madill62d31cb2015-09-11 13:25:51 -0400772 D3DUniform *d3dUniform =
773 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
774 linkedUniform.isInDefaultBlock());
775 stream->readInt(&d3dUniform->psRegisterIndex);
776 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800777 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400778 stream->readInt(&d3dUniform->registerCount);
779 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700780
Jamie Madill62d31cb2015-09-11 13:25:51 -0400781 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700782 }
783
Jamie Madill4a3c2342015-10-08 12:58:45 -0400784 const unsigned int blockCount = stream->readInt<unsigned int>();
785 if (stream->error())
786 {
787 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500788 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400789 }
790
791 ASSERT(mD3DUniformBlocks.empty());
792 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
793 {
794 D3DUniformBlock uniformBlock;
795 stream->readInt(&uniformBlock.psRegisterIndex);
796 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800797 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400798 mD3DUniformBlocks.push_back(uniformBlock);
799 }
800
Jamie Madill9fc36822015-11-18 13:08:07 -0500801 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
802 mStreamOutVaryings.resize(streamOutVaryingCount);
803 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700804 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500805 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700806
Jamie Madill28afae52015-11-09 15:07:57 -0500807 stream->readString(&varying->semanticName);
808 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500809 stream->readInt(&varying->componentCount);
810 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700811 }
812
Brandon Jones22502d52014-08-29 16:58:36 -0700813 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400814 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500815 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700816 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400817 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500818 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700819 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700820 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400821 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700822
823 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
824 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400825 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
826 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700827 {
828 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
829 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
830 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
831 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
832 }
833
Jamie Madill4e31ad52015-10-29 10:32:57 -0400834 stream->readString(&mGeometryShaderPreamble);
835
Jamie Madill334d6152015-10-22 14:00:28 -0400836 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700837
Jamie Madillb0a838b2016-11-13 20:02:12 -0500838 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
839
Brandon Joneseb994362014-09-24 10:27:28 -0700840 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400841 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
842 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700843 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400844 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400845 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700846
Jamie Madilld3dfda22015-07-06 08:28:49 -0400847 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700848 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400849 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700850 }
851
Jamie Madill334d6152015-10-22 14:00:28 -0400852 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700853 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400854
Jamie Madillada9ecc2015-08-17 12:53:37 -0400855 ShaderExecutableD3D *shaderExecutable = nullptr;
856
Jamie Madillb0a838b2016-11-13 20:02:12 -0500857 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
858 mStreamOutVaryings, separateAttribs,
859 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400860
Brandon Joneseb994362014-09-24 10:27:28 -0700861 if (!shaderExecutable)
862 {
Jamie Madillf6113162015-05-07 11:49:21 -0400863 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500864 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700865 }
866
867 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400868 VertexExecutable::Signature signature;
869 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700870
871 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800872 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
873 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700874
875 stream->skip(vertexShaderSize);
876 }
877
878 const size_t pixelShaderCount = stream->readInt<unsigned int>();
879 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
880 {
881 const size_t outputCount = stream->readInt<unsigned int>();
882 std::vector<GLenum> outputs(outputCount);
883 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
884 {
885 stream->readInt(&outputs[outputIndex]);
886 }
887
Jamie Madill334d6152015-10-22 14:00:28 -0400888 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700889 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400890 ShaderExecutableD3D *shaderExecutable = nullptr;
891
Jamie Madillb0a838b2016-11-13 20:02:12 -0500892 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
893 mStreamOutVaryings, separateAttribs,
894 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700895
896 if (!shaderExecutable)
897 {
Jamie Madillf6113162015-05-07 11:49:21 -0400898 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500899 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700900 }
901
902 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800903 mPixelExecutables.push_back(
904 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700905
906 stream->skip(pixelShaderSize);
907 }
908
Jamie Madill4e31ad52015-10-29 10:32:57 -0400909 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
910 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700911 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400912 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
913 if (geometryShaderSize == 0)
914 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400915 continue;
916 }
917
Brandon Joneseb994362014-09-24 10:27:28 -0700918 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400919
Xinghua Caob1239382016-12-13 15:07:05 +0800920 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -0500921 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
922 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
Xinghua Caob1239382016-12-13 15:07:05 +0800923 &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700924
Xinghua Caob1239382016-12-13 15:07:05 +0800925 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700926 {
Jamie Madillf6113162015-05-07 11:49:21 -0400927 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500928 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700929 }
Xinghua Caob1239382016-12-13 15:07:05 +0800930
931 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
932
Brandon Joneseb994362014-09-24 10:27:28 -0700933 stream->skip(geometryShaderSize);
934 }
935
Xinghua Caob1239382016-12-13 15:07:05 +0800936 unsigned int computeShaderSize = stream->readInt<unsigned int>();
937 if (computeShaderSize > 0)
938 {
939 const unsigned char *computeShaderFunction = binary + stream->offset();
940
941 ShaderExecutableD3D *computeExecutable = nullptr;
942 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
943 SHADER_COMPUTE, std::vector<D3DVarying>(), false,
944 &computeExecutable));
945
946 if (!computeExecutable)
947 {
948 infoLog << "Could not create compute shader.";
949 return false;
950 }
951
952 mComputeExecutable.reset(computeExecutable);
953 }
954
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700955 initializeUniformStorage();
956
Jamie Madillb0a838b2016-11-13 20:02:12 -0500957 return true;
Brandon Jones22502d52014-08-29 16:58:36 -0700958}
959
Geoff Langb543aff2014-09-30 14:52:54 -0400960gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700961{
Austin Kinross137b1512015-06-17 16:14:53 -0700962 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400963 // When we load the binary again later, we can validate the device identifier before trying to
964 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700965 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400966 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
967 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700968
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500969 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
970
Jamie Madill8047c0d2016-03-07 13:02:12 -0500971 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400972 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500973 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400974 }
975
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700976 stream->writeInt(mSamplersPS.size());
977 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
978 {
979 stream->writeInt(mSamplersPS[i].active);
980 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
981 stream->writeInt(mSamplersPS[i].textureType);
982 }
983
984 stream->writeInt(mSamplersVS.size());
985 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
986 {
987 stream->writeInt(mSamplersVS[i].active);
988 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
989 stream->writeInt(mSamplersVS[i].textureType);
990 }
991
Xinghua Caob1239382016-12-13 15:07:05 +0800992 stream->writeInt(mSamplersCS.size());
993 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
994 {
995 stream->writeInt(mSamplersCS[i].active);
996 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
997 stream->writeInt(mSamplersCS[i].textureType);
998 }
999
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001000 stream->writeInt(mUsedVertexSamplerRange);
1001 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001002 stream->writeInt(mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001003
Jamie Madill62d31cb2015-09-11 13:25:51 -04001004 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001005 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001006 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001007 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001008 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1009 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001010 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001011 stream->writeInt(uniform->registerCount);
1012 stream->writeInt(uniform->registerElement);
1013 }
1014
Jamie Madill51f522f2016-12-21 15:10:55 -05001015 // Ensure we init the uniform block structure data if we should.
1016 // http://anglebug.com/1637
1017 ensureUniformBlocksInitialized();
1018
Jamie Madill4a3c2342015-10-08 12:58:45 -04001019 stream->writeInt(mD3DUniformBlocks.size());
1020 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1021 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001022 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1023 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001024 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001025 }
1026
Jamie Madill9fc36822015-11-18 13:08:07 -05001027 stream->writeInt(mStreamOutVaryings.size());
1028 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001029 {
Brandon Joneseb994362014-09-24 10:27:28 -07001030 stream->writeString(varying.semanticName);
1031 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001032 stream->writeInt(varying.componentCount);
1033 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001034 }
1035
Brandon Jones22502d52014-08-29 16:58:36 -07001036 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001037 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001038 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001039 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001040 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001041 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001042 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001043 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001044 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001045
Brandon Joneseb994362014-09-24 10:27:28 -07001046 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001047 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001048 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1049 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001050 {
Brandon Joneseb994362014-09-24 10:27:28 -07001051 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001052 stream->writeInt(variable.type);
1053 stream->writeString(variable.name);
1054 stream->writeString(variable.source);
1055 stream->writeInt(variable.outputIndex);
1056 }
1057
Jamie Madill4e31ad52015-10-29 10:32:57 -04001058 stream->writeString(mGeometryShaderPreamble);
1059
Brandon Joneseb994362014-09-24 10:27:28 -07001060 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001061 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1062 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001063 {
Xinghua Caob1239382016-12-13 15:07:05 +08001064 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001065
Jamie Madilld3dfda22015-07-06 08:28:49 -04001066 const auto &inputLayout = vertexExecutable->inputs();
1067 stream->writeInt(inputLayout.size());
1068
1069 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001070 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001071 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001072 }
1073
1074 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1075 stream->writeInt(vertexShaderSize);
1076
1077 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1078 stream->writeBytes(vertexBlob, vertexShaderSize);
1079 }
1080
1081 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001082 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1083 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001084 {
Xinghua Caob1239382016-12-13 15:07:05 +08001085 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001086
1087 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1088 stream->writeInt(outputs.size());
1089 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1090 {
1091 stream->writeInt(outputs[outputIndex]);
1092 }
1093
1094 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1095 stream->writeInt(pixelShaderSize);
1096
1097 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1098 stream->writeBytes(pixelBlob, pixelShaderSize);
1099 }
1100
Xinghua Caob1239382016-12-13 15:07:05 +08001101 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001102 {
Xinghua Caob1239382016-12-13 15:07:05 +08001103 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001104 {
1105 stream->writeInt(0);
1106 continue;
1107 }
1108
Xinghua Caob1239382016-12-13 15:07:05 +08001109 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001110 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001111 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1112 }
1113
1114 if (mComputeExecutable)
1115 {
1116 size_t computeShaderSize = mComputeExecutable->getLength();
1117 stream->writeInt(computeShaderSize);
1118 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1119 }
1120 else
1121 {
1122 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001123 }
1124
Jamie Madill51f522f2016-12-21 15:10:55 -05001125 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001126}
1127
Geoff Langc5629752015-12-07 16:29:04 -05001128void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1129{
1130}
1131
Yunchao He61afff12017-03-14 15:34:03 +08001132void ProgramD3D::setSeparable(bool /* separable */)
1133{
1134}
1135
Jamie Madill334d6152015-10-22 14:00:28 -04001136gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1137 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001138{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001139 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001140
Jamie Madill85a18042015-03-05 15:41:41 -05001141 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001142 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001143
1144 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1145 {
1146 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1147
1148 if (colorbuffer)
1149 {
Jamie Madill334d6152015-10-22 14:00:28 -04001150 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1151 ? GL_COLOR_ATTACHMENT0
1152 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001153 }
1154 else
1155 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001156 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001157 }
1158 }
1159
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001160 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001161}
1162
Jamie Madill97399232014-12-23 12:31:15 -05001163gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001164 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001165 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001166{
1167 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1168 {
1169 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1170 {
Geoff Langb543aff2014-09-30 14:52:54 -04001171 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001172 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001173 }
1174 }
1175
Jamie Madill334d6152015-10-22 14:00:28 -04001176 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1177 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001178
1179 // Generate new pixel executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001180 ShaderExecutableD3D *pixelExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001181
1182 gl::InfoLog tempInfoLog;
1183 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1184
Jamie Madill01074252016-11-28 15:55:51 -05001185 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001186 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001187 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001188 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001189
Jamie Madill97399232014-12-23 12:31:15 -05001190 if (pixelExecutable)
1191 {
Xinghua Caob1239382016-12-13 15:07:05 +08001192 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
1193 new PixelExecutable(outputSignature, pixelExecutable)));
Jamie Madill97399232014-12-23 12:31:15 -05001194 }
1195 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001196 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001197 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1198 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001199 }
Brandon Jones22502d52014-08-29 16:58:36 -07001200
Geoff Langb543aff2014-09-30 14:52:54 -04001201 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001202 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001203}
1204
Jamie Madilld3dfda22015-07-06 08:28:49 -04001205gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001206 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001207 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001208{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001209 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001210
1211 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1212 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001213 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001214 {
Geoff Langb543aff2014-09-30 14:52:54 -04001215 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001216 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001217 }
1218 }
1219
Brandon Jones22502d52014-08-29 16:58:36 -07001220 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001221 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001222 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001223
1224 // Generate new vertex executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001225 ShaderExecutableD3D *vertexExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001226
1227 gl::InfoLog tempInfoLog;
1228 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1229
Jamie Madill01074252016-11-28 15:55:51 -05001230 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001231 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001232 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001233 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001234
Jamie Madill97399232014-12-23 12:31:15 -05001235 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001236 {
Xinghua Caob1239382016-12-13 15:07:05 +08001237 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
1238 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001239 }
Jamie Madill97399232014-12-23 12:31:15 -05001240 else if (!infoLog)
1241 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001242 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1243 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001244 }
Brandon Jones22502d52014-08-29 16:58:36 -07001245
Geoff Langb543aff2014-09-30 14:52:54 -04001246 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001247 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001248}
1249
Jamie Madill9082b982016-04-27 15:21:51 -04001250gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001251 GLenum drawMode,
1252 ShaderExecutableD3D **outExecutable,
1253 gl::InfoLog *infoLog)
1254{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001255 if (outExecutable)
1256 {
1257 *outExecutable = nullptr;
1258 }
1259
Austin Kinross88829e82016-01-12 13:04:41 -08001260 // Return a null shader if the current rendering doesn't use a geometry shader
1261 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001262 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001263 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001264 }
1265
Jamie Madill4e31ad52015-10-29 10:32:57 -04001266 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1267
Xinghua Caob1239382016-12-13 15:07:05 +08001268 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001269 {
1270 if (outExecutable)
1271 {
Xinghua Caob1239382016-12-13 15:07:05 +08001272 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001273 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001274 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001275 }
1276
1277 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001278 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001279 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001280
1281 gl::InfoLog tempInfoLog;
1282 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1283
Xinghua Caob1239382016-12-13 15:07:05 +08001284 ShaderExecutableD3D *geometryExecutable = nullptr;
1285 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001286 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001287 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001288 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001289
1290 if (!infoLog && error.isError())
1291 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001292 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1293 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001294 }
1295
Xinghua Caob1239382016-12-13 15:07:05 +08001296 if (geometryExecutable != nullptr)
1297 {
1298 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1299 }
1300
Jamie Madill4e31ad52015-10-29 10:32:57 -04001301 if (outExecutable)
1302 {
Xinghua Caob1239382016-12-13 15:07:05 +08001303 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001304 }
1305 return error;
1306}
1307
Jamie Madill01074252016-11-28 15:55:51 -05001308class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001309{
Jamie Madill01074252016-11-28 15:55:51 -05001310 public:
1311 GetExecutableTask(ProgramD3D *program)
1312 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001313 {
Brandon Joneseb994362014-09-24 10:27:28 -07001314 }
1315
Jamie Madill01074252016-11-28 15:55:51 -05001316 virtual gl::Error run() = 0;
1317
1318 void operator()() override { mError = run(); }
1319
1320 const gl::Error &getError() const { return mError; }
1321 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1322 ShaderExecutableD3D *getResult() { return mResult; }
1323
1324 protected:
1325 ProgramD3D *mProgram;
1326 gl::Error mError;
1327 gl::InfoLog mInfoLog;
1328 ShaderExecutableD3D *mResult;
1329};
1330
1331class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1332{
1333 public:
1334 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1335 gl::Error run() override
1336 {
1337 const auto &defaultInputLayout =
1338 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1339
1340 ANGLE_TRY(
1341 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1342
1343 return gl::NoError();
1344 }
1345};
1346
1347class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1348{
1349 public:
1350 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1351 gl::Error run() override
1352 {
1353 const auto &defaultPixelOutput =
1354 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1355
1356 ANGLE_TRY(
1357 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1358
1359 return gl::NoError();
1360 }
1361};
1362
1363class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1364{
1365 public:
1366 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1367 : GetExecutableTask(program), mContextState(contextState)
1368 {
1369 }
1370
1371 gl::Error run() override
1372 {
1373 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1374 // D3D11.
1375 if (mProgram->usesGeometryShader(GL_POINTS))
1376 {
1377 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1378 &mResult, &mInfoLog));
1379 }
1380
1381 return gl::NoError();
1382 }
1383
1384 private:
1385 const gl::ContextState &mContextState;
1386};
1387
Xinghua Cao73badc02017-03-29 19:14:53 +08001388gl::Error ProgramD3D::getComputeExecutable(ShaderExecutableD3D **outExecutable)
1389{
1390 if (outExecutable)
1391 {
1392 *outExecutable = mComputeExecutable.get();
1393 }
1394
1395 return gl::NoError();
1396}
1397
Jamie Madill01074252016-11-28 15:55:51 -05001398LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1399 gl::InfoLog &infoLog)
1400{
1401 // Ensure the compiler is initialized to avoid race conditions.
1402 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1403
1404 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1405
1406 GetVertexExecutableTask vertexTask(this);
1407 GetPixelExecutableTask pixelTask(this);
1408 GetGeometryExecutableTask geometryTask(this, contextState);
1409
1410 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1411 workerPool->postWorkerTask(&pixelTask),
1412 workerPool->postWorkerTask(&geometryTask)}};
1413
1414 WaitableEvent::WaitMany(&waitEvents);
1415
1416 infoLog << vertexTask.getInfoLog().str();
1417 infoLog << pixelTask.getInfoLog().str();
1418 infoLog << geometryTask.getInfoLog().str();
1419
1420 ANGLE_TRY(vertexTask.getError());
1421 ANGLE_TRY(pixelTask.getError());
1422 ANGLE_TRY(geometryTask.getError());
1423
1424 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1425 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1426 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1427
Jamie Madill48ef11b2016-04-27 15:21:52 -04001428 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001429
1430 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001431 {
Jamie Madill334d6152015-10-22 14:00:28 -04001432 // Geometry shaders are currently only used internally, so there is no corresponding shader
1433 // object at the interface level. For now the geometry shader debug info is prepended to
1434 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001435 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001436 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001437 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1438 }
1439
1440 if (defaultVertexExecutable)
1441 {
1442 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1443 }
1444
1445 if (defaultPixelExecutable)
1446 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001447 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001448 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001449 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1450 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001451
Jamie Madillb0a838b2016-11-13 20:02:12 -05001452 return (defaultVertexExecutable && defaultPixelExecutable &&
1453 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001454}
1455
Xinghua Caob1239382016-12-13 15:07:05 +08001456LinkResult ProgramD3D::compileComputeExecutable(gl::InfoLog &infoLog)
1457{
1458 // Ensure the compiler is initialized to avoid race conditions.
1459 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1460
1461 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(mState);
1462
1463 ShaderExecutableD3D *computeExecutable = nullptr;
1464 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, SHADER_COMPUTE,
1465 std::vector<D3DVarying>(), false,
1466 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1467
1468 if (computeExecutable == nullptr)
1469 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001470 ERR() << "Error compiling dynamic compute executable:" << std::endl
1471 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001472 }
1473 else
1474 {
1475 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1476 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1477 mComputeExecutable.reset(computeExecutable);
1478 }
1479
1480 return mComputeExecutable.get() != nullptr;
1481}
1482
Jamie Madillc564c072017-06-01 12:45:42 -04001483LinkResult ProgramD3D::link(const gl::Context *context,
Jamie Madill192745a2016-12-22 15:58:21 -05001484 const gl::VaryingPacking &packing,
1485 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001486{
Jamie Madillc564c072017-06-01 12:45:42 -04001487 const auto &data = context->getContextState();
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001488
Jamie Madill62d31cb2015-09-11 13:25:51 -04001489 reset();
1490
Xinghua Caob1239382016-12-13 15:07:05 +08001491 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1492 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001493 {
Xinghua Caob1239382016-12-13 15:07:05 +08001494 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1495
1496 defineUniformsAndAssignRegisters();
1497
1498 LinkResult result = compileComputeExecutable(infoLog);
1499 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001500 {
Xinghua Caob1239382016-12-13 15:07:05 +08001501 infoLog << result.getError().getMessage();
1502 return result;
1503 }
1504 else if (!result.getResult())
1505 {
1506 infoLog << "Failed to create D3D compute shader.";
1507 return result;
1508 }
1509
1510 initUniformBlockInfo(computeShader);
1511 }
1512 else
1513 {
1514 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1515 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1516
1517 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1518 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1519
1520 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1521 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1522
1523 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1524 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1525
1526 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1527 {
1528 if (fragmentShaderD3D->usesFrontFacing())
1529 {
1530 infoLog << "The current renderer doesn't support gl_FrontFacing";
1531 return false;
1532 }
1533 }
1534
1535 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1536 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1537 // intelligently, but D3D9 assumes one semantic per register.
1538 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1539 packing.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
1540 {
1541 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001542 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001543 }
Xinghua Caob1239382016-12-13 15:07:05 +08001544
1545 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
1546 BuiltinVaryingsD3D builtins(metadata, packing);
1547
1548 mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, packing, builtins, &mPixelHLSL,
1549 &mVertexHLSL);
1550
1551 mUsesPointSize = vertexShaderD3D->usesPointSize();
1552 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1553 mUsesFragDepth = metadata.usesFragDepth();
1554
1555 // Cache if we use flat shading
1556 mUsesFlatInterpolation = (FindFlatInterpolationVarying(fragmentShader->getVaryings()) ||
1557 FindFlatInterpolationVarying(vertexShader->getVaryings()));
1558
1559 if (mRenderer->getMajorShaderModel() >= 4)
1560 {
1561 mGeometryShaderPreamble =
1562 mDynamicHLSL->generateGeometryShaderPreamble(packing, builtins);
1563 }
1564
1565 initAttribLocationsToD3DSemantic();
1566
1567 defineUniformsAndAssignRegisters();
1568
1569 gatherTransformFeedbackVaryings(packing, builtins[SHADER_VERTEX]);
1570
1571 LinkResult result = compileProgramExecutables(data, infoLog);
1572 if (result.isError())
1573 {
1574 infoLog << result.getError().getMessage();
1575 return result;
1576 }
1577 else if (!result.getResult())
1578 {
1579 infoLog << "Failed to create D3D shaders.";
1580 return result;
1581 }
1582
1583 initUniformBlockInfo(vertexShader);
1584 initUniformBlockInfo(fragmentShader);
Austin Kinross02df7962015-07-01 10:03:42 -07001585 }
1586
Jamie Madillb0a838b2016-11-13 20:02:12 -05001587 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001588}
1589
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001590GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001591{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001592 // TODO(jmadill): Do something useful here?
1593 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001594}
1595
Xinghua Caob1239382016-12-13 15:07:05 +08001596void ProgramD3D::initUniformBlockInfo(const gl::Shader *shader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001597{
Xinghua Caob1239382016-12-13 15:07:05 +08001598 for (const sh::InterfaceBlock &interfaceBlock : shader->getInterfaceBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001599 {
Xinghua Caob1239382016-12-13 15:07:05 +08001600 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001601 continue;
1602
Xinghua Caob1239382016-12-13 15:07:05 +08001603 if (mBlockDataSizes.count(interfaceBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001604 continue;
1605
Xinghua Caob1239382016-12-13 15:07:05 +08001606 size_t dataSize = getUniformBlockInfo(interfaceBlock);
1607 mBlockDataSizes[interfaceBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001608 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001609}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001610
Jamie Madill51f522f2016-12-21 15:10:55 -05001611void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001612{
Jamie Madill51f522f2016-12-21 15:10:55 -05001613 // Lazy init.
1614 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1615 {
1616 return;
1617 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001618
1619 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001620 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1621 const ShaderD3D *fragmentShaderD3D =
1622 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1623 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001624
Jamie Madill48ef11b2016-04-27 15:21:52 -04001625 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001626 {
1627 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1628
Jamie Madill4a3c2342015-10-08 12:58:45 -04001629 D3DUniformBlock d3dUniformBlock;
1630
Jamie Madill62d31cb2015-09-11 13:25:51 -04001631 if (uniformBlock.vertexStaticUse)
1632 {
Xinghua Caob1239382016-12-13 15:07:05 +08001633 ASSERT(vertexShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001634 unsigned int baseRegister =
1635 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001636 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001637 }
1638
1639 if (uniformBlock.fragmentStaticUse)
1640 {
Xinghua Caob1239382016-12-13 15:07:05 +08001641 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001642 unsigned int baseRegister =
1643 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001644 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001645 }
1646
Xinghua Caob1239382016-12-13 15:07:05 +08001647 if (uniformBlock.computeStaticUse)
1648 {
1649 ASSERT(computeShaderD3D != nullptr);
1650 unsigned int baseRegister =
1651 computeShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
1652 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1653 }
1654
Jamie Madill4a3c2342015-10-08 12:58:45 -04001655 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001656 }
1657}
1658
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001659void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001660{
1661 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001662 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001663 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001664 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001665 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001666 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001667 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001668 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001669 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001670 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001671 vertexRegisters = std::max(vertexRegisters,
1672 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001673 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001674 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001675 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001676 fragmentRegisters = std::max(
1677 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001678 }
Xinghua Caob1239382016-12-13 15:07:05 +08001679 if (d3dUniform->isReferencedByComputeShader())
1680 {
1681 computeRegisters = std::max(
1682 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1683 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001684 }
1685 }
1686
Xinghua Caob1239382016-12-13 15:07:05 +08001687 mVertexUniformStorage =
1688 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1689 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1690 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1691 mComputeUniformStorage =
1692 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Brandon Jonesc9610c52014-08-25 17:02:59 -07001693}
1694
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001695gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001696{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001697 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001698
Jamie Madill01074252016-11-28 15:55:51 -05001699 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001700
Jamie Madill62d31cb2015-09-11 13:25:51 -04001701 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001702 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001703 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001704 }
1705
Jamie Madill51f522f2016-12-21 15:10:55 -05001706 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001707}
1708
Xinghua Cao73badc02017-03-29 19:14:53 +08001709gl::Error ProgramD3D::applyComputeUniforms()
1710{
1711 ASSERT(!mDirtySamplerMapping);
1712 ANGLE_TRY(mRenderer->applyComputeUniforms(*this, mD3DUniforms));
1713
1714 for (D3DUniform *d3dUniform : mD3DUniforms)
1715 {
1716 d3dUniform->dirty = false;
1717 }
1718
1719 return gl::NoError();
1720}
1721
Jamie Madill9082b982016-04-27 15:21:51 -04001722gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001723{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001724 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001725 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001726 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001727 }
1728
Jamie Madill51f522f2016-12-21 15:10:55 -05001729 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001730
Jamie Madill03260fa2015-06-22 13:57:22 -04001731 mVertexUBOCache.clear();
1732 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001733
1734 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1735 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1736
Jamie Madill4a3c2342015-10-08 12:58:45 -04001737 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001738 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001739 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001740 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001741 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001742
Brandon Jones18bd4102014-09-22 14:21:44 -07001743 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001744 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001745 {
1746 continue;
1747 }
1748
Jamie Madill4a3c2342015-10-08 12:58:45 -04001749 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001750 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001751 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001752 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001753
Jamie Madill969194d2015-07-20 14:36:56 -04001754 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001755 {
1756 mVertexUBOCache.resize(registerIndex + 1, -1);
1757 }
1758
1759 ASSERT(mVertexUBOCache[registerIndex] == -1);
1760 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001761 }
1762
Jamie Madill4a3c2342015-10-08 12:58:45 -04001763 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001764 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001765 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001766 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001767
1768 if (mFragmentUBOCache.size() <= registerIndex)
1769 {
1770 mFragmentUBOCache.resize(registerIndex + 1, -1);
1771 }
1772
1773 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1774 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001775 }
1776 }
1777
Jamie Madill03260fa2015-06-22 13:57:22 -04001778 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001779}
1780
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001781void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001782{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001783 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001784 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001785 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001786 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001787}
1788
Jamie Madill334d6152015-10-22 14:00:28 -04001789void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001790{
1791 setUniform(location, count, v, GL_FLOAT);
1792}
1793
1794void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1795{
1796 setUniform(location, count, v, GL_FLOAT_VEC2);
1797}
1798
1799void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1800{
1801 setUniform(location, count, v, GL_FLOAT_VEC3);
1802}
1803
1804void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1805{
1806 setUniform(location, count, v, GL_FLOAT_VEC4);
1807}
1808
Jamie Madill334d6152015-10-22 14:00:28 -04001809void ProgramD3D::setUniformMatrix2fv(GLint location,
1810 GLsizei count,
1811 GLboolean transpose,
1812 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001813{
1814 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1815}
1816
Jamie Madill334d6152015-10-22 14:00:28 -04001817void ProgramD3D::setUniformMatrix3fv(GLint location,
1818 GLsizei count,
1819 GLboolean transpose,
1820 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001821{
1822 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1823}
1824
Jamie Madill334d6152015-10-22 14:00:28 -04001825void ProgramD3D::setUniformMatrix4fv(GLint location,
1826 GLsizei count,
1827 GLboolean transpose,
1828 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001829{
1830 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1831}
1832
Jamie Madill334d6152015-10-22 14:00:28 -04001833void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1834 GLsizei count,
1835 GLboolean transpose,
1836 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001837{
1838 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1839}
1840
Jamie Madill334d6152015-10-22 14:00:28 -04001841void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1842 GLsizei count,
1843 GLboolean transpose,
1844 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001845{
1846 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1847}
1848
Jamie Madill334d6152015-10-22 14:00:28 -04001849void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1850 GLsizei count,
1851 GLboolean transpose,
1852 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001853{
1854 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1855}
1856
Jamie Madill334d6152015-10-22 14:00:28 -04001857void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1858 GLsizei count,
1859 GLboolean transpose,
1860 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001861{
1862 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1863}
1864
Jamie Madill334d6152015-10-22 14:00:28 -04001865void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1866 GLsizei count,
1867 GLboolean transpose,
1868 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001869{
1870 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1871}
1872
Jamie Madill334d6152015-10-22 14:00:28 -04001873void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1874 GLsizei count,
1875 GLboolean transpose,
1876 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001877{
1878 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1879}
1880
1881void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1882{
1883 setUniform(location, count, v, GL_INT);
1884}
1885
1886void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1887{
1888 setUniform(location, count, v, GL_INT_VEC2);
1889}
1890
1891void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1892{
1893 setUniform(location, count, v, GL_INT_VEC3);
1894}
1895
1896void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1897{
1898 setUniform(location, count, v, GL_INT_VEC4);
1899}
1900
1901void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1902{
1903 setUniform(location, count, v, GL_UNSIGNED_INT);
1904}
1905
1906void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1907{
1908 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1909}
1910
1911void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1912{
1913 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1914}
1915
1916void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1917{
1918 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1919}
1920
Jamie Madill4a3c2342015-10-08 12:58:45 -04001921void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1922 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001923{
1924}
1925
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001926void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001928 D3DUniformMap uniformMap;
Xinghua Caob1239382016-12-13 15:07:05 +08001929 const gl::Shader *computeShader = mState.getAttachedComputeShader();
1930 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05001931 {
Xinghua Caob1239382016-12-13 15:07:05 +08001932 for (const sh::Uniform &computeUniform : computeShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001933 {
Xinghua Caob1239382016-12-13 15:07:05 +08001934 if (computeUniform.staticUse)
1935 {
1936 defineUniformBase(computeShader, computeUniform, &uniformMap);
1937 }
Jamie Madillfb536032015-09-11 13:19:49 -04001938 }
1939 }
Xinghua Caob1239382016-12-13 15:07:05 +08001940 else
Jamie Madillfb536032015-09-11 13:19:49 -04001941 {
Xinghua Caob1239382016-12-13 15:07:05 +08001942 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1943 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001944 {
Xinghua Caob1239382016-12-13 15:07:05 +08001945 if (vertexUniform.staticUse)
1946 {
1947 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
1948 }
1949 }
1950
1951 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1952 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
1953 {
1954 if (fragmentUniform.staticUse)
1955 {
1956 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
1957 }
Jamie Madillfb536032015-09-11 13:19:49 -04001958 }
1959 }
1960
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001961 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001962 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001963 {
1964 if (!glUniform.isInDefaultBlock())
1965 continue;
1966
1967 auto mapEntry = uniformMap.find(glUniform.name);
1968 ASSERT(mapEntry != uniformMap.end());
1969 mD3DUniforms.push_back(mapEntry->second);
1970 }
1971
Jamie Madill62d31cb2015-09-11 13:25:51 -04001972 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001973 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001974}
1975
Jamie Madill91445bc2015-09-23 16:47:53 -04001976void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001977 const sh::Uniform &uniform,
1978 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001979{
Olli Etuaho96963162016-03-21 11:54:33 +02001980 // Samplers get their registers assigned in assignAllSamplerRegisters.
1981 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001982 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001983 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001984 return;
1985 }
1986
Jamie Madill91445bc2015-09-23 16:47:53 -04001987 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1988
1989 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1990 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04001991 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001992 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001993
Jamie Madill91445bc2015-09-23 16:47:53 -04001994 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001995}
1996
Jamie Madill62d31cb2015-09-11 13:25:51 -04001997D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1998{
1999 for (D3DUniform *d3dUniform : mD3DUniforms)
2000 {
2001 if (d3dUniform->name == name)
2002 {
2003 return d3dUniform;
2004 }
2005 }
2006
2007 return nullptr;
2008}
2009
Jamie Madill91445bc2015-09-23 16:47:53 -04002010void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002011 const sh::ShaderVariable &uniform,
2012 const std::string &fullName,
2013 sh::HLSLBlockEncoder *encoder,
2014 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002015{
2016 if (uniform.isStruct())
2017 {
2018 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2019 {
2020 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2021
Jamie Madill55def582015-05-04 11:24:57 -04002022 if (encoder)
2023 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002024
2025 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2026 {
Jamie Madill334d6152015-10-22 14:00:28 -04002027 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002028 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2029
Olli Etuaho96963162016-03-21 11:54:33 +02002030 // Samplers get their registers assigned in assignAllSamplerRegisters.
2031 // Also they couldn't use the same encoder as the rest of the struct, since they are
2032 // extracted out of the struct by the shader translator.
2033 if (gl::IsSamplerType(field.type))
2034 {
2035 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2036 }
2037 else
2038 {
2039 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2040 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002041 }
2042
Jamie Madill55def582015-05-04 11:24:57 -04002043 if (encoder)
2044 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002045 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002046 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002047 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002048
2049 // Not a struct. Arrays are treated as aggregate types.
2050 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002051 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002052 encoder->enterAggregateType();
2053 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002054
Jamie Madill62d31cb2015-09-11 13:25:51 -04002055 // Advance the uniform offset, to track registers allocation for structs
2056 sh::BlockMemberInfo blockInfo =
2057 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2058 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002059
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002060 auto uniformMapEntry = uniformMap->find(fullName);
2061 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002062
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002063 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002064 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002065 d3dUniform = uniformMapEntry->second;
2066 }
2067 else
2068 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002069 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002070 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002071 }
2072
2073 if (encoder)
2074 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002075 d3dUniform->registerElement =
2076 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002077 unsigned int reg =
2078 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002079 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 {
2081 d3dUniform->psRegisterIndex = reg;
2082 }
Xinghua Caob1239382016-12-13 15:07:05 +08002083 else if (shaderType == GL_VERTEX_SHADER)
2084 {
2085 d3dUniform->vsRegisterIndex = reg;
2086 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002087 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088 {
Xinghua Caob1239382016-12-13 15:07:05 +08002089 ASSERT(shaderType == GL_COMPUTE_SHADER);
2090 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002091 }
Jamie Madillfb536032015-09-11 13:19:49 -04002092
2093 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002094 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002095 {
2096 encoder->exitAggregateType();
2097 }
2098 }
2099}
2100
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002101template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002102void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002103{
Jamie Madill334d6152015-10-22 14:00:28 -04002104 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002105 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2106
Jamie Madill62d31cb2015-09-11 13:25:51 -04002107 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108
Jamie Madill62d31cb2015-09-11 13:25:51 -04002109 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002110 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002111 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002112
2113 if (targetUniform->type == targetUniformType)
2114 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002115 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002116
Jamie Madill62d31cb2015-09-11 13:25:51 -04002117 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002118 {
Jamie Madill334d6152015-10-22 14:00:28 -04002119 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002120 const T *source = v + (i * components);
2121
2122 for (int c = 0; c < components; c++)
2123 {
2124 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2125 }
2126 for (int c = components; c < 4; c++)
2127 {
2128 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2129 }
2130 }
2131 }
2132 else if (targetUniform->type == targetBoolType)
2133 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002134 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002135
Jamie Madill62d31cb2015-09-11 13:25:51 -04002136 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002137 {
Jamie Madill334d6152015-10-22 14:00:28 -04002138 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002139 const T *source = v + (i * components);
2140
2141 for (int c = 0; c < components; c++)
2142 {
Jamie Madill334d6152015-10-22 14:00:28 -04002143 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2144 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002145 }
2146 for (int c = components; c < 4; c++)
2147 {
2148 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2149 }
2150 }
2151 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002152 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002153 {
2154 ASSERT(targetUniformType == GL_INT);
2155
Jamie Madill62d31cb2015-09-11 13:25:51 -04002156 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002157
2158 bool wasDirty = targetUniform->dirty;
2159
Jamie Madill62d31cb2015-09-11 13:25:51 -04002160 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002161 {
Jamie Madill334d6152015-10-22 14:00:28 -04002162 GLint *dest = target + (i * 4);
2163 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002164
2165 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2166 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2167 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2168 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2169 }
2170
2171 if (!wasDirty && targetUniform->dirty)
2172 {
2173 mDirtySamplerMapping = true;
2174 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002175 }
Jamie Madill334d6152015-10-22 14:00:28 -04002176 else
2177 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002178}
2179
2180template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002181void ProgramD3D::setUniformMatrixfv(GLint location,
2182 GLsizei countIn,
2183 GLboolean transpose,
2184 const GLfloat *value,
2185 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002186{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002187 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002188
Jamie Madill62d31cb2015-09-11 13:25:51 -04002189 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002190 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002191 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002192
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002193 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002194 GLfloat *target =
2195 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002196
Jamie Madill62d31cb2015-09-11 13:25:51 -04002197 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002198 {
2199 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2200 if (transpose == GL_FALSE)
2201 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002202 targetUniform->dirty =
2203 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002204 }
2205 else
2206 {
Jamie Madill334d6152015-10-22 14:00:28 -04002207 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002208 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002209 }
2210 target += targetMatrixStride;
2211 value += cols * rows;
2212 }
2213}
2214
Jamie Madill4a3c2342015-10-08 12:58:45 -04002215size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002216{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002217 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002218
Jamie Madill62d31cb2015-09-11 13:25:51 -04002219 // define member uniforms
2220 sh::Std140BlockEncoder std140Encoder;
Jamie Madillcc2ed612017-03-14 15:59:00 -04002221 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002222 sh::BlockLayoutEncoder *encoder = nullptr;
2223
2224 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002225 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002226 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002227 }
2228 else
2229 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002230 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002231 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002232
Jamie Madill39046162016-02-08 15:05:17 -05002233 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2234 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002235
2236 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002237}
2238
Jamie Madill62d31cb2015-09-11 13:25:51 -04002239void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002240{
Olli Etuaho96963162016-03-21 11:54:33 +02002241 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002242 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002243 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002244 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002245 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002246 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002247 }
2248}
2249
Olli Etuaho96963162016-03-21 11:54:33 +02002250void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002251{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002252 ASSERT(d3dUniform->isSampler());
Xinghua Caob1239382016-12-13 15:07:05 +08002253 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2254 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002255 {
Xinghua Caob1239382016-12-13 15:07:05 +08002256 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2257 ASSERT(computeShaderD3D->hasUniform(d3dUniform));
2258 d3dUniform->csRegisterIndex = computeShaderD3D->getUniformRegister(d3dUniform->name);
2259 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2260 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2261 mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002262 }
Xinghua Caob1239382016-12-13 15:07:05 +08002263 else
Jamie Madillfb536032015-09-11 13:19:49 -04002264 {
Xinghua Caob1239382016-12-13 15:07:05 +08002265 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2266 const ShaderD3D *fragmentShaderD3D =
2267 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2268 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) ||
2269 fragmentShaderD3D->hasUniform(d3dUniform));
2270 if (vertexShaderD3D->hasUniform(d3dUniform))
2271 {
2272 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2273 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
2274 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2275 mSamplersVS, &mUsedVertexSamplerRange);
2276 }
2277 if (fragmentShaderD3D->hasUniform(d3dUniform))
2278 {
2279 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2280 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
2281 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2282 mSamplersPS, &mUsedPixelSamplerRange);
2283 }
Jamie Madillfb536032015-09-11 13:19:49 -04002284 }
2285}
2286
Jamie Madill62d31cb2015-09-11 13:25:51 -04002287// static
2288void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002289 GLenum samplerType,
2290 unsigned int samplerCount,
2291 std::vector<Sampler> &outSamplers,
2292 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002293{
2294 unsigned int samplerIndex = startSamplerIndex;
2295
2296 do
2297 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002298 ASSERT(samplerIndex < outSamplers.size());
2299 Sampler *sampler = &outSamplers[samplerIndex];
2300 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002301 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002302 sampler->logicalTextureUnit = 0;
2303 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002304 samplerIndex++;
2305 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002306}
2307
Brandon Jonesc9610c52014-08-25 17:02:59 -07002308void ProgramD3D::reset()
2309{
Xinghua Caob1239382016-12-13 15:07:05 +08002310 mVertexExecutables.clear();
2311 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002312
Xinghua Caob1239382016-12-13 15:07:05 +08002313 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002314 {
Xinghua Caob1239382016-12-13 15:07:05 +08002315 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002316 }
Brandon Joneseb994362014-09-24 10:27:28 -07002317
Xinghua Caob1239382016-12-13 15:07:05 +08002318 mComputeExecutable.reset(nullptr);
2319
Brandon Jones22502d52014-08-29 16:58:36 -07002320 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002321 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002322
2323 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002324 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002325 mUsesFragDepth = false;
2326 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002327 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002328 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002329
Jamie Madill62d31cb2015-09-11 13:25:51 -04002330 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002331 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002332
Xinghua Caob1239382016-12-13 15:07:05 +08002333 mVertexUniformStorage.reset(nullptr);
2334 mFragmentUniformStorage.reset(nullptr);
2335 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002336
2337 mSamplersPS.clear();
2338 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002339 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002340
2341 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002342 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002343 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002344 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002345
Jamie Madill8047c0d2016-03-07 13:02:12 -05002346 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002347
Jamie Madill9fc36822015-11-18 13:08:07 -05002348 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002349
2350 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002351}
2352
Geoff Lang7dd2e102014-11-10 15:19:26 -05002353unsigned int ProgramD3D::getSerial() const
2354{
2355 return mSerial;
2356}
2357
2358unsigned int ProgramD3D::issueSerial()
2359{
2360 return mCurrentSerial++;
2361}
2362
Jamie Madill8047c0d2016-03-07 13:02:12 -05002363void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002364{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002365 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002366 ASSERT(vertexShader != nullptr);
2367
2368 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002369 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002370 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002371 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2372 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002373
Jamie Madill8047c0d2016-03-07 13:02:12 -05002374 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002375 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002376 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002377 }
2378 }
Jamie Madill437d2662014-12-05 14:23:35 -05002379}
2380
Jamie Madill63805b42015-08-25 13:17:39 -04002381void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002382{
Jamie Madillbd136f92015-08-10 14:51:37 -04002383 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002384 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002385
Jamie Madill6de51852017-04-12 09:53:01 -04002386 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002387 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002388 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002389
Jamie Madill8047c0d2016-03-07 13:02:12 -05002390 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002391 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002392 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002393 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002394 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002395 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002396 mCachedInputLayout[d3dSemantic] =
2397 GetVertexFormatType(vertexAttributes[locationIndex],
2398 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002399 }
2400 }
2401}
2402
Jamie Madill192745a2016-12-22 15:58:21 -05002403void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2404 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002405{
Jamie Madill9fc36822015-11-18 13:08:07 -05002406 const std::string &varyingSemantic =
2407 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2408
Jamie Madillccdf74b2015-08-18 10:46:12 -04002409 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002410 mStreamOutVaryings.clear();
2411
Jamie Madill48ef11b2016-04-27 15:21:52 -04002412 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002413 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2414 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002415 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002416 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2417 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002418 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002419 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002420 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002421 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2422 builtins.glPosition.index, 4, outputSlot));
2423 }
2424 }
2425 else if (tfVaryingName == "gl_FragCoord")
2426 {
2427 if (builtins.glFragCoord.enabled)
2428 {
2429 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2430 builtins.glFragCoord.index, 4, outputSlot));
2431 }
2432 }
2433 else if (tfVaryingName == "gl_PointSize")
2434 {
2435 if (builtins.glPointSize.enabled)
2436 {
2437 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2438 }
2439 }
2440 else
2441 {
jchen10a9042d32017-03-17 08:50:45 +08002442 size_t subscript = GL_INVALID_INDEX;
2443 std::string baseName = gl::ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002444 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002445 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002446 const auto &varying = *registerInfo.packedVarying->varying;
2447 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002448 int componentCount = gl::VariableColumnCount(transposedType);
2449 ASSERT(!varying.isBuiltIn());
2450
Jamie Madill55c25d02015-11-18 13:08:08 -05002451 // Transform feedback for varying structs is underspecified.
2452 // See Khronos bug 9856.
2453 // TODO(jmadill): Figure out how to be spec-compliant here.
2454 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2455 continue;
2456
Jamie Madill9fc36822015-11-18 13:08:07 -05002457 // There can be more than one register assigned to a particular varying, and each
2458 // register needs its own stream out entry.
jchen10a9042d32017-03-17 08:50:45 +08002459 if (baseName == registerInfo.packedVarying->varying->name &&
2460 (subscript == GL_INVALID_INDEX || subscript == registerInfo.varyingArrayIndex))
Jamie Madill9fc36822015-11-18 13:08:07 -05002461 {
2462 mStreamOutVaryings.push_back(D3DVarying(
2463 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2464 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002465 }
2466 }
2467 }
2468}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002469
2470D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2471{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002472 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002473}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002474
2475bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2476{
2477 std::string baseName = blockName;
2478 gl::ParseAndStripArrayIndex(&baseName);
2479
2480 auto sizeIter = mBlockDataSizes.find(baseName);
2481 if (sizeIter == mBlockDataSizes.end())
2482 {
2483 *sizeOut = 0;
2484 return false;
2485 }
2486
2487 *sizeOut = sizeIter->second;
2488 return true;
2489}
2490
2491bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2492 sh::BlockMemberInfo *memberInfoOut) const
2493{
2494 auto infoIter = mBlockInfo.find(memberUniformName);
2495 if (infoIter == mBlockInfo.end())
2496 {
2497 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2498 return false;
2499 }
2500
2501 *memberInfoOut = infoIter->second;
2502 return true;
2503}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002504
2505void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2506 GLenum genMode,
2507 GLint components,
2508 const GLfloat *coeffs)
2509{
2510 UNREACHABLE();
2511}
2512
Jamie Madill8047c0d2016-03-07 13:02:12 -05002513} // namespace rx