blob: b28a3eaaca0a53bfd840a95a41fdfc8b1ce997f8 [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Jamie Madill20e005b2017-04-07 14:19:22 -040011#include "common/bitset_utils.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Jamie Madillc564c072017-06-01 12:45:42 -040013#include "libANGLE/Context.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Framebuffer.h"
15#include "libANGLE/FramebufferAttachment.h"
16#include "libANGLE/Program.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040017#include "libANGLE/Uniform.h"
Jamie Madill192745a2016-12-22 15:58:21 -050018#include "libANGLE/VaryingPacking.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040019#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000020#include "libANGLE/features.h"
Jamie Madill54164b02017-08-28 15:17:37 -040021#include "libANGLE/queryconversions.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050022#include "libANGLE/renderer/ContextImpl.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050023#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050024#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050025#include "libANGLE/renderer/d3d/RendererD3D.h"
26#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050027#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050028#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050029
Jamie Madill01074252016-11-28 15:55:51 -050030using namespace angle;
31
Brandon Jonesc9610c52014-08-25 17:02:59 -070032namespace rx
33{
34
Brandon Joneseb994362014-09-24 10:27:28 -070035namespace
36{
37
Jamie Madill4c19a8a2017-07-24 11:46:06 -040038void GetDefaultInputLayoutFromShader(const gl::Context *context,
39 gl::Shader *vertexShader,
40 gl::InputLayout *inputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070041{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040042 inputLayoutOut->clear();
43
Jamie Madillbd044ed2017-06-05 12:59:21 -040044 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes(context))
Brandon Joneseb994362014-09-24 10:27:28 -070045 {
Brandon Joneseb994362014-09-24 10:27:28 -070046 if (shaderAttr.type != GL_NONE)
47 {
48 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
49
Jamie Madilld3dfda22015-07-06 08:28:49 -040050 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040051 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070052 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040053 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040054 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040055 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040056 gl::VertexFormatType defaultType =
57 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070058
Jamie Madill4c19a8a2017-07-24 11:46:06 -040059 inputLayoutOut->push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070060 }
61 }
62 }
63}
64
Jamie Madill4c19a8a2017-07-24 11:46:06 -040065void GetDefaultOutputLayoutFromShader(
66 const std::vector<PixelShaderOutputVariable> &shaderOutputVars,
67 std::vector<GLenum> *outputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070068{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040069 outputLayoutOut->clear();
Brandon Joneseb994362014-09-24 10:27:28 -070070
Jamie Madillb4463142014-12-19 14:56:54 -050071 if (!shaderOutputVars.empty())
72 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -040073 outputLayoutOut->push_back(GL_COLOR_ATTACHMENT0 +
74 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050075 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -040076}
Brandon Joneseb994362014-09-24 10:27:28 -070077
Brandon Jones1a8a7e32014-10-01 12:49:30 -070078bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
79{
80 return var.isRowMajorLayout;
81}
82
83bool IsRowMajorLayout(const sh::ShaderVariable &var)
84{
85 return false;
86}
87
Jamie Madill62d31cb2015-09-11 13:25:51 -040088template <typename VarT>
89void GetUniformBlockInfo(const std::vector<VarT> &fields,
90 const std::string &prefix,
91 sh::BlockLayoutEncoder *encoder,
92 bool inRowMajorLayout,
93 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
94{
95 for (const VarT &field : fields)
96 {
97 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
98
99 if (field.isStruct())
100 {
101 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
102
103 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
104 {
105 encoder->enterAggregateType();
106
107 const std::string uniformElementName =
108 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
109 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
110 blockInfoOut);
111
112 encoder->exitAggregateType();
113 }
114 }
115 else
116 {
117 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
118 (*blockInfoOut)[fieldName] =
119 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
120 }
121 }
122}
123
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800124template <typename T, int cols, int rows>
125bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400126{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800127 constexpr int targetWidth = 4;
128 constexpr int targetHeight = rows;
129 constexpr int srcWidth = rows;
130 constexpr int srcHeight = cols;
131
132 constexpr int copyWidth = std::min(targetHeight, srcWidth);
133 constexpr int copyHeight = std::min(targetWidth, srcHeight);
134
135 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400136
137 for (int x = 0; x < copyWidth; x++)
138 {
139 for (int y = 0; y < copyHeight; y++)
140 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800141 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400142 }
143 }
144
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800145 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
146 {
147 return false;
148 }
149
150 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
151 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400152}
153
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800154template <typename T, int cols, int rows>
155bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400156{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800157 constexpr int targetWidth = 4;
158 constexpr int targetHeight = rows;
159 constexpr int srcWidth = cols;
160 constexpr int srcHeight = rows;
161
162 constexpr int copyWidth = std::min(targetWidth, srcWidth);
163 constexpr int copyHeight = std::min(targetHeight, srcHeight);
164
165 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400166
167 for (int y = 0; y < copyHeight; y++)
168 {
169 for (int x = 0; x < copyWidth; x++)
170 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800171 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400172 }
173 }
174
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800175 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
176 {
177 return false;
178 }
179
180 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
181 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400182}
183
Jamie Madill4e31ad52015-10-29 10:32:57 -0400184gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
185{
186 switch (drawMode)
187 {
188 // Uses the point sprite geometry shader.
189 case GL_POINTS:
190 return gl::PRIMITIVE_POINTS;
191
192 // All line drawing uses the same geometry shader.
193 case GL_LINES:
194 case GL_LINE_STRIP:
195 case GL_LINE_LOOP:
196 return gl::PRIMITIVE_LINES;
197
198 // The triangle fan primitive is emulated with strips in D3D11.
199 case GL_TRIANGLES:
200 case GL_TRIANGLE_FAN:
201 return gl::PRIMITIVE_TRIANGLES;
202
203 // Special case for triangle strips.
204 case GL_TRIANGLE_STRIP:
205 return gl::PRIMITIVE_TRIANGLE_STRIP;
206
207 default:
208 UNREACHABLE();
209 return gl::PRIMITIVE_TYPE_MAX;
210 }
211}
212
Jamie Madill192745a2016-12-22 15:58:21 -0500213bool FindFlatInterpolationVarying(const std::vector<sh::Varying> &varyings)
214{
215 // Note: this assumes nested structs can only be packed with one interpolation.
216 for (const auto &varying : varyings)
217 {
218 if (varying.interpolation == sh::INTERPOLATION_FLAT)
219 {
220 return true;
221 }
222 }
223
224 return false;
225}
226
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400227// Helper method to de-tranpose a matrix uniform for an API query.
228void GetMatrixUniform(GLint columns, GLint rows, GLfloat *dataOut, const GLfloat *source)
229{
230 for (GLint col = 0; col < columns; ++col)
231 {
232 for (GLint row = 0; row < rows; ++row)
233 {
234 GLfloat *outptr = dataOut + ((col * rows) + row);
235 const GLfloat *inptr = source + ((row * 4) + col);
236 *outptr = *inptr;
237 }
238 }
239}
240
241template <typename NonFloatT>
242void GetMatrixUniform(GLint columns, GLint rows, NonFloatT *dataOut, const NonFloatT *source)
243{
244 UNREACHABLE();
245}
246
Jamie Madillada9ecc2015-08-17 12:53:37 -0400247} // anonymous namespace
248
Jamie Madill28afae52015-11-09 15:07:57 -0500249// D3DUniform Implementation
250
Jamie Madill62d31cb2015-09-11 13:25:51 -0400251D3DUniform::D3DUniform(GLenum typeIn,
252 const std::string &nameIn,
253 unsigned int arraySizeIn,
254 bool defaultBlock)
255 : type(typeIn),
256 name(nameIn),
257 arraySize(arraySizeIn),
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400258 vsData(nullptr),
259 psData(nullptr),
260 csData(nullptr),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400261 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400262 psRegisterIndex(GL_INVALID_INDEX),
Xinghua Caob1239382016-12-13 15:07:05 +0800263 csRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400264 registerCount(0),
265 registerElement(0)
266{
267 // We use data storage for default block uniforms to cache values that are sent to D3D during
268 // rendering
269 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
270 if (defaultBlock)
271 {
Jamie Madillcc2ed612017-03-14 15:59:00 -0400272 // Use the row count as register count, will work for non-square matrices.
Jamie Madill62d31cb2015-09-11 13:25:51 -0400273 registerCount = gl::VariableRowCount(type) * elementCount();
274 }
275}
276
277D3DUniform::~D3DUniform()
278{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400279}
280
281const uint8_t *D3DUniform::getDataPtrToElement(size_t elementIndex) const
282{
283 ASSERT((arraySize == 0 && elementIndex == 0) || (arraySize > 0 && elementIndex < arraySize));
284
285 if (isSampler())
286 {
287 return reinterpret_cast<const uint8_t *>(&mSamplerData[elementIndex]);
288 }
289
290 return firstNonNullData() +
291 (elementIndex > 0 ? (gl::VariableInternalSize(type) * elementIndex) : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400292}
293
294bool D3DUniform::isSampler() const
295{
296 return gl::IsSamplerType(type);
297}
298
299bool D3DUniform::isReferencedByVertexShader() const
300{
301 return vsRegisterIndex != GL_INVALID_INDEX;
302}
303
304bool D3DUniform::isReferencedByFragmentShader() const
305{
306 return psRegisterIndex != GL_INVALID_INDEX;
307}
308
Xinghua Caob1239382016-12-13 15:07:05 +0800309bool D3DUniform::isReferencedByComputeShader() const
310{
311 return csRegisterIndex != GL_INVALID_INDEX;
312}
313
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400314const uint8_t *D3DUniform::firstNonNullData() const
315{
316 ASSERT(vsData || psData || csData || !mSamplerData.empty());
317
318 if (!mSamplerData.empty())
319 {
320 return reinterpret_cast<const uint8_t *>(mSamplerData.data());
321 }
322
323 return vsData ? vsData : (psData ? psData : csData);
324}
325
Jamie Madill28afae52015-11-09 15:07:57 -0500326// D3DVarying Implementation
327
Jamie Madill9fc36822015-11-18 13:08:07 -0500328D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500329{
330}
331
Jamie Madill9fc36822015-11-18 13:08:07 -0500332D3DVarying::D3DVarying(const std::string &semanticNameIn,
333 unsigned int semanticIndexIn,
334 unsigned int componentCountIn,
335 unsigned int outputSlotIn)
336 : semanticName(semanticNameIn),
337 semanticIndex(semanticIndexIn),
338 componentCount(componentCountIn),
339 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500340{
341}
342
Jamie Madille39a3f02015-11-17 20:42:15 -0500343// ProgramD3DMetadata Implementation
344
Jamie Madillc9bde922016-07-24 17:58:50 -0400345ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500346 const ShaderD3D *vertexShader,
347 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400348 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
349 mShaderModelSuffix(renderer->getShaderModelSuffix()),
350 mUsesInstancedPointSpriteEmulation(
351 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
352 mUsesViewScale(renderer->presentPathFastEnabled()),
Martin Radev41ac68e2017-06-06 12:16:58 +0300353 mHasANGLEMultiviewEnabled(vertexShader->hasANGLEMultiviewEnabled()),
354 mUsesViewID(fragmentShader->usesViewID()),
Martin Radevc1d4e552017-08-21 12:01:10 +0300355 mCanSelectViewInVertexShader(renderer->canSelectViewInVertexShader()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500356 mVertexShader(vertexShader),
357 mFragmentShader(fragmentShader)
358{
359}
360
361int ProgramD3DMetadata::getRendererMajorShaderModel() const
362{
363 return mRendererMajorShaderModel;
364}
365
Jamie Madill9082b982016-04-27 15:21:51 -0400366bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500367{
Corentin Wallezc084de12017-06-05 14:28:52 -0700368 return (mFragmentShader->usesFragColor() && mFragmentShader->usesMultipleRenderTargets() &&
369 data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500370}
371
Jamie Madill48ef11b2016-04-27 15:21:52 -0400372bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500373{
Jamie Madill63286672015-11-24 13:00:08 -0500374 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500375}
376
377bool ProgramD3DMetadata::usesPointCoord() const
378{
379 return mFragmentShader->usesPointCoord();
380}
381
382bool ProgramD3DMetadata::usesFragCoord() const
383{
384 return mFragmentShader->usesFragCoord();
385}
386
387bool ProgramD3DMetadata::usesPointSize() const
388{
389 return mVertexShader->usesPointSize();
390}
391
392bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
393{
Jamie Madillc9bde922016-07-24 17:58:50 -0400394 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
395 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500396}
397
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800398bool ProgramD3DMetadata::usesViewScale() const
399{
400 return mUsesViewScale;
401}
402
Martin Radev41ac68e2017-06-06 12:16:58 +0300403bool ProgramD3DMetadata::hasANGLEMultiviewEnabled() const
404{
405 return mHasANGLEMultiviewEnabled;
406}
407
408bool ProgramD3DMetadata::usesViewID() const
409{
410 return mUsesViewID;
411}
412
Martin Radevc1d4e552017-08-21 12:01:10 +0300413bool ProgramD3DMetadata::canSelectViewInVertexShader() const
414{
415 return mCanSelectViewInVertexShader;
416}
417
Jamie Madille39a3f02015-11-17 20:42:15 -0500418bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
419{
Jamie Madillc9bde922016-07-24 17:58:50 -0400420 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500421 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400422 // Even with a geometry shader, the app can render triangles or lines and reference
423 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
424 // simplicity, we always add this to the vertex shader when the fragment shader
425 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500426 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
427 usesInsertedPointCoordValue();
428}
429
430bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
431{
432 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
433 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
434 // the vertex shader in this case. This saves us 1 output vector.
435 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
436}
437
438bool ProgramD3DMetadata::usesSystemValuePointSize() const
439{
440 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
441}
442
443bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
444{
445 return mFragmentShader->usesMultipleRenderTargets();
446}
447
448GLint ProgramD3DMetadata::getMajorShaderVersion() const
449{
450 return mVertexShader->getData().getShaderVersion();
451}
452
453const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
454{
455 return mFragmentShader;
456}
457
Jamie Madill28afae52015-11-09 15:07:57 -0500458// ProgramD3D Implementation
459
Jamie Madilld3dfda22015-07-06 08:28:49 -0400460ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
461 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500462 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400463 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700464{
Brandon Joneseb994362014-09-24 10:27:28 -0700465}
466
467ProgramD3D::VertexExecutable::~VertexExecutable()
468{
469 SafeDelete(mShaderExecutable);
470}
471
Jamie Madilld3dfda22015-07-06 08:28:49 -0400472// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500473ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
474 GLenum type)
475{
476 switch (type)
477 {
478 case GL_INT:
479 return HLSLAttribType::SIGNED_INT;
480 case GL_UNSIGNED_INT:
481 return HLSLAttribType::UNSIGNED_INT;
482 case GL_SIGNED_NORMALIZED:
483 case GL_UNSIGNED_NORMALIZED:
484 case GL_FLOAT:
485 return HLSLAttribType::FLOAT;
486 default:
487 UNREACHABLE();
488 return HLSLAttribType::FLOAT;
489 }
490}
491
492// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400493void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
494 const gl::InputLayout &inputLayout,
495 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700496{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500497 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400498
499 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700500 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400501 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500502 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
503 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400504
Jamie Madillbdec2f42016-03-02 16:35:32 -0500505 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
506 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
507 continue;
508
509 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
510 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700511 }
Brandon Joneseb994362014-09-24 10:27:28 -0700512}
513
Jamie Madilld3dfda22015-07-06 08:28:49 -0400514bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
515{
Jamie Madillbd136f92015-08-10 14:51:37 -0400516 size_t limit = std::max(mSignature.size(), signature.size());
517 for (size_t index = 0; index < limit; ++index)
518 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500519 // treat undefined indexes as FLOAT
520 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
521 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400522 if (a != b)
523 return false;
524 }
525
526 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400527}
528
529ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
530 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400531 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700532{
533}
534
535ProgramD3D::PixelExecutable::~PixelExecutable()
536{
537 SafeDelete(mShaderExecutable);
538}
539
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700540ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
541{
542}
543
Geoff Lang7dd2e102014-11-10 15:19:26 -0500544unsigned int ProgramD3D::mCurrentSerial = 1;
545
Jamie Madill48ef11b2016-04-27 15:21:52 -0400546ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
547 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700548 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800549 mDynamicHLSL(nullptr),
550 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
551 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700552 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400553 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800554 mVertexUniformStorage(nullptr),
555 mFragmentUniformStorage(nullptr),
556 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700557 mUsedVertexSamplerRange(0),
558 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800559 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700560 mDirtySamplerMapping(true),
Jamie Madill561ed3a2017-08-31 16:48:09 -0400561 mSerial(issueSerial()),
562 mUniformsDirty(true)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700563{
Brandon Joneseb994362014-09-24 10:27:28 -0700564 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700565}
566
567ProgramD3D::~ProgramD3D()
568{
569 reset();
570 SafeDelete(mDynamicHLSL);
571}
572
Brandon Jones44151a92014-09-10 11:32:25 -0700573bool ProgramD3D::usesPointSpriteEmulation() const
574{
575 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
576}
577
Martin Radev41ac68e2017-06-06 12:16:58 +0300578bool ProgramD3D::usesGeometryShaderForPointSpriteEmulation() const
579{
580 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
581}
582
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400583bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700584{
Martin Radevc1d4e552017-08-21 12:01:10 +0300585 if (mHasANGLEMultiviewEnabled && !mRenderer->canSelectViewInVertexShader())
Martin Radev41ac68e2017-06-06 12:16:58 +0300586 {
587 return true;
588 }
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400589 if (drawMode != GL_POINTS)
590 {
591 return mUsesFlatInterpolation;
592 }
Martin Radev41ac68e2017-06-06 12:16:58 +0300593 return usesGeometryShaderForPointSpriteEmulation();
Cooper Partine6664f02015-01-09 16:22:24 -0800594}
595
596bool ProgramD3D::usesInstancedPointSpriteEmulation() const
597{
598 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700599}
600
Jamie Madill334d6152015-10-22 14:00:28 -0400601GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
602 unsigned int samplerIndex,
603 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700604{
605 GLint logicalTextureUnit = -1;
606
607 switch (type)
608 {
Jamie Madill334d6152015-10-22 14:00:28 -0400609 case gl::SAMPLER_PIXEL:
610 ASSERT(samplerIndex < caps.maxTextureImageUnits);
611 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
612 {
613 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
614 }
615 break;
616 case gl::SAMPLER_VERTEX:
617 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
618 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
619 {
620 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
621 }
622 break;
Xinghua Caob1239382016-12-13 15:07:05 +0800623 case gl::SAMPLER_COMPUTE:
624 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
625 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
626 {
627 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
628 }
629 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400630 default:
631 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700632 }
633
Jamie Madill334d6152015-10-22 14:00:28 -0400634 if (logicalTextureUnit >= 0 &&
635 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700636 {
637 return logicalTextureUnit;
638 }
639
640 return -1;
641}
642
643// Returns the texture type for a given Direct3D 9 sampler type and
644// index (0-15 for the pixel shader and 0-3 for the vertex shader).
645GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
646{
647 switch (type)
648 {
Jamie Madill334d6152015-10-22 14:00:28 -0400649 case gl::SAMPLER_PIXEL:
650 ASSERT(samplerIndex < mSamplersPS.size());
651 ASSERT(mSamplersPS[samplerIndex].active);
652 return mSamplersPS[samplerIndex].textureType;
653 case gl::SAMPLER_VERTEX:
654 ASSERT(samplerIndex < mSamplersVS.size());
655 ASSERT(mSamplersVS[samplerIndex].active);
656 return mSamplersVS[samplerIndex].textureType;
Xinghua Caob1239382016-12-13 15:07:05 +0800657 case gl::SAMPLER_COMPUTE:
658 ASSERT(samplerIndex < mSamplersCS.size());
659 ASSERT(mSamplersCS[samplerIndex].active);
660 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400661 default:
662 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700663 }
664
665 return GL_TEXTURE_2D;
666}
667
Olli Etuaho618bebc2016-01-15 16:40:00 +0200668GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700669{
670 switch (type)
671 {
Jamie Madill334d6152015-10-22 14:00:28 -0400672 case gl::SAMPLER_PIXEL:
673 return mUsedPixelSamplerRange;
674 case gl::SAMPLER_VERTEX:
675 return mUsedVertexSamplerRange;
Xinghua Caob1239382016-12-13 15:07:05 +0800676 case gl::SAMPLER_COMPUTE:
677 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400678 default:
679 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200680 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700681 }
682}
683
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400684ProgramD3D::SamplerMapping ProgramD3D::updateSamplerMapping()
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685{
686 if (!mDirtySamplerMapping)
687 {
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400688 return SamplerMapping::WasClean;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689 }
690
691 mDirtySamplerMapping = false;
692
693 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400694 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700695 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400696 if (!d3dUniform->isSampler())
697 continue;
698
699 int count = d3dUniform->elementCount();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700
701 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700702 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400703 unsigned int firstIndex = d3dUniform->psRegisterIndex;
704
705 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700706 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400707 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700708
Jamie Madill62d31cb2015-09-11 13:25:51 -0400709 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700710 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400711 ASSERT(mSamplersPS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400712 mSamplersPS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700713 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400714 }
715 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700716
Jamie Madill62d31cb2015-09-11 13:25:51 -0400717 if (d3dUniform->isReferencedByVertexShader())
718 {
719 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
720
721 for (int i = 0; i < count; i++)
722 {
723 unsigned int samplerIndex = firstIndex + i;
724
725 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700726 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400727 ASSERT(mSamplersVS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400728 mSamplersVS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700729 }
730 }
731 }
Xinghua Caob1239382016-12-13 15:07:05 +0800732
733 if (d3dUniform->isReferencedByComputeShader())
734 {
735 unsigned int firstIndex = d3dUniform->csRegisterIndex;
736
737 for (int i = 0; i < count; i++)
738 {
739 unsigned int samplerIndex = firstIndex + i;
740
741 if (samplerIndex < mSamplersCS.size())
742 {
743 ASSERT(mSamplersCS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400744 mSamplersCS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Xinghua Caob1239382016-12-13 15:07:05 +0800745 }
746 }
747 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700748 }
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400749
750 return SamplerMapping::WasDirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700751}
752
Jamie Madill9cf9e872017-06-05 12:59:25 -0400753gl::LinkResult ProgramD3D::load(const gl::Context *context,
754 gl::InfoLog &infoLog,
755 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700756{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500757 // TODO(jmadill): Use Renderer from contextImpl.
758
Jamie Madill62d31cb2015-09-11 13:25:51 -0400759 reset();
760
Jamie Madill334d6152015-10-22 14:00:28 -0400761 DeviceIdentifier binaryDeviceIdentifier = {0};
762 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
763 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700764
765 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
766 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
767 {
768 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500769 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700770 }
771
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500772 int compileFlags = stream->readInt<int>();
773 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
774 {
Jamie Madillf6113162015-05-07 11:49:21 -0400775 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500776 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500777 }
778
Jamie Madill8047c0d2016-03-07 13:02:12 -0500779 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400780 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500781 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400782 }
783
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700784 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
785 for (unsigned int i = 0; i < psSamplerCount; ++i)
786 {
787 Sampler sampler;
788 stream->readBool(&sampler.active);
789 stream->readInt(&sampler.logicalTextureUnit);
790 stream->readInt(&sampler.textureType);
791 mSamplersPS.push_back(sampler);
792 }
793 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
794 for (unsigned int i = 0; i < vsSamplerCount; ++i)
795 {
796 Sampler sampler;
797 stream->readBool(&sampler.active);
798 stream->readInt(&sampler.logicalTextureUnit);
799 stream->readInt(&sampler.textureType);
800 mSamplersVS.push_back(sampler);
801 }
802
Xinghua Caob1239382016-12-13 15:07:05 +0800803 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
804 for (unsigned int i = 0; i < csSamplerCount; ++i)
805 {
806 Sampler sampler;
807 stream->readBool(&sampler.active);
808 stream->readInt(&sampler.logicalTextureUnit);
809 stream->readInt(&sampler.textureType);
810 mSamplersCS.push_back(sampler);
811 }
812
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700813 stream->readInt(&mUsedVertexSamplerRange);
814 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800815 stream->readInt(&mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700816
817 const unsigned int uniformCount = stream->readInt<unsigned int>();
818 if (stream->error())
819 {
Jamie Madillf6113162015-05-07 11:49:21 -0400820 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500821 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700822 }
823
Jamie Madill48ef11b2016-04-27 15:21:52 -0400824 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400825 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700826 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
827 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400828 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700829
Jamie Madill62d31cb2015-09-11 13:25:51 -0400830 D3DUniform *d3dUniform =
831 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
832 linkedUniform.isInDefaultBlock());
833 stream->readInt(&d3dUniform->psRegisterIndex);
834 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800835 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400836 stream->readInt(&d3dUniform->registerCount);
837 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700838
Jamie Madill62d31cb2015-09-11 13:25:51 -0400839 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700840 }
841
Jamie Madill4a3c2342015-10-08 12:58:45 -0400842 const unsigned int blockCount = stream->readInt<unsigned int>();
843 if (stream->error())
844 {
845 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500846 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400847 }
848
849 ASSERT(mD3DUniformBlocks.empty());
850 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
851 {
852 D3DUniformBlock uniformBlock;
853 stream->readInt(&uniformBlock.psRegisterIndex);
854 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800855 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400856 mD3DUniformBlocks.push_back(uniformBlock);
857 }
858
Jamie Madill9fc36822015-11-18 13:08:07 -0500859 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
860 mStreamOutVaryings.resize(streamOutVaryingCount);
861 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700862 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500863 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700864
Jamie Madill28afae52015-11-09 15:07:57 -0500865 stream->readString(&varying->semanticName);
866 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500867 stream->readInt(&varying->componentCount);
868 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700869 }
870
Brandon Jones22502d52014-08-29 16:58:36 -0700871 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400872 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500873 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700874 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400875 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500876 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700877 stream->readBool(&mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +0300878 stream->readBool(&mHasANGLEMultiviewEnabled);
879 stream->readBool(&mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -0700880 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400881 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700882
883 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
884 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400885 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
886 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700887 {
888 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
889 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
890 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
891 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
892 }
893
Jamie Madill4e31ad52015-10-29 10:32:57 -0400894 stream->readString(&mGeometryShaderPreamble);
895
Jamie Madill334d6152015-10-22 14:00:28 -0400896 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700897
Jamie Madillb0a838b2016-11-13 20:02:12 -0500898 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
899
Brandon Joneseb994362014-09-24 10:27:28 -0700900 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400901 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
902 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700903 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400904 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400905 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700906
Jamie Madilld3dfda22015-07-06 08:28:49 -0400907 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700908 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400909 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700910 }
911
Jamie Madill334d6152015-10-22 14:00:28 -0400912 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700913 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400914
Jamie Madillada9ecc2015-08-17 12:53:37 -0400915 ShaderExecutableD3D *shaderExecutable = nullptr;
916
Jamie Madillb0a838b2016-11-13 20:02:12 -0500917 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
918 mStreamOutVaryings, separateAttribs,
919 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400920
Brandon Joneseb994362014-09-24 10:27:28 -0700921 if (!shaderExecutable)
922 {
Jamie Madillf6113162015-05-07 11:49:21 -0400923 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500924 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700925 }
926
927 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400928 VertexExecutable::Signature signature;
929 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700930
931 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800932 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
933 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700934
935 stream->skip(vertexShaderSize);
936 }
937
938 const size_t pixelShaderCount = stream->readInt<unsigned int>();
939 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
940 {
941 const size_t outputCount = stream->readInt<unsigned int>();
942 std::vector<GLenum> outputs(outputCount);
943 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
944 {
945 stream->readInt(&outputs[outputIndex]);
946 }
947
Jamie Madill334d6152015-10-22 14:00:28 -0400948 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700949 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400950 ShaderExecutableD3D *shaderExecutable = nullptr;
951
Jamie Madillb0a838b2016-11-13 20:02:12 -0500952 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
953 mStreamOutVaryings, separateAttribs,
954 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700955
956 if (!shaderExecutable)
957 {
Jamie Madillf6113162015-05-07 11:49:21 -0400958 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500959 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700960 }
961
962 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800963 mPixelExecutables.push_back(
964 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700965
966 stream->skip(pixelShaderSize);
967 }
968
Jamie Madill4e31ad52015-10-29 10:32:57 -0400969 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
970 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700971 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400972 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
973 if (geometryShaderSize == 0)
974 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400975 continue;
976 }
977
Brandon Joneseb994362014-09-24 10:27:28 -0700978 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400979
Xinghua Caob1239382016-12-13 15:07:05 +0800980 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -0500981 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
982 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
Xinghua Caob1239382016-12-13 15:07:05 +0800983 &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700984
Xinghua Caob1239382016-12-13 15:07:05 +0800985 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700986 {
Jamie Madillf6113162015-05-07 11:49:21 -0400987 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500988 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700989 }
Xinghua Caob1239382016-12-13 15:07:05 +0800990
991 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
992
Brandon Joneseb994362014-09-24 10:27:28 -0700993 stream->skip(geometryShaderSize);
994 }
995
Xinghua Caob1239382016-12-13 15:07:05 +0800996 unsigned int computeShaderSize = stream->readInt<unsigned int>();
997 if (computeShaderSize > 0)
998 {
999 const unsigned char *computeShaderFunction = binary + stream->offset();
1000
1001 ShaderExecutableD3D *computeExecutable = nullptr;
1002 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
1003 SHADER_COMPUTE, std::vector<D3DVarying>(), false,
1004 &computeExecutable));
1005
1006 if (!computeExecutable)
1007 {
1008 infoLog << "Could not create compute shader.";
1009 return false;
1010 }
1011
1012 mComputeExecutable.reset(computeExecutable);
1013 }
1014
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001015 initializeUniformStorage();
1016
Jamie Madillb0a838b2016-11-13 20:02:12 -05001017 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001018}
1019
Jamie Madill27a60632017-06-30 15:12:01 -04001020void ProgramD3D::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -07001021{
Austin Kinross137b1512015-06-17 16:14:53 -07001022 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -04001023 // When we load the binary again later, we can validate the device identifier before trying to
1024 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -07001025 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -04001026 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
1027 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -07001028
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001029 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1030
Jamie Madill8047c0d2016-03-07 13:02:12 -05001031 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -04001032 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05001033 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -04001034 }
1035
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001036 stream->writeInt(mSamplersPS.size());
1037 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1038 {
1039 stream->writeInt(mSamplersPS[i].active);
1040 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
1041 stream->writeInt(mSamplersPS[i].textureType);
1042 }
1043
1044 stream->writeInt(mSamplersVS.size());
1045 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1046 {
1047 stream->writeInt(mSamplersVS[i].active);
1048 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1049 stream->writeInt(mSamplersVS[i].textureType);
1050 }
1051
Xinghua Caob1239382016-12-13 15:07:05 +08001052 stream->writeInt(mSamplersCS.size());
1053 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
1054 {
1055 stream->writeInt(mSamplersCS[i].active);
1056 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
1057 stream->writeInt(mSamplersCS[i].textureType);
1058 }
1059
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001060 stream->writeInt(mUsedVertexSamplerRange);
1061 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001062 stream->writeInt(mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001063
Jamie Madill62d31cb2015-09-11 13:25:51 -04001064 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001065 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001066 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001067 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001068 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1069 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001070 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001071 stream->writeInt(uniform->registerCount);
1072 stream->writeInt(uniform->registerElement);
1073 }
1074
Jamie Madill51f522f2016-12-21 15:10:55 -05001075 // Ensure we init the uniform block structure data if we should.
1076 // http://anglebug.com/1637
1077 ensureUniformBlocksInitialized();
1078
Jamie Madill4a3c2342015-10-08 12:58:45 -04001079 stream->writeInt(mD3DUniformBlocks.size());
1080 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1081 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001082 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1083 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001084 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001085 }
1086
Jamie Madill9fc36822015-11-18 13:08:07 -05001087 stream->writeInt(mStreamOutVaryings.size());
1088 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001089 {
Brandon Joneseb994362014-09-24 10:27:28 -07001090 stream->writeString(varying.semanticName);
1091 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001092 stream->writeInt(varying.componentCount);
1093 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001094 }
1095
Brandon Jones22502d52014-08-29 16:58:36 -07001096 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001097 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001098 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001099 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001100 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001101 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001102 stream->writeInt(mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +03001103 stream->writeInt(mHasANGLEMultiviewEnabled);
1104 stream->writeInt(mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -07001105 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001106 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001107
Brandon Joneseb994362014-09-24 10:27:28 -07001108 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001109 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001110 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1111 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001112 {
Brandon Joneseb994362014-09-24 10:27:28 -07001113 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001114 stream->writeInt(variable.type);
1115 stream->writeString(variable.name);
1116 stream->writeString(variable.source);
1117 stream->writeInt(variable.outputIndex);
1118 }
1119
Jamie Madill4e31ad52015-10-29 10:32:57 -04001120 stream->writeString(mGeometryShaderPreamble);
1121
Brandon Joneseb994362014-09-24 10:27:28 -07001122 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001123 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1124 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001125 {
Xinghua Caob1239382016-12-13 15:07:05 +08001126 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001127
Jamie Madilld3dfda22015-07-06 08:28:49 -04001128 const auto &inputLayout = vertexExecutable->inputs();
1129 stream->writeInt(inputLayout.size());
1130
1131 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001132 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001133 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001134 }
1135
1136 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1137 stream->writeInt(vertexShaderSize);
1138
1139 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1140 stream->writeBytes(vertexBlob, vertexShaderSize);
1141 }
1142
1143 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001144 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1145 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001146 {
Xinghua Caob1239382016-12-13 15:07:05 +08001147 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001148
1149 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1150 stream->writeInt(outputs.size());
1151 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1152 {
1153 stream->writeInt(outputs[outputIndex]);
1154 }
1155
1156 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1157 stream->writeInt(pixelShaderSize);
1158
1159 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1160 stream->writeBytes(pixelBlob, pixelShaderSize);
1161 }
1162
Xinghua Caob1239382016-12-13 15:07:05 +08001163 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001164 {
Xinghua Caob1239382016-12-13 15:07:05 +08001165 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001166 {
1167 stream->writeInt(0);
1168 continue;
1169 }
1170
Xinghua Caob1239382016-12-13 15:07:05 +08001171 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001172 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001173 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1174 }
1175
1176 if (mComputeExecutable)
1177 {
1178 size_t computeShaderSize = mComputeExecutable->getLength();
1179 stream->writeInt(computeShaderSize);
1180 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1181 }
1182 else
1183 {
1184 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001185 }
Brandon Jones22502d52014-08-29 16:58:36 -07001186}
1187
Geoff Langc5629752015-12-07 16:29:04 -05001188void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1189{
1190}
1191
Yunchao He61afff12017-03-14 15:34:03 +08001192void ProgramD3D::setSeparable(bool /* separable */)
1193{
1194}
1195
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001196gl::Error ProgramD3D::getPixelExecutableForCachedOutputLayout(ShaderExecutableD3D **outExecutable,
1197 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001198{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001199 if (mCachedPixelExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001200 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001201 *outExecutable = mPixelExecutables[mCachedPixelExecutableIndex.value()]->shaderExecutable();
1202 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001203 }
1204
Jamie Madill334d6152015-10-22 14:00:28 -04001205 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001206 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, mPixelShaderOutputLayoutCache);
Brandon Jones22502d52014-08-29 16:58:36 -07001207
1208 // Generate new pixel executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001209 ShaderExecutableD3D *pixelExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001210
1211 gl::InfoLog tempInfoLog;
1212 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1213
Jamie Madill01074252016-11-28 15:55:51 -05001214 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001215 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001216 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001217 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001218
Jamie Madill97399232014-12-23 12:31:15 -05001219 if (pixelExecutable)
1220 {
Xinghua Caob1239382016-12-13 15:07:05 +08001221 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001222 new PixelExecutable(mPixelShaderOutputLayoutCache, pixelExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001223 mCachedPixelExecutableIndex = mPixelExecutables.size() - 1;
Jamie Madill97399232014-12-23 12:31:15 -05001224 }
1225 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001226 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001227 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1228 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001229 }
Brandon Jones22502d52014-08-29 16:58:36 -07001230
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001231 *outExecutable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001232 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001233}
1234
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001235gl::Error ProgramD3D::getVertexExecutableForCachedInputLayout(ShaderExecutableD3D **outExectuable,
1236 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001237{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001238 if (mCachedVertexExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001239 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001240 *outExectuable =
1241 mVertexExecutables[mCachedVertexExecutableIndex.value()]->shaderExecutable();
1242 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001243 }
1244
Brandon Jones22502d52014-08-29 16:58:36 -07001245 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001246 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001247 mVertexHLSL, mCachedInputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001248
1249 // Generate new vertex executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001250 ShaderExecutableD3D *vertexExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001251
1252 gl::InfoLog tempInfoLog;
1253 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1254
Jamie Madill01074252016-11-28 15:55:51 -05001255 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001256 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001257 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001258 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001259
Jamie Madill97399232014-12-23 12:31:15 -05001260 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001261 {
Xinghua Caob1239382016-12-13 15:07:05 +08001262 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001263 new VertexExecutable(mCachedInputLayout, mCachedVertexSignature, vertexExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001264 mCachedVertexExecutableIndex = mVertexExecutables.size() - 1;
Brandon Joneseb994362014-09-24 10:27:28 -07001265 }
Jamie Madill97399232014-12-23 12:31:15 -05001266 else if (!infoLog)
1267 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001268 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1269 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001270 }
Brandon Jones22502d52014-08-29 16:58:36 -07001271
Geoff Langb543aff2014-09-30 14:52:54 -04001272 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001273 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001274}
1275
Jamie Madill9082b982016-04-27 15:21:51 -04001276gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001277 GLenum drawMode,
1278 ShaderExecutableD3D **outExecutable,
1279 gl::InfoLog *infoLog)
1280{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001281 if (outExecutable)
1282 {
1283 *outExecutable = nullptr;
1284 }
1285
Austin Kinross88829e82016-01-12 13:04:41 -08001286 // Return a null shader if the current rendering doesn't use a geometry shader
1287 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001288 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001289 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001290 }
1291
Jamie Madill4e31ad52015-10-29 10:32:57 -04001292 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1293
Xinghua Caob1239382016-12-13 15:07:05 +08001294 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001295 {
1296 if (outExecutable)
1297 {
Xinghua Caob1239382016-12-13 15:07:05 +08001298 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001299 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001300 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001301 }
1302
1303 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001304 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Martin Radevc1d4e552017-08-21 12:01:10 +03001305 mHasANGLEMultiviewEnabled, mRenderer->canSelectViewInVertexShader(),
1306 usesGeometryShaderForPointSpriteEmulation(), mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001307
1308 gl::InfoLog tempInfoLog;
1309 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1310
Xinghua Caob1239382016-12-13 15:07:05 +08001311 ShaderExecutableD3D *geometryExecutable = nullptr;
1312 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001313 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001314 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001315 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001316
1317 if (!infoLog && error.isError())
1318 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001319 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1320 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001321 }
1322
Xinghua Caob1239382016-12-13 15:07:05 +08001323 if (geometryExecutable != nullptr)
1324 {
1325 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1326 }
1327
Jamie Madill4e31ad52015-10-29 10:32:57 -04001328 if (outExecutable)
1329 {
Xinghua Caob1239382016-12-13 15:07:05 +08001330 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001331 }
1332 return error;
1333}
1334
Jamie Madill01074252016-11-28 15:55:51 -05001335class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001336{
Jamie Madill01074252016-11-28 15:55:51 -05001337 public:
1338 GetExecutableTask(ProgramD3D *program)
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001339 : mProgram(program), mError(gl::NoError()), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001340 {
Brandon Joneseb994362014-09-24 10:27:28 -07001341 }
1342
Jamie Madill01074252016-11-28 15:55:51 -05001343 virtual gl::Error run() = 0;
1344
1345 void operator()() override { mError = run(); }
1346
1347 const gl::Error &getError() const { return mError; }
1348 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1349 ShaderExecutableD3D *getResult() { return mResult; }
1350
1351 protected:
1352 ProgramD3D *mProgram;
1353 gl::Error mError;
1354 gl::InfoLog mInfoLog;
1355 ShaderExecutableD3D *mResult;
1356};
1357
1358class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1359{
1360 public:
Jamie Madillbd044ed2017-06-05 12:59:21 -04001361 GetVertexExecutableTask(ProgramD3D *program, const gl::Context *context)
1362 : GetExecutableTask(program), mContext(context)
1363 {
1364 }
Jamie Madill01074252016-11-28 15:55:51 -05001365 gl::Error run() override
1366 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001367 mProgram->updateCachedInputLayoutFromShader(mContext);
Jamie Madill01074252016-11-28 15:55:51 -05001368
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001369 ANGLE_TRY(mProgram->getVertexExecutableForCachedInputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001370
1371 return gl::NoError();
1372 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001373
1374 private:
1375 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001376};
1377
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001378void ProgramD3D::updateCachedInputLayoutFromShader(const gl::Context *context)
1379{
1380 GetDefaultInputLayoutFromShader(context, mState.getAttachedVertexShader(), &mCachedInputLayout);
1381 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001382 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001383}
1384
Jamie Madill01074252016-11-28 15:55:51 -05001385class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1386{
1387 public:
1388 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1389 gl::Error run() override
1390 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001391 mProgram->updateCachedOutputLayoutFromShader();
Jamie Madill01074252016-11-28 15:55:51 -05001392
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001393 ANGLE_TRY(mProgram->getPixelExecutableForCachedOutputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001394
1395 return gl::NoError();
1396 }
1397};
1398
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001399void ProgramD3D::updateCachedOutputLayoutFromShader()
1400{
1401 GetDefaultOutputLayoutFromShader(mPixelShaderKey, &mPixelShaderOutputLayoutCache);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001402 updateCachedPixelExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001403}
1404
Jamie Madill01074252016-11-28 15:55:51 -05001405class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1406{
1407 public:
1408 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1409 : GetExecutableTask(program), mContextState(contextState)
1410 {
1411 }
1412
1413 gl::Error run() override
1414 {
1415 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1416 // D3D11.
1417 if (mProgram->usesGeometryShader(GL_POINTS))
1418 {
1419 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1420 &mResult, &mInfoLog));
1421 }
1422
1423 return gl::NoError();
1424 }
1425
1426 private:
1427 const gl::ContextState &mContextState;
1428};
1429
Xinghua Cao73badc02017-03-29 19:14:53 +08001430gl::Error ProgramD3D::getComputeExecutable(ShaderExecutableD3D **outExecutable)
1431{
1432 if (outExecutable)
1433 {
1434 *outExecutable = mComputeExecutable.get();
1435 }
1436
1437 return gl::NoError();
1438}
1439
Jamie Madill9cf9e872017-06-05 12:59:25 -04001440gl::LinkResult ProgramD3D::compileProgramExecutables(const gl::Context *context,
1441 gl::InfoLog &infoLog)
Jamie Madill01074252016-11-28 15:55:51 -05001442{
1443 // Ensure the compiler is initialized to avoid race conditions.
1444 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1445
1446 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1447
Jamie Madillbd044ed2017-06-05 12:59:21 -04001448 GetVertexExecutableTask vertexTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001449 GetPixelExecutableTask pixelTask(this);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001450 GetGeometryExecutableTask geometryTask(this, context->getContextState());
Jamie Madill01074252016-11-28 15:55:51 -05001451
1452 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1453 workerPool->postWorkerTask(&pixelTask),
1454 workerPool->postWorkerTask(&geometryTask)}};
1455
1456 WaitableEvent::WaitMany(&waitEvents);
1457
1458 infoLog << vertexTask.getInfoLog().str();
1459 infoLog << pixelTask.getInfoLog().str();
1460 infoLog << geometryTask.getInfoLog().str();
1461
1462 ANGLE_TRY(vertexTask.getError());
1463 ANGLE_TRY(pixelTask.getError());
1464 ANGLE_TRY(geometryTask.getError());
1465
1466 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1467 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1468 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1469
Jamie Madill48ef11b2016-04-27 15:21:52 -04001470 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001471
1472 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001473 {
Jamie Madill334d6152015-10-22 14:00:28 -04001474 // Geometry shaders are currently only used internally, so there is no corresponding shader
1475 // object at the interface level. For now the geometry shader debug info is prepended to
1476 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001477 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001478 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001479 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1480 }
1481
1482 if (defaultVertexExecutable)
1483 {
1484 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1485 }
1486
1487 if (defaultPixelExecutable)
1488 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001489 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001490 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001491 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1492 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001493
Jamie Madillb0a838b2016-11-13 20:02:12 -05001494 return (defaultVertexExecutable && defaultPixelExecutable &&
1495 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001496}
1497
Jamie Madill9cf9e872017-06-05 12:59:25 -04001498gl::LinkResult ProgramD3D::compileComputeExecutable(const gl::Context *context,
1499 gl::InfoLog &infoLog)
Xinghua Caob1239382016-12-13 15:07:05 +08001500{
1501 // Ensure the compiler is initialized to avoid race conditions.
1502 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1503
Jamie Madillbd044ed2017-06-05 12:59:21 -04001504 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(context, mState);
Xinghua Caob1239382016-12-13 15:07:05 +08001505
1506 ShaderExecutableD3D *computeExecutable = nullptr;
1507 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, SHADER_COMPUTE,
1508 std::vector<D3DVarying>(), false,
1509 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1510
1511 if (computeExecutable == nullptr)
1512 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001513 ERR() << "Error compiling dynamic compute executable:" << std::endl
1514 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001515 }
1516 else
1517 {
1518 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1519 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1520 mComputeExecutable.reset(computeExecutable);
1521 }
1522
1523 return mComputeExecutable.get() != nullptr;
1524}
1525
Jamie Madill9cf9e872017-06-05 12:59:25 -04001526gl::LinkResult ProgramD3D::link(const gl::Context *context,
1527 const gl::VaryingPacking &packing,
1528 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001529{
Jamie Madillc564c072017-06-01 12:45:42 -04001530 const auto &data = context->getContextState();
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001531
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532 reset();
1533
Jamie Madillbd044ed2017-06-05 12:59:21 -04001534 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001535 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001536 {
Xinghua Caob1239382016-12-13 15:07:05 +08001537 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1538
Jamie Madillbd044ed2017-06-05 12:59:21 -04001539 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001540
Jamie Madill9cf9e872017-06-05 12:59:25 -04001541 gl::LinkResult result = compileComputeExecutable(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001542 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001543 {
Xinghua Caob1239382016-12-13 15:07:05 +08001544 infoLog << result.getError().getMessage();
1545 return result;
1546 }
1547 else if (!result.getResult())
1548 {
1549 infoLog << "Failed to create D3D compute shader.";
1550 return result;
1551 }
1552
Jamie Madillbd044ed2017-06-05 12:59:21 -04001553 initUniformBlockInfo(context, computeShader);
Xinghua Caob1239382016-12-13 15:07:05 +08001554 }
1555 else
1556 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001557 gl::Shader *vertexShader = mState.getAttachedVertexShader();
1558 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001559
1560 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1561 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1562
1563 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1564 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1565
1566 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1567 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1568
1569 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1570 {
1571 if (fragmentShaderD3D->usesFrontFacing())
1572 {
1573 infoLog << "The current renderer doesn't support gl_FrontFacing";
1574 return false;
1575 }
1576 }
1577
1578 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1579 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1580 // intelligently, but D3D9 assumes one semantic per register.
1581 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1582 packing.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
1583 {
1584 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001585 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001586 }
Xinghua Caob1239382016-12-13 15:07:05 +08001587
1588 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
1589 BuiltinVaryingsD3D builtins(metadata, packing);
1590
Jamie Madillbd044ed2017-06-05 12:59:21 -04001591 mDynamicHLSL->generateShaderLinkHLSL(context, mState, metadata, packing, builtins,
1592 &mPixelHLSL, &mVertexHLSL);
Xinghua Caob1239382016-12-13 15:07:05 +08001593
1594 mUsesPointSize = vertexShaderD3D->usesPointSize();
1595 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1596 mUsesFragDepth = metadata.usesFragDepth();
Martin Radev41ac68e2017-06-06 12:16:58 +03001597 mUsesViewID = metadata.usesViewID();
1598 mHasANGLEMultiviewEnabled = metadata.hasANGLEMultiviewEnabled();
Xinghua Caob1239382016-12-13 15:07:05 +08001599
1600 // Cache if we use flat shading
Jamie Madillbd044ed2017-06-05 12:59:21 -04001601 mUsesFlatInterpolation =
1602 (FindFlatInterpolationVarying(fragmentShader->getVaryings(context)) ||
1603 FindFlatInterpolationVarying(vertexShader->getVaryings(context)));
Xinghua Caob1239382016-12-13 15:07:05 +08001604
1605 if (mRenderer->getMajorShaderModel() >= 4)
1606 {
Martin Radev41ac68e2017-06-06 12:16:58 +03001607 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(
Martin Radevc1d4e552017-08-21 12:01:10 +03001608 packing, builtins, mHasANGLEMultiviewEnabled,
1609 metadata.canSelectViewInVertexShader());
Xinghua Caob1239382016-12-13 15:07:05 +08001610 }
1611
Jamie Madillbd044ed2017-06-05 12:59:21 -04001612 initAttribLocationsToD3DSemantic(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001613
Jamie Madillbd044ed2017-06-05 12:59:21 -04001614 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001615
1616 gatherTransformFeedbackVaryings(packing, builtins[SHADER_VERTEX]);
1617
Jamie Madill9cf9e872017-06-05 12:59:25 -04001618 gl::LinkResult result = compileProgramExecutables(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001619 if (result.isError())
1620 {
1621 infoLog << result.getError().getMessage();
1622 return result;
1623 }
1624 else if (!result.getResult())
1625 {
1626 infoLog << "Failed to create D3D shaders.";
1627 return result;
1628 }
1629
Jamie Madillbd044ed2017-06-05 12:59:21 -04001630 initUniformBlockInfo(context, vertexShader);
1631 initUniformBlockInfo(context, fragmentShader);
Austin Kinross02df7962015-07-01 10:03:42 -07001632 }
1633
Jamie Madillb0a838b2016-11-13 20:02:12 -05001634 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001635}
1636
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001637GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001638{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001639 // TODO(jmadill): Do something useful here?
1640 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001641}
1642
Jamie Madillbd044ed2017-06-05 12:59:21 -04001643void ProgramD3D::initUniformBlockInfo(const gl::Context *context, gl::Shader *shader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001644{
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001645 for (const sh::InterfaceBlock &interfaceBlock : shader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001646 {
Xinghua Caob1239382016-12-13 15:07:05 +08001647 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001648 continue;
1649
Xinghua Caob1239382016-12-13 15:07:05 +08001650 if (mBlockDataSizes.count(interfaceBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001651 continue;
1652
Xinghua Caob1239382016-12-13 15:07:05 +08001653 size_t dataSize = getUniformBlockInfo(interfaceBlock);
1654 mBlockDataSizes[interfaceBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001655 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001656}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001657
Jamie Madill51f522f2016-12-21 15:10:55 -05001658void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001659{
Jamie Madill51f522f2016-12-21 15:10:55 -05001660 // Lazy init.
1661 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1662 {
1663 return;
1664 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001665
1666 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001667 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1668 const ShaderD3D *fragmentShaderD3D =
1669 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1670 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001671
Jamie Madill48ef11b2016-04-27 15:21:52 -04001672 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001673 {
1674 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1675
Jamie Madill4a3c2342015-10-08 12:58:45 -04001676 D3DUniformBlock d3dUniformBlock;
1677
Jamie Madill62d31cb2015-09-11 13:25:51 -04001678 if (uniformBlock.vertexStaticUse)
1679 {
Xinghua Caob1239382016-12-13 15:07:05 +08001680 ASSERT(vertexShaderD3D != nullptr);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001681 unsigned int baseRegister = vertexShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001682 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001683 }
1684
1685 if (uniformBlock.fragmentStaticUse)
1686 {
Xinghua Caob1239382016-12-13 15:07:05 +08001687 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001688 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001689 fragmentShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001690 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001691 }
1692
Xinghua Caob1239382016-12-13 15:07:05 +08001693 if (uniformBlock.computeStaticUse)
1694 {
1695 ASSERT(computeShaderD3D != nullptr);
1696 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001697 computeShaderD3D->getUniformBlockRegister(uniformBlock.name);
Xinghua Caob1239382016-12-13 15:07:05 +08001698 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1699 }
1700
Jamie Madill4a3c2342015-10-08 12:58:45 -04001701 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001702 }
1703}
1704
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001705void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001706{
1707 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001708 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001709 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001710 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001711 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001712 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001713 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001714 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001715 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001716 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001717 vertexRegisters = std::max(vertexRegisters,
1718 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001719 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001720 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001721 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001722 fragmentRegisters = std::max(
1723 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001724 }
Xinghua Caob1239382016-12-13 15:07:05 +08001725 if (d3dUniform->isReferencedByComputeShader())
1726 {
1727 computeRegisters = std::max(
1728 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1729 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001730 }
1731 }
1732
Xinghua Caob1239382016-12-13 15:07:05 +08001733 mVertexUniformStorage =
1734 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1735 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1736 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1737 mComputeUniformStorage =
1738 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001739
1740 // Iterate the uniforms again to assign data pointers to default block uniforms.
1741 for (D3DUniform *d3dUniform : mD3DUniforms)
1742 {
1743 if (d3dUniform->isSampler())
1744 {
1745 d3dUniform->mSamplerData.resize(d3dUniform->elementCount(), 0);
1746 continue;
1747 }
1748
1749 if (d3dUniform->isReferencedByVertexShader())
1750 {
1751 d3dUniform->vsData = mVertexUniformStorage->getDataPointer(d3dUniform->vsRegisterIndex,
1752 d3dUniform->registerElement);
1753 }
1754
1755 if (d3dUniform->isReferencedByFragmentShader())
1756 {
1757 d3dUniform->psData = mFragmentUniformStorage->getDataPointer(
1758 d3dUniform->psRegisterIndex, d3dUniform->registerElement);
1759 }
1760
1761 if (d3dUniform->isReferencedByComputeShader())
1762 {
1763 d3dUniform->csData = mComputeUniformStorage->getDataPointer(
1764 d3dUniform->csRegisterIndex, d3dUniform->registerElement);
1765 }
1766 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001767}
1768
Jamie Madill561ed3a2017-08-31 16:48:09 -04001769gl::Error ProgramD3D::applyUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001770{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001771 ASSERT(!mDirtySamplerMapping);
Jamie Madill561ed3a2017-08-31 16:48:09 -04001772 ANGLE_TRY(mRenderer->applyUniforms(*this, mD3DUniforms));
1773 mUniformsDirty = false;
Jamie Madill51f522f2016-12-21 15:10:55 -05001774 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001775}
1776
Xinghua Cao73badc02017-03-29 19:14:53 +08001777gl::Error ProgramD3D::applyComputeUniforms()
1778{
1779 ASSERT(!mDirtySamplerMapping);
1780 ANGLE_TRY(mRenderer->applyComputeUniforms(*this, mD3DUniforms));
Jamie Madill561ed3a2017-08-31 16:48:09 -04001781 mUniformsDirty = false;
Xinghua Cao73badc02017-03-29 19:14:53 +08001782 return gl::NoError();
1783}
1784
Jamie Madill9082b982016-04-27 15:21:51 -04001785gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001786{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001787 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001788 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001789 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001790 }
1791
Jamie Madill51f522f2016-12-21 15:10:55 -05001792 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001793
Jamie Madill03260fa2015-06-22 13:57:22 -04001794 mVertexUBOCache.clear();
1795 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001796
1797 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1798 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1799
Jamie Madill4a3c2342015-10-08 12:58:45 -04001800 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001801 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001802 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001803 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001804 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001805
Brandon Jones18bd4102014-09-22 14:21:44 -07001806 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001807 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001808 {
1809 continue;
1810 }
1811
Jamie Madill4a3c2342015-10-08 12:58:45 -04001812 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001813 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001814 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001815 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001816
Jamie Madill969194d2015-07-20 14:36:56 -04001817 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001818 {
1819 mVertexUBOCache.resize(registerIndex + 1, -1);
1820 }
1821
1822 ASSERT(mVertexUBOCache[registerIndex] == -1);
1823 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001824 }
1825
Jamie Madill4a3c2342015-10-08 12:58:45 -04001826 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001827 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001828 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001829 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001830
1831 if (mFragmentUBOCache.size() <= registerIndex)
1832 {
1833 mFragmentUBOCache.resize(registerIndex + 1, -1);
1834 }
1835
1836 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1837 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001838 }
1839 }
1840
Jamie Madill03260fa2015-06-22 13:57:22 -04001841 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001842}
1843
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001844void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001845{
Jamie Madill561ed3a2017-08-31 16:48:09 -04001846 mUniformsDirty = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001847}
1848
Jamie Madill334d6152015-10-22 14:00:28 -04001849void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001850{
Jamie Madill134f93d2017-08-31 17:11:00 -04001851 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_FLOAT));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001852}
1853
1854void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1855{
Jamie Madill134f93d2017-08-31 17:11:00 -04001856 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_FLOAT_VEC2));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001857}
1858
1859void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1860{
Jamie Madill134f93d2017-08-31 17:11:00 -04001861 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_FLOAT_VEC3));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001862}
1863
1864void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1865{
Jamie Madill134f93d2017-08-31 17:11:00 -04001866 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_FLOAT_VEC4));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001867}
1868
Jamie Madill334d6152015-10-22 14:00:28 -04001869void ProgramD3D::setUniformMatrix2fv(GLint location,
1870 GLsizei count,
1871 GLboolean transpose,
1872 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001873{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001874 setUniformMatrixfvInternal<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001875}
1876
Jamie Madill334d6152015-10-22 14:00:28 -04001877void ProgramD3D::setUniformMatrix3fv(GLint location,
1878 GLsizei count,
1879 GLboolean transpose,
1880 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001881{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001882 setUniformMatrixfvInternal<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001883}
1884
Jamie Madill334d6152015-10-22 14:00:28 -04001885void ProgramD3D::setUniformMatrix4fv(GLint location,
1886 GLsizei count,
1887 GLboolean transpose,
1888 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001889{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001890 setUniformMatrixfvInternal<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001891}
1892
Jamie Madill334d6152015-10-22 14:00:28 -04001893void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1894 GLsizei count,
1895 GLboolean transpose,
1896 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001897{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001898 setUniformMatrixfvInternal<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001899}
1900
Jamie Madill334d6152015-10-22 14:00:28 -04001901void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1902 GLsizei count,
1903 GLboolean transpose,
1904 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001905{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001906 setUniformMatrixfvInternal<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001907}
1908
Jamie Madill334d6152015-10-22 14:00:28 -04001909void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1910 GLsizei count,
1911 GLboolean transpose,
1912 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001913{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001914 setUniformMatrixfvInternal<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001915}
1916
Jamie Madill334d6152015-10-22 14:00:28 -04001917void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1918 GLsizei count,
1919 GLboolean transpose,
1920 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001921{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001922 setUniformMatrixfvInternal<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001923}
1924
Jamie Madill334d6152015-10-22 14:00:28 -04001925void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1926 GLsizei count,
1927 GLboolean transpose,
1928 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001929{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001930 setUniformMatrixfvInternal<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001931}
1932
Jamie Madill334d6152015-10-22 14:00:28 -04001933void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1934 GLsizei count,
1935 GLboolean transpose,
1936 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001937{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001938 setUniformMatrixfvInternal<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001939}
1940
1941void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1942{
Jamie Madill134f93d2017-08-31 17:11:00 -04001943 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_INT));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001944}
1945
1946void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1947{
Jamie Madill134f93d2017-08-31 17:11:00 -04001948 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_INT_VEC2));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001949}
1950
1951void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1952{
Jamie Madill134f93d2017-08-31 17:11:00 -04001953 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_INT_VEC3));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001954}
1955
1956void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1957{
Jamie Madill134f93d2017-08-31 17:11:00 -04001958 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_INT_VEC4));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001959}
1960
1961void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1962{
Jamie Madill134f93d2017-08-31 17:11:00 -04001963 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_UNSIGNED_INT));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964}
1965
1966void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1967{
Jamie Madill134f93d2017-08-31 17:11:00 -04001968 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_UNSIGNED_INT_VEC2));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001969}
1970
1971void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1972{
Jamie Madill134f93d2017-08-31 17:11:00 -04001973 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_UNSIGNED_INT_VEC3));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001974}
1975
1976void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1977{
Jamie Madill134f93d2017-08-31 17:11:00 -04001978 setUniformInternal(location, count, v, gl::GetUniformTypeInfo(GL_UNSIGNED_INT_VEC4));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001979}
1980
Jamie Madill4a3c2342015-10-08 12:58:45 -04001981void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1982 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001983{
1984}
1985
Jamie Madillbd044ed2017-06-05 12:59:21 -04001986void ProgramD3D::defineUniformsAndAssignRegisters(const gl::Context *context)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001987{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001988 D3DUniformMap uniformMap;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001989 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001990 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05001991 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001992 for (const sh::Uniform &computeUniform : computeShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04001993 {
Xinghua Caob1239382016-12-13 15:07:05 +08001994 if (computeUniform.staticUse)
1995 {
1996 defineUniformBase(computeShader, computeUniform, &uniformMap);
1997 }
Jamie Madillfb536032015-09-11 13:19:49 -04001998 }
1999 }
Xinghua Caob1239382016-12-13 15:07:05 +08002000 else
Jamie Madillfb536032015-09-11 13:19:49 -04002001 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002002 gl::Shader *vertexShader = mState.getAttachedVertexShader();
2003 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04002004 {
Xinghua Caob1239382016-12-13 15:07:05 +08002005 if (vertexUniform.staticUse)
2006 {
2007 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
2008 }
2009 }
2010
Jamie Madillbd044ed2017-06-05 12:59:21 -04002011 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
2012 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms(context))
Xinghua Caob1239382016-12-13 15:07:05 +08002013 {
2014 if (fragmentUniform.staticUse)
2015 {
2016 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
2017 }
Jamie Madillfb536032015-09-11 13:19:49 -04002018 }
2019 }
2020
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002021 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002022 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002023 {
2024 if (!glUniform.isInDefaultBlock())
2025 continue;
2026
2027 auto mapEntry = uniformMap.find(glUniform.name);
2028 ASSERT(mapEntry != uniformMap.end());
2029 mD3DUniforms.push_back(mapEntry->second);
2030 }
2031
Jamie Madill62d31cb2015-09-11 13:25:51 -04002032 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04002033 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04002034}
2035
Jamie Madill91445bc2015-09-23 16:47:53 -04002036void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002037 const sh::Uniform &uniform,
2038 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04002039{
Olli Etuaho96963162016-03-21 11:54:33 +02002040 // Samplers get their registers assigned in assignAllSamplerRegisters.
2041 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04002042 {
Jamie Madill91445bc2015-09-23 16:47:53 -04002043 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04002044 return;
2045 }
2046
Jamie Madill91445bc2015-09-23 16:47:53 -04002047 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
2048
2049 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
2050 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04002051 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002052 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002053
Jamie Madill91445bc2015-09-23 16:47:53 -04002054 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002055}
2056
Jamie Madill62d31cb2015-09-11 13:25:51 -04002057D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
2058{
2059 for (D3DUniform *d3dUniform : mD3DUniforms)
2060 {
2061 if (d3dUniform->name == name)
2062 {
2063 return d3dUniform;
2064 }
2065 }
2066
2067 return nullptr;
2068}
2069
Jamie Madill91445bc2015-09-23 16:47:53 -04002070void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002071 const sh::ShaderVariable &uniform,
2072 const std::string &fullName,
2073 sh::HLSLBlockEncoder *encoder,
2074 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002075{
2076 if (uniform.isStruct())
2077 {
2078 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2079 {
2080 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2081
Jamie Madill55def582015-05-04 11:24:57 -04002082 if (encoder)
2083 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002084
2085 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2086 {
Jamie Madill334d6152015-10-22 14:00:28 -04002087 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002088 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2089
Olli Etuaho96963162016-03-21 11:54:33 +02002090 // Samplers get their registers assigned in assignAllSamplerRegisters.
2091 // Also they couldn't use the same encoder as the rest of the struct, since they are
2092 // extracted out of the struct by the shader translator.
2093 if (gl::IsSamplerType(field.type))
2094 {
2095 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2096 }
2097 else
2098 {
2099 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2100 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002101 }
2102
Jamie Madill55def582015-05-04 11:24:57 -04002103 if (encoder)
2104 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002105 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002106 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002107 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002108
2109 // Not a struct. Arrays are treated as aggregate types.
2110 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002111 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002112 encoder->enterAggregateType();
2113 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002114
Jamie Madill62d31cb2015-09-11 13:25:51 -04002115 // Advance the uniform offset, to track registers allocation for structs
2116 sh::BlockMemberInfo blockInfo =
2117 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2118 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002119
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002120 auto uniformMapEntry = uniformMap->find(fullName);
2121 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002122
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002123 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002124 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002125 d3dUniform = uniformMapEntry->second;
2126 }
2127 else
2128 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002129 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002130 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002131 }
2132
2133 if (encoder)
2134 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002135 d3dUniform->registerElement =
2136 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002137 unsigned int reg =
2138 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002139 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002140 {
2141 d3dUniform->psRegisterIndex = reg;
2142 }
Xinghua Caob1239382016-12-13 15:07:05 +08002143 else if (shaderType == GL_VERTEX_SHADER)
2144 {
2145 d3dUniform->vsRegisterIndex = reg;
2146 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002147 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002148 {
Xinghua Caob1239382016-12-13 15:07:05 +08002149 ASSERT(shaderType == GL_COMPUTE_SHADER);
2150 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002151 }
Jamie Madillfb536032015-09-11 13:19:49 -04002152
2153 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002154 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002155 {
2156 encoder->exitAggregateType();
2157 }
2158 }
2159}
2160
Jamie Madill134f93d2017-08-31 17:11:00 -04002161// Assume count is already clamped.
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002162template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002163void ProgramD3D::setUniformImpl(const gl::VariableLocation &locationInfo,
Jamie Madill134f93d2017-08-31 17:11:00 -04002164 GLsizei count,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002165 const T *v,
2166 uint8_t *targetData,
Jamie Madill134f93d2017-08-31 17:11:00 -04002167 const gl::UniformTypeInfo &uniformTypeInfo)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002168{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002169 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
Jamie Madill134f93d2017-08-31 17:11:00 -04002170 const int components = uniformTypeInfo.componentCount;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002171 unsigned int arrayElement = locationInfo.element;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002172
Jamie Madill134f93d2017-08-31 17:11:00 -04002173 if (targetUniform->type == uniformTypeInfo.type)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002174 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002175 T *target = reinterpret_cast<T *>(targetData) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002176
Jamie Madill134f93d2017-08-31 17:11:00 -04002177 for (GLint i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002178 {
Jamie Madill334d6152015-10-22 14:00:28 -04002179 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002180 const T *source = v + (i * components);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002181 memcpy(dest, source, components * sizeof(T));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002182 }
2183 }
Jamie Madill134f93d2017-08-31 17:11:00 -04002184 else if (targetUniform->type == uniformTypeInfo.boolVectorType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002185 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002186 GLint *boolParams = reinterpret_cast<GLint *>(targetData) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002187
Jamie Madill134f93d2017-08-31 17:11:00 -04002188 for (GLint i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002189 {
Jamie Madill334d6152015-10-22 14:00:28 -04002190 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002191 const T *source = v + (i * components);
2192
2193 for (int c = 0; c < components; c++)
2194 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002195 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002196 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002197 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002198 }
Jamie Madill334d6152015-10-22 14:00:28 -04002199 else
2200 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002201}
2202
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002203template <typename T>
2204void ProgramD3D::setUniformInternal(GLint location,
2205 GLsizei count,
2206 const T *v,
Jamie Madill134f93d2017-08-31 17:11:00 -04002207 const gl::UniformTypeInfo &uniformTypeInfo)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002208{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002209 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2210 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
2211
Jamie Madill561ed3a2017-08-31 16:48:09 -04002212 mUniformsDirty = true;
2213
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002214 if (!targetUniform->mSamplerData.empty())
2215 {
Jamie Madill134f93d2017-08-31 17:11:00 -04002216 ASSERT(uniformTypeInfo.type == GL_INT);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002217 memcpy(&targetUniform->mSamplerData[locationInfo.element], v, count * sizeof(T));
2218 mDirtySamplerMapping = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002219 return;
2220 }
2221
2222 if (targetUniform->vsData)
2223 {
Jamie Madill134f93d2017-08-31 17:11:00 -04002224 setUniformImpl(locationInfo, count, v, targetUniform->vsData, uniformTypeInfo);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002225 }
2226
2227 if (targetUniform->psData)
2228 {
Jamie Madill134f93d2017-08-31 17:11:00 -04002229 setUniformImpl(locationInfo, count, v, targetUniform->psData, uniformTypeInfo);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002230 }
2231
2232 if (targetUniform->csData)
2233 {
Jamie Madill134f93d2017-08-31 17:11:00 -04002234 setUniformImpl(locationInfo, count, v, targetUniform->csData, uniformTypeInfo);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002235 }
2236}
2237
2238template <int cols, int rows>
2239void ProgramD3D::setUniformMatrixfvImpl(GLint location,
2240 GLsizei countIn,
2241 GLboolean transpose,
2242 const GLfloat *value,
2243 uint8_t *targetData,
2244 GLenum targetUniformType)
2245{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002246 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002247
Jamie Madill62d31cb2015-09-11 13:25:51 -04002248 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002249 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002250 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002251
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002252 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002253 GLfloat *target = reinterpret_cast<GLfloat *>(targetData + arrayElement * sizeof(GLfloat) *
2254 targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002255
Jamie Madill62d31cb2015-09-11 13:25:51 -04002256 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002257 {
2258 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2259 if (transpose == GL_FALSE)
2260 {
Jamie Madill561ed3a2017-08-31 16:48:09 -04002261 TransposeExpandMatrix<GLfloat, cols, rows>(target, value);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002262 }
2263 else
2264 {
Jamie Madill561ed3a2017-08-31 16:48:09 -04002265 ExpandMatrix<GLfloat, cols, rows>(target, value);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002266 }
2267 target += targetMatrixStride;
2268 value += cols * rows;
2269 }
Jamie Madill561ed3a2017-08-31 16:48:09 -04002270
2271 mUniformsDirty = true;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002272}
2273
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002274template <int cols, int rows>
2275void ProgramD3D::setUniformMatrixfvInternal(GLint location,
2276 GLsizei countIn,
2277 GLboolean transpose,
2278 const GLfloat *value,
2279 GLenum targetUniformType)
2280{
2281 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
2282
2283 if (targetUniform->vsData)
2284 {
2285 setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2286 targetUniform->vsData, targetUniformType);
2287 }
2288
2289 if (targetUniform->psData)
2290 {
2291 setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2292 targetUniform->psData, targetUniformType);
2293 }
2294
2295 if (targetUniform->csData)
2296 {
2297 setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2298 targetUniform->csData, targetUniformType);
2299 }
2300}
2301
Jamie Madill4a3c2342015-10-08 12:58:45 -04002302size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002303{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002304 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002305
Jamie Madill62d31cb2015-09-11 13:25:51 -04002306 // define member uniforms
2307 sh::Std140BlockEncoder std140Encoder;
Jamie Madillcc2ed612017-03-14 15:59:00 -04002308 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002309 sh::BlockLayoutEncoder *encoder = nullptr;
2310
2311 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002312 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002313 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002314 }
2315 else
2316 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002317 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002318 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002319
Jamie Madill39046162016-02-08 15:05:17 -05002320 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2321 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002322
2323 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002324}
2325
Jamie Madill62d31cb2015-09-11 13:25:51 -04002326void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002327{
Olli Etuaho96963162016-03-21 11:54:33 +02002328 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002329 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002330 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002331 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002332 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002333 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002334 }
2335}
2336
Olli Etuaho96963162016-03-21 11:54:33 +02002337void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002338{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002339 ASSERT(d3dUniform->isSampler());
Xinghua Caob1239382016-12-13 15:07:05 +08002340 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2341 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002342 {
Xinghua Caob1239382016-12-13 15:07:05 +08002343 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2344 ASSERT(computeShaderD3D->hasUniform(d3dUniform));
2345 d3dUniform->csRegisterIndex = computeShaderD3D->getUniformRegister(d3dUniform->name);
2346 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2347 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2348 mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002349 }
Xinghua Caob1239382016-12-13 15:07:05 +08002350 else
Jamie Madillfb536032015-09-11 13:19:49 -04002351 {
Xinghua Caob1239382016-12-13 15:07:05 +08002352 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2353 const ShaderD3D *fragmentShaderD3D =
2354 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2355 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) ||
2356 fragmentShaderD3D->hasUniform(d3dUniform));
2357 if (vertexShaderD3D->hasUniform(d3dUniform))
2358 {
2359 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2360 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
2361 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2362 mSamplersVS, &mUsedVertexSamplerRange);
2363 }
2364 if (fragmentShaderD3D->hasUniform(d3dUniform))
2365 {
2366 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2367 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
2368 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2369 mSamplersPS, &mUsedPixelSamplerRange);
2370 }
Jamie Madillfb536032015-09-11 13:19:49 -04002371 }
2372}
2373
Jamie Madill62d31cb2015-09-11 13:25:51 -04002374// static
2375void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002376 GLenum samplerType,
2377 unsigned int samplerCount,
2378 std::vector<Sampler> &outSamplers,
2379 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002380{
2381 unsigned int samplerIndex = startSamplerIndex;
2382
2383 do
2384 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002385 ASSERT(samplerIndex < outSamplers.size());
2386 Sampler *sampler = &outSamplers[samplerIndex];
2387 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002388 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002389 sampler->logicalTextureUnit = 0;
2390 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002391 samplerIndex++;
2392 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002393}
2394
Brandon Jonesc9610c52014-08-25 17:02:59 -07002395void ProgramD3D::reset()
2396{
Xinghua Caob1239382016-12-13 15:07:05 +08002397 mVertexExecutables.clear();
2398 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002399
Xinghua Caob1239382016-12-13 15:07:05 +08002400 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002401 {
Xinghua Caob1239382016-12-13 15:07:05 +08002402 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002403 }
Brandon Joneseb994362014-09-24 10:27:28 -07002404
Xinghua Caob1239382016-12-13 15:07:05 +08002405 mComputeExecutable.reset(nullptr);
2406
Brandon Jones22502d52014-08-29 16:58:36 -07002407 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002408 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002409
2410 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002411 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002412 mUsesFragDepth = false;
Martin Radev41ac68e2017-06-06 12:16:58 +03002413 mHasANGLEMultiviewEnabled = false;
2414 mUsesViewID = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002415 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002416 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002417 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002418
Jamie Madill62d31cb2015-09-11 13:25:51 -04002419 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002420 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002421
Xinghua Caob1239382016-12-13 15:07:05 +08002422 mVertexUniformStorage.reset(nullptr);
2423 mFragmentUniformStorage.reset(nullptr);
2424 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002425
2426 mSamplersPS.clear();
2427 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002428 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002429
2430 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002431 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002432 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002433 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002434
Jamie Madill8047c0d2016-03-07 13:02:12 -05002435 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002436
Jamie Madill9fc36822015-11-18 13:08:07 -05002437 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002438
2439 mGeometryShaderPreamble.clear();
Jamie Madill561ed3a2017-08-31 16:48:09 -04002440
2441 mUniformsDirty = true;
Jamie Madill0e7f1732017-09-09 23:32:50 -04002442
2443 mCachedPixelExecutableIndex.reset();
2444 mCachedVertexExecutableIndex.reset();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002445}
2446
Geoff Lang7dd2e102014-11-10 15:19:26 -05002447unsigned int ProgramD3D::getSerial() const
2448{
2449 return mSerial;
2450}
2451
2452unsigned int ProgramD3D::issueSerial()
2453{
2454 return mCurrentSerial++;
2455}
2456
Jamie Madillbd044ed2017-06-05 12:59:21 -04002457void ProgramD3D::initAttribLocationsToD3DSemantic(const gl::Context *context)
Jamie Madill63805b42015-08-25 13:17:39 -04002458{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002459 gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002460 ASSERT(vertexShader != nullptr);
2461
2462 // Init semantic index
Jamie Madill34ca4f52017-06-13 11:49:39 -04002463 int semanticIndex = 0;
2464 for (const sh::Attribute &attribute : vertexShader->getActiveAttributes(context))
Jamie Madill63805b42015-08-25 13:17:39 -04002465 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002466 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill34ca4f52017-06-13 11:49:39 -04002467 GLuint location = mState.getAttributeLocation(attribute.name);
2468 ASSERT(location != std::numeric_limits<GLuint>::max());
Jamie Madill63805b42015-08-25 13:17:39 -04002469
Jamie Madill8047c0d2016-03-07 13:02:12 -05002470 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002471 {
Jamie Madill34ca4f52017-06-13 11:49:39 -04002472 mAttribLocationToD3DSemantic[location + reg] = semanticIndex++;
Jamie Madill63805b42015-08-25 13:17:39 -04002473 }
2474 }
Jamie Madill437d2662014-12-05 14:23:35 -05002475}
2476
Jamie Madilla779b612017-07-24 11:46:05 -04002477void ProgramD3D::updateCachedInputLayout(Serial associatedSerial, const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002478{
Jamie Madilla779b612017-07-24 11:46:05 -04002479 if (mCurrentVertexArrayStateSerial == associatedSerial)
2480 {
2481 return;
2482 }
2483
2484 mCurrentVertexArrayStateSerial = associatedSerial;
Jamie Madillbd136f92015-08-10 14:51:37 -04002485 mCachedInputLayout.clear();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002486
Jamie Madilld3dfda22015-07-06 08:28:49 -04002487 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002488
Jamie Madill6de51852017-04-12 09:53:01 -04002489 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002490 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002491 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002492
Jamie Madill8047c0d2016-03-07 13:02:12 -05002493 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002494 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002495 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002496 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002497 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002498 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002499 mCachedInputLayout[d3dSemantic] =
2500 GetVertexFormatType(vertexAttributes[locationIndex],
2501 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002502 }
2503 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002504
2505 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04002506
2507 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002508}
2509
2510void ProgramD3D::updateCachedOutputLayout(const gl::Context *context,
2511 const gl::Framebuffer *framebuffer)
2512{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002513 mPixelShaderOutputLayoutCache.clear();
2514
2515 FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(framebuffer);
2516 const auto &colorbuffers = fboD3D->getColorAttachmentsForRender(context);
2517
2518 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
2519 {
2520 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
2521
2522 if (colorbuffer)
2523 {
2524 auto binding = colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0
2525 : colorbuffer->getBinding();
2526 mPixelShaderOutputLayoutCache.push_back(binding);
2527 }
2528 else
2529 {
2530 mPixelShaderOutputLayoutCache.push_back(GL_NONE);
2531 }
2532 }
2533
2534 updateCachedPixelExecutableIndex();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002535}
2536
Jamie Madill192745a2016-12-22 15:58:21 -05002537void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2538 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002539{
Jamie Madill9fc36822015-11-18 13:08:07 -05002540 const std::string &varyingSemantic =
2541 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2542
Jamie Madillccdf74b2015-08-18 10:46:12 -04002543 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002544 mStreamOutVaryings.clear();
2545
Jamie Madill48ef11b2016-04-27 15:21:52 -04002546 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002547 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2548 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002549 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002550 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2551 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002552 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002553 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002554 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002555 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2556 builtins.glPosition.index, 4, outputSlot));
2557 }
2558 }
2559 else if (tfVaryingName == "gl_FragCoord")
2560 {
2561 if (builtins.glFragCoord.enabled)
2562 {
2563 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2564 builtins.glFragCoord.index, 4, outputSlot));
2565 }
2566 }
2567 else if (tfVaryingName == "gl_PointSize")
2568 {
2569 if (builtins.glPointSize.enabled)
2570 {
2571 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2572 }
2573 }
2574 else
2575 {
jchen10a9042d32017-03-17 08:50:45 +08002576 size_t subscript = GL_INVALID_INDEX;
2577 std::string baseName = gl::ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002578 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002579 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002580 const auto &varying = *registerInfo.packedVarying->varying;
2581 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002582 int componentCount = gl::VariableColumnCount(transposedType);
2583 ASSERT(!varying.isBuiltIn());
2584
Jamie Madill55c25d02015-11-18 13:08:08 -05002585 // Transform feedback for varying structs is underspecified.
2586 // See Khronos bug 9856.
2587 // TODO(jmadill): Figure out how to be spec-compliant here.
2588 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2589 continue;
2590
Jamie Madill9fc36822015-11-18 13:08:07 -05002591 // There can be more than one register assigned to a particular varying, and each
2592 // register needs its own stream out entry.
jchen10a9042d32017-03-17 08:50:45 +08002593 if (baseName == registerInfo.packedVarying->varying->name &&
2594 (subscript == GL_INVALID_INDEX || subscript == registerInfo.varyingArrayIndex))
Jamie Madill9fc36822015-11-18 13:08:07 -05002595 {
2596 mStreamOutVaryings.push_back(D3DVarying(
2597 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2598 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002599 }
2600 }
2601 }
2602}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002603
2604D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2605{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002606 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002607}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002608
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002609const D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location) const
2610{
2611 return mD3DUniforms[mState.getUniformLocations()[location].index];
2612}
2613
Olli Etuaho855d9642017-05-17 14:05:06 +03002614bool ProgramD3D::getUniformBlockSize(const std::string &blockName,
2615 const std::string & /* blockMappedName */,
2616 size_t *sizeOut) const
Jamie Madill4a3c2342015-10-08 12:58:45 -04002617{
2618 std::string baseName = blockName;
2619 gl::ParseAndStripArrayIndex(&baseName);
2620
2621 auto sizeIter = mBlockDataSizes.find(baseName);
2622 if (sizeIter == mBlockDataSizes.end())
2623 {
2624 *sizeOut = 0;
2625 return false;
2626 }
2627
2628 *sizeOut = sizeIter->second;
2629 return true;
2630}
2631
2632bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
Olli Etuaho855d9642017-05-17 14:05:06 +03002633 const std::string & /* memberUniformMappedName */,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002634 sh::BlockMemberInfo *memberInfoOut) const
2635{
2636 auto infoIter = mBlockInfo.find(memberUniformName);
2637 if (infoIter == mBlockInfo.end())
2638 {
2639 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2640 return false;
2641 }
2642
2643 *memberInfoOut = infoIter->second;
2644 return true;
2645}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002646
2647void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2648 GLenum genMode,
2649 GLint components,
2650 const GLfloat *coeffs)
2651{
2652 UNREACHABLE();
2653}
2654
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002655bool ProgramD3D::hasVertexExecutableForCachedInputLayout()
2656{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002657 return mCachedVertexExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002658}
2659
2660bool ProgramD3D::hasGeometryExecutableForPrimitiveType(GLenum drawMode)
2661{
2662 if (!usesGeometryShader(drawMode))
2663 {
2664 // No shader necessary mean we have the required (null) executable.
2665 return true;
2666 }
2667
2668 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
2669 return mGeometryExecutables[geometryShaderType].get() != nullptr;
2670}
2671
2672bool ProgramD3D::hasPixelExecutableForCachedOutputLayout()
2673{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002674 return mCachedPixelExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002675}
2676
Jamie Madill54164b02017-08-28 15:17:37 -04002677template <typename DestT>
2678void ProgramD3D::getUniformInternal(GLint location, DestT *dataOut) const
2679{
2680 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2681 const gl::LinkedUniform &uniform = mState.getUniforms()[locationInfo.index];
2682
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002683 const D3DUniform *targetUniform = getD3DUniformFromLocation(location);
2684 const uint8_t *srcPointer = targetUniform->getDataPtrToElement(locationInfo.element);
2685
2686 if (gl::IsMatrixType(uniform.type))
2687 {
2688 GetMatrixUniform(gl::VariableColumnCount(uniform.type), gl::VariableRowCount(uniform.type),
2689 dataOut, reinterpret_cast<const DestT *>(srcPointer));
2690 }
2691 else
2692 {
2693 memcpy(dataOut, srcPointer, uniform.getElementSize());
2694 }
Jamie Madill54164b02017-08-28 15:17:37 -04002695}
2696
2697void ProgramD3D::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
2698{
2699 getUniformInternal(location, params);
2700}
2701
2702void ProgramD3D::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
2703{
2704 getUniformInternal(location, params);
2705}
2706
2707void ProgramD3D::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
2708{
2709 getUniformInternal(location, params);
2710}
2711
Jamie Madill0e7f1732017-09-09 23:32:50 -04002712void ProgramD3D::updateCachedVertexExecutableIndex()
2713{
2714 mCachedVertexExecutableIndex.reset();
2715 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
2716 {
2717 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
2718 {
2719 mCachedVertexExecutableIndex = executableIndex;
2720 break;
2721 }
2722 }
2723}
2724
2725void ProgramD3D::updateCachedPixelExecutableIndex()
2726{
2727 mCachedPixelExecutableIndex.reset();
2728 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
2729 {
2730 if (mPixelExecutables[executableIndex]->matchesSignature(mPixelShaderOutputLayoutCache))
2731 {
2732 mCachedPixelExecutableIndex = executableIndex;
2733 break;
2734 }
2735 }
2736}
2737
Jamie Madill8047c0d2016-03-07 13:02:12 -05002738} // namespace rx