blob: 0af46aaed2623ea0b9967010666afc89d99bdc2f [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"
Olli Etuahod2551232017-10-26 20:03:33 +030012#include "common/string_utils.h"
Jamie Madill437d2662014-12-05 14:23:35 -050013#include "common/utilities.h"
Jamie Madillc564c072017-06-01 12:45:42 -040014#include "libANGLE/Context.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050015#include "libANGLE/Framebuffer.h"
16#include "libANGLE/FramebufferAttachment.h"
17#include "libANGLE/Program.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040018#include "libANGLE/Uniform.h"
Jamie Madillc9727f32017-11-07 12:37:07 -050019#include "libANGLE/UniformLinker.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040020#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000021#include "libANGLE/features.h"
Jamie Madill54164b02017-08-28 15:17:37 -040022#include "libANGLE/queryconversions.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050023#include "libANGLE/renderer/ContextImpl.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050024#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050025#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050026#include "libANGLE/renderer/d3d/RendererD3D.h"
27#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050028#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050029#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050030
Jamie Madill01074252016-11-28 15:55:51 -050031using namespace angle;
32
Brandon Jonesc9610c52014-08-25 17:02:59 -070033namespace rx
34{
35
Brandon Joneseb994362014-09-24 10:27:28 -070036namespace
37{
38
Jamie Madill4c19a8a2017-07-24 11:46:06 -040039void GetDefaultInputLayoutFromShader(const gl::Context *context,
40 gl::Shader *vertexShader,
41 gl::InputLayout *inputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070042{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040043 inputLayoutOut->clear();
44
Jamie Madillbd044ed2017-06-05 12:59:21 -040045 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes(context))
Brandon Joneseb994362014-09-24 10:27:28 -070046 {
Brandon Joneseb994362014-09-24 10:27:28 -070047 if (shaderAttr.type != GL_NONE)
48 {
49 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
50
Jamie Madilld3dfda22015-07-06 08:28:49 -040051 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040052 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070053 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040054 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040055 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040056 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040057 gl::VertexFormatType defaultType =
58 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070059
Jamie Madill4c19a8a2017-07-24 11:46:06 -040060 inputLayoutOut->push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070061 }
62 }
63 }
64}
65
Jamie Madill4c19a8a2017-07-24 11:46:06 -040066void GetDefaultOutputLayoutFromShader(
67 const std::vector<PixelShaderOutputVariable> &shaderOutputVars,
68 std::vector<GLenum> *outputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070069{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040070 outputLayoutOut->clear();
Brandon Joneseb994362014-09-24 10:27:28 -070071
Jamie Madillb4463142014-12-19 14:56:54 -050072 if (!shaderOutputVars.empty())
73 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -040074 outputLayoutOut->push_back(GL_COLOR_ATTACHMENT0 +
75 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050076 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -040077}
Brandon Joneseb994362014-09-24 10:27:28 -070078
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080079template <typename T, int cols, int rows>
80bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -040081{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080082 constexpr int targetWidth = 4;
83 constexpr int targetHeight = rows;
84 constexpr int srcWidth = rows;
85 constexpr int srcHeight = cols;
86
87 constexpr int copyWidth = std::min(targetHeight, srcWidth);
88 constexpr int copyHeight = std::min(targetWidth, srcHeight);
89
90 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -040091
92 for (int x = 0; x < copyWidth; x++)
93 {
94 for (int y = 0; y < copyHeight; y++)
95 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080096 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -040097 }
98 }
99
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800100 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
101 {
102 return false;
103 }
104
105 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
106 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400107}
108
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800109template <typename T, int cols, int rows>
110bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400111{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800112 constexpr int targetWidth = 4;
113 constexpr int targetHeight = rows;
114 constexpr int srcWidth = cols;
115 constexpr int srcHeight = rows;
116
117 constexpr int copyWidth = std::min(targetWidth, srcWidth);
118 constexpr int copyHeight = std::min(targetHeight, srcHeight);
119
120 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400121
122 for (int y = 0; y < copyHeight; y++)
123 {
124 for (int x = 0; x < copyWidth; x++)
125 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800126 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400127 }
128 }
129
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800130 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
131 {
132 return false;
133 }
134
135 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
136 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400137}
138
Jamie Madill4e31ad52015-10-29 10:32:57 -0400139gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
140{
141 switch (drawMode)
142 {
143 // Uses the point sprite geometry shader.
144 case GL_POINTS:
145 return gl::PRIMITIVE_POINTS;
146
147 // All line drawing uses the same geometry shader.
148 case GL_LINES:
149 case GL_LINE_STRIP:
150 case GL_LINE_LOOP:
151 return gl::PRIMITIVE_LINES;
152
153 // The triangle fan primitive is emulated with strips in D3D11.
154 case GL_TRIANGLES:
155 case GL_TRIANGLE_FAN:
156 return gl::PRIMITIVE_TRIANGLES;
157
158 // Special case for triangle strips.
159 case GL_TRIANGLE_STRIP:
160 return gl::PRIMITIVE_TRIANGLE_STRIP;
161
162 default:
163 UNREACHABLE();
164 return gl::PRIMITIVE_TYPE_MAX;
165 }
166}
167
Jamie Madill192745a2016-12-22 15:58:21 -0500168bool FindFlatInterpolationVarying(const std::vector<sh::Varying> &varyings)
169{
170 // Note: this assumes nested structs can only be packed with one interpolation.
171 for (const auto &varying : varyings)
172 {
173 if (varying.interpolation == sh::INTERPOLATION_FLAT)
174 {
175 return true;
176 }
177 }
178
179 return false;
180}
181
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400182// Helper method to de-tranpose a matrix uniform for an API query.
183void GetMatrixUniform(GLint columns, GLint rows, GLfloat *dataOut, const GLfloat *source)
184{
185 for (GLint col = 0; col < columns; ++col)
186 {
187 for (GLint row = 0; row < rows; ++row)
188 {
189 GLfloat *outptr = dataOut + ((col * rows) + row);
190 const GLfloat *inptr = source + ((row * 4) + col);
191 *outptr = *inptr;
192 }
193 }
194}
195
196template <typename NonFloatT>
197void GetMatrixUniform(GLint columns, GLint rows, NonFloatT *dataOut, const NonFloatT *source)
198{
199 UNREACHABLE();
200}
201
Jamie Madillada9ecc2015-08-17 12:53:37 -0400202} // anonymous namespace
203
Jamie Madill28afae52015-11-09 15:07:57 -0500204// D3DUniform Implementation
205
Jamie Madill33bb7c42017-09-09 23:32:51 -0400206D3DUniform::D3DUniform(GLenum type,
Jamie Madill62d31cb2015-09-11 13:25:51 -0400207 const std::string &nameIn,
208 unsigned int arraySizeIn,
209 bool defaultBlock)
Jamie Madill33bb7c42017-09-09 23:32:51 -0400210 : typeInfo(gl::GetUniformTypeInfo(type)),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400211 name(nameIn),
212 arraySize(arraySizeIn),
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400213 vsData(nullptr),
214 psData(nullptr),
215 csData(nullptr),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400216 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400217 psRegisterIndex(GL_INVALID_INDEX),
Xinghua Caob1239382016-12-13 15:07:05 +0800218 csRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400219 registerCount(0),
220 registerElement(0)
221{
222 // We use data storage for default block uniforms to cache values that are sent to D3D during
223 // rendering
224 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
225 if (defaultBlock)
226 {
Jamie Madillcc2ed612017-03-14 15:59:00 -0400227 // Use the row count as register count, will work for non-square matrices.
Jamie Madill33bb7c42017-09-09 23:32:51 -0400228 registerCount = typeInfo.rowCount * elementCount();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400229 }
230}
231
232D3DUniform::~D3DUniform()
233{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400234}
235
236const uint8_t *D3DUniform::getDataPtrToElement(size_t elementIndex) const
237{
238 ASSERT((arraySize == 0 && elementIndex == 0) || (arraySize > 0 && elementIndex < arraySize));
239
240 if (isSampler())
241 {
242 return reinterpret_cast<const uint8_t *>(&mSamplerData[elementIndex]);
243 }
244
Jamie Madill33bb7c42017-09-09 23:32:51 -0400245 return firstNonNullData() + (elementIndex > 0 ? (typeInfo.internalSize * elementIndex) : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400246}
247
248bool D3DUniform::isSampler() const
249{
Jamie Madill33bb7c42017-09-09 23:32:51 -0400250 return typeInfo.isSampler;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400251}
252
253bool D3DUniform::isReferencedByVertexShader() const
254{
255 return vsRegisterIndex != GL_INVALID_INDEX;
256}
257
258bool D3DUniform::isReferencedByFragmentShader() const
259{
260 return psRegisterIndex != GL_INVALID_INDEX;
261}
262
Xinghua Caob1239382016-12-13 15:07:05 +0800263bool D3DUniform::isReferencedByComputeShader() const
264{
265 return csRegisterIndex != GL_INVALID_INDEX;
266}
267
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400268const uint8_t *D3DUniform::firstNonNullData() const
269{
270 ASSERT(vsData || psData || csData || !mSamplerData.empty());
271
272 if (!mSamplerData.empty())
273 {
274 return reinterpret_cast<const uint8_t *>(mSamplerData.data());
275 }
276
277 return vsData ? vsData : (psData ? psData : csData);
278}
279
Jamie Madill28afae52015-11-09 15:07:57 -0500280// D3DVarying Implementation
281
Jamie Madill9fc36822015-11-18 13:08:07 -0500282D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500283{
284}
285
Jamie Madill9fc36822015-11-18 13:08:07 -0500286D3DVarying::D3DVarying(const std::string &semanticNameIn,
287 unsigned int semanticIndexIn,
288 unsigned int componentCountIn,
289 unsigned int outputSlotIn)
290 : semanticName(semanticNameIn),
291 semanticIndex(semanticIndexIn),
292 componentCount(componentCountIn),
293 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500294{
295}
296
Jamie Madille39a3f02015-11-17 20:42:15 -0500297// ProgramD3DMetadata Implementation
298
Jamie Madillc9bde922016-07-24 17:58:50 -0400299ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500300 const ShaderD3D *vertexShader,
301 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400302 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
303 mShaderModelSuffix(renderer->getShaderModelSuffix()),
304 mUsesInstancedPointSpriteEmulation(
305 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
306 mUsesViewScale(renderer->presentPathFastEnabled()),
Martin Radev41ac68e2017-06-06 12:16:58 +0300307 mHasANGLEMultiviewEnabled(vertexShader->hasANGLEMultiviewEnabled()),
308 mUsesViewID(fragmentShader->usesViewID()),
Martin Radevc1d4e552017-08-21 12:01:10 +0300309 mCanSelectViewInVertexShader(renderer->canSelectViewInVertexShader()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500310 mVertexShader(vertexShader),
311 mFragmentShader(fragmentShader)
312{
313}
314
315int ProgramD3DMetadata::getRendererMajorShaderModel() const
316{
317 return mRendererMajorShaderModel;
318}
319
Jamie Madill9082b982016-04-27 15:21:51 -0400320bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500321{
Corentin Wallezc084de12017-06-05 14:28:52 -0700322 return (mFragmentShader->usesFragColor() && mFragmentShader->usesMultipleRenderTargets() &&
323 data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500324}
325
Jamie Madill48ef11b2016-04-27 15:21:52 -0400326bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500327{
Jamie Madill63286672015-11-24 13:00:08 -0500328 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500329}
330
331bool ProgramD3DMetadata::usesPointCoord() const
332{
333 return mFragmentShader->usesPointCoord();
334}
335
336bool ProgramD3DMetadata::usesFragCoord() const
337{
338 return mFragmentShader->usesFragCoord();
339}
340
341bool ProgramD3DMetadata::usesPointSize() const
342{
343 return mVertexShader->usesPointSize();
344}
345
346bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
347{
Jamie Madillc9bde922016-07-24 17:58:50 -0400348 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
349 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500350}
351
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800352bool ProgramD3DMetadata::usesViewScale() const
353{
354 return mUsesViewScale;
355}
356
Martin Radev41ac68e2017-06-06 12:16:58 +0300357bool ProgramD3DMetadata::hasANGLEMultiviewEnabled() const
358{
359 return mHasANGLEMultiviewEnabled;
360}
361
362bool ProgramD3DMetadata::usesViewID() const
363{
364 return mUsesViewID;
365}
366
Martin Radevc1d4e552017-08-21 12:01:10 +0300367bool ProgramD3DMetadata::canSelectViewInVertexShader() const
368{
369 return mCanSelectViewInVertexShader;
370}
371
Jamie Madille39a3f02015-11-17 20:42:15 -0500372bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
373{
Jamie Madillc9bde922016-07-24 17:58:50 -0400374 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500375 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400376 // Even with a geometry shader, the app can render triangles or lines and reference
377 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
378 // simplicity, we always add this to the vertex shader when the fragment shader
379 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500380 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
381 usesInsertedPointCoordValue();
382}
383
384bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
385{
386 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
387 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
388 // the vertex shader in this case. This saves us 1 output vector.
389 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
390}
391
392bool ProgramD3DMetadata::usesSystemValuePointSize() const
393{
394 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
395}
396
397bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
398{
399 return mFragmentShader->usesMultipleRenderTargets();
400}
401
402GLint ProgramD3DMetadata::getMajorShaderVersion() const
403{
404 return mVertexShader->getData().getShaderVersion();
405}
406
407const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
408{
409 return mFragmentShader;
410}
411
Jamie Madill28afae52015-11-09 15:07:57 -0500412// ProgramD3D Implementation
413
Jamie Madilld3dfda22015-07-06 08:28:49 -0400414ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
415 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500416 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400417 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700418{
Brandon Joneseb994362014-09-24 10:27:28 -0700419}
420
421ProgramD3D::VertexExecutable::~VertexExecutable()
422{
423 SafeDelete(mShaderExecutable);
424}
425
Jamie Madilld3dfda22015-07-06 08:28:49 -0400426// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500427ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
428 GLenum type)
429{
430 switch (type)
431 {
432 case GL_INT:
433 return HLSLAttribType::SIGNED_INT;
434 case GL_UNSIGNED_INT:
435 return HLSLAttribType::UNSIGNED_INT;
436 case GL_SIGNED_NORMALIZED:
437 case GL_UNSIGNED_NORMALIZED:
438 case GL_FLOAT:
439 return HLSLAttribType::FLOAT;
440 default:
441 UNREACHABLE();
442 return HLSLAttribType::FLOAT;
443 }
444}
445
446// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400447void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
448 const gl::InputLayout &inputLayout,
449 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700450{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500451 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400452
453 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700454 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400455 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500456 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
457 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400458
Jamie Madillbdec2f42016-03-02 16:35:32 -0500459 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
460 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
461 continue;
462
463 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
464 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700465 }
Brandon Joneseb994362014-09-24 10:27:28 -0700466}
467
Jamie Madilld3dfda22015-07-06 08:28:49 -0400468bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
469{
Jamie Madillbd136f92015-08-10 14:51:37 -0400470 size_t limit = std::max(mSignature.size(), signature.size());
471 for (size_t index = 0; index < limit; ++index)
472 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500473 // treat undefined indexes as FLOAT
474 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
475 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400476 if (a != b)
477 return false;
478 }
479
480 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400481}
482
483ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
484 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400485 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700486{
487}
488
489ProgramD3D::PixelExecutable::~PixelExecutable()
490{
491 SafeDelete(mShaderExecutable);
492}
493
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700494ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
495{
496}
497
Geoff Lang7dd2e102014-11-10 15:19:26 -0500498unsigned int ProgramD3D::mCurrentSerial = 1;
499
Jamie Madill48ef11b2016-04-27 15:21:52 -0400500ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
501 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700502 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800503 mDynamicHLSL(nullptr),
504 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
505 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700506 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400507 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800508 mVertexUniformStorage(nullptr),
509 mFragmentUniformStorage(nullptr),
510 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700511 mUsedVertexSamplerRange(0),
512 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800513 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700514 mDirtySamplerMapping(true),
Jamie Madill561ed3a2017-08-31 16:48:09 -0400515 mSerial(issueSerial()),
Jamie Madill4148fd72017-09-14 15:46:20 -0400516 mVertexUniformsDirty(true),
517 mFragmentUniformsDirty(true),
518 mComputeUniformsDirty(true)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700519{
Brandon Joneseb994362014-09-24 10:27:28 -0700520 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700521}
522
523ProgramD3D::~ProgramD3D()
524{
525 reset();
526 SafeDelete(mDynamicHLSL);
527}
528
Brandon Jones44151a92014-09-10 11:32:25 -0700529bool ProgramD3D::usesPointSpriteEmulation() const
530{
531 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
532}
533
Martin Radev41ac68e2017-06-06 12:16:58 +0300534bool ProgramD3D::usesGeometryShaderForPointSpriteEmulation() const
535{
536 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
537}
538
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400539bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700540{
Martin Radevc1d4e552017-08-21 12:01:10 +0300541 if (mHasANGLEMultiviewEnabled && !mRenderer->canSelectViewInVertexShader())
Martin Radev41ac68e2017-06-06 12:16:58 +0300542 {
543 return true;
544 }
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400545 if (drawMode != GL_POINTS)
546 {
547 return mUsesFlatInterpolation;
548 }
Martin Radev41ac68e2017-06-06 12:16:58 +0300549 return usesGeometryShaderForPointSpriteEmulation();
Cooper Partine6664f02015-01-09 16:22:24 -0800550}
551
552bool ProgramD3D::usesInstancedPointSpriteEmulation() const
553{
554 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700555}
556
Jamie Madill334d6152015-10-22 14:00:28 -0400557GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
558 unsigned int samplerIndex,
559 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700560{
561 GLint logicalTextureUnit = -1;
562
563 switch (type)
564 {
Jamie Madill334d6152015-10-22 14:00:28 -0400565 case gl::SAMPLER_PIXEL:
566 ASSERT(samplerIndex < caps.maxTextureImageUnits);
567 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
568 {
569 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
570 }
571 break;
572 case gl::SAMPLER_VERTEX:
573 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
574 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
575 {
576 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
577 }
578 break;
Xinghua Caob1239382016-12-13 15:07:05 +0800579 case gl::SAMPLER_COMPUTE:
580 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
581 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
582 {
583 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
584 }
585 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400586 default:
587 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700588 }
589
Jamie Madill334d6152015-10-22 14:00:28 -0400590 if (logicalTextureUnit >= 0 &&
591 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700592 {
593 return logicalTextureUnit;
594 }
595
596 return -1;
597}
598
599// Returns the texture type for a given Direct3D 9 sampler type and
600// index (0-15 for the pixel shader and 0-3 for the vertex shader).
601GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
602{
603 switch (type)
604 {
Jamie Madill334d6152015-10-22 14:00:28 -0400605 case gl::SAMPLER_PIXEL:
606 ASSERT(samplerIndex < mSamplersPS.size());
607 ASSERT(mSamplersPS[samplerIndex].active);
608 return mSamplersPS[samplerIndex].textureType;
609 case gl::SAMPLER_VERTEX:
610 ASSERT(samplerIndex < mSamplersVS.size());
611 ASSERT(mSamplersVS[samplerIndex].active);
612 return mSamplersVS[samplerIndex].textureType;
Xinghua Caob1239382016-12-13 15:07:05 +0800613 case gl::SAMPLER_COMPUTE:
614 ASSERT(samplerIndex < mSamplersCS.size());
615 ASSERT(mSamplersCS[samplerIndex].active);
616 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400617 default:
618 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700619 }
620
621 return GL_TEXTURE_2D;
622}
623
Olli Etuaho618bebc2016-01-15 16:40:00 +0200624GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700625{
626 switch (type)
627 {
Jamie Madill334d6152015-10-22 14:00:28 -0400628 case gl::SAMPLER_PIXEL:
629 return mUsedPixelSamplerRange;
630 case gl::SAMPLER_VERTEX:
631 return mUsedVertexSamplerRange;
Xinghua Caob1239382016-12-13 15:07:05 +0800632 case gl::SAMPLER_COMPUTE:
633 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400634 default:
635 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200636 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700637 }
638}
639
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400640ProgramD3D::SamplerMapping ProgramD3D::updateSamplerMapping()
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700641{
642 if (!mDirtySamplerMapping)
643 {
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400644 return SamplerMapping::WasClean;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700645 }
646
647 mDirtySamplerMapping = false;
648
649 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400650 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700651 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400652 if (!d3dUniform->isSampler())
653 continue;
654
655 int count = d3dUniform->elementCount();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400656
657 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700658 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400659 unsigned int firstIndex = d3dUniform->psRegisterIndex;
660
661 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700662 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400663 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700664
Jamie Madill62d31cb2015-09-11 13:25:51 -0400665 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700666 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400667 ASSERT(mSamplersPS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400668 mSamplersPS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700669 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400670 }
671 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700672
Jamie Madill62d31cb2015-09-11 13:25:51 -0400673 if (d3dUniform->isReferencedByVertexShader())
674 {
675 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
676
677 for (int i = 0; i < count; i++)
678 {
679 unsigned int samplerIndex = firstIndex + i;
680
681 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700682 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400683 ASSERT(mSamplersVS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400684 mSamplersVS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685 }
686 }
687 }
Xinghua Caob1239382016-12-13 15:07:05 +0800688
689 if (d3dUniform->isReferencedByComputeShader())
690 {
691 unsigned int firstIndex = d3dUniform->csRegisterIndex;
692
693 for (int i = 0; i < count; i++)
694 {
695 unsigned int samplerIndex = firstIndex + i;
696
697 if (samplerIndex < mSamplersCS.size())
698 {
699 ASSERT(mSamplersCS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400700 mSamplersCS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Xinghua Caob1239382016-12-13 15:07:05 +0800701 }
702 }
703 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700704 }
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400705
706 return SamplerMapping::WasDirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700707}
708
Jamie Madill9cf9e872017-06-05 12:59:25 -0400709gl::LinkResult ProgramD3D::load(const gl::Context *context,
710 gl::InfoLog &infoLog,
711 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700712{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500713 // TODO(jmadill): Use Renderer from contextImpl.
714
Jamie Madill62d31cb2015-09-11 13:25:51 -0400715 reset();
716
Jamie Madill334d6152015-10-22 14:00:28 -0400717 DeviceIdentifier binaryDeviceIdentifier = {0};
718 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
719 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700720
721 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
722 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
723 {
724 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500725 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700726 }
727
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500728 int compileFlags = stream->readInt<int>();
729 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
730 {
Jamie Madillf6113162015-05-07 11:49:21 -0400731 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500732 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500733 }
734
Jamie Madill8047c0d2016-03-07 13:02:12 -0500735 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400736 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500737 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400738 }
739
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700740 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
741 for (unsigned int i = 0; i < psSamplerCount; ++i)
742 {
743 Sampler sampler;
744 stream->readBool(&sampler.active);
745 stream->readInt(&sampler.logicalTextureUnit);
746 stream->readInt(&sampler.textureType);
747 mSamplersPS.push_back(sampler);
748 }
749 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
750 for (unsigned int i = 0; i < vsSamplerCount; ++i)
751 {
752 Sampler sampler;
753 stream->readBool(&sampler.active);
754 stream->readInt(&sampler.logicalTextureUnit);
755 stream->readInt(&sampler.textureType);
756 mSamplersVS.push_back(sampler);
757 }
758
Xinghua Caob1239382016-12-13 15:07:05 +0800759 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
760 for (unsigned int i = 0; i < csSamplerCount; ++i)
761 {
762 Sampler sampler;
763 stream->readBool(&sampler.active);
764 stream->readInt(&sampler.logicalTextureUnit);
765 stream->readInt(&sampler.textureType);
766 mSamplersCS.push_back(sampler);
767 }
768
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700769 stream->readInt(&mUsedVertexSamplerRange);
770 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800771 stream->readInt(&mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700772
773 const unsigned int uniformCount = stream->readInt<unsigned int>();
774 if (stream->error())
775 {
Jamie Madillf6113162015-05-07 11:49:21 -0400776 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500777 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700778 }
779
Jamie Madill48ef11b2016-04-27 15:21:52 -0400780 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400781 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700782 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
783 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400784 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700785
Jamie Madill62d31cb2015-09-11 13:25:51 -0400786 D3DUniform *d3dUniform =
787 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
788 linkedUniform.isInDefaultBlock());
789 stream->readInt(&d3dUniform->psRegisterIndex);
790 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800791 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400792 stream->readInt(&d3dUniform->registerCount);
793 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700794
Jamie Madill62d31cb2015-09-11 13:25:51 -0400795 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700796 }
797
Jamie Madill4a3c2342015-10-08 12:58:45 -0400798 const unsigned int blockCount = stream->readInt<unsigned int>();
799 if (stream->error())
800 {
801 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500802 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400803 }
804
805 ASSERT(mD3DUniformBlocks.empty());
806 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
807 {
808 D3DUniformBlock uniformBlock;
809 stream->readInt(&uniformBlock.psRegisterIndex);
810 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800811 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400812 mD3DUniformBlocks.push_back(uniformBlock);
813 }
814
Jamie Madill9fc36822015-11-18 13:08:07 -0500815 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
816 mStreamOutVaryings.resize(streamOutVaryingCount);
817 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700818 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500819 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700820
Jamie Madill28afae52015-11-09 15:07:57 -0500821 stream->readString(&varying->semanticName);
822 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500823 stream->readInt(&varying->componentCount);
824 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700825 }
826
Brandon Jones22502d52014-08-29 16:58:36 -0700827 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400828 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500829 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700830 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400831 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500832 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700833 stream->readBool(&mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +0300834 stream->readBool(&mHasANGLEMultiviewEnabled);
835 stream->readBool(&mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -0700836 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400837 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700838
839 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
840 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400841 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
842 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700843 {
844 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
845 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
846 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
847 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
848 }
849
Jamie Madill4e31ad52015-10-29 10:32:57 -0400850 stream->readString(&mGeometryShaderPreamble);
851
Jamie Madill334d6152015-10-22 14:00:28 -0400852 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700853
Jamie Madillb0a838b2016-11-13 20:02:12 -0500854 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
855
Brandon Joneseb994362014-09-24 10:27:28 -0700856 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400857 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
858 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700859 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400860 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400861 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700862
Jamie Madilld3dfda22015-07-06 08:28:49 -0400863 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700864 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400865 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700866 }
867
Jamie Madill334d6152015-10-22 14:00:28 -0400868 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700869 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400870
Jamie Madillada9ecc2015-08-17 12:53:37 -0400871 ShaderExecutableD3D *shaderExecutable = nullptr;
872
Jamie Madillb0a838b2016-11-13 20:02:12 -0500873 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
874 mStreamOutVaryings, separateAttribs,
875 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400876
Brandon Joneseb994362014-09-24 10:27:28 -0700877 if (!shaderExecutable)
878 {
Jamie Madillf6113162015-05-07 11:49:21 -0400879 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500880 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700881 }
882
883 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400884 VertexExecutable::Signature signature;
885 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700886
887 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800888 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
889 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700890
891 stream->skip(vertexShaderSize);
892 }
893
894 const size_t pixelShaderCount = stream->readInt<unsigned int>();
895 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
896 {
897 const size_t outputCount = stream->readInt<unsigned int>();
898 std::vector<GLenum> outputs(outputCount);
899 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
900 {
901 stream->readInt(&outputs[outputIndex]);
902 }
903
Jamie Madill334d6152015-10-22 14:00:28 -0400904 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700905 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400906 ShaderExecutableD3D *shaderExecutable = nullptr;
907
Jamie Madillb0a838b2016-11-13 20:02:12 -0500908 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
909 mStreamOutVaryings, separateAttribs,
910 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700911
912 if (!shaderExecutable)
913 {
Jamie Madillf6113162015-05-07 11:49:21 -0400914 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500915 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700916 }
917
918 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800919 mPixelExecutables.push_back(
920 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700921
922 stream->skip(pixelShaderSize);
923 }
924
Jamie Madill4e31ad52015-10-29 10:32:57 -0400925 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
926 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700927 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400928 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
929 if (geometryShaderSize == 0)
930 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400931 continue;
932 }
933
Brandon Joneseb994362014-09-24 10:27:28 -0700934 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400935
Xinghua Caob1239382016-12-13 15:07:05 +0800936 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -0500937 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
938 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
Xinghua Caob1239382016-12-13 15:07:05 +0800939 &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700940
Xinghua Caob1239382016-12-13 15:07:05 +0800941 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700942 {
Jamie Madillf6113162015-05-07 11:49:21 -0400943 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500944 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700945 }
Xinghua Caob1239382016-12-13 15:07:05 +0800946
947 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
948
Brandon Joneseb994362014-09-24 10:27:28 -0700949 stream->skip(geometryShaderSize);
950 }
951
Xinghua Caob1239382016-12-13 15:07:05 +0800952 unsigned int computeShaderSize = stream->readInt<unsigned int>();
953 if (computeShaderSize > 0)
954 {
955 const unsigned char *computeShaderFunction = binary + stream->offset();
956
957 ShaderExecutableD3D *computeExecutable = nullptr;
958 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
959 SHADER_COMPUTE, std::vector<D3DVarying>(), false,
960 &computeExecutable));
961
962 if (!computeExecutable)
963 {
964 infoLog << "Could not create compute shader.";
965 return false;
966 }
967
968 mComputeExecutable.reset(computeExecutable);
969 }
970
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700971 initializeUniformStorage();
972
Jamie Madillb0a838b2016-11-13 20:02:12 -0500973 return true;
Brandon Jones22502d52014-08-29 16:58:36 -0700974}
975
Jamie Madill27a60632017-06-30 15:12:01 -0400976void ProgramD3D::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700977{
Austin Kinross137b1512015-06-17 16:14:53 -0700978 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400979 // When we load the binary again later, we can validate the device identifier before trying to
980 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -0700981 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -0400982 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
983 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700984
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500985 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
986
Jamie Madill8047c0d2016-03-07 13:02:12 -0500987 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400988 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500989 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -0400990 }
991
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700992 stream->writeInt(mSamplersPS.size());
993 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
994 {
995 stream->writeInt(mSamplersPS[i].active);
996 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
997 stream->writeInt(mSamplersPS[i].textureType);
998 }
999
1000 stream->writeInt(mSamplersVS.size());
1001 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1002 {
1003 stream->writeInt(mSamplersVS[i].active);
1004 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1005 stream->writeInt(mSamplersVS[i].textureType);
1006 }
1007
Xinghua Caob1239382016-12-13 15:07:05 +08001008 stream->writeInt(mSamplersCS.size());
1009 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
1010 {
1011 stream->writeInt(mSamplersCS[i].active);
1012 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
1013 stream->writeInt(mSamplersCS[i].textureType);
1014 }
1015
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001016 stream->writeInt(mUsedVertexSamplerRange);
1017 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001018 stream->writeInt(mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001019
Jamie Madill62d31cb2015-09-11 13:25:51 -04001020 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001021 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001022 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001023 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001024 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1025 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001026 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001027 stream->writeInt(uniform->registerCount);
1028 stream->writeInt(uniform->registerElement);
1029 }
1030
Jamie Madill51f522f2016-12-21 15:10:55 -05001031 // Ensure we init the uniform block structure data if we should.
1032 // http://anglebug.com/1637
1033 ensureUniformBlocksInitialized();
1034
Jamie Madill4a3c2342015-10-08 12:58:45 -04001035 stream->writeInt(mD3DUniformBlocks.size());
1036 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1037 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001038 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1039 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001040 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001041 }
1042
Jamie Madill9fc36822015-11-18 13:08:07 -05001043 stream->writeInt(mStreamOutVaryings.size());
1044 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001045 {
Brandon Joneseb994362014-09-24 10:27:28 -07001046 stream->writeString(varying.semanticName);
1047 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001048 stream->writeInt(varying.componentCount);
1049 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001050 }
1051
Brandon Jones22502d52014-08-29 16:58:36 -07001052 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001053 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001054 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001055 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001056 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001057 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001058 stream->writeInt(mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +03001059 stream->writeInt(mHasANGLEMultiviewEnabled);
1060 stream->writeInt(mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -07001061 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001062 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001063
Brandon Joneseb994362014-09-24 10:27:28 -07001064 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001065 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001066 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1067 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001068 {
Brandon Joneseb994362014-09-24 10:27:28 -07001069 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001070 stream->writeInt(variable.type);
1071 stream->writeString(variable.name);
1072 stream->writeString(variable.source);
1073 stream->writeInt(variable.outputIndex);
1074 }
1075
Jamie Madill4e31ad52015-10-29 10:32:57 -04001076 stream->writeString(mGeometryShaderPreamble);
1077
Brandon Joneseb994362014-09-24 10:27:28 -07001078 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001079 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1080 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001081 {
Xinghua Caob1239382016-12-13 15:07:05 +08001082 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001083
Jamie Madilld3dfda22015-07-06 08:28:49 -04001084 const auto &inputLayout = vertexExecutable->inputs();
1085 stream->writeInt(inputLayout.size());
1086
1087 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001088 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001089 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001090 }
1091
1092 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1093 stream->writeInt(vertexShaderSize);
1094
1095 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1096 stream->writeBytes(vertexBlob, vertexShaderSize);
1097 }
1098
1099 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001100 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1101 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001102 {
Xinghua Caob1239382016-12-13 15:07:05 +08001103 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001104
1105 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1106 stream->writeInt(outputs.size());
1107 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1108 {
1109 stream->writeInt(outputs[outputIndex]);
1110 }
1111
1112 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1113 stream->writeInt(pixelShaderSize);
1114
1115 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1116 stream->writeBytes(pixelBlob, pixelShaderSize);
1117 }
1118
Xinghua Caob1239382016-12-13 15:07:05 +08001119 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001120 {
Xinghua Caob1239382016-12-13 15:07:05 +08001121 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001122 {
1123 stream->writeInt(0);
1124 continue;
1125 }
1126
Xinghua Caob1239382016-12-13 15:07:05 +08001127 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001128 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001129 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1130 }
1131
1132 if (mComputeExecutable)
1133 {
1134 size_t computeShaderSize = mComputeExecutable->getLength();
1135 stream->writeInt(computeShaderSize);
1136 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1137 }
1138 else
1139 {
1140 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001141 }
Brandon Jones22502d52014-08-29 16:58:36 -07001142}
1143
Geoff Langc5629752015-12-07 16:29:04 -05001144void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1145{
1146}
1147
Yunchao He61afff12017-03-14 15:34:03 +08001148void ProgramD3D::setSeparable(bool /* separable */)
1149{
1150}
1151
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001152gl::Error ProgramD3D::getPixelExecutableForCachedOutputLayout(ShaderExecutableD3D **outExecutable,
1153 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001154{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001155 if (mCachedPixelExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001156 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001157 *outExecutable = mPixelExecutables[mCachedPixelExecutableIndex.value()]->shaderExecutable();
1158 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001159 }
1160
Jamie Madill334d6152015-10-22 14:00:28 -04001161 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001162 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, mPixelShaderOutputLayoutCache);
Brandon Jones22502d52014-08-29 16:58:36 -07001163
1164 // Generate new pixel executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001165 ShaderExecutableD3D *pixelExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001166
1167 gl::InfoLog tempInfoLog;
1168 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1169
Jamie Madill01074252016-11-28 15:55:51 -05001170 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001171 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001172 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001173 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001174
Jamie Madill97399232014-12-23 12:31:15 -05001175 if (pixelExecutable)
1176 {
Xinghua Caob1239382016-12-13 15:07:05 +08001177 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001178 new PixelExecutable(mPixelShaderOutputLayoutCache, pixelExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001179 mCachedPixelExecutableIndex = mPixelExecutables.size() - 1;
Jamie Madill97399232014-12-23 12:31:15 -05001180 }
1181 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001182 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001183 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1184 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001185 }
Brandon Jones22502d52014-08-29 16:58:36 -07001186
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001187 *outExecutable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001188 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001189}
1190
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001191gl::Error ProgramD3D::getVertexExecutableForCachedInputLayout(ShaderExecutableD3D **outExectuable,
1192 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001193{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001194 if (mCachedVertexExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001195 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001196 *outExectuable =
1197 mVertexExecutables[mCachedVertexExecutableIndex.value()]->shaderExecutable();
1198 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001199 }
1200
Brandon Jones22502d52014-08-29 16:58:36 -07001201 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001202 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001203 mVertexHLSL, mCachedInputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001204
1205 // Generate new vertex executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001206 ShaderExecutableD3D *vertexExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001207
1208 gl::InfoLog tempInfoLog;
1209 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1210
Jamie Madill01074252016-11-28 15:55:51 -05001211 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001212 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001213 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001214 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001215
Jamie Madill97399232014-12-23 12:31:15 -05001216 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001217 {
Xinghua Caob1239382016-12-13 15:07:05 +08001218 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001219 new VertexExecutable(mCachedInputLayout, mCachedVertexSignature, vertexExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001220 mCachedVertexExecutableIndex = mVertexExecutables.size() - 1;
Brandon Joneseb994362014-09-24 10:27:28 -07001221 }
Jamie Madill97399232014-12-23 12:31:15 -05001222 else if (!infoLog)
1223 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001224 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1225 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001226 }
Brandon Jones22502d52014-08-29 16:58:36 -07001227
Geoff Langb543aff2014-09-30 14:52:54 -04001228 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001229 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001230}
1231
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001232gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Context *context,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001233 GLenum drawMode,
1234 ShaderExecutableD3D **outExecutable,
1235 gl::InfoLog *infoLog)
1236{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001237 if (outExecutable)
1238 {
1239 *outExecutable = nullptr;
1240 }
1241
Austin Kinross88829e82016-01-12 13:04:41 -08001242 // Return a null shader if the current rendering doesn't use a geometry shader
1243 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001244 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001245 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001246 }
1247
Jamie Madill4e31ad52015-10-29 10:32:57 -04001248 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1249
Xinghua Caob1239382016-12-13 15:07:05 +08001250 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001251 {
1252 if (outExecutable)
1253 {
Xinghua Caob1239382016-12-13 15:07:05 +08001254 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001255 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001256 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001257 }
1258
1259 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001260 context, geometryShaderType, mState, mRenderer->presentPathFastEnabled(),
Martin Radevc1d4e552017-08-21 12:01:10 +03001261 mHasANGLEMultiviewEnabled, mRenderer->canSelectViewInVertexShader(),
1262 usesGeometryShaderForPointSpriteEmulation(), mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001263
1264 gl::InfoLog tempInfoLog;
1265 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1266
Xinghua Caob1239382016-12-13 15:07:05 +08001267 ShaderExecutableD3D *geometryExecutable = nullptr;
1268 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001269 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001270 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001271 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001272
1273 if (!infoLog && error.isError())
1274 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001275 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1276 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001277 }
1278
Xinghua Caob1239382016-12-13 15:07:05 +08001279 if (geometryExecutable != nullptr)
1280 {
1281 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1282 }
1283
Jamie Madill4e31ad52015-10-29 10:32:57 -04001284 if (outExecutable)
1285 {
Xinghua Caob1239382016-12-13 15:07:05 +08001286 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001287 }
1288 return error;
1289}
1290
Jamie Madill01074252016-11-28 15:55:51 -05001291class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001292{
Jamie Madill01074252016-11-28 15:55:51 -05001293 public:
1294 GetExecutableTask(ProgramD3D *program)
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001295 : mProgram(program), mError(gl::NoError()), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001296 {
Brandon Joneseb994362014-09-24 10:27:28 -07001297 }
1298
Jamie Madill01074252016-11-28 15:55:51 -05001299 virtual gl::Error run() = 0;
1300
1301 void operator()() override { mError = run(); }
1302
1303 const gl::Error &getError() const { return mError; }
1304 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1305 ShaderExecutableD3D *getResult() { return mResult; }
1306
1307 protected:
1308 ProgramD3D *mProgram;
1309 gl::Error mError;
1310 gl::InfoLog mInfoLog;
1311 ShaderExecutableD3D *mResult;
1312};
1313
1314class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1315{
1316 public:
Jamie Madillbd044ed2017-06-05 12:59:21 -04001317 GetVertexExecutableTask(ProgramD3D *program, const gl::Context *context)
1318 : GetExecutableTask(program), mContext(context)
1319 {
1320 }
Jamie Madill01074252016-11-28 15:55:51 -05001321 gl::Error run() override
1322 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001323 mProgram->updateCachedInputLayoutFromShader(mContext);
Jamie Madill01074252016-11-28 15:55:51 -05001324
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001325 ANGLE_TRY(mProgram->getVertexExecutableForCachedInputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001326
1327 return gl::NoError();
1328 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001329
1330 private:
1331 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001332};
1333
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001334void ProgramD3D::updateCachedInputLayoutFromShader(const gl::Context *context)
1335{
1336 GetDefaultInputLayoutFromShader(context, mState.getAttachedVertexShader(), &mCachedInputLayout);
1337 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001338 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001339}
1340
Jamie Madill01074252016-11-28 15:55:51 -05001341class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1342{
1343 public:
1344 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1345 gl::Error run() override
1346 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001347 mProgram->updateCachedOutputLayoutFromShader();
Jamie Madill01074252016-11-28 15:55:51 -05001348
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001349 ANGLE_TRY(mProgram->getPixelExecutableForCachedOutputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001350
1351 return gl::NoError();
1352 }
1353};
1354
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001355void ProgramD3D::updateCachedOutputLayoutFromShader()
1356{
1357 GetDefaultOutputLayoutFromShader(mPixelShaderKey, &mPixelShaderOutputLayoutCache);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001358 updateCachedPixelExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001359}
1360
Jamie Madill01074252016-11-28 15:55:51 -05001361class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1362{
1363 public:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001364 GetGeometryExecutableTask(ProgramD3D *program, const gl::Context *context)
1365 : GetExecutableTask(program), mContext(context)
Jamie Madill01074252016-11-28 15:55:51 -05001366 {
1367 }
1368
1369 gl::Error run() override
1370 {
1371 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1372 // D3D11.
1373 if (mProgram->usesGeometryShader(GL_POINTS))
1374 {
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001375 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContext, GL_POINTS, &mResult,
1376 &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001377 }
1378
1379 return gl::NoError();
1380 }
1381
1382 private:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001383 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001384};
1385
Xinghua Cao73badc02017-03-29 19:14:53 +08001386gl::Error ProgramD3D::getComputeExecutable(ShaderExecutableD3D **outExecutable)
1387{
1388 if (outExecutable)
1389 {
1390 *outExecutable = mComputeExecutable.get();
1391 }
1392
1393 return gl::NoError();
1394}
1395
Jamie Madill9cf9e872017-06-05 12:59:25 -04001396gl::LinkResult ProgramD3D::compileProgramExecutables(const gl::Context *context,
1397 gl::InfoLog &infoLog)
Jamie Madill01074252016-11-28 15:55:51 -05001398{
1399 // Ensure the compiler is initialized to avoid race conditions.
1400 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1401
1402 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1403
Jamie Madillbd044ed2017-06-05 12:59:21 -04001404 GetVertexExecutableTask vertexTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001405 GetPixelExecutableTask pixelTask(this);
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001406 GetGeometryExecutableTask geometryTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001407
1408 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1409 workerPool->postWorkerTask(&pixelTask),
1410 workerPool->postWorkerTask(&geometryTask)}};
1411
1412 WaitableEvent::WaitMany(&waitEvents);
1413
1414 infoLog << vertexTask.getInfoLog().str();
1415 infoLog << pixelTask.getInfoLog().str();
1416 infoLog << geometryTask.getInfoLog().str();
1417
1418 ANGLE_TRY(vertexTask.getError());
1419 ANGLE_TRY(pixelTask.getError());
1420 ANGLE_TRY(geometryTask.getError());
1421
1422 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1423 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1424 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1425
Jamie Madill48ef11b2016-04-27 15:21:52 -04001426 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001427
1428 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001429 {
Jamie Madill334d6152015-10-22 14:00:28 -04001430 // Geometry shaders are currently only used internally, so there is no corresponding shader
1431 // object at the interface level. For now the geometry shader debug info is prepended to
1432 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001433 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001434 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001435 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1436 }
1437
1438 if (defaultVertexExecutable)
1439 {
1440 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1441 }
1442
1443 if (defaultPixelExecutable)
1444 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001445 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001446 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001447 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1448 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001449
Jamie Madillb0a838b2016-11-13 20:02:12 -05001450 return (defaultVertexExecutable && defaultPixelExecutable &&
1451 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001452}
1453
Jamie Madill9cf9e872017-06-05 12:59:25 -04001454gl::LinkResult ProgramD3D::compileComputeExecutable(const gl::Context *context,
1455 gl::InfoLog &infoLog)
Xinghua Caob1239382016-12-13 15:07:05 +08001456{
1457 // Ensure the compiler is initialized to avoid race conditions.
1458 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1459
Jamie Madillbd044ed2017-06-05 12:59:21 -04001460 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(context, mState);
Xinghua Caob1239382016-12-13 15:07:05 +08001461
1462 ShaderExecutableD3D *computeExecutable = nullptr;
1463 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, SHADER_COMPUTE,
1464 std::vector<D3DVarying>(), false,
1465 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1466
1467 if (computeExecutable == nullptr)
1468 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001469 ERR() << "Error compiling dynamic compute executable:" << std::endl
1470 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001471 }
1472 else
1473 {
1474 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1475 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1476 mComputeExecutable.reset(computeExecutable);
1477 }
1478
1479 return mComputeExecutable.get() != nullptr;
1480}
1481
Jamie Madill9cf9e872017-06-05 12:59:25 -04001482gl::LinkResult ProgramD3D::link(const gl::Context *context,
Jamie Madillc9727f32017-11-07 12:37:07 -05001483 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -04001484 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001485{
Jamie Madillc564c072017-06-01 12:45:42 -04001486 const auto &data = context->getContextState();
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001487
Jamie Madill62d31cb2015-09-11 13:25:51 -04001488 reset();
1489
Jamie Madillbd044ed2017-06-05 12:59:21 -04001490 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001491 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001492 {
Xinghua Caob1239382016-12-13 15:07:05 +08001493 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1494
Jamie Madillbd044ed2017-06-05 12:59:21 -04001495 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001496
Jamie Madill9cf9e872017-06-05 12:59:25 -04001497 gl::LinkResult result = compileComputeExecutable(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001498 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001499 {
Xinghua Caob1239382016-12-13 15:07:05 +08001500 infoLog << result.getError().getMessage();
1501 return result;
1502 }
1503 else if (!result.getResult())
1504 {
1505 infoLog << "Failed to create D3D compute shader.";
1506 return result;
1507 }
1508
Jamie Madillbd044ed2017-06-05 12:59:21 -04001509 initUniformBlockInfo(context, computeShader);
Xinghua Caob1239382016-12-13 15:07:05 +08001510 }
1511 else
1512 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001513 gl::Shader *vertexShader = mState.getAttachedVertexShader();
1514 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001515
1516 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1517 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1518
1519 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1520 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1521
1522 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1523 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1524
1525 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1526 {
1527 if (fragmentShaderD3D->usesFrontFacing())
1528 {
1529 infoLog << "The current renderer doesn't support gl_FrontFacing";
1530 return false;
1531 }
1532 }
1533
1534 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1535 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1536 // intelligently, but D3D9 assumes one semantic per register.
1537 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madillc9727f32017-11-07 12:37:07 -05001538 resources.varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Xinghua Caob1239382016-12-13 15:07:05 +08001539 {
1540 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001541 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001542 }
Xinghua Caob1239382016-12-13 15:07:05 +08001543
1544 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madillc9727f32017-11-07 12:37:07 -05001545 BuiltinVaryingsD3D builtins(metadata, resources.varyingPacking);
Xinghua Caob1239382016-12-13 15:07:05 +08001546
Jamie Madillc9727f32017-11-07 12:37:07 -05001547 mDynamicHLSL->generateShaderLinkHLSL(context, mState, metadata, resources.varyingPacking,
1548 builtins, &mPixelHLSL, &mVertexHLSL);
Xinghua Caob1239382016-12-13 15:07:05 +08001549
1550 mUsesPointSize = vertexShaderD3D->usesPointSize();
1551 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1552 mUsesFragDepth = metadata.usesFragDepth();
Martin Radev41ac68e2017-06-06 12:16:58 +03001553 mUsesViewID = metadata.usesViewID();
1554 mHasANGLEMultiviewEnabled = metadata.hasANGLEMultiviewEnabled();
Xinghua Caob1239382016-12-13 15:07:05 +08001555
1556 // Cache if we use flat shading
Jamie Madillbd044ed2017-06-05 12:59:21 -04001557 mUsesFlatInterpolation =
Jiawei Shao3d404882017-10-16 13:30:48 +08001558 (FindFlatInterpolationVarying(fragmentShader->getInputVaryings(context)) ||
1559 FindFlatInterpolationVarying(vertexShader->getOutputVaryings(context)));
Xinghua Caob1239382016-12-13 15:07:05 +08001560
1561 if (mRenderer->getMajorShaderModel() >= 4)
1562 {
Martin Radev41ac68e2017-06-06 12:16:58 +03001563 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(
Jamie Madillc9727f32017-11-07 12:37:07 -05001564 resources.varyingPacking, builtins, mHasANGLEMultiviewEnabled,
Martin Radevc1d4e552017-08-21 12:01:10 +03001565 metadata.canSelectViewInVertexShader());
Xinghua Caob1239382016-12-13 15:07:05 +08001566 }
1567
Jamie Madillbd044ed2017-06-05 12:59:21 -04001568 initAttribLocationsToD3DSemantic(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001569
Jamie Madillbd044ed2017-06-05 12:59:21 -04001570 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001571
Jamie Madillc9727f32017-11-07 12:37:07 -05001572 gatherTransformFeedbackVaryings(resources.varyingPacking, builtins[SHADER_VERTEX]);
Xinghua Caob1239382016-12-13 15:07:05 +08001573
Jamie Madill9cf9e872017-06-05 12:59:25 -04001574 gl::LinkResult result = compileProgramExecutables(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001575 if (result.isError())
1576 {
1577 infoLog << result.getError().getMessage();
1578 return result;
1579 }
1580 else if (!result.getResult())
1581 {
1582 infoLog << "Failed to create D3D shaders.";
1583 return result;
1584 }
1585
Jamie Madillbd044ed2017-06-05 12:59:21 -04001586 initUniformBlockInfo(context, vertexShader);
1587 initUniformBlockInfo(context, fragmentShader);
Austin Kinross02df7962015-07-01 10:03:42 -07001588 }
1589
Jamie Madillb0a838b2016-11-13 20:02:12 -05001590 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001591}
1592
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001593GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001594{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001595 // TODO(jmadill): Do something useful here?
1596 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001597}
1598
Jamie Madillbd044ed2017-06-05 12:59:21 -04001599void ProgramD3D::initUniformBlockInfo(const gl::Context *context, gl::Shader *shader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001600{
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001601 for (const sh::InterfaceBlock &interfaceBlock : shader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001602 {
Xinghua Caob1239382016-12-13 15:07:05 +08001603 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001604 continue;
1605
Xinghua Caob1239382016-12-13 15:07:05 +08001606 if (mBlockDataSizes.count(interfaceBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001607 continue;
1608
Xinghua Caob1239382016-12-13 15:07:05 +08001609 size_t dataSize = getUniformBlockInfo(interfaceBlock);
1610 mBlockDataSizes[interfaceBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001611 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001612}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001613
Jamie Madill51f522f2016-12-21 15:10:55 -05001614void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001615{
Jamie Madill51f522f2016-12-21 15:10:55 -05001616 // Lazy init.
1617 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1618 {
1619 return;
1620 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001621
1622 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001623 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1624 const ShaderD3D *fragmentShaderD3D =
1625 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1626 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001627
Jiajia Qin729b2c62017-08-14 09:36:11 +08001628 for (const gl::InterfaceBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001629 {
1630 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1631
Jamie Madill4a3c2342015-10-08 12:58:45 -04001632 D3DUniformBlock d3dUniformBlock;
1633
Jamie Madill62d31cb2015-09-11 13:25:51 -04001634 if (uniformBlock.vertexStaticUse)
1635 {
Xinghua Caob1239382016-12-13 15:07:05 +08001636 ASSERT(vertexShaderD3D != nullptr);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001637 unsigned int baseRegister = vertexShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001638 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001639 }
1640
1641 if (uniformBlock.fragmentStaticUse)
1642 {
Xinghua Caob1239382016-12-13 15:07:05 +08001643 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001644 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001645 fragmentShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001646 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001647 }
1648
Xinghua Caob1239382016-12-13 15:07:05 +08001649 if (uniformBlock.computeStaticUse)
1650 {
1651 ASSERT(computeShaderD3D != nullptr);
1652 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001653 computeShaderD3D->getUniformBlockRegister(uniformBlock.name);
Xinghua Caob1239382016-12-13 15:07:05 +08001654 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1655 }
1656
Jamie Madill4a3c2342015-10-08 12:58:45 -04001657 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001658 }
1659}
1660
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001661void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001662{
1663 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001664 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001665 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001666 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001667 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001668 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001669 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001670 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001671 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001672 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001673 vertexRegisters = std::max(vertexRegisters,
1674 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001675 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001676 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001677 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001678 fragmentRegisters = std::max(
1679 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001680 }
Xinghua Caob1239382016-12-13 15:07:05 +08001681 if (d3dUniform->isReferencedByComputeShader())
1682 {
1683 computeRegisters = std::max(
1684 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1685 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001686 }
1687 }
1688
Xinghua Caob1239382016-12-13 15:07:05 +08001689 mVertexUniformStorage =
1690 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1691 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1692 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1693 mComputeUniformStorage =
1694 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001695
1696 // Iterate the uniforms again to assign data pointers to default block uniforms.
1697 for (D3DUniform *d3dUniform : mD3DUniforms)
1698 {
1699 if (d3dUniform->isSampler())
1700 {
1701 d3dUniform->mSamplerData.resize(d3dUniform->elementCount(), 0);
1702 continue;
1703 }
1704
1705 if (d3dUniform->isReferencedByVertexShader())
1706 {
1707 d3dUniform->vsData = mVertexUniformStorage->getDataPointer(d3dUniform->vsRegisterIndex,
1708 d3dUniform->registerElement);
1709 }
1710
1711 if (d3dUniform->isReferencedByFragmentShader())
1712 {
1713 d3dUniform->psData = mFragmentUniformStorage->getDataPointer(
1714 d3dUniform->psRegisterIndex, d3dUniform->registerElement);
1715 }
1716
1717 if (d3dUniform->isReferencedByComputeShader())
1718 {
1719 d3dUniform->csData = mComputeUniformStorage->getDataPointer(
1720 d3dUniform->csRegisterIndex, d3dUniform->registerElement);
1721 }
1722 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001723}
1724
Jamie Madilld63961d2017-09-12 15:22:57 -04001725void ProgramD3D::updateUniformBufferCache(const gl::Caps &caps,
1726 unsigned int reservedVertex,
1727 unsigned int reservedFragment)
Brandon Jones18bd4102014-09-22 14:21:44 -07001728{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001729 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001730 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001731 return;
Jamie Madill4a3c2342015-10-08 12:58:45 -04001732 }
1733
Jamie Madill51f522f2016-12-21 15:10:55 -05001734 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001735
Jamie Madill03260fa2015-06-22 13:57:22 -04001736 mVertexUBOCache.clear();
1737 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001738
Jamie Madill4a3c2342015-10-08 12:58:45 -04001739 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001740 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001741 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001742 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001743 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001744
Brandon Jones18bd4102014-09-22 14:21:44 -07001745 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001746 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001747 {
1748 continue;
1749 }
1750
Jamie Madill4a3c2342015-10-08 12:58:45 -04001751 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001752 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001753 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedVertex;
1754 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001755
Jamie Madill969194d2015-07-20 14:36:56 -04001756 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001757 {
1758 mVertexUBOCache.resize(registerIndex + 1, -1);
1759 }
1760
1761 ASSERT(mVertexUBOCache[registerIndex] == -1);
1762 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001763 }
1764
Jamie Madill4a3c2342015-10-08 12:58:45 -04001765 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001766 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001767 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedFragment;
1768 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001769
1770 if (mFragmentUBOCache.size() <= registerIndex)
1771 {
1772 mFragmentUBOCache.resize(registerIndex + 1, -1);
1773 }
1774
1775 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1776 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001777 }
1778 }
Jamie Madilld63961d2017-09-12 15:22:57 -04001779}
Brandon Jones18bd4102014-09-22 14:21:44 -07001780
Jamie Madilld63961d2017-09-12 15:22:57 -04001781const std::vector<GLint> &ProgramD3D::getVertexUniformBufferCache() const
1782{
1783 return mVertexUBOCache;
1784}
1785
1786const std::vector<GLint> &ProgramD3D::getFragmentUniformBufferCache() const
1787{
1788 return mFragmentUBOCache;
Brandon Jones18bd4102014-09-22 14:21:44 -07001789}
1790
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001791void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001792{
Jamie Madill4148fd72017-09-14 15:46:20 -04001793 mVertexUniformsDirty = true;
1794 mFragmentUniformsDirty = true;
1795 mComputeUniformsDirty = true;
1796}
1797
1798void ProgramD3D::markUniformsClean()
1799{
1800 mVertexUniformsDirty = false;
1801 mFragmentUniformsDirty = false;
1802 mComputeUniformsDirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001803}
1804
Jamie Madill334d6152015-10-22 14:00:28 -04001805void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001806{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001807 setUniformInternal(location, count, v, GL_FLOAT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001808}
1809
1810void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1811{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001812 setUniformInternal(location, count, v, GL_FLOAT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001813}
1814
1815void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1816{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001817 setUniformInternal(location, count, v, GL_FLOAT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001818}
1819
1820void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1821{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001822 setUniformInternal(location, count, v, GL_FLOAT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001823}
1824
Jamie Madill334d6152015-10-22 14:00:28 -04001825void ProgramD3D::setUniformMatrix2fv(GLint location,
1826 GLsizei count,
1827 GLboolean transpose,
1828 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001829{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001830 setUniformMatrixfvInternal<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001831}
1832
Jamie Madill334d6152015-10-22 14:00:28 -04001833void ProgramD3D::setUniformMatrix3fv(GLint location,
1834 GLsizei count,
1835 GLboolean transpose,
1836 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001837{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001838 setUniformMatrixfvInternal<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001839}
1840
Jamie Madill334d6152015-10-22 14:00:28 -04001841void ProgramD3D::setUniformMatrix4fv(GLint location,
1842 GLsizei count,
1843 GLboolean transpose,
1844 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001845{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001846 setUniformMatrixfvInternal<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001847}
1848
Jamie Madill334d6152015-10-22 14:00:28 -04001849void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1850 GLsizei count,
1851 GLboolean transpose,
1852 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001853{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001854 setUniformMatrixfvInternal<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001855}
1856
Jamie Madill334d6152015-10-22 14:00:28 -04001857void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1858 GLsizei count,
1859 GLboolean transpose,
1860 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001861{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001862 setUniformMatrixfvInternal<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001863}
1864
Jamie Madill334d6152015-10-22 14:00:28 -04001865void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1866 GLsizei count,
1867 GLboolean transpose,
1868 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001869{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001870 setUniformMatrixfvInternal<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001871}
1872
Jamie Madill334d6152015-10-22 14:00:28 -04001873void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1874 GLsizei count,
1875 GLboolean transpose,
1876 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001877{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001878 setUniformMatrixfvInternal<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001879}
1880
Jamie Madill334d6152015-10-22 14:00:28 -04001881void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1882 GLsizei count,
1883 GLboolean transpose,
1884 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001885{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001886 setUniformMatrixfvInternal<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001887}
1888
Jamie Madill334d6152015-10-22 14:00:28 -04001889void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1890 GLsizei count,
1891 GLboolean transpose,
1892 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001893{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001894 setUniformMatrixfvInternal<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001895}
1896
1897void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1898{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001899 setUniformInternal(location, count, v, GL_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001900}
1901
1902void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1903{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001904 setUniformInternal(location, count, v, GL_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001905}
1906
1907void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1908{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001909 setUniformInternal(location, count, v, GL_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001910}
1911
1912void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1913{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001914 setUniformInternal(location, count, v, GL_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001915}
1916
1917void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1918{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001919 setUniformInternal(location, count, v, GL_UNSIGNED_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001920}
1921
1922void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1923{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001924 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001925}
1926
1927void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1928{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001929 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001930}
1931
1932void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1933{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001934 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001935}
1936
Jamie Madill4a3c2342015-10-08 12:58:45 -04001937void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1938 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001939{
1940}
1941
Jamie Madillbd044ed2017-06-05 12:59:21 -04001942void ProgramD3D::defineUniformsAndAssignRegisters(const gl::Context *context)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001944 D3DUniformMap uniformMap;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001945 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001946 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05001947 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001948 for (const sh::Uniform &computeUniform : computeShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04001949 {
Xinghua Caob1239382016-12-13 15:07:05 +08001950 if (computeUniform.staticUse)
1951 {
1952 defineUniformBase(computeShader, computeUniform, &uniformMap);
1953 }
Jamie Madillfb536032015-09-11 13:19:49 -04001954 }
1955 }
Xinghua Caob1239382016-12-13 15:07:05 +08001956 else
Jamie Madillfb536032015-09-11 13:19:49 -04001957 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001958 gl::Shader *vertexShader = mState.getAttachedVertexShader();
1959 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04001960 {
Xinghua Caob1239382016-12-13 15:07:05 +08001961 if (vertexUniform.staticUse)
1962 {
1963 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
1964 }
1965 }
1966
Jamie Madillbd044ed2017-06-05 12:59:21 -04001967 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1968 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms(context))
Xinghua Caob1239382016-12-13 15:07:05 +08001969 {
1970 if (fragmentUniform.staticUse)
1971 {
1972 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
1973 }
Jamie Madillfb536032015-09-11 13:19:49 -04001974 }
1975 }
1976
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001977 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001978 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001979 {
1980 if (!glUniform.isInDefaultBlock())
1981 continue;
1982
Olli Etuahod2551232017-10-26 20:03:33 +03001983 std::string name = glUniform.name;
1984 if (glUniform.isArray())
1985 {
1986 // In the program state, array uniform names include [0] as in the program resource
1987 // spec. Here we don't include it.
1988 // TODO(oetuaho@nvidia.com): consider using the same uniform naming here as in the GL
1989 // layer.
1990 ASSERT(angle::EndsWith(name, "[0]"));
1991 name.resize(name.length() - 3);
1992 }
1993 auto mapEntry = uniformMap.find(name);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001994 ASSERT(mapEntry != uniformMap.end());
1995 mD3DUniforms.push_back(mapEntry->second);
1996 }
1997
Jamie Madill62d31cb2015-09-11 13:25:51 -04001998 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001999 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04002000}
2001
Jamie Madill91445bc2015-09-23 16:47:53 -04002002void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002003 const sh::Uniform &uniform,
2004 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04002005{
Olli Etuaho96963162016-03-21 11:54:33 +02002006 // Samplers get their registers assigned in assignAllSamplerRegisters.
2007 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04002008 {
Jamie Madill91445bc2015-09-23 16:47:53 -04002009 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04002010 return;
2011 }
2012
Jamie Madill91445bc2015-09-23 16:47:53 -04002013 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
2014
2015 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
2016 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04002017 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002018 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002019
Jamie Madill91445bc2015-09-23 16:47:53 -04002020 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002021}
2022
Jamie Madill62d31cb2015-09-11 13:25:51 -04002023D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
2024{
2025 for (D3DUniform *d3dUniform : mD3DUniforms)
2026 {
2027 if (d3dUniform->name == name)
2028 {
2029 return d3dUniform;
2030 }
2031 }
2032
2033 return nullptr;
2034}
2035
Jamie Madill91445bc2015-09-23 16:47:53 -04002036void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002037 const sh::ShaderVariable &uniform,
2038 const std::string &fullName,
2039 sh::HLSLBlockEncoder *encoder,
2040 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002041{
2042 if (uniform.isStruct())
2043 {
2044 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2045 {
2046 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2047
Jamie Madill55def582015-05-04 11:24:57 -04002048 if (encoder)
2049 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002050
2051 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2052 {
Jamie Madill334d6152015-10-22 14:00:28 -04002053 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002054 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2055
Olli Etuaho96963162016-03-21 11:54:33 +02002056 // Samplers get their registers assigned in assignAllSamplerRegisters.
2057 // Also they couldn't use the same encoder as the rest of the struct, since they are
2058 // extracted out of the struct by the shader translator.
2059 if (gl::IsSamplerType(field.type))
2060 {
2061 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2062 }
2063 else
2064 {
2065 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2066 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002067 }
2068
Jamie Madill55def582015-05-04 11:24:57 -04002069 if (encoder)
2070 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002071 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002072 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002073 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002074
2075 // Not a struct. Arrays are treated as aggregate types.
2076 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002077 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002078 encoder->enterAggregateType();
2079 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002080
Jamie Madill62d31cb2015-09-11 13:25:51 -04002081 // Advance the uniform offset, to track registers allocation for structs
2082 sh::BlockMemberInfo blockInfo =
2083 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2084 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002085
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002086 auto uniformMapEntry = uniformMap->find(fullName);
2087 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002088
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002089 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002090 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002091 d3dUniform = uniformMapEntry->second;
2092 }
2093 else
2094 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002095 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002096 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002097 }
2098
2099 if (encoder)
2100 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002101 d3dUniform->registerElement =
2102 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002103 unsigned int reg =
2104 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002105 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002106 {
2107 d3dUniform->psRegisterIndex = reg;
2108 }
Xinghua Caob1239382016-12-13 15:07:05 +08002109 else if (shaderType == GL_VERTEX_SHADER)
2110 {
2111 d3dUniform->vsRegisterIndex = reg;
2112 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002113 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002114 {
Xinghua Caob1239382016-12-13 15:07:05 +08002115 ASSERT(shaderType == GL_COMPUTE_SHADER);
2116 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002117 }
Jamie Madillfb536032015-09-11 13:19:49 -04002118
2119 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002120 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002121 {
2122 encoder->exitAggregateType();
2123 }
2124 }
2125}
2126
Jamie Madill134f93d2017-08-31 17:11:00 -04002127// Assume count is already clamped.
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002128template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002129void ProgramD3D::setUniformImpl(const gl::VariableLocation &locationInfo,
Jamie Madill134f93d2017-08-31 17:11:00 -04002130 GLsizei count,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002131 const T *v,
2132 uint8_t *targetData,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002133 GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002134{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002135 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
Jamie Madill33bb7c42017-09-09 23:32:51 -04002136 const int components = targetUniform->typeInfo.componentCount;
Olli Etuaho1734e172017-10-27 15:30:27 +03002137 const unsigned int arrayElementOffset = locationInfo.arrayIndex;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002138
Jamie Madill33bb7c42017-09-09 23:32:51 -04002139 if (targetUniform->typeInfo.type == uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002140 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002141 T *dest = reinterpret_cast<T *>(targetData) + arrayElementOffset * 4;
Jamie Madill33bb7c42017-09-09 23:32:51 -04002142 const T *source = v;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002143
Jamie Madill33bb7c42017-09-09 23:32:51 -04002144 for (GLint i = 0; i < count; i++, dest += 4, source += components)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002145 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002146 memcpy(dest, source, components * sizeof(T));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002147 }
2148 }
Jamie Madill33bb7c42017-09-09 23:32:51 -04002149 else
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002150 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002151 ASSERT(targetUniform->typeInfo.type == gl::VariableBoolVectorType(uniformType));
Olli Etuahoc8538042017-09-27 11:20:15 +03002152 GLint *boolParams = reinterpret_cast<GLint *>(targetData) + arrayElementOffset * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002153
Jamie Madill134f93d2017-08-31 17:11:00 -04002154 for (GLint i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002155 {
Jamie Madill334d6152015-10-22 14:00:28 -04002156 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002157 const T *source = v + (i * components);
2158
2159 for (int c = 0; c < components; c++)
2160 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002161 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002162 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002163 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002164 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002165}
2166
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002167template <typename T>
Jamie Madill33bb7c42017-09-09 23:32:51 -04002168void ProgramD3D::setUniformInternal(GLint location, GLsizei count, const T *v, GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002169{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002170 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2171 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
2172
Jamie Madill33bb7c42017-09-09 23:32:51 -04002173 if (targetUniform->typeInfo.isSampler)
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002174 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002175 ASSERT(uniformType == GL_INT);
Jamie Madill80823cc2017-09-14 15:46:21 -04002176 size_t size = count * sizeof(T);
Olli Etuaho1734e172017-10-27 15:30:27 +03002177 auto dest = &targetUniform->mSamplerData[locationInfo.arrayIndex];
Jamie Madill80823cc2017-09-14 15:46:21 -04002178 if (memcmp(dest, v, size) != 0)
2179 {
2180 memcpy(dest, v, size);
2181 mDirtySamplerMapping = true;
2182 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002183 return;
2184 }
2185
2186 if (targetUniform->vsData)
2187 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002188 setUniformImpl(locationInfo, count, v, targetUniform->vsData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002189 mVertexUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002190 }
2191
2192 if (targetUniform->psData)
2193 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002194 setUniformImpl(locationInfo, count, v, targetUniform->psData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002195 mFragmentUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002196 }
2197
2198 if (targetUniform->csData)
2199 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002200 setUniformImpl(locationInfo, count, v, targetUniform->csData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002201 mComputeUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002202 }
2203}
2204
2205template <int cols, int rows>
Jamie Madill80823cc2017-09-14 15:46:21 -04002206bool ProgramD3D::setUniformMatrixfvImpl(GLint location,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002207 GLsizei countIn,
2208 GLboolean transpose,
2209 const GLfloat *value,
2210 uint8_t *targetData,
2211 GLenum targetUniformType)
2212{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002213 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002214
Jamie Madill62d31cb2015-09-11 13:25:51 -04002215 unsigned int elementCount = targetUniform->elementCount();
Olli Etuaho1734e172017-10-27 15:30:27 +03002216 unsigned int arrayElementOffset = mState.getUniformLocations()[location].arrayIndex;
Olli Etuahoc8538042017-09-27 11:20:15 +03002217 unsigned int count =
2218 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002219
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002220 const unsigned int targetMatrixStride = (4 * rows);
Olli Etuahoc8538042017-09-27 11:20:15 +03002221 GLfloat *target = reinterpret_cast<GLfloat *>(
2222 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002223
Jamie Madill80823cc2017-09-14 15:46:21 -04002224 bool dirty = false;
2225
Jamie Madill62d31cb2015-09-11 13:25:51 -04002226 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002227 {
2228 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2229 if (transpose == GL_FALSE)
2230 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002231 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002232 }
2233 else
2234 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002235 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002236 }
2237 target += targetMatrixStride;
2238 value += cols * rows;
2239 }
Jamie Madill80823cc2017-09-14 15:46:21 -04002240
2241 return dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002242}
2243
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002244template <int cols, int rows>
2245void ProgramD3D::setUniformMatrixfvInternal(GLint location,
2246 GLsizei countIn,
2247 GLboolean transpose,
2248 const GLfloat *value,
2249 GLenum targetUniformType)
2250{
2251 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
2252
2253 if (targetUniform->vsData)
2254 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002255 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2256 targetUniform->vsData, targetUniformType))
2257 {
2258 mVertexUniformsDirty = true;
2259 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002260 }
2261
2262 if (targetUniform->psData)
2263 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002264 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2265 targetUniform->psData, targetUniformType))
2266 {
2267 mFragmentUniformsDirty = true;
2268 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002269 }
2270
2271 if (targetUniform->csData)
2272 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002273 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2274 targetUniform->csData, targetUniformType))
2275 {
2276 mComputeUniformsDirty = true;
2277 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002278 }
2279}
2280
Jamie Madill4a3c2342015-10-08 12:58:45 -04002281size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002282{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002283 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002284
Jamie Madill62d31cb2015-09-11 13:25:51 -04002285 // define member uniforms
2286 sh::Std140BlockEncoder std140Encoder;
Jamie Madillcc2ed612017-03-14 15:59:00 -04002287 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002288 sh::BlockLayoutEncoder *encoder = nullptr;
2289
Qin Jiajiaca68d982017-09-18 16:41:56 +08002290 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STD140)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002291 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002292 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002293 }
2294 else
2295 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002296 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002297 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002298
Jamie Madill1bfa6b72017-10-13 14:07:45 -04002299 sh::GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2300 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002301
2302 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002303}
2304
Jamie Madill62d31cb2015-09-11 13:25:51 -04002305void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002306{
Olli Etuaho96963162016-03-21 11:54:33 +02002307 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002308 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002309 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002310 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002311 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002312 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002313 }
2314}
2315
Olli Etuaho96963162016-03-21 11:54:33 +02002316void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002317{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002318 ASSERT(d3dUniform->isSampler());
Xinghua Caob1239382016-12-13 15:07:05 +08002319 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2320 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002321 {
Xinghua Caob1239382016-12-13 15:07:05 +08002322 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2323 ASSERT(computeShaderD3D->hasUniform(d3dUniform));
2324 d3dUniform->csRegisterIndex = computeShaderD3D->getUniformRegister(d3dUniform->name);
2325 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
Jamie Madill33bb7c42017-09-09 23:32:51 -04002326 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->typeInfo, d3dUniform->arraySize,
Xinghua Caob1239382016-12-13 15:07:05 +08002327 mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002328 }
Xinghua Caob1239382016-12-13 15:07:05 +08002329 else
Jamie Madillfb536032015-09-11 13:19:49 -04002330 {
Xinghua Caob1239382016-12-13 15:07:05 +08002331 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2332 const ShaderD3D *fragmentShaderD3D =
2333 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2334 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) ||
2335 fragmentShaderD3D->hasUniform(d3dUniform));
2336 if (vertexShaderD3D->hasUniform(d3dUniform))
2337 {
2338 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2339 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Jamie Madill33bb7c42017-09-09 23:32:51 -04002340 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->typeInfo, d3dUniform->arraySize,
Xinghua Caob1239382016-12-13 15:07:05 +08002341 mSamplersVS, &mUsedVertexSamplerRange);
2342 }
2343 if (fragmentShaderD3D->hasUniform(d3dUniform))
2344 {
2345 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2346 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madill33bb7c42017-09-09 23:32:51 -04002347 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->typeInfo, d3dUniform->arraySize,
Xinghua Caob1239382016-12-13 15:07:05 +08002348 mSamplersPS, &mUsedPixelSamplerRange);
2349 }
Jamie Madillfb536032015-09-11 13:19:49 -04002350 }
2351}
2352
Jamie Madill62d31cb2015-09-11 13:25:51 -04002353// static
2354void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002355 const gl::UniformTypeInfo &typeInfo,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002356 unsigned int samplerCount,
2357 std::vector<Sampler> &outSamplers,
2358 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002359{
2360 unsigned int samplerIndex = startSamplerIndex;
2361
2362 do
2363 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002364 ASSERT(samplerIndex < outSamplers.size());
2365 Sampler *sampler = &outSamplers[samplerIndex];
2366 sampler->active = true;
Jamie Madill33bb7c42017-09-09 23:32:51 -04002367 sampler->textureType = typeInfo.samplerTextureType;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002368 sampler->logicalTextureUnit = 0;
2369 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002370 samplerIndex++;
2371 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002372}
2373
Brandon Jonesc9610c52014-08-25 17:02:59 -07002374void ProgramD3D::reset()
2375{
Xinghua Caob1239382016-12-13 15:07:05 +08002376 mVertexExecutables.clear();
2377 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002378
Xinghua Caob1239382016-12-13 15:07:05 +08002379 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002380 {
Xinghua Caob1239382016-12-13 15:07:05 +08002381 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002382 }
Brandon Joneseb994362014-09-24 10:27:28 -07002383
Xinghua Caob1239382016-12-13 15:07:05 +08002384 mComputeExecutable.reset(nullptr);
2385
Brandon Jones22502d52014-08-29 16:58:36 -07002386 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002387 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002388
2389 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002390 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002391 mUsesFragDepth = false;
Martin Radev41ac68e2017-06-06 12:16:58 +03002392 mHasANGLEMultiviewEnabled = false;
2393 mUsesViewID = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002394 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002395 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002396 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002397
Jamie Madill62d31cb2015-09-11 13:25:51 -04002398 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002399 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002400
Xinghua Caob1239382016-12-13 15:07:05 +08002401 mVertexUniformStorage.reset(nullptr);
2402 mFragmentUniformStorage.reset(nullptr);
2403 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002404
2405 mSamplersPS.clear();
2406 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002407 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002408
2409 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002410 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002411 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002412 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002413
Jamie Madill8047c0d2016-03-07 13:02:12 -05002414 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002415
Jamie Madill9fc36822015-11-18 13:08:07 -05002416 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002417
2418 mGeometryShaderPreamble.clear();
Jamie Madill561ed3a2017-08-31 16:48:09 -04002419
Jamie Madill4148fd72017-09-14 15:46:20 -04002420 dirtyAllUniforms();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002421
2422 mCachedPixelExecutableIndex.reset();
2423 mCachedVertexExecutableIndex.reset();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002424}
2425
Geoff Lang7dd2e102014-11-10 15:19:26 -05002426unsigned int ProgramD3D::getSerial() const
2427{
2428 return mSerial;
2429}
2430
2431unsigned int ProgramD3D::issueSerial()
2432{
2433 return mCurrentSerial++;
2434}
2435
Jamie Madillbd044ed2017-06-05 12:59:21 -04002436void ProgramD3D::initAttribLocationsToD3DSemantic(const gl::Context *context)
Jamie Madill63805b42015-08-25 13:17:39 -04002437{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002438 gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002439 ASSERT(vertexShader != nullptr);
2440
2441 // Init semantic index
Jamie Madill34ca4f52017-06-13 11:49:39 -04002442 int semanticIndex = 0;
2443 for (const sh::Attribute &attribute : vertexShader->getActiveAttributes(context))
Jamie Madill63805b42015-08-25 13:17:39 -04002444 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002445 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill34ca4f52017-06-13 11:49:39 -04002446 GLuint location = mState.getAttributeLocation(attribute.name);
2447 ASSERT(location != std::numeric_limits<GLuint>::max());
Jamie Madill63805b42015-08-25 13:17:39 -04002448
Jamie Madill8047c0d2016-03-07 13:02:12 -05002449 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002450 {
Jamie Madill34ca4f52017-06-13 11:49:39 -04002451 mAttribLocationToD3DSemantic[location + reg] = semanticIndex++;
Jamie Madill63805b42015-08-25 13:17:39 -04002452 }
2453 }
Jamie Madill437d2662014-12-05 14:23:35 -05002454}
2455
Jamie Madilla779b612017-07-24 11:46:05 -04002456void ProgramD3D::updateCachedInputLayout(Serial associatedSerial, const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002457{
Jamie Madilla779b612017-07-24 11:46:05 -04002458 if (mCurrentVertexArrayStateSerial == associatedSerial)
2459 {
2460 return;
2461 }
2462
2463 mCurrentVertexArrayStateSerial = associatedSerial;
Jamie Madillbd136f92015-08-10 14:51:37 -04002464 mCachedInputLayout.clear();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002465
Jamie Madilld3dfda22015-07-06 08:28:49 -04002466 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002467
Jamie Madill6de51852017-04-12 09:53:01 -04002468 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002469 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002470 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002471
Jamie Madill8047c0d2016-03-07 13:02:12 -05002472 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002473 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002474 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002475 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002476 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002477 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002478 mCachedInputLayout[d3dSemantic] =
2479 GetVertexFormatType(vertexAttributes[locationIndex],
2480 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002481 }
2482 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002483
2484 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04002485
2486 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002487}
2488
2489void ProgramD3D::updateCachedOutputLayout(const gl::Context *context,
2490 const gl::Framebuffer *framebuffer)
2491{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002492 mPixelShaderOutputLayoutCache.clear();
2493
2494 FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(framebuffer);
2495 const auto &colorbuffers = fboD3D->getColorAttachmentsForRender(context);
2496
2497 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
2498 {
2499 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
2500
2501 if (colorbuffer)
2502 {
2503 auto binding = colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0
2504 : colorbuffer->getBinding();
2505 mPixelShaderOutputLayoutCache.push_back(binding);
2506 }
2507 else
2508 {
2509 mPixelShaderOutputLayoutCache.push_back(GL_NONE);
2510 }
2511 }
2512
2513 updateCachedPixelExecutableIndex();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002514}
2515
Jamie Madill192745a2016-12-22 15:58:21 -05002516void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2517 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002518{
Jamie Madill9fc36822015-11-18 13:08:07 -05002519 const std::string &varyingSemantic =
2520 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2521
Jamie Madillccdf74b2015-08-18 10:46:12 -04002522 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002523 mStreamOutVaryings.clear();
2524
Jamie Madill48ef11b2016-04-27 15:21:52 -04002525 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002526 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2527 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002528 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002529 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2530 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002531 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002532 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002533 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002534 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2535 builtins.glPosition.index, 4, outputSlot));
2536 }
2537 }
2538 else if (tfVaryingName == "gl_FragCoord")
2539 {
2540 if (builtins.glFragCoord.enabled)
2541 {
2542 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2543 builtins.glFragCoord.index, 4, outputSlot));
2544 }
2545 }
2546 else if (tfVaryingName == "gl_PointSize")
2547 {
2548 if (builtins.glPointSize.enabled)
2549 {
2550 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2551 }
2552 }
2553 else
2554 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002555 std::vector<unsigned int> subscripts;
2556 std::string baseName = gl::ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002557 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002558 if (!subscripts.empty())
2559 {
2560 subscript = subscripts.back();
2561 }
Jamie Madill192745a2016-12-22 15:58:21 -05002562 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002563 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002564 const auto &varying = *registerInfo.packedVarying->varying;
2565 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002566 int componentCount = gl::VariableColumnCount(transposedType);
2567 ASSERT(!varying.isBuiltIn());
2568
Jamie Madill55c25d02015-11-18 13:08:08 -05002569 // Transform feedback for varying structs is underspecified.
2570 // See Khronos bug 9856.
2571 // TODO(jmadill): Figure out how to be spec-compliant here.
2572 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2573 continue;
2574
Jamie Madill9fc36822015-11-18 13:08:07 -05002575 // There can be more than one register assigned to a particular varying, and each
2576 // register needs its own stream out entry.
jchen10a9042d32017-03-17 08:50:45 +08002577 if (baseName == registerInfo.packedVarying->varying->name &&
2578 (subscript == GL_INVALID_INDEX || subscript == registerInfo.varyingArrayIndex))
Jamie Madill9fc36822015-11-18 13:08:07 -05002579 {
2580 mStreamOutVaryings.push_back(D3DVarying(
2581 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2582 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002583 }
2584 }
2585 }
2586}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002587
2588D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2589{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002590 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002591}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002592
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002593const D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location) const
2594{
2595 return mD3DUniforms[mState.getUniformLocations()[location].index];
2596}
2597
Olli Etuaho855d9642017-05-17 14:05:06 +03002598bool ProgramD3D::getUniformBlockSize(const std::string &blockName,
2599 const std::string & /* blockMappedName */,
2600 size_t *sizeOut) const
Jamie Madill4a3c2342015-10-08 12:58:45 -04002601{
Olli Etuahod2551232017-10-26 20:03:33 +03002602 size_t nameLengthWithoutArrayIndex;
2603 gl::ParseArrayIndex(blockName, &nameLengthWithoutArrayIndex);
2604 std::string baseName = blockName.substr(0u, nameLengthWithoutArrayIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002605
2606 auto sizeIter = mBlockDataSizes.find(baseName);
2607 if (sizeIter == mBlockDataSizes.end())
2608 {
2609 *sizeOut = 0;
2610 return false;
2611 }
2612
2613 *sizeOut = sizeIter->second;
2614 return true;
2615}
2616
2617bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
Olli Etuaho855d9642017-05-17 14:05:06 +03002618 const std::string & /* memberUniformMappedName */,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002619 sh::BlockMemberInfo *memberInfoOut) const
2620{
2621 auto infoIter = mBlockInfo.find(memberUniformName);
2622 if (infoIter == mBlockInfo.end())
2623 {
2624 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2625 return false;
2626 }
2627
2628 *memberInfoOut = infoIter->second;
2629 return true;
2630}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002631
2632void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2633 GLenum genMode,
2634 GLint components,
2635 const GLfloat *coeffs)
2636{
2637 UNREACHABLE();
2638}
2639
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002640bool ProgramD3D::hasVertexExecutableForCachedInputLayout()
2641{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002642 return mCachedVertexExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002643}
2644
2645bool ProgramD3D::hasGeometryExecutableForPrimitiveType(GLenum drawMode)
2646{
2647 if (!usesGeometryShader(drawMode))
2648 {
2649 // No shader necessary mean we have the required (null) executable.
2650 return true;
2651 }
2652
2653 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
2654 return mGeometryExecutables[geometryShaderType].get() != nullptr;
2655}
2656
2657bool ProgramD3D::hasPixelExecutableForCachedOutputLayout()
2658{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002659 return mCachedPixelExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002660}
2661
Jamie Madill54164b02017-08-28 15:17:37 -04002662template <typename DestT>
2663void ProgramD3D::getUniformInternal(GLint location, DestT *dataOut) const
2664{
2665 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2666 const gl::LinkedUniform &uniform = mState.getUniforms()[locationInfo.index];
2667
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002668 const D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Olli Etuaho1734e172017-10-27 15:30:27 +03002669 const uint8_t *srcPointer = targetUniform->getDataPtrToElement(locationInfo.arrayIndex);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002670
2671 if (gl::IsMatrixType(uniform.type))
2672 {
2673 GetMatrixUniform(gl::VariableColumnCount(uniform.type), gl::VariableRowCount(uniform.type),
2674 dataOut, reinterpret_cast<const DestT *>(srcPointer));
2675 }
2676 else
2677 {
2678 memcpy(dataOut, srcPointer, uniform.getElementSize());
2679 }
Jamie Madill54164b02017-08-28 15:17:37 -04002680}
2681
2682void ProgramD3D::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
2683{
2684 getUniformInternal(location, params);
2685}
2686
2687void ProgramD3D::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
2688{
2689 getUniformInternal(location, params);
2690}
2691
2692void ProgramD3D::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
2693{
2694 getUniformInternal(location, params);
2695}
2696
Jamie Madill0e7f1732017-09-09 23:32:50 -04002697void ProgramD3D::updateCachedVertexExecutableIndex()
2698{
2699 mCachedVertexExecutableIndex.reset();
2700 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
2701 {
2702 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
2703 {
2704 mCachedVertexExecutableIndex = executableIndex;
2705 break;
2706 }
2707 }
2708}
2709
2710void ProgramD3D::updateCachedPixelExecutableIndex()
2711{
2712 mCachedPixelExecutableIndex.reset();
2713 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
2714 {
2715 if (mPixelExecutables[executableIndex]->matchesSignature(mPixelShaderOutputLayoutCache))
2716 {
2717 mCachedPixelExecutableIndex = executableIndex;
2718 break;
2719 }
2720 }
2721}
2722
Jamie Madill8047c0d2016-03-07 13:02:12 -05002723} // namespace rx