blob: 3c4c43293d7b9d5fb9d52b822343ba44719ebefe [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Jamie Madill20e005b2017-04-07 14:19:22 -040011#include "common/bitset_utils.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Jamie Madillc564c072017-06-01 12:45:42 -040013#include "libANGLE/Context.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Framebuffer.h"
15#include "libANGLE/FramebufferAttachment.h"
16#include "libANGLE/Program.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040017#include "libANGLE/Uniform.h"
Jamie Madill192745a2016-12-22 15:58:21 -050018#include "libANGLE/VaryingPacking.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040019#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000020#include "libANGLE/features.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050021#include "libANGLE/renderer/ContextImpl.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050022#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050023#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050024#include "libANGLE/renderer/d3d/RendererD3D.h"
25#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050026#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050027#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050028
Jamie Madill01074252016-11-28 15:55:51 -050029using namespace angle;
30
Brandon Jonesc9610c52014-08-25 17:02:59 -070031namespace rx
32{
33
Brandon Joneseb994362014-09-24 10:27:28 -070034namespace
35{
36
Jamie Madill4c19a8a2017-07-24 11:46:06 -040037void GetDefaultInputLayoutFromShader(const gl::Context *context,
38 gl::Shader *vertexShader,
39 gl::InputLayout *inputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070040{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040041 inputLayoutOut->clear();
42
Jamie Madillbd044ed2017-06-05 12:59:21 -040043 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes(context))
Brandon Joneseb994362014-09-24 10:27:28 -070044 {
Brandon Joneseb994362014-09-24 10:27:28 -070045 if (shaderAttr.type != GL_NONE)
46 {
47 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
48
Jamie Madilld3dfda22015-07-06 08:28:49 -040049 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040050 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070051 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040052 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040053 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040054 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040055 gl::VertexFormatType defaultType =
56 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070057
Jamie Madill4c19a8a2017-07-24 11:46:06 -040058 inputLayoutOut->push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070059 }
60 }
61 }
62}
63
Jamie Madill4c19a8a2017-07-24 11:46:06 -040064void GetDefaultOutputLayoutFromShader(
65 const std::vector<PixelShaderOutputVariable> &shaderOutputVars,
66 std::vector<GLenum> *outputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070067{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040068 outputLayoutOut->clear();
Brandon Joneseb994362014-09-24 10:27:28 -070069
Jamie Madillb4463142014-12-19 14:56:54 -050070 if (!shaderOutputVars.empty())
71 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -040072 outputLayoutOut->push_back(GL_COLOR_ATTACHMENT0 +
73 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050074 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -040075}
Brandon Joneseb994362014-09-24 10:27:28 -070076
Jamie Madill4c19a8a2017-07-24 11:46:06 -040077void GetPixelOutputLayoutFromFramebuffer(const gl::Context *context,
78 const gl::Framebuffer *framebuffer,
79 std::vector<GLenum> *signature)
80{
81 signature->clear();
82
83 FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(framebuffer);
84 const auto &colorbuffers = fboD3D->getColorAttachmentsForRender(context);
85
86 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
87 {
88 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
89
90 if (colorbuffer)
91 {
92 signature->push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0
93 : colorbuffer->getBinding());
94 }
95 else
96 {
97 signature->push_back(GL_NONE);
98 }
99 }
Brandon Joneseb994362014-09-24 10:27:28 -0700100}
101
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700102bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
103{
104 return var.isRowMajorLayout;
105}
106
107bool IsRowMajorLayout(const sh::ShaderVariable &var)
108{
109 return false;
110}
111
Jamie Madill62d31cb2015-09-11 13:25:51 -0400112template <typename VarT>
113void GetUniformBlockInfo(const std::vector<VarT> &fields,
114 const std::string &prefix,
115 sh::BlockLayoutEncoder *encoder,
116 bool inRowMajorLayout,
117 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
118{
119 for (const VarT &field : fields)
120 {
121 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
122
123 if (field.isStruct())
124 {
125 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
126
127 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
128 {
129 encoder->enterAggregateType();
130
131 const std::string uniformElementName =
132 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
133 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
134 blockInfoOut);
135
136 encoder->exitAggregateType();
137 }
138 }
139 else
140 {
141 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
142 (*blockInfoOut)[fieldName] =
143 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
144 }
145 }
146}
147
Jamie Madill334d6152015-10-22 14:00:28 -0400148template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100149static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
150{
Yunchao He4f285442017-04-21 12:15:49 +0800151 ASSERT(dest != nullptr);
152 ASSERT(dirtyFlag != nullptr);
Jacek Caban2302e692015-12-01 12:44:43 +0100153
154 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
155 *dest = source;
156}
157
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800158template <typename T, int cols, int rows>
159bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400160{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800161 constexpr int targetWidth = 4;
162 constexpr int targetHeight = rows;
163 constexpr int srcWidth = rows;
164 constexpr int srcHeight = cols;
165
166 constexpr int copyWidth = std::min(targetHeight, srcWidth);
167 constexpr int copyHeight = std::min(targetWidth, srcHeight);
168
169 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400170
171 for (int x = 0; x < copyWidth; x++)
172 {
173 for (int y = 0; y < copyHeight; y++)
174 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800175 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400176 }
177 }
178
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800179 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
180 {
181 return false;
182 }
183
184 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
185 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400186}
187
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800188template <typename T, int cols, int rows>
189bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400190{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800191 constexpr int targetWidth = 4;
192 constexpr int targetHeight = rows;
193 constexpr int srcWidth = cols;
194 constexpr int srcHeight = rows;
195
196 constexpr int copyWidth = std::min(targetWidth, srcWidth);
197 constexpr int copyHeight = std::min(targetHeight, srcHeight);
198
199 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400200
201 for (int y = 0; y < copyHeight; y++)
202 {
203 for (int x = 0; x < copyWidth; x++)
204 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800205 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400206 }
207 }
208
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800209 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
210 {
211 return false;
212 }
213
214 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
215 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400216}
217
Jamie Madill4e31ad52015-10-29 10:32:57 -0400218gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
219{
220 switch (drawMode)
221 {
222 // Uses the point sprite geometry shader.
223 case GL_POINTS:
224 return gl::PRIMITIVE_POINTS;
225
226 // All line drawing uses the same geometry shader.
227 case GL_LINES:
228 case GL_LINE_STRIP:
229 case GL_LINE_LOOP:
230 return gl::PRIMITIVE_LINES;
231
232 // The triangle fan primitive is emulated with strips in D3D11.
233 case GL_TRIANGLES:
234 case GL_TRIANGLE_FAN:
235 return gl::PRIMITIVE_TRIANGLES;
236
237 // Special case for triangle strips.
238 case GL_TRIANGLE_STRIP:
239 return gl::PRIMITIVE_TRIANGLE_STRIP;
240
241 default:
242 UNREACHABLE();
243 return gl::PRIMITIVE_TYPE_MAX;
244 }
245}
246
Jamie Madill192745a2016-12-22 15:58:21 -0500247bool FindFlatInterpolationVarying(const std::vector<sh::Varying> &varyings)
248{
249 // Note: this assumes nested structs can only be packed with one interpolation.
250 for (const auto &varying : varyings)
251 {
252 if (varying.interpolation == sh::INTERPOLATION_FLAT)
253 {
254 return true;
255 }
256 }
257
258 return false;
259}
260
Jamie Madillada9ecc2015-08-17 12:53:37 -0400261} // anonymous namespace
262
Jamie Madill28afae52015-11-09 15:07:57 -0500263// D3DUniform Implementation
264
Jamie Madill62d31cb2015-09-11 13:25:51 -0400265D3DUniform::D3DUniform(GLenum typeIn,
266 const std::string &nameIn,
267 unsigned int arraySizeIn,
268 bool defaultBlock)
269 : type(typeIn),
270 name(nameIn),
271 arraySize(arraySizeIn),
272 data(nullptr),
273 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400274 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400275 psRegisterIndex(GL_INVALID_INDEX),
Xinghua Caob1239382016-12-13 15:07:05 +0800276 csRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400277 registerCount(0),
278 registerElement(0)
279{
280 // We use data storage for default block uniforms to cache values that are sent to D3D during
281 // rendering
282 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
283 if (defaultBlock)
284 {
285 size_t bytes = gl::VariableInternalSize(type) * elementCount();
286 data = new uint8_t[bytes];
287 memset(data, 0, bytes);
288
Jamie Madillcc2ed612017-03-14 15:59:00 -0400289 // Use the row count as register count, will work for non-square matrices.
Jamie Madill62d31cb2015-09-11 13:25:51 -0400290 registerCount = gl::VariableRowCount(type) * elementCount();
291 }
292}
293
294D3DUniform::~D3DUniform()
295{
296 SafeDeleteArray(data);
297}
298
299bool D3DUniform::isSampler() const
300{
301 return gl::IsSamplerType(type);
302}
303
304bool D3DUniform::isReferencedByVertexShader() const
305{
306 return vsRegisterIndex != GL_INVALID_INDEX;
307}
308
309bool D3DUniform::isReferencedByFragmentShader() const
310{
311 return psRegisterIndex != GL_INVALID_INDEX;
312}
313
Xinghua Caob1239382016-12-13 15:07:05 +0800314bool D3DUniform::isReferencedByComputeShader() const
315{
316 return csRegisterIndex != GL_INVALID_INDEX;
317}
318
Jamie Madill28afae52015-11-09 15:07:57 -0500319// D3DVarying Implementation
320
Jamie Madill9fc36822015-11-18 13:08:07 -0500321D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500322{
323}
324
Jamie Madill9fc36822015-11-18 13:08:07 -0500325D3DVarying::D3DVarying(const std::string &semanticNameIn,
326 unsigned int semanticIndexIn,
327 unsigned int componentCountIn,
328 unsigned int outputSlotIn)
329 : semanticName(semanticNameIn),
330 semanticIndex(semanticIndexIn),
331 componentCount(componentCountIn),
332 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500333{
334}
335
Jamie Madille39a3f02015-11-17 20:42:15 -0500336// ProgramD3DMetadata Implementation
337
Jamie Madillc9bde922016-07-24 17:58:50 -0400338ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500339 const ShaderD3D *vertexShader,
340 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400341 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
342 mShaderModelSuffix(renderer->getShaderModelSuffix()),
343 mUsesInstancedPointSpriteEmulation(
344 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
345 mUsesViewScale(renderer->presentPathFastEnabled()),
Martin Radev41ac68e2017-06-06 12:16:58 +0300346 mHasANGLEMultiviewEnabled(vertexShader->hasANGLEMultiviewEnabled()),
347 mUsesViewID(fragmentShader->usesViewID()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500348 mVertexShader(vertexShader),
349 mFragmentShader(fragmentShader)
350{
351}
352
353int ProgramD3DMetadata::getRendererMajorShaderModel() const
354{
355 return mRendererMajorShaderModel;
356}
357
Jamie Madill9082b982016-04-27 15:21:51 -0400358bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500359{
Corentin Wallezc084de12017-06-05 14:28:52 -0700360 return (mFragmentShader->usesFragColor() && mFragmentShader->usesMultipleRenderTargets() &&
361 data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500362}
363
Jamie Madill48ef11b2016-04-27 15:21:52 -0400364bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500365{
Jamie Madill63286672015-11-24 13:00:08 -0500366 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500367}
368
369bool ProgramD3DMetadata::usesPointCoord() const
370{
371 return mFragmentShader->usesPointCoord();
372}
373
374bool ProgramD3DMetadata::usesFragCoord() const
375{
376 return mFragmentShader->usesFragCoord();
377}
378
379bool ProgramD3DMetadata::usesPointSize() const
380{
381 return mVertexShader->usesPointSize();
382}
383
384bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
385{
Jamie Madillc9bde922016-07-24 17:58:50 -0400386 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
387 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500388}
389
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800390bool ProgramD3DMetadata::usesViewScale() const
391{
392 return mUsesViewScale;
393}
394
Martin Radev41ac68e2017-06-06 12:16:58 +0300395bool ProgramD3DMetadata::hasANGLEMultiviewEnabled() const
396{
397 return mHasANGLEMultiviewEnabled;
398}
399
400bool ProgramD3DMetadata::usesViewID() const
401{
402 return mUsesViewID;
403}
404
Jamie Madille39a3f02015-11-17 20:42:15 -0500405bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
406{
Jamie Madillc9bde922016-07-24 17:58:50 -0400407 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500408 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400409 // Even with a geometry shader, the app can render triangles or lines and reference
410 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
411 // simplicity, we always add this to the vertex shader when the fragment shader
412 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500413 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
414 usesInsertedPointCoordValue();
415}
416
417bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
418{
419 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
420 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
421 // the vertex shader in this case. This saves us 1 output vector.
422 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
423}
424
425bool ProgramD3DMetadata::usesSystemValuePointSize() const
426{
427 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
428}
429
430bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
431{
432 return mFragmentShader->usesMultipleRenderTargets();
433}
434
435GLint ProgramD3DMetadata::getMajorShaderVersion() const
436{
437 return mVertexShader->getData().getShaderVersion();
438}
439
440const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
441{
442 return mFragmentShader;
443}
444
Jamie Madill28afae52015-11-09 15:07:57 -0500445// ProgramD3D Implementation
446
Jamie Madilld3dfda22015-07-06 08:28:49 -0400447ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
448 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500449 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400450 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700451{
Brandon Joneseb994362014-09-24 10:27:28 -0700452}
453
454ProgramD3D::VertexExecutable::~VertexExecutable()
455{
456 SafeDelete(mShaderExecutable);
457}
458
Jamie Madilld3dfda22015-07-06 08:28:49 -0400459// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500460ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
461 GLenum type)
462{
463 switch (type)
464 {
465 case GL_INT:
466 return HLSLAttribType::SIGNED_INT;
467 case GL_UNSIGNED_INT:
468 return HLSLAttribType::UNSIGNED_INT;
469 case GL_SIGNED_NORMALIZED:
470 case GL_UNSIGNED_NORMALIZED:
471 case GL_FLOAT:
472 return HLSLAttribType::FLOAT;
473 default:
474 UNREACHABLE();
475 return HLSLAttribType::FLOAT;
476 }
477}
478
479// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400480void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
481 const gl::InputLayout &inputLayout,
482 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700483{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500484 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400485
486 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700487 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400488 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500489 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
490 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400491
Jamie Madillbdec2f42016-03-02 16:35:32 -0500492 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
493 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
494 continue;
495
496 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
497 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700498 }
Brandon Joneseb994362014-09-24 10:27:28 -0700499}
500
Jamie Madilld3dfda22015-07-06 08:28:49 -0400501bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
502{
Jamie Madillbd136f92015-08-10 14:51:37 -0400503 size_t limit = std::max(mSignature.size(), signature.size());
504 for (size_t index = 0; index < limit; ++index)
505 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500506 // treat undefined indexes as FLOAT
507 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
508 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400509 if (a != b)
510 return false;
511 }
512
513 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400514}
515
516ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
517 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400518 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700519{
520}
521
522ProgramD3D::PixelExecutable::~PixelExecutable()
523{
524 SafeDelete(mShaderExecutable);
525}
526
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700527ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
528{
529}
530
Geoff Lang7dd2e102014-11-10 15:19:26 -0500531unsigned int ProgramD3D::mCurrentSerial = 1;
532
Jamie Madill48ef11b2016-04-27 15:21:52 -0400533ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
534 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700535 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800536 mDynamicHLSL(nullptr),
537 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
538 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700539 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400540 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800541 mVertexUniformStorage(nullptr),
542 mFragmentUniformStorage(nullptr),
543 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700544 mUsedVertexSamplerRange(0),
545 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800546 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700547 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500548 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700549{
Brandon Joneseb994362014-09-24 10:27:28 -0700550 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700551}
552
553ProgramD3D::~ProgramD3D()
554{
555 reset();
556 SafeDelete(mDynamicHLSL);
557}
558
Brandon Jones44151a92014-09-10 11:32:25 -0700559bool ProgramD3D::usesPointSpriteEmulation() const
560{
561 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
562}
563
Martin Radev41ac68e2017-06-06 12:16:58 +0300564bool ProgramD3D::usesGeometryShaderForPointSpriteEmulation() const
565{
566 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
567}
568
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400569bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700570{
Martin Radev41ac68e2017-06-06 12:16:58 +0300571 if (mHasANGLEMultiviewEnabled)
572 {
573 return true;
574 }
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400575 if (drawMode != GL_POINTS)
576 {
577 return mUsesFlatInterpolation;
578 }
Martin Radev41ac68e2017-06-06 12:16:58 +0300579 return usesGeometryShaderForPointSpriteEmulation();
Cooper Partine6664f02015-01-09 16:22:24 -0800580}
581
582bool ProgramD3D::usesInstancedPointSpriteEmulation() const
583{
584 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700585}
586
Jamie Madill334d6152015-10-22 14:00:28 -0400587GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
588 unsigned int samplerIndex,
589 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700590{
591 GLint logicalTextureUnit = -1;
592
593 switch (type)
594 {
Jamie Madill334d6152015-10-22 14:00:28 -0400595 case gl::SAMPLER_PIXEL:
596 ASSERT(samplerIndex < caps.maxTextureImageUnits);
597 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
598 {
599 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
600 }
601 break;
602 case gl::SAMPLER_VERTEX:
603 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
604 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
605 {
606 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
607 }
608 break;
Xinghua Caob1239382016-12-13 15:07:05 +0800609 case gl::SAMPLER_COMPUTE:
610 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
611 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
612 {
613 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
614 }
615 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400616 default:
617 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700618 }
619
Jamie Madill334d6152015-10-22 14:00:28 -0400620 if (logicalTextureUnit >= 0 &&
621 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700622 {
623 return logicalTextureUnit;
624 }
625
626 return -1;
627}
628
629// Returns the texture type for a given Direct3D 9 sampler type and
630// index (0-15 for the pixel shader and 0-3 for the vertex shader).
631GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
632{
633 switch (type)
634 {
Jamie Madill334d6152015-10-22 14:00:28 -0400635 case gl::SAMPLER_PIXEL:
636 ASSERT(samplerIndex < mSamplersPS.size());
637 ASSERT(mSamplersPS[samplerIndex].active);
638 return mSamplersPS[samplerIndex].textureType;
639 case gl::SAMPLER_VERTEX:
640 ASSERT(samplerIndex < mSamplersVS.size());
641 ASSERT(mSamplersVS[samplerIndex].active);
642 return mSamplersVS[samplerIndex].textureType;
Xinghua Caob1239382016-12-13 15:07:05 +0800643 case gl::SAMPLER_COMPUTE:
644 ASSERT(samplerIndex < mSamplersCS.size());
645 ASSERT(mSamplersCS[samplerIndex].active);
646 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400647 default:
648 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700649 }
650
651 return GL_TEXTURE_2D;
652}
653
Olli Etuaho618bebc2016-01-15 16:40:00 +0200654GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700655{
656 switch (type)
657 {
Jamie Madill334d6152015-10-22 14:00:28 -0400658 case gl::SAMPLER_PIXEL:
659 return mUsedPixelSamplerRange;
660 case gl::SAMPLER_VERTEX:
661 return mUsedVertexSamplerRange;
Xinghua Caob1239382016-12-13 15:07:05 +0800662 case gl::SAMPLER_COMPUTE:
663 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400664 default:
665 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200666 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700667 }
668}
669
670void ProgramD3D::updateSamplerMapping()
671{
672 if (!mDirtySamplerMapping)
673 {
674 return;
675 }
676
677 mDirtySamplerMapping = false;
678
679 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400680 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700681 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400682 if (!d3dUniform->dirty)
683 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700684
Jamie Madill62d31cb2015-09-11 13:25:51 -0400685 if (!d3dUniform->isSampler())
686 continue;
687
688 int count = d3dUniform->elementCount();
689 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
690
691 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700692 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400693 unsigned int firstIndex = d3dUniform->psRegisterIndex;
694
695 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700696 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400697 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700698
Jamie Madill62d31cb2015-09-11 13:25:51 -0400699 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700700 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400701 ASSERT(mSamplersPS[samplerIndex].active);
702 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700703 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400704 }
705 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700706
Jamie Madill62d31cb2015-09-11 13:25:51 -0400707 if (d3dUniform->isReferencedByVertexShader())
708 {
709 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
710
711 for (int i = 0; i < count; i++)
712 {
713 unsigned int samplerIndex = firstIndex + i;
714
715 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700716 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400717 ASSERT(mSamplersVS[samplerIndex].active);
718 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700719 }
720 }
721 }
Xinghua Caob1239382016-12-13 15:07:05 +0800722
723 if (d3dUniform->isReferencedByComputeShader())
724 {
725 unsigned int firstIndex = d3dUniform->csRegisterIndex;
726
727 for (int i = 0; i < count; i++)
728 {
729 unsigned int samplerIndex = firstIndex + i;
730
731 if (samplerIndex < mSamplersCS.size())
732 {
733 ASSERT(mSamplersCS[samplerIndex].active);
734 mSamplersCS[samplerIndex].logicalTextureUnit = v[i][0];
735 }
736 }
737 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700738 }
739}
740
Jamie Madill9cf9e872017-06-05 12:59:25 -0400741gl::LinkResult ProgramD3D::load(const gl::Context *context,
742 gl::InfoLog &infoLog,
743 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700744{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500745 // TODO(jmadill): Use Renderer from contextImpl.
746
Jamie Madill62d31cb2015-09-11 13:25:51 -0400747 reset();
748
Jamie Madill334d6152015-10-22 14:00:28 -0400749 DeviceIdentifier binaryDeviceIdentifier = {0};
750 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
751 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700752
753 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
754 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
755 {
756 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500757 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700758 }
759
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500760 int compileFlags = stream->readInt<int>();
761 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
762 {
Jamie Madillf6113162015-05-07 11:49:21 -0400763 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500764 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500765 }
766
Jamie Madill8047c0d2016-03-07 13:02:12 -0500767 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400768 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500769 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400770 }
771
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700772 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
773 for (unsigned int i = 0; i < psSamplerCount; ++i)
774 {
775 Sampler sampler;
776 stream->readBool(&sampler.active);
777 stream->readInt(&sampler.logicalTextureUnit);
778 stream->readInt(&sampler.textureType);
779 mSamplersPS.push_back(sampler);
780 }
781 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
782 for (unsigned int i = 0; i < vsSamplerCount; ++i)
783 {
784 Sampler sampler;
785 stream->readBool(&sampler.active);
786 stream->readInt(&sampler.logicalTextureUnit);
787 stream->readInt(&sampler.textureType);
788 mSamplersVS.push_back(sampler);
789 }
790
Xinghua Caob1239382016-12-13 15:07:05 +0800791 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
792 for (unsigned int i = 0; i < csSamplerCount; ++i)
793 {
794 Sampler sampler;
795 stream->readBool(&sampler.active);
796 stream->readInt(&sampler.logicalTextureUnit);
797 stream->readInt(&sampler.textureType);
798 mSamplersCS.push_back(sampler);
799 }
800
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700801 stream->readInt(&mUsedVertexSamplerRange);
802 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800803 stream->readInt(&mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700804
805 const unsigned int uniformCount = stream->readInt<unsigned int>();
806 if (stream->error())
807 {
Jamie Madillf6113162015-05-07 11:49:21 -0400808 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500809 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700810 }
811
Jamie Madill48ef11b2016-04-27 15:21:52 -0400812 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400813 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700814 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
815 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400816 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700817
Jamie Madill62d31cb2015-09-11 13:25:51 -0400818 D3DUniform *d3dUniform =
819 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
820 linkedUniform.isInDefaultBlock());
821 stream->readInt(&d3dUniform->psRegisterIndex);
822 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800823 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400824 stream->readInt(&d3dUniform->registerCount);
825 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700826
Jamie Madill62d31cb2015-09-11 13:25:51 -0400827 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700828 }
829
Jamie Madill4a3c2342015-10-08 12:58:45 -0400830 const unsigned int blockCount = stream->readInt<unsigned int>();
831 if (stream->error())
832 {
833 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500834 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400835 }
836
837 ASSERT(mD3DUniformBlocks.empty());
838 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
839 {
840 D3DUniformBlock uniformBlock;
841 stream->readInt(&uniformBlock.psRegisterIndex);
842 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800843 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400844 mD3DUniformBlocks.push_back(uniformBlock);
845 }
846
Jamie Madill9fc36822015-11-18 13:08:07 -0500847 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
848 mStreamOutVaryings.resize(streamOutVaryingCount);
849 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700850 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500851 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700852
Jamie Madill28afae52015-11-09 15:07:57 -0500853 stream->readString(&varying->semanticName);
854 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500855 stream->readInt(&varying->componentCount);
856 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700857 }
858
Brandon Jones22502d52014-08-29 16:58:36 -0700859 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400860 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500861 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700862 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400863 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500864 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700865 stream->readBool(&mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +0300866 stream->readBool(&mHasANGLEMultiviewEnabled);
867 stream->readBool(&mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -0700868 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400869 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700870
871 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
872 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400873 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
874 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700875 {
876 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
877 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
878 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
879 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
880 }
881
Jamie Madill4e31ad52015-10-29 10:32:57 -0400882 stream->readString(&mGeometryShaderPreamble);
883
Jamie Madill334d6152015-10-22 14:00:28 -0400884 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700885
Jamie Madillb0a838b2016-11-13 20:02:12 -0500886 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
887
Brandon Joneseb994362014-09-24 10:27:28 -0700888 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400889 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
890 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700891 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400892 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400893 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700894
Jamie Madilld3dfda22015-07-06 08:28:49 -0400895 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700896 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400897 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700898 }
899
Jamie Madill334d6152015-10-22 14:00:28 -0400900 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700901 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400902
Jamie Madillada9ecc2015-08-17 12:53:37 -0400903 ShaderExecutableD3D *shaderExecutable = nullptr;
904
Jamie Madillb0a838b2016-11-13 20:02:12 -0500905 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
906 mStreamOutVaryings, separateAttribs,
907 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400908
Brandon Joneseb994362014-09-24 10:27:28 -0700909 if (!shaderExecutable)
910 {
Jamie Madillf6113162015-05-07 11:49:21 -0400911 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500912 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700913 }
914
915 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400916 VertexExecutable::Signature signature;
917 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700918
919 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800920 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
921 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700922
923 stream->skip(vertexShaderSize);
924 }
925
926 const size_t pixelShaderCount = stream->readInt<unsigned int>();
927 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
928 {
929 const size_t outputCount = stream->readInt<unsigned int>();
930 std::vector<GLenum> outputs(outputCount);
931 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
932 {
933 stream->readInt(&outputs[outputIndex]);
934 }
935
Jamie Madill334d6152015-10-22 14:00:28 -0400936 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700937 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400938 ShaderExecutableD3D *shaderExecutable = nullptr;
939
Jamie Madillb0a838b2016-11-13 20:02:12 -0500940 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
941 mStreamOutVaryings, separateAttribs,
942 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700943
944 if (!shaderExecutable)
945 {
Jamie Madillf6113162015-05-07 11:49:21 -0400946 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500947 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700948 }
949
950 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800951 mPixelExecutables.push_back(
952 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700953
954 stream->skip(pixelShaderSize);
955 }
956
Jamie Madill4e31ad52015-10-29 10:32:57 -0400957 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
958 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700959 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400960 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
961 if (geometryShaderSize == 0)
962 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400963 continue;
964 }
965
Brandon Joneseb994362014-09-24 10:27:28 -0700966 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400967
Xinghua Caob1239382016-12-13 15:07:05 +0800968 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -0500969 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
970 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
Xinghua Caob1239382016-12-13 15:07:05 +0800971 &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700972
Xinghua Caob1239382016-12-13 15:07:05 +0800973 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700974 {
Jamie Madillf6113162015-05-07 11:49:21 -0400975 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500976 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700977 }
Xinghua Caob1239382016-12-13 15:07:05 +0800978
979 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
980
Brandon Joneseb994362014-09-24 10:27:28 -0700981 stream->skip(geometryShaderSize);
982 }
983
Xinghua Caob1239382016-12-13 15:07:05 +0800984 unsigned int computeShaderSize = stream->readInt<unsigned int>();
985 if (computeShaderSize > 0)
986 {
987 const unsigned char *computeShaderFunction = binary + stream->offset();
988
989 ShaderExecutableD3D *computeExecutable = nullptr;
990 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
991 SHADER_COMPUTE, std::vector<D3DVarying>(), false,
992 &computeExecutable));
993
994 if (!computeExecutable)
995 {
996 infoLog << "Could not create compute shader.";
997 return false;
998 }
999
1000 mComputeExecutable.reset(computeExecutable);
1001 }
1002
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001003 initializeUniformStorage();
1004
Jamie Madillb0a838b2016-11-13 20:02:12 -05001005 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001006}
1007
Jamie Madill27a60632017-06-30 15:12:01 -04001008void ProgramD3D::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -07001009{
Austin Kinross137b1512015-06-17 16:14:53 -07001010 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -04001011 // When we load the binary again later, we can validate the device identifier before trying to
1012 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -07001013 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -04001014 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
1015 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -07001016
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001017 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1018
Jamie Madill8047c0d2016-03-07 13:02:12 -05001019 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -04001020 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05001021 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -04001022 }
1023
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001024 stream->writeInt(mSamplersPS.size());
1025 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1026 {
1027 stream->writeInt(mSamplersPS[i].active);
1028 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
1029 stream->writeInt(mSamplersPS[i].textureType);
1030 }
1031
1032 stream->writeInt(mSamplersVS.size());
1033 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1034 {
1035 stream->writeInt(mSamplersVS[i].active);
1036 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1037 stream->writeInt(mSamplersVS[i].textureType);
1038 }
1039
Xinghua Caob1239382016-12-13 15:07:05 +08001040 stream->writeInt(mSamplersCS.size());
1041 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
1042 {
1043 stream->writeInt(mSamplersCS[i].active);
1044 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
1045 stream->writeInt(mSamplersCS[i].textureType);
1046 }
1047
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001048 stream->writeInt(mUsedVertexSamplerRange);
1049 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001050 stream->writeInt(mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001051
Jamie Madill62d31cb2015-09-11 13:25:51 -04001052 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001053 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001054 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001055 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001056 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1057 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001058 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001059 stream->writeInt(uniform->registerCount);
1060 stream->writeInt(uniform->registerElement);
1061 }
1062
Jamie Madill51f522f2016-12-21 15:10:55 -05001063 // Ensure we init the uniform block structure data if we should.
1064 // http://anglebug.com/1637
1065 ensureUniformBlocksInitialized();
1066
Jamie Madill4a3c2342015-10-08 12:58:45 -04001067 stream->writeInt(mD3DUniformBlocks.size());
1068 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1069 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001070 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1071 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001072 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001073 }
1074
Jamie Madill9fc36822015-11-18 13:08:07 -05001075 stream->writeInt(mStreamOutVaryings.size());
1076 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001077 {
Brandon Joneseb994362014-09-24 10:27:28 -07001078 stream->writeString(varying.semanticName);
1079 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001080 stream->writeInt(varying.componentCount);
1081 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001082 }
1083
Brandon Jones22502d52014-08-29 16:58:36 -07001084 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001085 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001086 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001087 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001088 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001089 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001090 stream->writeInt(mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +03001091 stream->writeInt(mHasANGLEMultiviewEnabled);
1092 stream->writeInt(mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -07001093 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001094 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001095
Brandon Joneseb994362014-09-24 10:27:28 -07001096 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001097 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001098 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1099 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001100 {
Brandon Joneseb994362014-09-24 10:27:28 -07001101 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001102 stream->writeInt(variable.type);
1103 stream->writeString(variable.name);
1104 stream->writeString(variable.source);
1105 stream->writeInt(variable.outputIndex);
1106 }
1107
Jamie Madill4e31ad52015-10-29 10:32:57 -04001108 stream->writeString(mGeometryShaderPreamble);
1109
Brandon Joneseb994362014-09-24 10:27:28 -07001110 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001111 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1112 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001113 {
Xinghua Caob1239382016-12-13 15:07:05 +08001114 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001115
Jamie Madilld3dfda22015-07-06 08:28:49 -04001116 const auto &inputLayout = vertexExecutable->inputs();
1117 stream->writeInt(inputLayout.size());
1118
1119 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001120 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001121 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001122 }
1123
1124 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1125 stream->writeInt(vertexShaderSize);
1126
1127 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1128 stream->writeBytes(vertexBlob, vertexShaderSize);
1129 }
1130
1131 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001132 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1133 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001134 {
Xinghua Caob1239382016-12-13 15:07:05 +08001135 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001136
1137 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1138 stream->writeInt(outputs.size());
1139 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1140 {
1141 stream->writeInt(outputs[outputIndex]);
1142 }
1143
1144 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1145 stream->writeInt(pixelShaderSize);
1146
1147 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1148 stream->writeBytes(pixelBlob, pixelShaderSize);
1149 }
1150
Xinghua Caob1239382016-12-13 15:07:05 +08001151 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001152 {
Xinghua Caob1239382016-12-13 15:07:05 +08001153 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001154 {
1155 stream->writeInt(0);
1156 continue;
1157 }
1158
Xinghua Caob1239382016-12-13 15:07:05 +08001159 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001160 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001161 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1162 }
1163
1164 if (mComputeExecutable)
1165 {
1166 size_t computeShaderSize = mComputeExecutable->getLength();
1167 stream->writeInt(computeShaderSize);
1168 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1169 }
1170 else
1171 {
1172 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001173 }
Brandon Jones22502d52014-08-29 16:58:36 -07001174}
1175
Geoff Langc5629752015-12-07 16:29:04 -05001176void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1177{
1178}
1179
Yunchao He61afff12017-03-14 15:34:03 +08001180void ProgramD3D::setSeparable(bool /* separable */)
1181{
1182}
1183
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001184gl::Error ProgramD3D::getPixelExecutableForCachedOutputLayout(ShaderExecutableD3D **outExecutable,
1185 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001186{
1187 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1188 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001189 if (mPixelExecutables[executableIndex]->matchesSignature(mPixelShaderOutputLayoutCache))
Brandon Joneseb994362014-09-24 10:27:28 -07001190 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001191 *outExecutable = mPixelExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001192 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001193 }
1194 }
1195
Jamie Madill334d6152015-10-22 14:00:28 -04001196 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001197 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, mPixelShaderOutputLayoutCache);
Brandon Jones22502d52014-08-29 16:58:36 -07001198
1199 // Generate new pixel executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001200 ShaderExecutableD3D *pixelExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001201
1202 gl::InfoLog tempInfoLog;
1203 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1204
Jamie Madill01074252016-11-28 15:55:51 -05001205 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001206 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001207 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001208 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001209
Jamie Madill97399232014-12-23 12:31:15 -05001210 if (pixelExecutable)
1211 {
Xinghua Caob1239382016-12-13 15:07:05 +08001212 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001213 new PixelExecutable(mPixelShaderOutputLayoutCache, pixelExecutable)));
Jamie Madill97399232014-12-23 12:31:15 -05001214 }
1215 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001216 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001217 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1218 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001219 }
Brandon Jones22502d52014-08-29 16:58:36 -07001220
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001221 *outExecutable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001222 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001223}
1224
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001225gl::Error ProgramD3D::getVertexExecutableForCachedInputLayout(ShaderExecutableD3D **outExectuable,
1226 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001227{
Brandon Joneseb994362014-09-24 10:27:28 -07001228 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1229 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001230 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001231 {
Geoff Langb543aff2014-09-30 14:52:54 -04001232 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001233 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001234 }
1235 }
1236
Brandon Jones22502d52014-08-29 16:58:36 -07001237 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001238 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001239 mVertexHLSL, mCachedInputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001240
1241 // Generate new vertex executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001242 ShaderExecutableD3D *vertexExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001243
1244 gl::InfoLog tempInfoLog;
1245 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1246
Jamie Madill01074252016-11-28 15:55:51 -05001247 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001248 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001249 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001250 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001251
Jamie Madill97399232014-12-23 12:31:15 -05001252 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001253 {
Xinghua Caob1239382016-12-13 15:07:05 +08001254 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001255 new VertexExecutable(mCachedInputLayout, mCachedVertexSignature, vertexExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001256 }
Jamie Madill97399232014-12-23 12:31:15 -05001257 else if (!infoLog)
1258 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001259 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1260 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001261 }
Brandon Jones22502d52014-08-29 16:58:36 -07001262
Geoff Langb543aff2014-09-30 14:52:54 -04001263 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001264 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001265}
1266
Jamie Madill9082b982016-04-27 15:21:51 -04001267gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001268 GLenum drawMode,
1269 ShaderExecutableD3D **outExecutable,
1270 gl::InfoLog *infoLog)
1271{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001272 if (outExecutable)
1273 {
1274 *outExecutable = nullptr;
1275 }
1276
Austin Kinross88829e82016-01-12 13:04:41 -08001277 // Return a null shader if the current rendering doesn't use a geometry shader
1278 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001279 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001280 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001281 }
1282
Jamie Madill4e31ad52015-10-29 10:32:57 -04001283 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1284
Xinghua Caob1239382016-12-13 15:07:05 +08001285 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001286 {
1287 if (outExecutable)
1288 {
Xinghua Caob1239382016-12-13 15:07:05 +08001289 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001290 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001291 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001292 }
1293
1294 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001295 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Martin Radev41ac68e2017-06-06 12:16:58 +03001296 mHasANGLEMultiviewEnabled, usesGeometryShaderForPointSpriteEmulation(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001297 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001298
1299 gl::InfoLog tempInfoLog;
1300 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1301
Xinghua Caob1239382016-12-13 15:07:05 +08001302 ShaderExecutableD3D *geometryExecutable = nullptr;
1303 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001304 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001305 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001306 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001307
1308 if (!infoLog && error.isError())
1309 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001310 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1311 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001312 }
1313
Xinghua Caob1239382016-12-13 15:07:05 +08001314 if (geometryExecutable != nullptr)
1315 {
1316 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1317 }
1318
Jamie Madill4e31ad52015-10-29 10:32:57 -04001319 if (outExecutable)
1320 {
Xinghua Caob1239382016-12-13 15:07:05 +08001321 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001322 }
1323 return error;
1324}
1325
Jamie Madill01074252016-11-28 15:55:51 -05001326class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001327{
Jamie Madill01074252016-11-28 15:55:51 -05001328 public:
1329 GetExecutableTask(ProgramD3D *program)
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001330 : mProgram(program), mError(gl::NoError()), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001331 {
Brandon Joneseb994362014-09-24 10:27:28 -07001332 }
1333
Jamie Madill01074252016-11-28 15:55:51 -05001334 virtual gl::Error run() = 0;
1335
1336 void operator()() override { mError = run(); }
1337
1338 const gl::Error &getError() const { return mError; }
1339 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1340 ShaderExecutableD3D *getResult() { return mResult; }
1341
1342 protected:
1343 ProgramD3D *mProgram;
1344 gl::Error mError;
1345 gl::InfoLog mInfoLog;
1346 ShaderExecutableD3D *mResult;
1347};
1348
1349class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1350{
1351 public:
Jamie Madillbd044ed2017-06-05 12:59:21 -04001352 GetVertexExecutableTask(ProgramD3D *program, const gl::Context *context)
1353 : GetExecutableTask(program), mContext(context)
1354 {
1355 }
Jamie Madill01074252016-11-28 15:55:51 -05001356 gl::Error run() override
1357 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001358 mProgram->updateCachedInputLayoutFromShader(mContext);
Jamie Madill01074252016-11-28 15:55:51 -05001359
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001360 ANGLE_TRY(mProgram->getVertexExecutableForCachedInputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001361
1362 return gl::NoError();
1363 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001364
1365 private:
1366 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001367};
1368
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001369void ProgramD3D::updateCachedInputLayoutFromShader(const gl::Context *context)
1370{
1371 GetDefaultInputLayoutFromShader(context, mState.getAttachedVertexShader(), &mCachedInputLayout);
1372 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
1373}
1374
Jamie Madill01074252016-11-28 15:55:51 -05001375class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1376{
1377 public:
1378 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1379 gl::Error run() override
1380 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001381 mProgram->updateCachedOutputLayoutFromShader();
Jamie Madill01074252016-11-28 15:55:51 -05001382
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001383 ANGLE_TRY(mProgram->getPixelExecutableForCachedOutputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001384
1385 return gl::NoError();
1386 }
1387};
1388
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001389void ProgramD3D::updateCachedOutputLayoutFromShader()
1390{
1391 GetDefaultOutputLayoutFromShader(mPixelShaderKey, &mPixelShaderOutputLayoutCache);
1392}
1393
Jamie Madill01074252016-11-28 15:55:51 -05001394class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1395{
1396 public:
1397 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1398 : GetExecutableTask(program), mContextState(contextState)
1399 {
1400 }
1401
1402 gl::Error run() override
1403 {
1404 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1405 // D3D11.
1406 if (mProgram->usesGeometryShader(GL_POINTS))
1407 {
1408 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1409 &mResult, &mInfoLog));
1410 }
1411
1412 return gl::NoError();
1413 }
1414
1415 private:
1416 const gl::ContextState &mContextState;
1417};
1418
Xinghua Cao73badc02017-03-29 19:14:53 +08001419gl::Error ProgramD3D::getComputeExecutable(ShaderExecutableD3D **outExecutable)
1420{
1421 if (outExecutable)
1422 {
1423 *outExecutable = mComputeExecutable.get();
1424 }
1425
1426 return gl::NoError();
1427}
1428
Jamie Madill9cf9e872017-06-05 12:59:25 -04001429gl::LinkResult ProgramD3D::compileProgramExecutables(const gl::Context *context,
1430 gl::InfoLog &infoLog)
Jamie Madill01074252016-11-28 15:55:51 -05001431{
1432 // Ensure the compiler is initialized to avoid race conditions.
1433 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1434
1435 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1436
Jamie Madillbd044ed2017-06-05 12:59:21 -04001437 GetVertexExecutableTask vertexTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001438 GetPixelExecutableTask pixelTask(this);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001439 GetGeometryExecutableTask geometryTask(this, context->getContextState());
Jamie Madill01074252016-11-28 15:55:51 -05001440
1441 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1442 workerPool->postWorkerTask(&pixelTask),
1443 workerPool->postWorkerTask(&geometryTask)}};
1444
1445 WaitableEvent::WaitMany(&waitEvents);
1446
1447 infoLog << vertexTask.getInfoLog().str();
1448 infoLog << pixelTask.getInfoLog().str();
1449 infoLog << geometryTask.getInfoLog().str();
1450
1451 ANGLE_TRY(vertexTask.getError());
1452 ANGLE_TRY(pixelTask.getError());
1453 ANGLE_TRY(geometryTask.getError());
1454
1455 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1456 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1457 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1458
Jamie Madill48ef11b2016-04-27 15:21:52 -04001459 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001460
1461 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001462 {
Jamie Madill334d6152015-10-22 14:00:28 -04001463 // Geometry shaders are currently only used internally, so there is no corresponding shader
1464 // object at the interface level. For now the geometry shader debug info is prepended to
1465 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001466 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001467 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001468 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1469 }
1470
1471 if (defaultVertexExecutable)
1472 {
1473 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1474 }
1475
1476 if (defaultPixelExecutable)
1477 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001478 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001479 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001480 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1481 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001482
Jamie Madillb0a838b2016-11-13 20:02:12 -05001483 return (defaultVertexExecutable && defaultPixelExecutable &&
1484 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001485}
1486
Jamie Madill9cf9e872017-06-05 12:59:25 -04001487gl::LinkResult ProgramD3D::compileComputeExecutable(const gl::Context *context,
1488 gl::InfoLog &infoLog)
Xinghua Caob1239382016-12-13 15:07:05 +08001489{
1490 // Ensure the compiler is initialized to avoid race conditions.
1491 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1492
Jamie Madillbd044ed2017-06-05 12:59:21 -04001493 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(context, mState);
Xinghua Caob1239382016-12-13 15:07:05 +08001494
1495 ShaderExecutableD3D *computeExecutable = nullptr;
1496 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, SHADER_COMPUTE,
1497 std::vector<D3DVarying>(), false,
1498 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1499
1500 if (computeExecutable == nullptr)
1501 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001502 ERR() << "Error compiling dynamic compute executable:" << std::endl
1503 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001504 }
1505 else
1506 {
1507 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1508 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1509 mComputeExecutable.reset(computeExecutable);
1510 }
1511
1512 return mComputeExecutable.get() != nullptr;
1513}
1514
Jamie Madill9cf9e872017-06-05 12:59:25 -04001515gl::LinkResult ProgramD3D::link(const gl::Context *context,
1516 const gl::VaryingPacking &packing,
1517 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001518{
Jamie Madillc564c072017-06-01 12:45:42 -04001519 const auto &data = context->getContextState();
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001520
Jamie Madill62d31cb2015-09-11 13:25:51 -04001521 reset();
1522
Jamie Madillbd044ed2017-06-05 12:59:21 -04001523 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001524 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001525 {
Xinghua Caob1239382016-12-13 15:07:05 +08001526 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1527
Jamie Madillbd044ed2017-06-05 12:59:21 -04001528 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001529
Jamie Madill9cf9e872017-06-05 12:59:25 -04001530 gl::LinkResult result = compileComputeExecutable(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001531 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001532 {
Xinghua Caob1239382016-12-13 15:07:05 +08001533 infoLog << result.getError().getMessage();
1534 return result;
1535 }
1536 else if (!result.getResult())
1537 {
1538 infoLog << "Failed to create D3D compute shader.";
1539 return result;
1540 }
1541
Jamie Madillbd044ed2017-06-05 12:59:21 -04001542 initUniformBlockInfo(context, computeShader);
Xinghua Caob1239382016-12-13 15:07:05 +08001543 }
1544 else
1545 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001546 gl::Shader *vertexShader = mState.getAttachedVertexShader();
1547 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001548
1549 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1550 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1551
1552 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1553 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1554
1555 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1556 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1557
1558 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1559 {
1560 if (fragmentShaderD3D->usesFrontFacing())
1561 {
1562 infoLog << "The current renderer doesn't support gl_FrontFacing";
1563 return false;
1564 }
1565 }
1566
1567 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1568 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1569 // intelligently, but D3D9 assumes one semantic per register.
1570 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
1571 packing.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
1572 {
1573 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001574 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001575 }
Xinghua Caob1239382016-12-13 15:07:05 +08001576
1577 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
1578 BuiltinVaryingsD3D builtins(metadata, packing);
1579
Jamie Madillbd044ed2017-06-05 12:59:21 -04001580 mDynamicHLSL->generateShaderLinkHLSL(context, mState, metadata, packing, builtins,
1581 &mPixelHLSL, &mVertexHLSL);
Xinghua Caob1239382016-12-13 15:07:05 +08001582
1583 mUsesPointSize = vertexShaderD3D->usesPointSize();
1584 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1585 mUsesFragDepth = metadata.usesFragDepth();
Martin Radev41ac68e2017-06-06 12:16:58 +03001586 mUsesViewID = metadata.usesViewID();
1587 mHasANGLEMultiviewEnabled = metadata.hasANGLEMultiviewEnabled();
Xinghua Caob1239382016-12-13 15:07:05 +08001588
1589 // Cache if we use flat shading
Jamie Madillbd044ed2017-06-05 12:59:21 -04001590 mUsesFlatInterpolation =
1591 (FindFlatInterpolationVarying(fragmentShader->getVaryings(context)) ||
1592 FindFlatInterpolationVarying(vertexShader->getVaryings(context)));
Xinghua Caob1239382016-12-13 15:07:05 +08001593
1594 if (mRenderer->getMajorShaderModel() >= 4)
1595 {
Martin Radev41ac68e2017-06-06 12:16:58 +03001596 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(
1597 packing, builtins, mHasANGLEMultiviewEnabled);
Xinghua Caob1239382016-12-13 15:07:05 +08001598 }
1599
Jamie Madillbd044ed2017-06-05 12:59:21 -04001600 initAttribLocationsToD3DSemantic(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001601
Jamie Madillbd044ed2017-06-05 12:59:21 -04001602 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001603
1604 gatherTransformFeedbackVaryings(packing, builtins[SHADER_VERTEX]);
1605
Jamie Madill9cf9e872017-06-05 12:59:25 -04001606 gl::LinkResult result = compileProgramExecutables(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001607 if (result.isError())
1608 {
1609 infoLog << result.getError().getMessage();
1610 return result;
1611 }
1612 else if (!result.getResult())
1613 {
1614 infoLog << "Failed to create D3D shaders.";
1615 return result;
1616 }
1617
Jamie Madillbd044ed2017-06-05 12:59:21 -04001618 initUniformBlockInfo(context, vertexShader);
1619 initUniformBlockInfo(context, fragmentShader);
Austin Kinross02df7962015-07-01 10:03:42 -07001620 }
1621
Jamie Madillb0a838b2016-11-13 20:02:12 -05001622 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001623}
1624
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001625GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001626{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001627 // TODO(jmadill): Do something useful here?
1628 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001629}
1630
Jamie Madillbd044ed2017-06-05 12:59:21 -04001631void ProgramD3D::initUniformBlockInfo(const gl::Context *context, gl::Shader *shader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001632{
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001633 for (const sh::InterfaceBlock &interfaceBlock : shader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001634 {
Xinghua Caob1239382016-12-13 15:07:05 +08001635 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001636 continue;
1637
Xinghua Caob1239382016-12-13 15:07:05 +08001638 if (mBlockDataSizes.count(interfaceBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001639 continue;
1640
Xinghua Caob1239382016-12-13 15:07:05 +08001641 size_t dataSize = getUniformBlockInfo(interfaceBlock);
1642 mBlockDataSizes[interfaceBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001643 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001644}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001645
Jamie Madill51f522f2016-12-21 15:10:55 -05001646void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001647{
Jamie Madill51f522f2016-12-21 15:10:55 -05001648 // Lazy init.
1649 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1650 {
1651 return;
1652 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001653
1654 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001655 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1656 const ShaderD3D *fragmentShaderD3D =
1657 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1658 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001659
Jamie Madill48ef11b2016-04-27 15:21:52 -04001660 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001661 {
1662 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1663
Jamie Madill4a3c2342015-10-08 12:58:45 -04001664 D3DUniformBlock d3dUniformBlock;
1665
Jamie Madill62d31cb2015-09-11 13:25:51 -04001666 if (uniformBlock.vertexStaticUse)
1667 {
Xinghua Caob1239382016-12-13 15:07:05 +08001668 ASSERT(vertexShaderD3D != nullptr);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001669 unsigned int baseRegister = vertexShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001670 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001671 }
1672
1673 if (uniformBlock.fragmentStaticUse)
1674 {
Xinghua Caob1239382016-12-13 15:07:05 +08001675 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001676 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001677 fragmentShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001678 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679 }
1680
Xinghua Caob1239382016-12-13 15:07:05 +08001681 if (uniformBlock.computeStaticUse)
1682 {
1683 ASSERT(computeShaderD3D != nullptr);
1684 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001685 computeShaderD3D->getUniformBlockRegister(uniformBlock.name);
Xinghua Caob1239382016-12-13 15:07:05 +08001686 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1687 }
1688
Jamie Madill4a3c2342015-10-08 12:58:45 -04001689 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001690 }
1691}
1692
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001693void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001694{
1695 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001696 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001697 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001698 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001699 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001700 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001701 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001702 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001703 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001704 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001705 vertexRegisters = std::max(vertexRegisters,
1706 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001707 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001708 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001709 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001710 fragmentRegisters = std::max(
1711 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001712 }
Xinghua Caob1239382016-12-13 15:07:05 +08001713 if (d3dUniform->isReferencedByComputeShader())
1714 {
1715 computeRegisters = std::max(
1716 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1717 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001718 }
1719 }
1720
Xinghua Caob1239382016-12-13 15:07:05 +08001721 mVertexUniformStorage =
1722 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1723 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1724 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1725 mComputeUniformStorage =
1726 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Brandon Jonesc9610c52014-08-25 17:02:59 -07001727}
1728
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001729gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001730{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001731 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001732
Jamie Madill01074252016-11-28 15:55:51 -05001733 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001734
Jamie Madill62d31cb2015-09-11 13:25:51 -04001735 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001736 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001737 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001738 }
1739
Jamie Madill51f522f2016-12-21 15:10:55 -05001740 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001741}
1742
Xinghua Cao73badc02017-03-29 19:14:53 +08001743gl::Error ProgramD3D::applyComputeUniforms()
1744{
1745 ASSERT(!mDirtySamplerMapping);
1746 ANGLE_TRY(mRenderer->applyComputeUniforms(*this, mD3DUniforms));
1747
1748 for (D3DUniform *d3dUniform : mD3DUniforms)
1749 {
1750 d3dUniform->dirty = false;
1751 }
1752
1753 return gl::NoError();
1754}
1755
Jamie Madill9082b982016-04-27 15:21:51 -04001756gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001757{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001758 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001759 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001760 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001761 }
1762
Jamie Madill51f522f2016-12-21 15:10:55 -05001763 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001764
Jamie Madill03260fa2015-06-22 13:57:22 -04001765 mVertexUBOCache.clear();
1766 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001767
1768 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1769 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1770
Jamie Madill4a3c2342015-10-08 12:58:45 -04001771 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001772 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001773 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001774 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001775 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001776
Brandon Jones18bd4102014-09-22 14:21:44 -07001777 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001778 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001779 {
1780 continue;
1781 }
1782
Jamie Madill4a3c2342015-10-08 12:58:45 -04001783 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001784 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001785 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001786 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001787
Jamie Madill969194d2015-07-20 14:36:56 -04001788 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001789 {
1790 mVertexUBOCache.resize(registerIndex + 1, -1);
1791 }
1792
1793 ASSERT(mVertexUBOCache[registerIndex] == -1);
1794 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001795 }
1796
Jamie Madill4a3c2342015-10-08 12:58:45 -04001797 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001798 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001799 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001800 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001801
1802 if (mFragmentUBOCache.size() <= registerIndex)
1803 {
1804 mFragmentUBOCache.resize(registerIndex + 1, -1);
1805 }
1806
1807 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1808 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001809 }
1810 }
1811
Jamie Madill03260fa2015-06-22 13:57:22 -04001812 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001813}
1814
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001815void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001816{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001817 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001818 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001819 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001820 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001821}
1822
Jamie Madill334d6152015-10-22 14:00:28 -04001823void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001824{
1825 setUniform(location, count, v, GL_FLOAT);
1826}
1827
1828void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1829{
1830 setUniform(location, count, v, GL_FLOAT_VEC2);
1831}
1832
1833void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1834{
1835 setUniform(location, count, v, GL_FLOAT_VEC3);
1836}
1837
1838void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1839{
1840 setUniform(location, count, v, GL_FLOAT_VEC4);
1841}
1842
Jamie Madill334d6152015-10-22 14:00:28 -04001843void ProgramD3D::setUniformMatrix2fv(GLint location,
1844 GLsizei count,
1845 GLboolean transpose,
1846 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001847{
1848 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1849}
1850
Jamie Madill334d6152015-10-22 14:00:28 -04001851void ProgramD3D::setUniformMatrix3fv(GLint location,
1852 GLsizei count,
1853 GLboolean transpose,
1854 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001855{
1856 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1857}
1858
Jamie Madill334d6152015-10-22 14:00:28 -04001859void ProgramD3D::setUniformMatrix4fv(GLint location,
1860 GLsizei count,
1861 GLboolean transpose,
1862 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001863{
1864 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1865}
1866
Jamie Madill334d6152015-10-22 14:00:28 -04001867void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1868 GLsizei count,
1869 GLboolean transpose,
1870 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001871{
1872 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1873}
1874
Jamie Madill334d6152015-10-22 14:00:28 -04001875void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1876 GLsizei count,
1877 GLboolean transpose,
1878 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001879{
1880 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1881}
1882
Jamie Madill334d6152015-10-22 14:00:28 -04001883void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1884 GLsizei count,
1885 GLboolean transpose,
1886 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001887{
1888 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1889}
1890
Jamie Madill334d6152015-10-22 14:00:28 -04001891void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1892 GLsizei count,
1893 GLboolean transpose,
1894 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001895{
1896 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1897}
1898
Jamie Madill334d6152015-10-22 14:00:28 -04001899void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1900 GLsizei count,
1901 GLboolean transpose,
1902 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001903{
1904 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1905}
1906
Jamie Madill334d6152015-10-22 14:00:28 -04001907void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1908 GLsizei count,
1909 GLboolean transpose,
1910 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001911{
1912 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1913}
1914
1915void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1916{
1917 setUniform(location, count, v, GL_INT);
1918}
1919
1920void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1921{
1922 setUniform(location, count, v, GL_INT_VEC2);
1923}
1924
1925void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1926{
1927 setUniform(location, count, v, GL_INT_VEC3);
1928}
1929
1930void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1931{
1932 setUniform(location, count, v, GL_INT_VEC4);
1933}
1934
1935void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1936{
1937 setUniform(location, count, v, GL_UNSIGNED_INT);
1938}
1939
1940void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1941{
1942 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1943}
1944
1945void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1946{
1947 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1948}
1949
1950void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1951{
1952 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1953}
1954
Jamie Madill4a3c2342015-10-08 12:58:45 -04001955void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1956 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001957{
1958}
1959
Jamie Madillbd044ed2017-06-05 12:59:21 -04001960void ProgramD3D::defineUniformsAndAssignRegisters(const gl::Context *context)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001962 D3DUniformMap uniformMap;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001963 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001964 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05001965 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001966 for (const sh::Uniform &computeUniform : computeShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04001967 {
Xinghua Caob1239382016-12-13 15:07:05 +08001968 if (computeUniform.staticUse)
1969 {
1970 defineUniformBase(computeShader, computeUniform, &uniformMap);
1971 }
Jamie Madillfb536032015-09-11 13:19:49 -04001972 }
1973 }
Xinghua Caob1239382016-12-13 15:07:05 +08001974 else
Jamie Madillfb536032015-09-11 13:19:49 -04001975 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001976 gl::Shader *vertexShader = mState.getAttachedVertexShader();
1977 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04001978 {
Xinghua Caob1239382016-12-13 15:07:05 +08001979 if (vertexUniform.staticUse)
1980 {
1981 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
1982 }
1983 }
1984
Jamie Madillbd044ed2017-06-05 12:59:21 -04001985 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
1986 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms(context))
Xinghua Caob1239382016-12-13 15:07:05 +08001987 {
1988 if (fragmentUniform.staticUse)
1989 {
1990 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
1991 }
Jamie Madillfb536032015-09-11 13:19:49 -04001992 }
1993 }
1994
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001995 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001996 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001997 {
1998 if (!glUniform.isInDefaultBlock())
1999 continue;
2000
2001 auto mapEntry = uniformMap.find(glUniform.name);
2002 ASSERT(mapEntry != uniformMap.end());
2003 mD3DUniforms.push_back(mapEntry->second);
2004 }
2005
Jamie Madill62d31cb2015-09-11 13:25:51 -04002006 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04002007 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04002008}
2009
Jamie Madill91445bc2015-09-23 16:47:53 -04002010void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002011 const sh::Uniform &uniform,
2012 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04002013{
Olli Etuaho96963162016-03-21 11:54:33 +02002014 // Samplers get their registers assigned in assignAllSamplerRegisters.
2015 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04002016 {
Jamie Madill91445bc2015-09-23 16:47:53 -04002017 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04002018 return;
2019 }
2020
Jamie Madill91445bc2015-09-23 16:47:53 -04002021 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
2022
2023 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
2024 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04002025 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002026 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002027
Jamie Madill91445bc2015-09-23 16:47:53 -04002028 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002029}
2030
Jamie Madill62d31cb2015-09-11 13:25:51 -04002031D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
2032{
2033 for (D3DUniform *d3dUniform : mD3DUniforms)
2034 {
2035 if (d3dUniform->name == name)
2036 {
2037 return d3dUniform;
2038 }
2039 }
2040
2041 return nullptr;
2042}
2043
Jamie Madill91445bc2015-09-23 16:47:53 -04002044void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002045 const sh::ShaderVariable &uniform,
2046 const std::string &fullName,
2047 sh::HLSLBlockEncoder *encoder,
2048 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049{
2050 if (uniform.isStruct())
2051 {
2052 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2053 {
2054 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2055
Jamie Madill55def582015-05-04 11:24:57 -04002056 if (encoder)
2057 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002058
2059 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2060 {
Jamie Madill334d6152015-10-22 14:00:28 -04002061 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002062 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2063
Olli Etuaho96963162016-03-21 11:54:33 +02002064 // Samplers get their registers assigned in assignAllSamplerRegisters.
2065 // Also they couldn't use the same encoder as the rest of the struct, since they are
2066 // extracted out of the struct by the shader translator.
2067 if (gl::IsSamplerType(field.type))
2068 {
2069 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2070 }
2071 else
2072 {
2073 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2074 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002075 }
2076
Jamie Madill55def582015-05-04 11:24:57 -04002077 if (encoder)
2078 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002079 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002081 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002082
2083 // Not a struct. Arrays are treated as aggregate types.
2084 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002085 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002086 encoder->enterAggregateType();
2087 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002088
Jamie Madill62d31cb2015-09-11 13:25:51 -04002089 // Advance the uniform offset, to track registers allocation for structs
2090 sh::BlockMemberInfo blockInfo =
2091 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2092 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002093
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002094 auto uniformMapEntry = uniformMap->find(fullName);
2095 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002096
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002097 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002098 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002099 d3dUniform = uniformMapEntry->second;
2100 }
2101 else
2102 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002103 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002104 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002105 }
2106
2107 if (encoder)
2108 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002109 d3dUniform->registerElement =
2110 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002111 unsigned int reg =
2112 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002113 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002114 {
2115 d3dUniform->psRegisterIndex = reg;
2116 }
Xinghua Caob1239382016-12-13 15:07:05 +08002117 else if (shaderType == GL_VERTEX_SHADER)
2118 {
2119 d3dUniform->vsRegisterIndex = reg;
2120 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002121 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002122 {
Xinghua Caob1239382016-12-13 15:07:05 +08002123 ASSERT(shaderType == GL_COMPUTE_SHADER);
2124 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002125 }
Jamie Madillfb536032015-09-11 13:19:49 -04002126
2127 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002128 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002129 {
2130 encoder->exitAggregateType();
2131 }
2132 }
2133}
2134
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002135template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002136void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002137{
Jamie Madill334d6152015-10-22 14:00:28 -04002138 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002139 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2140
Jamie Madill62d31cb2015-09-11 13:25:51 -04002141 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002142
Jamie Madill62d31cb2015-09-11 13:25:51 -04002143 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002144 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002145 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002146
2147 if (targetUniform->type == targetUniformType)
2148 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002149 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002150
Jamie Madill62d31cb2015-09-11 13:25:51 -04002151 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002152 {
Jamie Madill334d6152015-10-22 14:00:28 -04002153 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002154 const T *source = v + (i * components);
2155
2156 for (int c = 0; c < components; c++)
2157 {
2158 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2159 }
2160 for (int c = components; c < 4; c++)
2161 {
2162 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2163 }
2164 }
2165 }
2166 else if (targetUniform->type == targetBoolType)
2167 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002168 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002169
Jamie Madill62d31cb2015-09-11 13:25:51 -04002170 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002171 {
Jamie Madill334d6152015-10-22 14:00:28 -04002172 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002173 const T *source = v + (i * components);
2174
2175 for (int c = 0; c < components; c++)
2176 {
Jamie Madill334d6152015-10-22 14:00:28 -04002177 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2178 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002179 }
2180 for (int c = components; c < 4; c++)
2181 {
2182 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2183 }
2184 }
2185 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002186 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002187 {
2188 ASSERT(targetUniformType == GL_INT);
2189
Jamie Madill62d31cb2015-09-11 13:25:51 -04002190 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002191
2192 bool wasDirty = targetUniform->dirty;
2193
Jamie Madill62d31cb2015-09-11 13:25:51 -04002194 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002195 {
Jamie Madill334d6152015-10-22 14:00:28 -04002196 GLint *dest = target + (i * 4);
2197 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002198
2199 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2200 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2201 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2202 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2203 }
2204
2205 if (!wasDirty && targetUniform->dirty)
2206 {
2207 mDirtySamplerMapping = true;
2208 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002209 }
Jamie Madill334d6152015-10-22 14:00:28 -04002210 else
2211 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002212}
2213
2214template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002215void ProgramD3D::setUniformMatrixfv(GLint location,
2216 GLsizei countIn,
2217 GLboolean transpose,
2218 const GLfloat *value,
2219 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002220{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002221 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002222
Jamie Madill62d31cb2015-09-11 13:25:51 -04002223 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002224 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002225 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002226
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002227 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002228 GLfloat *target =
2229 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002230
Jamie Madill62d31cb2015-09-11 13:25:51 -04002231 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002232 {
2233 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2234 if (transpose == GL_FALSE)
2235 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002236 targetUniform->dirty =
2237 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002238 }
2239 else
2240 {
Jamie Madill334d6152015-10-22 14:00:28 -04002241 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002242 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002243 }
2244 target += targetMatrixStride;
2245 value += cols * rows;
2246 }
2247}
2248
Jamie Madill4a3c2342015-10-08 12:58:45 -04002249size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002250{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002251 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002252
Jamie Madill62d31cb2015-09-11 13:25:51 -04002253 // define member uniforms
2254 sh::Std140BlockEncoder std140Encoder;
Jamie Madillcc2ed612017-03-14 15:59:00 -04002255 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002256 sh::BlockLayoutEncoder *encoder = nullptr;
2257
2258 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002259 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002260 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002261 }
2262 else
2263 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002264 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002265 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002266
Jamie Madill39046162016-02-08 15:05:17 -05002267 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2268 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002269
2270 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002271}
2272
Jamie Madill62d31cb2015-09-11 13:25:51 -04002273void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002274{
Olli Etuaho96963162016-03-21 11:54:33 +02002275 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002276 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002277 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002278 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002279 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002280 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002281 }
2282}
2283
Olli Etuaho96963162016-03-21 11:54:33 +02002284void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002285{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002286 ASSERT(d3dUniform->isSampler());
Xinghua Caob1239382016-12-13 15:07:05 +08002287 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2288 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002289 {
Xinghua Caob1239382016-12-13 15:07:05 +08002290 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2291 ASSERT(computeShaderD3D->hasUniform(d3dUniform));
2292 d3dUniform->csRegisterIndex = computeShaderD3D->getUniformRegister(d3dUniform->name);
2293 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2294 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2295 mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002296 }
Xinghua Caob1239382016-12-13 15:07:05 +08002297 else
Jamie Madillfb536032015-09-11 13:19:49 -04002298 {
Xinghua Caob1239382016-12-13 15:07:05 +08002299 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2300 const ShaderD3D *fragmentShaderD3D =
2301 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
2302 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) ||
2303 fragmentShaderD3D->hasUniform(d3dUniform));
2304 if (vertexShaderD3D->hasUniform(d3dUniform))
2305 {
2306 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2307 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
2308 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2309 mSamplersVS, &mUsedVertexSamplerRange);
2310 }
2311 if (fragmentShaderD3D->hasUniform(d3dUniform))
2312 {
2313 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2314 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
2315 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2316 mSamplersPS, &mUsedPixelSamplerRange);
2317 }
Jamie Madillfb536032015-09-11 13:19:49 -04002318 }
2319}
2320
Jamie Madill62d31cb2015-09-11 13:25:51 -04002321// static
2322void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002323 GLenum samplerType,
2324 unsigned int samplerCount,
2325 std::vector<Sampler> &outSamplers,
2326 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002327{
2328 unsigned int samplerIndex = startSamplerIndex;
2329
2330 do
2331 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002332 ASSERT(samplerIndex < outSamplers.size());
2333 Sampler *sampler = &outSamplers[samplerIndex];
2334 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002335 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002336 sampler->logicalTextureUnit = 0;
2337 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002338 samplerIndex++;
2339 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002340}
2341
Brandon Jonesc9610c52014-08-25 17:02:59 -07002342void ProgramD3D::reset()
2343{
Xinghua Caob1239382016-12-13 15:07:05 +08002344 mVertexExecutables.clear();
2345 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002346
Xinghua Caob1239382016-12-13 15:07:05 +08002347 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002348 {
Xinghua Caob1239382016-12-13 15:07:05 +08002349 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002350 }
Brandon Joneseb994362014-09-24 10:27:28 -07002351
Xinghua Caob1239382016-12-13 15:07:05 +08002352 mComputeExecutable.reset(nullptr);
2353
Brandon Jones22502d52014-08-29 16:58:36 -07002354 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002355 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002356
2357 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002358 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002359 mUsesFragDepth = false;
Martin Radev41ac68e2017-06-06 12:16:58 +03002360 mHasANGLEMultiviewEnabled = false;
2361 mUsesViewID = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002362 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002363 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002364 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002365
Jamie Madill62d31cb2015-09-11 13:25:51 -04002366 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002367 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002368
Xinghua Caob1239382016-12-13 15:07:05 +08002369 mVertexUniformStorage.reset(nullptr);
2370 mFragmentUniformStorage.reset(nullptr);
2371 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002372
2373 mSamplersPS.clear();
2374 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002375 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002376
2377 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002378 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002379 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002380 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002381
Jamie Madill8047c0d2016-03-07 13:02:12 -05002382 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002383
Jamie Madill9fc36822015-11-18 13:08:07 -05002384 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002385
2386 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002387}
2388
Geoff Lang7dd2e102014-11-10 15:19:26 -05002389unsigned int ProgramD3D::getSerial() const
2390{
2391 return mSerial;
2392}
2393
2394unsigned int ProgramD3D::issueSerial()
2395{
2396 return mCurrentSerial++;
2397}
2398
Jamie Madillbd044ed2017-06-05 12:59:21 -04002399void ProgramD3D::initAttribLocationsToD3DSemantic(const gl::Context *context)
Jamie Madill63805b42015-08-25 13:17:39 -04002400{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002401 gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002402 ASSERT(vertexShader != nullptr);
2403
2404 // Init semantic index
Jamie Madill34ca4f52017-06-13 11:49:39 -04002405 int semanticIndex = 0;
2406 for (const sh::Attribute &attribute : vertexShader->getActiveAttributes(context))
Jamie Madill63805b42015-08-25 13:17:39 -04002407 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002408 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill34ca4f52017-06-13 11:49:39 -04002409 GLuint location = mState.getAttributeLocation(attribute.name);
2410 ASSERT(location != std::numeric_limits<GLuint>::max());
Jamie Madill63805b42015-08-25 13:17:39 -04002411
Jamie Madill8047c0d2016-03-07 13:02:12 -05002412 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002413 {
Jamie Madill34ca4f52017-06-13 11:49:39 -04002414 mAttribLocationToD3DSemantic[location + reg] = semanticIndex++;
Jamie Madill63805b42015-08-25 13:17:39 -04002415 }
2416 }
Jamie Madill437d2662014-12-05 14:23:35 -05002417}
2418
Jamie Madilla779b612017-07-24 11:46:05 -04002419void ProgramD3D::updateCachedInputLayout(Serial associatedSerial, const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002420{
Jamie Madilla779b612017-07-24 11:46:05 -04002421 if (mCurrentVertexArrayStateSerial == associatedSerial)
2422 {
2423 return;
2424 }
2425
2426 mCurrentVertexArrayStateSerial = associatedSerial;
Jamie Madillbd136f92015-08-10 14:51:37 -04002427 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002428 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002429
Jamie Madill6de51852017-04-12 09:53:01 -04002430 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002431 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002432 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002433
Jamie Madill8047c0d2016-03-07 13:02:12 -05002434 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002435 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002436 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002437 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002438 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002439 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002440 mCachedInputLayout[d3dSemantic] =
2441 GetVertexFormatType(vertexAttributes[locationIndex],
2442 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002443 }
2444 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002445
2446 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
2447}
2448
2449void ProgramD3D::updateCachedOutputLayout(const gl::Context *context,
2450 const gl::Framebuffer *framebuffer)
2451{
2452 GetPixelOutputLayoutFromFramebuffer(context, framebuffer, &mPixelShaderOutputLayoutCache);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002453}
2454
Jamie Madill192745a2016-12-22 15:58:21 -05002455void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2456 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002457{
Jamie Madill9fc36822015-11-18 13:08:07 -05002458 const std::string &varyingSemantic =
2459 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2460
Jamie Madillccdf74b2015-08-18 10:46:12 -04002461 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002462 mStreamOutVaryings.clear();
2463
Jamie Madill48ef11b2016-04-27 15:21:52 -04002464 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002465 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2466 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002467 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002468 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2469 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002470 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002471 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002472 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002473 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2474 builtins.glPosition.index, 4, outputSlot));
2475 }
2476 }
2477 else if (tfVaryingName == "gl_FragCoord")
2478 {
2479 if (builtins.glFragCoord.enabled)
2480 {
2481 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2482 builtins.glFragCoord.index, 4, outputSlot));
2483 }
2484 }
2485 else if (tfVaryingName == "gl_PointSize")
2486 {
2487 if (builtins.glPointSize.enabled)
2488 {
2489 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2490 }
2491 }
2492 else
2493 {
jchen10a9042d32017-03-17 08:50:45 +08002494 size_t subscript = GL_INVALID_INDEX;
2495 std::string baseName = gl::ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002496 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002497 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002498 const auto &varying = *registerInfo.packedVarying->varying;
2499 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002500 int componentCount = gl::VariableColumnCount(transposedType);
2501 ASSERT(!varying.isBuiltIn());
2502
Jamie Madill55c25d02015-11-18 13:08:08 -05002503 // Transform feedback for varying structs is underspecified.
2504 // See Khronos bug 9856.
2505 // TODO(jmadill): Figure out how to be spec-compliant here.
2506 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2507 continue;
2508
Jamie Madill9fc36822015-11-18 13:08:07 -05002509 // There can be more than one register assigned to a particular varying, and each
2510 // register needs its own stream out entry.
jchen10a9042d32017-03-17 08:50:45 +08002511 if (baseName == registerInfo.packedVarying->varying->name &&
2512 (subscript == GL_INVALID_INDEX || subscript == registerInfo.varyingArrayIndex))
Jamie Madill9fc36822015-11-18 13:08:07 -05002513 {
2514 mStreamOutVaryings.push_back(D3DVarying(
2515 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2516 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002517 }
2518 }
2519 }
2520}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002521
2522D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2523{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002524 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002525}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002526
2527bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2528{
2529 std::string baseName = blockName;
2530 gl::ParseAndStripArrayIndex(&baseName);
2531
2532 auto sizeIter = mBlockDataSizes.find(baseName);
2533 if (sizeIter == mBlockDataSizes.end())
2534 {
2535 *sizeOut = 0;
2536 return false;
2537 }
2538
2539 *sizeOut = sizeIter->second;
2540 return true;
2541}
2542
2543bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2544 sh::BlockMemberInfo *memberInfoOut) const
2545{
2546 auto infoIter = mBlockInfo.find(memberUniformName);
2547 if (infoIter == mBlockInfo.end())
2548 {
2549 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2550 return false;
2551 }
2552
2553 *memberInfoOut = infoIter->second;
2554 return true;
2555}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002556
2557void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2558 GLenum genMode,
2559 GLint components,
2560 const GLfloat *coeffs)
2561{
2562 UNREACHABLE();
2563}
2564
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002565bool ProgramD3D::hasVertexExecutableForCachedInputLayout()
2566{
2567 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
2568
2569 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
2570 {
2571 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
2572 {
2573 return true;
2574 }
2575 }
2576
2577 return false;
2578}
2579
2580bool ProgramD3D::hasGeometryExecutableForPrimitiveType(GLenum drawMode)
2581{
2582 if (!usesGeometryShader(drawMode))
2583 {
2584 // No shader necessary mean we have the required (null) executable.
2585 return true;
2586 }
2587
2588 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
2589 return mGeometryExecutables[geometryShaderType].get() != nullptr;
2590}
2591
2592bool ProgramD3D::hasPixelExecutableForCachedOutputLayout()
2593{
2594 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
2595 {
2596 if (mPixelExecutables[executableIndex]->matchesSignature(mPixelShaderOutputLayoutCache))
2597 {
2598 return true;
2599 }
2600 }
2601
2602 return false;
2603}
2604
Jamie Madill8047c0d2016-03-07 13:02:12 -05002605} // namespace rx