blob: 74fecd07bd13794b27a218e93129ce9770234c4e [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050010
Jamie Madill20e005b2017-04-07 14:19:22 -040011#include "common/bitset_utils.h"
Olli Etuahod2551232017-10-26 20:03:33 +030012#include "common/string_utils.h"
Jamie Madill437d2662014-12-05 14:23:35 -050013#include "common/utilities.h"
Jamie Madillc564c072017-06-01 12:45:42 -040014#include "libANGLE/Context.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050015#include "libANGLE/Framebuffer.h"
16#include "libANGLE/FramebufferAttachment.h"
17#include "libANGLE/Program.h"
Jamie Madill7af0de52017-11-06 17:09:33 -050018#include "libANGLE/ProgramLinkedResources.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040019#include "libANGLE/Uniform.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040020#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000021#include "libANGLE/features.h"
Jamie Madill54164b02017-08-28 15:17:37 -040022#include "libANGLE/queryconversions.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050023#include "libANGLE/renderer/ContextImpl.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050024#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050025#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050026#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050027#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050028#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050029
Jamie Madill01074252016-11-28 15:55:51 -050030using namespace angle;
31
Brandon Jonesc9610c52014-08-25 17:02:59 -070032namespace rx
33{
34
Brandon Joneseb994362014-09-24 10:27:28 -070035namespace
36{
37
Jamie Madill4c19a8a2017-07-24 11:46:06 -040038void GetDefaultInputLayoutFromShader(const gl::Context *context,
39 gl::Shader *vertexShader,
40 gl::InputLayout *inputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070041{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040042 inputLayoutOut->clear();
43
Jamie Madillbd044ed2017-06-05 12:59:21 -040044 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes(context))
Brandon Joneseb994362014-09-24 10:27:28 -070045 {
Brandon Joneseb994362014-09-24 10:27:28 -070046 if (shaderAttr.type != GL_NONE)
47 {
48 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
49
Jamie Madilld3dfda22015-07-06 08:28:49 -040050 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040051 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070052 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040053 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040054 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Xinghua Cao26143fd2017-11-01 18:19:05 +080055 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040056 gl::VertexFormatType defaultType =
57 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070058
Jamie Madill4c19a8a2017-07-24 11:46:06 -040059 inputLayoutOut->push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070060 }
61 }
62 }
63}
64
Jamie Madill4c19a8a2017-07-24 11:46:06 -040065void GetDefaultOutputLayoutFromShader(
66 const std::vector<PixelShaderOutputVariable> &shaderOutputVars,
67 std::vector<GLenum> *outputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070068{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040069 outputLayoutOut->clear();
Brandon Joneseb994362014-09-24 10:27:28 -070070
Jamie Madillb4463142014-12-19 14:56:54 -050071 if (!shaderOutputVars.empty())
72 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -040073 outputLayoutOut->push_back(GL_COLOR_ATTACHMENT0 +
74 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050075 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -040076}
Brandon Joneseb994362014-09-24 10:27:28 -070077
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080078template <typename T, int cols, int rows>
79bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -040080{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080081 constexpr int targetWidth = 4;
82 constexpr int targetHeight = rows;
83 constexpr int srcWidth = rows;
84 constexpr int srcHeight = cols;
85
86 constexpr int copyWidth = std::min(targetHeight, srcWidth);
87 constexpr int copyHeight = std::min(targetWidth, srcHeight);
88
89 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -040090
91 for (int x = 0; x < copyWidth; x++)
92 {
93 for (int y = 0; y < copyHeight; y++)
94 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080095 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -040096 }
97 }
98
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080099 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
100 {
101 return false;
102 }
103
104 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
105 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400106}
107
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800108template <typename T, int cols, int rows>
109bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400110{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800111 constexpr int targetWidth = 4;
112 constexpr int targetHeight = rows;
Xinghua Cao26143fd2017-11-01 18:19:05 +0800113 constexpr int srcWidth = cols;
114 constexpr int srcHeight = rows;
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800115
116 constexpr int copyWidth = std::min(targetWidth, srcWidth);
117 constexpr int copyHeight = std::min(targetHeight, srcHeight);
118
119 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400120
121 for (int y = 0; y < copyHeight; y++)
122 {
123 for (int x = 0; x < copyWidth; x++)
124 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800125 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400126 }
127 }
128
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800129 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
130 {
131 return false;
132 }
133
134 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
135 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400136}
137
Jamie Madill4e31ad52015-10-29 10:32:57 -0400138gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
139{
140 switch (drawMode)
141 {
142 // Uses the point sprite geometry shader.
143 case GL_POINTS:
144 return gl::PRIMITIVE_POINTS;
145
146 // All line drawing uses the same geometry shader.
147 case GL_LINES:
148 case GL_LINE_STRIP:
149 case GL_LINE_LOOP:
150 return gl::PRIMITIVE_LINES;
151
152 // The triangle fan primitive is emulated with strips in D3D11.
153 case GL_TRIANGLES:
154 case GL_TRIANGLE_FAN:
155 return gl::PRIMITIVE_TRIANGLES;
156
157 // Special case for triangle strips.
158 case GL_TRIANGLE_STRIP:
159 return gl::PRIMITIVE_TRIANGLE_STRIP;
160
161 default:
162 UNREACHABLE();
163 return gl::PRIMITIVE_TYPE_MAX;
164 }
165}
166
Jamie Madill192745a2016-12-22 15:58:21 -0500167bool FindFlatInterpolationVarying(const std::vector<sh::Varying> &varyings)
168{
169 // Note: this assumes nested structs can only be packed with one interpolation.
170 for (const auto &varying : varyings)
171 {
172 if (varying.interpolation == sh::INTERPOLATION_FLAT)
173 {
174 return true;
175 }
176 }
177
178 return false;
179}
180
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400181// Helper method to de-tranpose a matrix uniform for an API query.
182void GetMatrixUniform(GLint columns, GLint rows, GLfloat *dataOut, const GLfloat *source)
183{
184 for (GLint col = 0; col < columns; ++col)
185 {
186 for (GLint row = 0; row < rows; ++row)
187 {
188 GLfloat *outptr = dataOut + ((col * rows) + row);
189 const GLfloat *inptr = source + ((row * 4) + col);
190 *outptr = *inptr;
191 }
192 }
193}
194
195template <typename NonFloatT>
196void GetMatrixUniform(GLint columns, GLint rows, NonFloatT *dataOut, const NonFloatT *source)
197{
198 UNREACHABLE();
199}
200
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500201class UniformBlockInfo final : angle::NonCopyable
202{
203 public:
204 UniformBlockInfo() {}
205
206 void getShaderBlockInfo(const gl::Context *context, gl::Shader *shader);
207
208 bool getBlockSize(const std::string &name, const std::string &mappedName, size_t *sizeOut);
209 bool getBlockMemberInfo(const std::string &name,
210 const std::string &mappedName,
211 sh::BlockMemberInfo *infoOut);
212
213 private:
214 size_t getBlockInfo(const sh::InterfaceBlock &interfaceBlock);
215
216 std::map<std::string, size_t> mBlockSizes;
217 sh::BlockLayoutMap mBlockLayout;
218};
219
220void UniformBlockInfo::getShaderBlockInfo(const gl::Context *context, gl::Shader *shader)
221{
222 for (const sh::InterfaceBlock &interfaceBlock : shader->getUniformBlocks(context))
223 {
Olli Etuaho107c7242018-03-20 15:45:35 +0200224 if (!interfaceBlock.active && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500225 continue;
226
227 if (mBlockSizes.count(interfaceBlock.name) > 0)
228 continue;
229
230 size_t dataSize = getBlockInfo(interfaceBlock);
231 mBlockSizes[interfaceBlock.name] = dataSize;
232 }
233}
234
235size_t UniformBlockInfo::getBlockInfo(const sh::InterfaceBlock &interfaceBlock)
236{
Olli Etuaho107c7242018-03-20 15:45:35 +0200237 ASSERT(interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500238
239 // define member uniforms
240 sh::Std140BlockEncoder std140Encoder;
241 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
242 sh::BlockLayoutEncoder *encoder = nullptr;
243
244 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STD140)
245 {
246 encoder = &std140Encoder;
247 }
248 else
249 {
250 encoder = &hlslEncoder;
251 }
252
253 sh::GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
Olli Etuaho3de27032017-11-30 12:16:47 +0200254 &mBlockLayout);
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500255
256 return encoder->getBlockSize();
257}
258
259bool UniformBlockInfo::getBlockSize(const std::string &name,
260 const std::string &mappedName,
261 size_t *sizeOut)
262{
263 size_t nameLengthWithoutArrayIndex;
264 gl::ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
265 std::string baseName = name.substr(0u, nameLengthWithoutArrayIndex);
266 auto sizeIter = mBlockSizes.find(baseName);
267 if (sizeIter == mBlockSizes.end())
268 {
269 *sizeOut = 0;
270 return false;
271 }
272
273 *sizeOut = sizeIter->second;
274 return true;
275};
276
277bool UniformBlockInfo::getBlockMemberInfo(const std::string &name,
278 const std::string &mappedName,
279 sh::BlockMemberInfo *infoOut)
280{
281 auto infoIter = mBlockLayout.find(name);
282 if (infoIter == mBlockLayout.end())
283 {
284 *infoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
285 return false;
286 }
287
288 *infoOut = infoIter->second;
289 return true;
290};
291
Jamie Madillada9ecc2015-08-17 12:53:37 -0400292} // anonymous namespace
293
Jamie Madill28afae52015-11-09 15:07:57 -0500294// D3DUniform Implementation
295
Jamie Madill33bb7c42017-09-09 23:32:51 -0400296D3DUniform::D3DUniform(GLenum type,
Xinghua Cao26143fd2017-11-01 18:19:05 +0800297 HLSLRegisterType reg,
Jamie Madill62d31cb2015-09-11 13:25:51 -0400298 const std::string &nameIn,
Olli Etuaho465835d2017-09-26 13:34:10 +0300299 const std::vector<unsigned int> &arraySizesIn,
Jamie Madill62d31cb2015-09-11 13:25:51 -0400300 bool defaultBlock)
Jamie Madill33bb7c42017-09-09 23:32:51 -0400301 : typeInfo(gl::GetUniformTypeInfo(type)),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400302 name(nameIn),
Olli Etuaho465835d2017-09-26 13:34:10 +0300303 arraySizes(arraySizesIn),
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400304 vsData(nullptr),
305 psData(nullptr),
306 csData(nullptr),
Xinghua Cao26143fd2017-11-01 18:19:05 +0800307 regType(reg),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400308 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400309 psRegisterIndex(GL_INVALID_INDEX),
Xinghua Caob1239382016-12-13 15:07:05 +0800310 csRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400311 registerCount(0),
312 registerElement(0)
313{
314 // We use data storage for default block uniforms to cache values that are sent to D3D during
315 // rendering
316 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
317 if (defaultBlock)
318 {
Jamie Madillcc2ed612017-03-14 15:59:00 -0400319 // Use the row count as register count, will work for non-square matrices.
Olli Etuaho465835d2017-09-26 13:34:10 +0300320 registerCount = typeInfo.rowCount * getArraySizeProduct();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400321 }
322}
323
324D3DUniform::~D3DUniform()
325{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400326}
327
Olli Etuaho465835d2017-09-26 13:34:10 +0300328unsigned int D3DUniform::getArraySizeProduct() const
329{
330 return gl::ArraySizeProduct(arraySizes);
331}
332
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400333const uint8_t *D3DUniform::getDataPtrToElement(size_t elementIndex) const
334{
Olli Etuaho465835d2017-09-26 13:34:10 +0300335 ASSERT((!isArray() && elementIndex == 0) ||
336 (isArray() && elementIndex < getArraySizeProduct()));
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400337
338 if (isSampler())
339 {
340 return reinterpret_cast<const uint8_t *>(&mSamplerData[elementIndex]);
341 }
342
Jamie Madill33bb7c42017-09-09 23:32:51 -0400343 return firstNonNullData() + (elementIndex > 0 ? (typeInfo.internalSize * elementIndex) : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400344}
345
346bool D3DUniform::isSampler() const
347{
Jamie Madill33bb7c42017-09-09 23:32:51 -0400348 return typeInfo.isSampler;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400349}
350
Xinghua Cao26143fd2017-11-01 18:19:05 +0800351bool D3DUniform::isImage() const
352{
353 return typeInfo.isImageType;
354}
355
Jamie Madill62d31cb2015-09-11 13:25:51 -0400356bool D3DUniform::isReferencedByVertexShader() const
357{
358 return vsRegisterIndex != GL_INVALID_INDEX;
359}
360
361bool D3DUniform::isReferencedByFragmentShader() const
362{
363 return psRegisterIndex != GL_INVALID_INDEX;
364}
365
Xinghua Caob1239382016-12-13 15:07:05 +0800366bool D3DUniform::isReferencedByComputeShader() const
367{
368 return csRegisterIndex != GL_INVALID_INDEX;
369}
370
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400371const uint8_t *D3DUniform::firstNonNullData() const
372{
373 ASSERT(vsData || psData || csData || !mSamplerData.empty());
374
375 if (!mSamplerData.empty())
376 {
377 return reinterpret_cast<const uint8_t *>(mSamplerData.data());
378 }
379
380 return vsData ? vsData : (psData ? psData : csData);
381}
382
Jamie Madill28afae52015-11-09 15:07:57 -0500383// D3DVarying Implementation
384
Jamie Madill9fc36822015-11-18 13:08:07 -0500385D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500386{
387}
388
Jamie Madill9fc36822015-11-18 13:08:07 -0500389D3DVarying::D3DVarying(const std::string &semanticNameIn,
390 unsigned int semanticIndexIn,
391 unsigned int componentCountIn,
392 unsigned int outputSlotIn)
393 : semanticName(semanticNameIn),
394 semanticIndex(semanticIndexIn),
395 componentCount(componentCountIn),
396 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500397{
398}
399
Jamie Madille39a3f02015-11-17 20:42:15 -0500400// ProgramD3DMetadata Implementation
401
Jamie Madillc9bde922016-07-24 17:58:50 -0400402ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500403 const ShaderD3D *vertexShader,
404 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400405 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
406 mShaderModelSuffix(renderer->getShaderModelSuffix()),
407 mUsesInstancedPointSpriteEmulation(
408 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
409 mUsesViewScale(renderer->presentPathFastEnabled()),
Martin Radev41ac68e2017-06-06 12:16:58 +0300410 mHasANGLEMultiviewEnabled(vertexShader->hasANGLEMultiviewEnabled()),
411 mUsesViewID(fragmentShader->usesViewID()),
Martin Radevc1d4e552017-08-21 12:01:10 +0300412 mCanSelectViewInVertexShader(renderer->canSelectViewInVertexShader()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500413 mVertexShader(vertexShader),
414 mFragmentShader(fragmentShader)
415{
416}
417
418int ProgramD3DMetadata::getRendererMajorShaderModel() const
419{
420 return mRendererMajorShaderModel;
421}
422
Jamie Madill9082b982016-04-27 15:21:51 -0400423bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500424{
Corentin Wallezc084de12017-06-05 14:28:52 -0700425 return (mFragmentShader->usesFragColor() && mFragmentShader->usesMultipleRenderTargets() &&
426 data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500427}
428
Jamie Madill48ef11b2016-04-27 15:21:52 -0400429bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500430{
Jamie Madill63286672015-11-24 13:00:08 -0500431 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500432}
433
434bool ProgramD3DMetadata::usesPointCoord() const
435{
436 return mFragmentShader->usesPointCoord();
437}
438
439bool ProgramD3DMetadata::usesFragCoord() const
440{
441 return mFragmentShader->usesFragCoord();
442}
443
444bool ProgramD3DMetadata::usesPointSize() const
445{
446 return mVertexShader->usesPointSize();
447}
448
449bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
450{
Jamie Madillc9bde922016-07-24 17:58:50 -0400451 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
452 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500453}
454
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800455bool ProgramD3DMetadata::usesViewScale() const
456{
457 return mUsesViewScale;
458}
459
Martin Radev41ac68e2017-06-06 12:16:58 +0300460bool ProgramD3DMetadata::hasANGLEMultiviewEnabled() const
461{
462 return mHasANGLEMultiviewEnabled;
463}
464
465bool ProgramD3DMetadata::usesViewID() const
466{
467 return mUsesViewID;
468}
469
Martin Radevc1d4e552017-08-21 12:01:10 +0300470bool ProgramD3DMetadata::canSelectViewInVertexShader() const
471{
472 return mCanSelectViewInVertexShader;
473}
474
Jamie Madille39a3f02015-11-17 20:42:15 -0500475bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
476{
Jamie Madillc9bde922016-07-24 17:58:50 -0400477 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500478 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400479 // Even with a geometry shader, the app can render triangles or lines and reference
480 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
481 // simplicity, we always add this to the vertex shader when the fragment shader
482 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500483 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
484 usesInsertedPointCoordValue();
485}
486
487bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
488{
489 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
490 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
491 // the vertex shader in this case. This saves us 1 output vector.
492 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
493}
494
495bool ProgramD3DMetadata::usesSystemValuePointSize() const
496{
497 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
498}
499
500bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
501{
502 return mFragmentShader->usesMultipleRenderTargets();
503}
504
505GLint ProgramD3DMetadata::getMajorShaderVersion() const
506{
507 return mVertexShader->getData().getShaderVersion();
508}
509
510const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
511{
512 return mFragmentShader;
513}
514
Jamie Madill28afae52015-11-09 15:07:57 -0500515// ProgramD3D Implementation
516
Jamie Madilld3dfda22015-07-06 08:28:49 -0400517ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
518 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500519 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400520 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700521{
Brandon Joneseb994362014-09-24 10:27:28 -0700522}
523
524ProgramD3D::VertexExecutable::~VertexExecutable()
525{
526 SafeDelete(mShaderExecutable);
527}
528
Jamie Madilld3dfda22015-07-06 08:28:49 -0400529// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500530ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
531 GLenum type)
532{
533 switch (type)
534 {
535 case GL_INT:
536 return HLSLAttribType::SIGNED_INT;
537 case GL_UNSIGNED_INT:
538 return HLSLAttribType::UNSIGNED_INT;
539 case GL_SIGNED_NORMALIZED:
540 case GL_UNSIGNED_NORMALIZED:
541 case GL_FLOAT:
542 return HLSLAttribType::FLOAT;
543 default:
544 UNREACHABLE();
545 return HLSLAttribType::FLOAT;
546 }
547}
548
549// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400550void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
551 const gl::InputLayout &inputLayout,
552 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700553{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500554 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400555
556 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700557 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400558 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500559 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
560 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400561
Jamie Madillbdec2f42016-03-02 16:35:32 -0500562 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
563 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
564 continue;
565
Xinghua Cao26143fd2017-11-01 18:19:05 +0800566 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
Jamie Madillbdec2f42016-03-02 16:35:32 -0500567 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700568 }
Brandon Joneseb994362014-09-24 10:27:28 -0700569}
570
Jamie Madilld3dfda22015-07-06 08:28:49 -0400571bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
572{
Jamie Madillbd136f92015-08-10 14:51:37 -0400573 size_t limit = std::max(mSignature.size(), signature.size());
574 for (size_t index = 0; index < limit; ++index)
575 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500576 // treat undefined indexes as FLOAT
577 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
578 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400579 if (a != b)
580 return false;
581 }
582
583 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400584}
585
586ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
587 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400588 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700589{
590}
591
592ProgramD3D::PixelExecutable::~PixelExecutable()
593{
594 SafeDelete(mShaderExecutable);
595}
596
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800597ProgramD3D::Sampler::Sampler()
598 : active(false), logicalTextureUnit(0), textureType(gl::TextureType::_2D)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700599{
600}
601
Xinghua Cao26143fd2017-11-01 18:19:05 +0800602ProgramD3D::Image::Image() : active(false), logicalImageUnit(0)
603{
604}
605
Geoff Lang7dd2e102014-11-10 15:19:26 -0500606unsigned int ProgramD3D::mCurrentSerial = 1;
607
Jamie Madill48ef11b2016-04-27 15:21:52 -0400608ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
609 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700610 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800611 mDynamicHLSL(nullptr),
612 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
613 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700614 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400615 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800616 mVertexUniformStorage(nullptr),
617 mFragmentUniformStorage(nullptr),
618 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700619 mUsedVertexSamplerRange(0),
620 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800621 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700622 mDirtySamplerMapping(true),
Xinghua Cao26143fd2017-11-01 18:19:05 +0800623 mUsedComputeImageRange(0),
624 mUsedComputeReadonlyImageRange(0),
Jamie Madill561ed3a2017-08-31 16:48:09 -0400625 mSerial(issueSerial()),
Jamie Madill4148fd72017-09-14 15:46:20 -0400626 mVertexUniformsDirty(true),
627 mFragmentUniformsDirty(true),
628 mComputeUniformsDirty(true)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700629{
Brandon Joneseb994362014-09-24 10:27:28 -0700630 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700631}
632
633ProgramD3D::~ProgramD3D()
634{
635 reset();
636 SafeDelete(mDynamicHLSL);
637}
638
Brandon Jones44151a92014-09-10 11:32:25 -0700639bool ProgramD3D::usesPointSpriteEmulation() const
640{
641 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
642}
643
Martin Radev41ac68e2017-06-06 12:16:58 +0300644bool ProgramD3D::usesGeometryShaderForPointSpriteEmulation() const
645{
646 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
647}
648
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400649bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700650{
Martin Radevc1d4e552017-08-21 12:01:10 +0300651 if (mHasANGLEMultiviewEnabled && !mRenderer->canSelectViewInVertexShader())
Martin Radev41ac68e2017-06-06 12:16:58 +0300652 {
653 return true;
654 }
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400655 if (drawMode != GL_POINTS)
656 {
657 return mUsesFlatInterpolation;
658 }
Martin Radev41ac68e2017-06-06 12:16:58 +0300659 return usesGeometryShaderForPointSpriteEmulation();
Cooper Partine6664f02015-01-09 16:22:24 -0800660}
661
662bool ProgramD3D::usesInstancedPointSpriteEmulation() const
663{
664 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700665}
666
Xinghua Cao37584b32017-12-01 11:04:03 +0800667GLint ProgramD3D::getSamplerMapping(gl::ShaderType type,
Jamie Madill334d6152015-10-22 14:00:28 -0400668 unsigned int samplerIndex,
669 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700670{
671 GLint logicalTextureUnit = -1;
672
673 switch (type)
674 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800675 case gl::ShaderType::Fragment:
Jamie Madill334d6152015-10-22 14:00:28 -0400676 ASSERT(samplerIndex < caps.maxTextureImageUnits);
677 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
678 {
679 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
680 }
681 break;
Jiawei Shao385b3e02018-03-21 09:43:28 +0800682 case gl::ShaderType::Vertex:
Jamie Madill334d6152015-10-22 14:00:28 -0400683 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
684 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
685 {
686 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
687 }
688 break;
Jiawei Shao385b3e02018-03-21 09:43:28 +0800689 case gl::ShaderType::Compute:
Xinghua Caob1239382016-12-13 15:07:05 +0800690 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
691 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
692 {
693 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
694 }
695 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400696 default:
697 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700698 }
699
Jamie Madill334d6152015-10-22 14:00:28 -0400700 if (logicalTextureUnit >= 0 &&
701 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700702 {
703 return logicalTextureUnit;
704 }
705
706 return -1;
707}
708
709// Returns the texture type for a given Direct3D 9 sampler type and
710// index (0-15 for the pixel shader and 0-3 for the vertex shader).
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800711gl::TextureType ProgramD3D::getSamplerTextureType(gl::ShaderType type,
712 unsigned int samplerIndex) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700713{
714 switch (type)
715 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800716 case gl::ShaderType::Fragment:
Jamie Madill334d6152015-10-22 14:00:28 -0400717 ASSERT(samplerIndex < mSamplersPS.size());
718 ASSERT(mSamplersPS[samplerIndex].active);
719 return mSamplersPS[samplerIndex].textureType;
Jiawei Shao385b3e02018-03-21 09:43:28 +0800720 case gl::ShaderType::Vertex:
Jamie Madill334d6152015-10-22 14:00:28 -0400721 ASSERT(samplerIndex < mSamplersVS.size());
722 ASSERT(mSamplersVS[samplerIndex].active);
723 return mSamplersVS[samplerIndex].textureType;
Jiawei Shao385b3e02018-03-21 09:43:28 +0800724 case gl::ShaderType::Compute:
Xinghua Caob1239382016-12-13 15:07:05 +0800725 ASSERT(samplerIndex < mSamplersCS.size());
726 ASSERT(mSamplersCS[samplerIndex].active);
727 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400728 default:
729 UNREACHABLE();
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800730 return gl::TextureType::InvalidEnum;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700731 }
732
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700733}
734
Xinghua Cao37584b32017-12-01 11:04:03 +0800735GLuint ProgramD3D::getUsedSamplerRange(gl::ShaderType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700736{
737 switch (type)
738 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800739 case gl::ShaderType::Fragment:
Jamie Madill334d6152015-10-22 14:00:28 -0400740 return mUsedPixelSamplerRange;
Jiawei Shao385b3e02018-03-21 09:43:28 +0800741 case gl::ShaderType::Vertex:
Jamie Madill334d6152015-10-22 14:00:28 -0400742 return mUsedVertexSamplerRange;
Jiawei Shao385b3e02018-03-21 09:43:28 +0800743 case gl::ShaderType::Compute:
Xinghua Caob1239382016-12-13 15:07:05 +0800744 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400745 default:
746 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200747 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700748 }
749}
750
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400751ProgramD3D::SamplerMapping ProgramD3D::updateSamplerMapping()
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700752{
753 if (!mDirtySamplerMapping)
754 {
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400755 return SamplerMapping::WasClean;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700756 }
757
758 mDirtySamplerMapping = false;
759
760 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400761 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700762 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400763 if (!d3dUniform->isSampler())
764 continue;
765
Olli Etuaho465835d2017-09-26 13:34:10 +0300766 int count = d3dUniform->getArraySizeProduct();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400767
768 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700769 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400770 unsigned int firstIndex = d3dUniform->psRegisterIndex;
771
772 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700773 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400774 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700775
Jamie Madill62d31cb2015-09-11 13:25:51 -0400776 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700777 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400778 ASSERT(mSamplersPS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400779 mSamplersPS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700780 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400781 }
782 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700783
Jamie Madill62d31cb2015-09-11 13:25:51 -0400784 if (d3dUniform->isReferencedByVertexShader())
785 {
786 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
787
788 for (int i = 0; i < count; i++)
789 {
790 unsigned int samplerIndex = firstIndex + i;
791
792 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700793 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400794 ASSERT(mSamplersVS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400795 mSamplersVS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700796 }
797 }
798 }
Xinghua Caob1239382016-12-13 15:07:05 +0800799
800 if (d3dUniform->isReferencedByComputeShader())
801 {
802 unsigned int firstIndex = d3dUniform->csRegisterIndex;
803
804 for (int i = 0; i < count; i++)
805 {
806 unsigned int samplerIndex = firstIndex + i;
807
808 if (samplerIndex < mSamplersCS.size())
809 {
810 ASSERT(mSamplersCS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400811 mSamplersCS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Xinghua Caob1239382016-12-13 15:07:05 +0800812 }
813 }
814 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700815 }
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400816
817 return SamplerMapping::WasDirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700818}
819
Xinghua Cao26143fd2017-11-01 18:19:05 +0800820GLint ProgramD3D::getImageMapping(gl::ShaderType type,
821 unsigned int imageIndex,
822 bool readonly,
823 const gl::Caps &caps) const
824{
825 GLint logicalImageUnit = -1;
826 ASSERT(imageIndex < caps.maxImageUnits);
827 switch (type)
828 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800829 case gl::ShaderType::Compute:
Xinghua Cao26143fd2017-11-01 18:19:05 +0800830 if (readonly && imageIndex < mReadonlyImagesCS.size() &&
831 mReadonlyImagesCS[imageIndex].active)
832 {
833 logicalImageUnit = mReadonlyImagesCS[imageIndex].logicalImageUnit;
834 }
835 else if (imageIndex < mImagesCS.size() && mImagesCS[imageIndex].active)
836 {
837 logicalImageUnit = mImagesCS[imageIndex].logicalImageUnit;
838 }
839 break;
840 // TODO(xinghua.cao@intel.com): add image mapping for vertex shader and pixel shader.
841 default:
842 UNREACHABLE();
843 }
844
845 if (logicalImageUnit >= 0 && logicalImageUnit < static_cast<GLint>(caps.maxImageUnits))
846 {
847 return logicalImageUnit;
848 }
849
850 return -1;
851}
852
853GLuint ProgramD3D::getUsedImageRange(gl::ShaderType type, bool readonly) const
854{
855 switch (type)
856 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800857 case gl::ShaderType::Compute:
Xinghua Cao26143fd2017-11-01 18:19:05 +0800858 return readonly ? mUsedComputeReadonlyImageRange : mUsedComputeImageRange;
859 // TODO(xinghua.cao@intel.com): add image range of vertex shader and pixel shader.
860 default:
861 UNREACHABLE();
862 return 0u;
863 }
864}
865
Jamie Madill9cf9e872017-06-05 12:59:25 -0400866gl::LinkResult ProgramD3D::load(const gl::Context *context,
867 gl::InfoLog &infoLog,
868 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700869{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500870 // TODO(jmadill): Use Renderer from contextImpl.
871
Jamie Madill62d31cb2015-09-11 13:25:51 -0400872 reset();
873
Jamie Madill334d6152015-10-22 14:00:28 -0400874 DeviceIdentifier binaryDeviceIdentifier = {0};
875 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
876 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700877
878 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
879 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
880 {
881 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500882 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700883 }
884
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500885 int compileFlags = stream->readInt<int>();
886 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
887 {
Jamie Madillf6113162015-05-07 11:49:21 -0400888 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500889 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500890 }
891
Jamie Madill8047c0d2016-03-07 13:02:12 -0500892 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400893 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500894 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400895 }
896
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700897 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
898 for (unsigned int i = 0; i < psSamplerCount; ++i)
899 {
900 Sampler sampler;
901 stream->readBool(&sampler.active);
902 stream->readInt(&sampler.logicalTextureUnit);
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800903 stream->readEnum(&sampler.textureType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700904 mSamplersPS.push_back(sampler);
905 }
906 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
907 for (unsigned int i = 0; i < vsSamplerCount; ++i)
908 {
909 Sampler sampler;
910 stream->readBool(&sampler.active);
911 stream->readInt(&sampler.logicalTextureUnit);
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800912 stream->readEnum(&sampler.textureType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700913 mSamplersVS.push_back(sampler);
914 }
915
Xinghua Caob1239382016-12-13 15:07:05 +0800916 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
917 for (unsigned int i = 0; i < csSamplerCount; ++i)
918 {
919 Sampler sampler;
920 stream->readBool(&sampler.active);
921 stream->readInt(&sampler.logicalTextureUnit);
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800922 stream->readEnum(&sampler.textureType);
Xinghua Caob1239382016-12-13 15:07:05 +0800923 mSamplersCS.push_back(sampler);
924 }
925
Xinghua Cao26143fd2017-11-01 18:19:05 +0800926 const unsigned int csImageCount = stream->readInt<unsigned int>();
927 for (unsigned int i = 0; i < csImageCount; ++i)
928 {
929 Image image;
930 stream->readBool(&image.active);
931 stream->readInt(&image.logicalImageUnit);
932 mImagesCS.push_back(image);
933 }
934
935 const unsigned int csReadonlyImageCount = stream->readInt<unsigned int>();
936 for (unsigned int i = 0; i < csReadonlyImageCount; ++i)
937 {
938 Image image;
939 stream->readBool(&image.active);
940 stream->readInt(&image.logicalImageUnit);
941 mReadonlyImagesCS.push_back(image);
942 }
943
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700944 stream->readInt(&mUsedVertexSamplerRange);
945 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800946 stream->readInt(&mUsedComputeSamplerRange);
Xinghua Cao26143fd2017-11-01 18:19:05 +0800947 stream->readInt(&mUsedComputeImageRange);
948 stream->readInt(&mUsedComputeReadonlyImageRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700949
950 const unsigned int uniformCount = stream->readInt<unsigned int>();
951 if (stream->error())
952 {
Jamie Madillf6113162015-05-07 11:49:21 -0400953 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500954 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700955 }
956
Jamie Madill48ef11b2016-04-27 15:21:52 -0400957 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400958 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700959 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
960 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400961 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700962
Jamie Madill62d31cb2015-09-11 13:25:51 -0400963 D3DUniform *d3dUniform =
Xinghua Cao26143fd2017-11-01 18:19:05 +0800964 new D3DUniform(linkedUniform.type, HLSLRegisterType::None, linkedUniform.name,
965 linkedUniform.arraySizes, linkedUniform.isInDefaultBlock());
966 stream->readInt<HLSLRegisterType>(&d3dUniform->regType);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400967 stream->readInt(&d3dUniform->psRegisterIndex);
968 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800969 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400970 stream->readInt(&d3dUniform->registerCount);
971 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700972
Jamie Madill62d31cb2015-09-11 13:25:51 -0400973 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700974 }
975
Jamie Madill4a3c2342015-10-08 12:58:45 -0400976 const unsigned int blockCount = stream->readInt<unsigned int>();
977 if (stream->error())
978 {
979 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500980 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400981 }
982
983 ASSERT(mD3DUniformBlocks.empty());
984 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
985 {
986 D3DUniformBlock uniformBlock;
987 stream->readInt(&uniformBlock.psRegisterIndex);
988 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800989 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400990 mD3DUniformBlocks.push_back(uniformBlock);
991 }
992
Jamie Madill9fc36822015-11-18 13:08:07 -0500993 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
994 mStreamOutVaryings.resize(streamOutVaryingCount);
995 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700996 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500997 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700998
Jamie Madill28afae52015-11-09 15:07:57 -0500999 stream->readString(&varying->semanticName);
1000 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001001 stream->readInt(&varying->componentCount);
1002 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001003 }
1004
Brandon Jones22502d52014-08-29 16:58:36 -07001005 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001006 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001007 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001008 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001009 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001010 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001011 stream->readBool(&mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +03001012 stream->readBool(&mHasANGLEMultiviewEnabled);
1013 stream->readBool(&mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -07001014 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001015 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001016
1017 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
1018 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -04001019 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
1020 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001021 {
1022 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
1023 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
1024 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
1025 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
1026 }
1027
Jamie Madill4e31ad52015-10-29 10:32:57 -04001028 stream->readString(&mGeometryShaderPreamble);
1029
Jamie Madill334d6152015-10-22 14:00:28 -04001030 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -07001031
Jamie Madillb0a838b2016-11-13 20:02:12 -05001032 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
1033
Brandon Joneseb994362014-09-24 10:27:28 -07001034 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -04001035 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
1036 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001037 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001038 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04001039 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -07001040
Jamie Madilld3dfda22015-07-06 08:28:49 -04001041 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001042 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -04001043 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -07001044 }
1045
Jamie Madill334d6152015-10-22 14:00:28 -04001046 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -07001047 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -04001048
Jamie Madillada9ecc2015-08-17 12:53:37 -04001049 ShaderExecutableD3D *shaderExecutable = nullptr;
1050
Yunchao He85072e82017-11-14 15:43:28 +08001051 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
Jiawei Shao385b3e02018-03-21 09:43:28 +08001052 gl::ShaderType::Vertex, mStreamOutVaryings,
1053 separateAttribs, &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001054
Brandon Joneseb994362014-09-24 10:27:28 -07001055 if (!shaderExecutable)
1056 {
Jamie Madillf6113162015-05-07 11:49:21 -04001057 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001058 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001059 }
1060
1061 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -04001062 VertexExecutable::Signature signature;
1063 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -07001064
1065 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +08001066 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
1067 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001068
1069 stream->skip(vertexShaderSize);
1070 }
1071
1072 const size_t pixelShaderCount = stream->readInt<unsigned int>();
1073 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
1074 {
1075 const size_t outputCount = stream->readInt<unsigned int>();
1076 std::vector<GLenum> outputs(outputCount);
1077 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
1078 {
1079 stream->readInt(&outputs[outputIndex]);
1080 }
1081
Jamie Madill334d6152015-10-22 14:00:28 -04001082 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -07001083 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -04001084 ShaderExecutableD3D *shaderExecutable = nullptr;
1085
Yunchao He85072e82017-11-14 15:43:28 +08001086 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize,
Jiawei Shao385b3e02018-03-21 09:43:28 +08001087 gl::ShaderType::Fragment, mStreamOutVaryings,
Yunchao He85072e82017-11-14 15:43:28 +08001088 separateAttribs, &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001089
1090 if (!shaderExecutable)
1091 {
Jamie Madillf6113162015-05-07 11:49:21 -04001092 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001093 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001094 }
1095
1096 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +08001097 mPixelExecutables.push_back(
1098 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001099
1100 stream->skip(pixelShaderSize);
1101 }
1102
Jamie Madill4e31ad52015-10-29 10:32:57 -04001103 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
1104 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -07001105 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001106 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
1107 if (geometryShaderSize == 0)
1108 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001109 continue;
1110 }
1111
Brandon Joneseb994362014-09-24 10:27:28 -07001112 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001113
Xinghua Caob1239382016-12-13 15:07:05 +08001114 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -05001115 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
Jiawei Shao385b3e02018-03-21 09:43:28 +08001116 gl::ShaderType::Geometry, mStreamOutVaryings,
Yunchao He85072e82017-11-14 15:43:28 +08001117 separateAttribs, &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001118
Xinghua Caob1239382016-12-13 15:07:05 +08001119 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001120 {
Jamie Madillf6113162015-05-07 11:49:21 -04001121 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001122 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001123 }
Xinghua Caob1239382016-12-13 15:07:05 +08001124
1125 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
1126
Brandon Joneseb994362014-09-24 10:27:28 -07001127 stream->skip(geometryShaderSize);
1128 }
1129
Xinghua Caob1239382016-12-13 15:07:05 +08001130 unsigned int computeShaderSize = stream->readInt<unsigned int>();
1131 if (computeShaderSize > 0)
1132 {
1133 const unsigned char *computeShaderFunction = binary + stream->offset();
1134
1135 ShaderExecutableD3D *computeExecutable = nullptr;
1136 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
Jiawei Shao385b3e02018-03-21 09:43:28 +08001137 gl::ShaderType::Compute, std::vector<D3DVarying>(),
1138 false, &computeExecutable));
Xinghua Caob1239382016-12-13 15:07:05 +08001139
1140 if (!computeExecutable)
1141 {
1142 infoLog << "Could not create compute shader.";
1143 return false;
1144 }
1145
1146 mComputeExecutable.reset(computeExecutable);
1147 }
1148
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001149 initializeUniformStorage();
1150
Jamie Madillb0a838b2016-11-13 20:02:12 -05001151 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001152}
1153
Jamie Madill27a60632017-06-30 15:12:01 -04001154void ProgramD3D::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -07001155{
Austin Kinross137b1512015-06-17 16:14:53 -07001156 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -04001157 // When we load the binary again later, we can validate the device identifier before trying to
1158 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -07001159 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -04001160 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
1161 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -07001162
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001163 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1164
Jamie Madill8047c0d2016-03-07 13:02:12 -05001165 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -04001166 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05001167 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -04001168 }
1169
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001170 stream->writeInt(mSamplersPS.size());
1171 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1172 {
1173 stream->writeInt(mSamplersPS[i].active);
1174 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
Corentin Wallezf0e89be2017-11-08 14:00:32 -08001175 stream->writeEnum(mSamplersPS[i].textureType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001176 }
1177
1178 stream->writeInt(mSamplersVS.size());
1179 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1180 {
1181 stream->writeInt(mSamplersVS[i].active);
1182 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
Corentin Wallezf0e89be2017-11-08 14:00:32 -08001183 stream->writeEnum(mSamplersVS[i].textureType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001184 }
1185
Xinghua Caob1239382016-12-13 15:07:05 +08001186 stream->writeInt(mSamplersCS.size());
1187 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
1188 {
1189 stream->writeInt(mSamplersCS[i].active);
1190 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
Corentin Wallezf0e89be2017-11-08 14:00:32 -08001191 stream->writeEnum(mSamplersCS[i].textureType);
Xinghua Caob1239382016-12-13 15:07:05 +08001192 }
1193
Xinghua Cao26143fd2017-11-01 18:19:05 +08001194 stream->writeInt(mImagesCS.size());
1195 for (unsigned int i = 0; i < mImagesCS.size(); ++i)
1196 {
1197 stream->writeInt(mImagesCS[i].active);
1198 stream->writeInt(mImagesCS[i].logicalImageUnit);
1199 }
1200
1201 stream->writeInt(mReadonlyImagesCS.size());
1202 for (unsigned int i = 0; i < mReadonlyImagesCS.size(); ++i)
1203 {
1204 stream->writeInt(mReadonlyImagesCS[i].active);
1205 stream->writeInt(mReadonlyImagesCS[i].logicalImageUnit);
1206 }
1207
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001208 stream->writeInt(mUsedVertexSamplerRange);
1209 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001210 stream->writeInt(mUsedComputeSamplerRange);
Xinghua Cao26143fd2017-11-01 18:19:05 +08001211 stream->writeInt(mUsedComputeImageRange);
1212 stream->writeInt(mUsedComputeReadonlyImageRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001213
Jamie Madill62d31cb2015-09-11 13:25:51 -04001214 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001215 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001216 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001217 // Type, name and arraySize are redundant, so aren't stored in the binary.
Xinghua Cao26143fd2017-11-01 18:19:05 +08001218 stream->writeInt(static_cast<unsigned int>(uniform->regType));
Jamie Madille2e406c2016-06-02 13:04:10 -04001219 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1220 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001221 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001222 stream->writeInt(uniform->registerCount);
1223 stream->writeInt(uniform->registerElement);
1224 }
1225
1226 stream->writeInt(mD3DUniformBlocks.size());
1227 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1228 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001229 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1230 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001231 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001232 }
1233
Jamie Madill9fc36822015-11-18 13:08:07 -05001234 stream->writeInt(mStreamOutVaryings.size());
1235 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001236 {
Brandon Joneseb994362014-09-24 10:27:28 -07001237 stream->writeString(varying.semanticName);
1238 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001239 stream->writeInt(varying.componentCount);
1240 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001241 }
1242
Brandon Jones22502d52014-08-29 16:58:36 -07001243 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001244 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001245 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001246 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001247 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001248 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001249 stream->writeInt(mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +03001250 stream->writeInt(mHasANGLEMultiviewEnabled);
1251 stream->writeInt(mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -07001252 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001253 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001254
Brandon Joneseb994362014-09-24 10:27:28 -07001255 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001256 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001257 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1258 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001259 {
Brandon Joneseb994362014-09-24 10:27:28 -07001260 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001261 stream->writeInt(variable.type);
1262 stream->writeString(variable.name);
1263 stream->writeString(variable.source);
1264 stream->writeInt(variable.outputIndex);
1265 }
1266
Jamie Madill4e31ad52015-10-29 10:32:57 -04001267 stream->writeString(mGeometryShaderPreamble);
1268
Brandon Joneseb994362014-09-24 10:27:28 -07001269 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001270 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1271 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001272 {
Xinghua Caob1239382016-12-13 15:07:05 +08001273 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001274
Jamie Madilld3dfda22015-07-06 08:28:49 -04001275 const auto &inputLayout = vertexExecutable->inputs();
1276 stream->writeInt(inputLayout.size());
1277
1278 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001279 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001280 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001281 }
1282
1283 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1284 stream->writeInt(vertexShaderSize);
1285
1286 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1287 stream->writeBytes(vertexBlob, vertexShaderSize);
1288 }
1289
1290 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001291 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1292 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001293 {
Xinghua Caob1239382016-12-13 15:07:05 +08001294 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001295
1296 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1297 stream->writeInt(outputs.size());
1298 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1299 {
1300 stream->writeInt(outputs[outputIndex]);
1301 }
1302
1303 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1304 stream->writeInt(pixelShaderSize);
1305
1306 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1307 stream->writeBytes(pixelBlob, pixelShaderSize);
1308 }
1309
Xinghua Caob1239382016-12-13 15:07:05 +08001310 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001311 {
Xinghua Caob1239382016-12-13 15:07:05 +08001312 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001313 {
1314 stream->writeInt(0);
1315 continue;
1316 }
1317
Xinghua Caob1239382016-12-13 15:07:05 +08001318 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001319 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001320 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1321 }
1322
1323 if (mComputeExecutable)
1324 {
1325 size_t computeShaderSize = mComputeExecutable->getLength();
1326 stream->writeInt(computeShaderSize);
1327 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1328 }
1329 else
1330 {
1331 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001332 }
Brandon Jones22502d52014-08-29 16:58:36 -07001333}
1334
Geoff Langc5629752015-12-07 16:29:04 -05001335void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1336{
1337}
1338
Yunchao He61afff12017-03-14 15:34:03 +08001339void ProgramD3D::setSeparable(bool /* separable */)
1340{
1341}
1342
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001343gl::Error ProgramD3D::getPixelExecutableForCachedOutputLayout(ShaderExecutableD3D **outExecutable,
1344 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001345{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001346 if (mCachedPixelExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001347 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001348 *outExecutable = mPixelExecutables[mCachedPixelExecutableIndex.value()]->shaderExecutable();
1349 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001350 }
1351
Jamie Madill334d6152015-10-22 14:00:28 -04001352 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001353 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, mPixelShaderOutputLayoutCache);
Brandon Jones22502d52014-08-29 16:58:36 -07001354
1355 // Generate new pixel executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001356 ShaderExecutableD3D *pixelExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001357
1358 gl::InfoLog tempInfoLog;
1359 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1360
Jamie Madill01074252016-11-28 15:55:51 -05001361 ANGLE_TRY(mRenderer->compileToExecutable(
Jiawei Shao385b3e02018-03-21 09:43:28 +08001362 *currentInfoLog, finalPixelHLSL, gl::ShaderType::Fragment, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001363 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001364 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001365
Jamie Madill97399232014-12-23 12:31:15 -05001366 if (pixelExecutable)
1367 {
Xinghua Caob1239382016-12-13 15:07:05 +08001368 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001369 new PixelExecutable(mPixelShaderOutputLayoutCache, pixelExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001370 mCachedPixelExecutableIndex = mPixelExecutables.size() - 1;
Jamie Madill97399232014-12-23 12:31:15 -05001371 }
1372 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001373 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001374 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1375 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001376 }
Brandon Jones22502d52014-08-29 16:58:36 -07001377
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001378 *outExecutable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001379 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001380}
1381
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001382gl::Error ProgramD3D::getVertexExecutableForCachedInputLayout(ShaderExecutableD3D **outExectuable,
1383 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001384{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001385 if (mCachedVertexExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001386 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001387 *outExectuable =
1388 mVertexExecutables[mCachedVertexExecutableIndex.value()]->shaderExecutable();
1389 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001390 }
1391
Brandon Jones22502d52014-08-29 16:58:36 -07001392 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001393 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001394 mVertexHLSL, mCachedInputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001395
1396 // Generate new vertex executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001397 ShaderExecutableD3D *vertexExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001398
1399 gl::InfoLog tempInfoLog;
1400 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1401
Jamie Madill01074252016-11-28 15:55:51 -05001402 ANGLE_TRY(mRenderer->compileToExecutable(
Jiawei Shao385b3e02018-03-21 09:43:28 +08001403 *currentInfoLog, finalVertexHLSL, gl::ShaderType::Vertex, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001404 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001405 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001406
Jamie Madill97399232014-12-23 12:31:15 -05001407 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001408 {
Xinghua Caob1239382016-12-13 15:07:05 +08001409 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001410 new VertexExecutable(mCachedInputLayout, mCachedVertexSignature, vertexExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001411 mCachedVertexExecutableIndex = mVertexExecutables.size() - 1;
Brandon Joneseb994362014-09-24 10:27:28 -07001412 }
Jamie Madill97399232014-12-23 12:31:15 -05001413 else if (!infoLog)
1414 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001415 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1416 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001417 }
Brandon Jones22502d52014-08-29 16:58:36 -07001418
Geoff Langb543aff2014-09-30 14:52:54 -04001419 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001420 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001421}
1422
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001423gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Context *context,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001424 GLenum drawMode,
1425 ShaderExecutableD3D **outExecutable,
1426 gl::InfoLog *infoLog)
1427{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001428 if (outExecutable)
1429 {
1430 *outExecutable = nullptr;
1431 }
1432
Austin Kinross88829e82016-01-12 13:04:41 -08001433 // Return a null shader if the current rendering doesn't use a geometry shader
1434 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001435 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001436 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001437 }
1438
Jamie Madill4e31ad52015-10-29 10:32:57 -04001439 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1440
Xinghua Caob1239382016-12-13 15:07:05 +08001441 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001442 {
1443 if (outExecutable)
1444 {
Xinghua Caob1239382016-12-13 15:07:05 +08001445 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001446 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001447 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001448 }
1449
1450 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001451 context, geometryShaderType, mState, mRenderer->presentPathFastEnabled(),
Martin Radevc1d4e552017-08-21 12:01:10 +03001452 mHasANGLEMultiviewEnabled, mRenderer->canSelectViewInVertexShader(),
1453 usesGeometryShaderForPointSpriteEmulation(), mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001454
1455 gl::InfoLog tempInfoLog;
1456 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1457
Xinghua Caob1239382016-12-13 15:07:05 +08001458 ShaderExecutableD3D *geometryExecutable = nullptr;
1459 gl::Error error = mRenderer->compileToExecutable(
Jiawei Shao385b3e02018-03-21 09:43:28 +08001460 *currentInfoLog, geometryHLSL, gl::ShaderType::Geometry, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001461 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001462 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001463
1464 if (!infoLog && error.isError())
1465 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001466 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1467 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001468 }
1469
Xinghua Caob1239382016-12-13 15:07:05 +08001470 if (geometryExecutable != nullptr)
1471 {
1472 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1473 }
1474
Jamie Madill4e31ad52015-10-29 10:32:57 -04001475 if (outExecutable)
1476 {
Xinghua Caob1239382016-12-13 15:07:05 +08001477 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001478 }
1479 return error;
1480}
1481
Jamie Madill01074252016-11-28 15:55:51 -05001482class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001483{
Jamie Madill01074252016-11-28 15:55:51 -05001484 public:
1485 GetExecutableTask(ProgramD3D *program)
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001486 : mProgram(program), mError(gl::NoError()), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001487 {
Brandon Joneseb994362014-09-24 10:27:28 -07001488 }
1489
Jamie Madill01074252016-11-28 15:55:51 -05001490 virtual gl::Error run() = 0;
1491
1492 void operator()() override { mError = run(); }
1493
1494 const gl::Error &getError() const { return mError; }
1495 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1496 ShaderExecutableD3D *getResult() { return mResult; }
1497
1498 protected:
1499 ProgramD3D *mProgram;
1500 gl::Error mError;
1501 gl::InfoLog mInfoLog;
1502 ShaderExecutableD3D *mResult;
1503};
1504
1505class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1506{
1507 public:
Jamie Madillbd044ed2017-06-05 12:59:21 -04001508 GetVertexExecutableTask(ProgramD3D *program, const gl::Context *context)
1509 : GetExecutableTask(program), mContext(context)
1510 {
1511 }
Jamie Madill01074252016-11-28 15:55:51 -05001512 gl::Error run() override
1513 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001514 mProgram->updateCachedInputLayoutFromShader(mContext);
Jamie Madill01074252016-11-28 15:55:51 -05001515
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001516 ANGLE_TRY(mProgram->getVertexExecutableForCachedInputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001517
1518 return gl::NoError();
1519 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001520
1521 private:
1522 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001523};
1524
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001525void ProgramD3D::updateCachedInputLayoutFromShader(const gl::Context *context)
1526{
Jiawei Shao385b3e02018-03-21 09:43:28 +08001527 GetDefaultInputLayoutFromShader(context, mState.getAttachedShader(gl::ShaderType::Vertex),
1528 &mCachedInputLayout);
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001529 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001530 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001531}
1532
Jamie Madill01074252016-11-28 15:55:51 -05001533class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1534{
1535 public:
1536 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1537 gl::Error run() override
1538 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001539 mProgram->updateCachedOutputLayoutFromShader();
Jamie Madill01074252016-11-28 15:55:51 -05001540
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001541 ANGLE_TRY(mProgram->getPixelExecutableForCachedOutputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001542
1543 return gl::NoError();
1544 }
1545};
1546
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001547void ProgramD3D::updateCachedOutputLayoutFromShader()
1548{
1549 GetDefaultOutputLayoutFromShader(mPixelShaderKey, &mPixelShaderOutputLayoutCache);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001550 updateCachedPixelExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001551}
1552
Jamie Madill01074252016-11-28 15:55:51 -05001553class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1554{
1555 public:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001556 GetGeometryExecutableTask(ProgramD3D *program, const gl::Context *context)
1557 : GetExecutableTask(program), mContext(context)
Jamie Madill01074252016-11-28 15:55:51 -05001558 {
1559 }
1560
1561 gl::Error run() override
1562 {
1563 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1564 // D3D11.
1565 if (mProgram->usesGeometryShader(GL_POINTS))
1566 {
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001567 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContext, GL_POINTS, &mResult,
1568 &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001569 }
1570
1571 return gl::NoError();
1572 }
1573
1574 private:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001575 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001576};
1577
Xinghua Cao73badc02017-03-29 19:14:53 +08001578gl::Error ProgramD3D::getComputeExecutable(ShaderExecutableD3D **outExecutable)
1579{
1580 if (outExecutable)
1581 {
1582 *outExecutable = mComputeExecutable.get();
1583 }
1584
1585 return gl::NoError();
1586}
1587
Jamie Madill9cf9e872017-06-05 12:59:25 -04001588gl::LinkResult ProgramD3D::compileProgramExecutables(const gl::Context *context,
1589 gl::InfoLog &infoLog)
Jamie Madill01074252016-11-28 15:55:51 -05001590{
1591 // Ensure the compiler is initialized to avoid race conditions.
1592 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1593
1594 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1595
Jamie Madillbd044ed2017-06-05 12:59:21 -04001596 GetVertexExecutableTask vertexTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001597 GetPixelExecutableTask pixelTask(this);
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001598 GetGeometryExecutableTask geometryTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001599
1600 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1601 workerPool->postWorkerTask(&pixelTask),
1602 workerPool->postWorkerTask(&geometryTask)}};
1603
1604 WaitableEvent::WaitMany(&waitEvents);
1605
Jiawei Shao02f15232017-12-27 10:10:28 +08001606 if (!vertexTask.getInfoLog().empty())
1607 {
1608 infoLog << vertexTask.getInfoLog().str();
1609 }
1610 if (!pixelTask.getInfoLog().empty())
1611 {
1612 infoLog << pixelTask.getInfoLog().str();
1613 }
1614 if (!geometryTask.getInfoLog().empty())
1615 {
1616 infoLog << geometryTask.getInfoLog().str();
1617 }
Jamie Madill01074252016-11-28 15:55:51 -05001618
1619 ANGLE_TRY(vertexTask.getError());
1620 ANGLE_TRY(pixelTask.getError());
1621 ANGLE_TRY(geometryTask.getError());
1622
1623 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1624 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1625 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1626
Jiawei Shao385b3e02018-03-21 09:43:28 +08001627 const ShaderD3D *vertexShaderD3D =
1628 GetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Vertex));
Jamie Madill847638a2015-11-20 13:01:41 -05001629
1630 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001631 {
Jamie Madill334d6152015-10-22 14:00:28 -04001632 // Geometry shaders are currently only used internally, so there is no corresponding shader
1633 // object at the interface level. For now the geometry shader debug info is prepended to
1634 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001635 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001636 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001637 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1638 }
1639
1640 if (defaultVertexExecutable)
1641 {
1642 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1643 }
1644
1645 if (defaultPixelExecutable)
1646 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001647 const ShaderD3D *fragmentShaderD3D =
Jiawei Shao385b3e02018-03-21 09:43:28 +08001648 GetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Fragment));
Tibor den Ouden97049c62014-10-06 21:39:16 +02001649 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1650 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001651
Jamie Madillb0a838b2016-11-13 20:02:12 -05001652 return (defaultVertexExecutable && defaultPixelExecutable &&
1653 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001654}
1655
Jamie Madill9cf9e872017-06-05 12:59:25 -04001656gl::LinkResult ProgramD3D::compileComputeExecutable(const gl::Context *context,
1657 gl::InfoLog &infoLog)
Xinghua Caob1239382016-12-13 15:07:05 +08001658{
1659 // Ensure the compiler is initialized to avoid race conditions.
1660 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1661
Jamie Madillbd044ed2017-06-05 12:59:21 -04001662 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(context, mState);
Xinghua Caob1239382016-12-13 15:07:05 +08001663
1664 ShaderExecutableD3D *computeExecutable = nullptr;
Jiawei Shao385b3e02018-03-21 09:43:28 +08001665 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, gl::ShaderType::Compute,
Xinghua Caob1239382016-12-13 15:07:05 +08001666 std::vector<D3DVarying>(), false,
1667 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1668
1669 if (computeExecutable == nullptr)
1670 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001671 ERR() << "Error compiling dynamic compute executable:" << std::endl
1672 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001673 }
1674 else
1675 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001676 const ShaderD3D *computeShaderD3D =
1677 GetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Compute));
Xinghua Caob1239382016-12-13 15:07:05 +08001678 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1679 mComputeExecutable.reset(computeExecutable);
1680 }
1681
1682 return mComputeExecutable.get() != nullptr;
1683}
1684
Jamie Madill9cf9e872017-06-05 12:59:25 -04001685gl::LinkResult ProgramD3D::link(const gl::Context *context,
Jamie Madillc9727f32017-11-07 12:37:07 -05001686 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -04001687 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001688{
Jamie Madillc564c072017-06-01 12:45:42 -04001689 const auto &data = context->getContextState();
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001690
Jamie Madill62d31cb2015-09-11 13:25:51 -04001691 reset();
1692
Jiawei Shao385b3e02018-03-21 09:43:28 +08001693 gl::Shader *computeShader = mState.getAttachedShader(gl::ShaderType::Compute);
Xinghua Caob1239382016-12-13 15:07:05 +08001694 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001695 {
Xinghua Caob1239382016-12-13 15:07:05 +08001696 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
Xinghua Cao26143fd2017-11-01 18:19:05 +08001697 mImagesCS.resize(data.getCaps().maxImageUnits);
1698 mReadonlyImagesCS.resize(data.getCaps().maxImageUnits);
Xinghua Caob1239382016-12-13 15:07:05 +08001699
Jamie Madillbd044ed2017-06-05 12:59:21 -04001700 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001701
Jamie Madill9cf9e872017-06-05 12:59:25 -04001702 gl::LinkResult result = compileComputeExecutable(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001703 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001704 {
Xinghua Caob1239382016-12-13 15:07:05 +08001705 infoLog << result.getError().getMessage();
1706 return result;
1707 }
1708 else if (!result.getResult())
1709 {
1710 infoLog << "Failed to create D3D compute shader.";
1711 return result;
1712 }
Xinghua Caob1239382016-12-13 15:07:05 +08001713 }
1714 else
1715 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001716 gl::Shader *vertexShader = mState.getAttachedShader(gl::ShaderType::Vertex);
1717 gl::Shader *fragmentShader = mState.getAttachedShader(gl::ShaderType::Fragment);
Xinghua Caob1239382016-12-13 15:07:05 +08001718
1719 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1720 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1721
1722 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1723 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1724
1725 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1726 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1727
1728 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1729 {
1730 if (fragmentShaderD3D->usesFrontFacing())
1731 {
1732 infoLog << "The current renderer doesn't support gl_FrontFacing";
1733 return false;
1734 }
1735 }
1736
Xinghua Caob1239382016-12-13 15:07:05 +08001737 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madillc9727f32017-11-07 12:37:07 -05001738 BuiltinVaryingsD3D builtins(metadata, resources.varyingPacking);
Xinghua Caob1239382016-12-13 15:07:05 +08001739
Jamie Madillc9727f32017-11-07 12:37:07 -05001740 mDynamicHLSL->generateShaderLinkHLSL(context, mState, metadata, resources.varyingPacking,
1741 builtins, &mPixelHLSL, &mVertexHLSL);
Xinghua Caob1239382016-12-13 15:07:05 +08001742
1743 mUsesPointSize = vertexShaderD3D->usesPointSize();
1744 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
Xinghua Cao26143fd2017-11-01 18:19:05 +08001745 mUsesFragDepth = metadata.usesFragDepth();
Martin Radev41ac68e2017-06-06 12:16:58 +03001746 mUsesViewID = metadata.usesViewID();
1747 mHasANGLEMultiviewEnabled = metadata.hasANGLEMultiviewEnabled();
Xinghua Caob1239382016-12-13 15:07:05 +08001748
1749 // Cache if we use flat shading
Jamie Madillbd044ed2017-06-05 12:59:21 -04001750 mUsesFlatInterpolation =
Jiawei Shao3d404882017-10-16 13:30:48 +08001751 (FindFlatInterpolationVarying(fragmentShader->getInputVaryings(context)) ||
1752 FindFlatInterpolationVarying(vertexShader->getOutputVaryings(context)));
Xinghua Caob1239382016-12-13 15:07:05 +08001753
1754 if (mRenderer->getMajorShaderModel() >= 4)
1755 {
Martin Radev41ac68e2017-06-06 12:16:58 +03001756 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(
Jamie Madillc9727f32017-11-07 12:37:07 -05001757 resources.varyingPacking, builtins, mHasANGLEMultiviewEnabled,
Martin Radevc1d4e552017-08-21 12:01:10 +03001758 metadata.canSelectViewInVertexShader());
Xinghua Caob1239382016-12-13 15:07:05 +08001759 }
1760
Jamie Madillbd044ed2017-06-05 12:59:21 -04001761 initAttribLocationsToD3DSemantic(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001762
Jamie Madillbd044ed2017-06-05 12:59:21 -04001763 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001764
Jiawei Shao385b3e02018-03-21 09:43:28 +08001765 gatherTransformFeedbackVaryings(resources.varyingPacking, builtins[gl::ShaderType::Vertex]);
Xinghua Caob1239382016-12-13 15:07:05 +08001766
Jamie Madill9cf9e872017-06-05 12:59:25 -04001767 gl::LinkResult result = compileProgramExecutables(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001768 if (result.isError())
1769 {
1770 infoLog << result.getError().getMessage();
1771 return result;
1772 }
1773 else if (!result.getResult())
1774 {
1775 infoLog << "Failed to create D3D shaders.";
1776 return result;
1777 }
Austin Kinross02df7962015-07-01 10:03:42 -07001778 }
1779
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001780 linkResources(context, resources);
1781
Jamie Madillb0a838b2016-11-13 20:02:12 -05001782 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001783}
1784
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001785GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001786{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001787 // TODO(jmadill): Do something useful here?
1788 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001789}
1790
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001791void ProgramD3D::initializeUniformBlocks()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001792{
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001793 if (mState.getUniformBlocks().empty())
Jamie Madill51f522f2016-12-21 15:10:55 -05001794 {
1795 return;
1796 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001797
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001798 ASSERT(mD3DUniformBlocks.empty());
1799
Jamie Madill62d31cb2015-09-11 13:25:51 -04001800 // Assign registers and update sizes.
Jiawei Shao385b3e02018-03-21 09:43:28 +08001801 const ShaderD3D *vertexShaderD3D =
1802 SafeGetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Vertex));
Xinghua Caob1239382016-12-13 15:07:05 +08001803 const ShaderD3D *fragmentShaderD3D =
Jiawei Shao385b3e02018-03-21 09:43:28 +08001804 SafeGetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Fragment));
1805 const ShaderD3D *computeShaderD3D =
1806 SafeGetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Compute));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001807
Jiajia Qin729b2c62017-08-14 09:36:11 +08001808 for (const gl::InterfaceBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001809 {
1810 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1811
Jamie Madill4a3c2342015-10-08 12:58:45 -04001812 D3DUniformBlock d3dUniformBlock;
1813
Olli Etuaho107c7242018-03-20 15:45:35 +02001814 if (uniformBlock.vertexActive)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001815 {
Xinghua Caob1239382016-12-13 15:07:05 +08001816 ASSERT(vertexShaderD3D != nullptr);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001817 unsigned int baseRegister = vertexShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001818 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001819 }
1820
Olli Etuaho107c7242018-03-20 15:45:35 +02001821 if (uniformBlock.fragmentActive)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001822 {
Xinghua Caob1239382016-12-13 15:07:05 +08001823 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001824 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001825 fragmentShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001826 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001827 }
1828
Olli Etuaho107c7242018-03-20 15:45:35 +02001829 if (uniformBlock.computeActive)
Xinghua Caob1239382016-12-13 15:07:05 +08001830 {
1831 ASSERT(computeShaderD3D != nullptr);
1832 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001833 computeShaderD3D->getUniformBlockRegister(uniformBlock.name);
Xinghua Caob1239382016-12-13 15:07:05 +08001834 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1835 }
1836
Jamie Madill4a3c2342015-10-08 12:58:45 -04001837 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001838 }
1839}
1840
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001841void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001842{
1843 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001844 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001845 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001846 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001847 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001848 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001849 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001850 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001851 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001852 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001853 vertexRegisters = std::max(vertexRegisters,
1854 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001855 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001856 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001857 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001858 fragmentRegisters = std::max(
1859 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001860 }
Xinghua Caob1239382016-12-13 15:07:05 +08001861 if (d3dUniform->isReferencedByComputeShader())
1862 {
1863 computeRegisters = std::max(
1864 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1865 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001866 }
1867 }
1868
Xinghua Caob1239382016-12-13 15:07:05 +08001869 mVertexUniformStorage =
1870 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1871 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1872 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1873 mComputeUniformStorage =
1874 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001875
1876 // Iterate the uniforms again to assign data pointers to default block uniforms.
1877 for (D3DUniform *d3dUniform : mD3DUniforms)
1878 {
1879 if (d3dUniform->isSampler())
1880 {
Olli Etuaho465835d2017-09-26 13:34:10 +03001881 d3dUniform->mSamplerData.resize(d3dUniform->getArraySizeProduct(), 0);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001882 continue;
1883 }
1884
1885 if (d3dUniform->isReferencedByVertexShader())
1886 {
1887 d3dUniform->vsData = mVertexUniformStorage->getDataPointer(d3dUniform->vsRegisterIndex,
1888 d3dUniform->registerElement);
1889 }
1890
1891 if (d3dUniform->isReferencedByFragmentShader())
1892 {
1893 d3dUniform->psData = mFragmentUniformStorage->getDataPointer(
1894 d3dUniform->psRegisterIndex, d3dUniform->registerElement);
1895 }
1896
1897 if (d3dUniform->isReferencedByComputeShader())
1898 {
1899 d3dUniform->csData = mComputeUniformStorage->getDataPointer(
1900 d3dUniform->csRegisterIndex, d3dUniform->registerElement);
1901 }
1902 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001903}
1904
Jamie Madilld63961d2017-09-12 15:22:57 -04001905void ProgramD3D::updateUniformBufferCache(const gl::Caps &caps,
1906 unsigned int reservedVertex,
1907 unsigned int reservedFragment)
Brandon Jones18bd4102014-09-22 14:21:44 -07001908{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001909 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001910 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001911 return;
Jamie Madill4a3c2342015-10-08 12:58:45 -04001912 }
1913
Jamie Madill03260fa2015-06-22 13:57:22 -04001914 mVertexUBOCache.clear();
1915 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001916
Jamie Madill4a3c2342015-10-08 12:58:45 -04001917 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001918 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001919 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001920 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001921 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001922
Brandon Jones18bd4102014-09-22 14:21:44 -07001923 // Unnecessary to apply an unreferenced standard or shared UBO
Olli Etuaho107c7242018-03-20 15:45:35 +02001924 if (!uniformBlock.vertexActive() && !uniformBlock.fragmentActive())
Brandon Jones18bd4102014-09-22 14:21:44 -07001925 {
1926 continue;
1927 }
1928
Olli Etuaho107c7242018-03-20 15:45:35 +02001929 if (uniformBlock.vertexActive())
Brandon Jones18bd4102014-09-22 14:21:44 -07001930 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001931 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedVertex;
1932 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001933
Jamie Madill969194d2015-07-20 14:36:56 -04001934 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001935 {
1936 mVertexUBOCache.resize(registerIndex + 1, -1);
1937 }
1938
1939 ASSERT(mVertexUBOCache[registerIndex] == -1);
1940 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001941 }
1942
Olli Etuaho107c7242018-03-20 15:45:35 +02001943 if (uniformBlock.fragmentActive())
Brandon Jones18bd4102014-09-22 14:21:44 -07001944 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001945 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedFragment;
1946 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001947
1948 if (mFragmentUBOCache.size() <= registerIndex)
1949 {
1950 mFragmentUBOCache.resize(registerIndex + 1, -1);
1951 }
1952
1953 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1954 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001955 }
1956 }
Jamie Madilld63961d2017-09-12 15:22:57 -04001957}
Brandon Jones18bd4102014-09-22 14:21:44 -07001958
Jamie Madilld63961d2017-09-12 15:22:57 -04001959const std::vector<GLint> &ProgramD3D::getVertexUniformBufferCache() const
1960{
1961 return mVertexUBOCache;
1962}
1963
1964const std::vector<GLint> &ProgramD3D::getFragmentUniformBufferCache() const
1965{
1966 return mFragmentUBOCache;
Brandon Jones18bd4102014-09-22 14:21:44 -07001967}
1968
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001969void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001970{
Jamie Madill4148fd72017-09-14 15:46:20 -04001971 mVertexUniformsDirty = true;
1972 mFragmentUniformsDirty = true;
1973 mComputeUniformsDirty = true;
1974}
1975
1976void ProgramD3D::markUniformsClean()
1977{
1978 mVertexUniformsDirty = false;
1979 mFragmentUniformsDirty = false;
1980 mComputeUniformsDirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001981}
1982
Jamie Madill334d6152015-10-22 14:00:28 -04001983void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001984{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001985 setUniformInternal(location, count, v, GL_FLOAT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986}
1987
1988void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1989{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001990 setUniformInternal(location, count, v, GL_FLOAT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001991}
1992
1993void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1994{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001995 setUniformInternal(location, count, v, GL_FLOAT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001996}
1997
1998void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1999{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002000 setUniformInternal(location, count, v, GL_FLOAT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002001}
2002
Jamie Madill334d6152015-10-22 14:00:28 -04002003void ProgramD3D::setUniformMatrix2fv(GLint location,
2004 GLsizei count,
2005 GLboolean transpose,
2006 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002007{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002008 setUniformMatrixfvInternal<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002009}
2010
Jamie Madill334d6152015-10-22 14:00:28 -04002011void ProgramD3D::setUniformMatrix3fv(GLint location,
2012 GLsizei count,
2013 GLboolean transpose,
2014 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002015{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002016 setUniformMatrixfvInternal<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017}
2018
Jamie Madill334d6152015-10-22 14:00:28 -04002019void ProgramD3D::setUniformMatrix4fv(GLint location,
2020 GLsizei count,
2021 GLboolean transpose,
2022 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002023{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002024 setUniformMatrixfvInternal<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002025}
2026
Jamie Madill334d6152015-10-22 14:00:28 -04002027void ProgramD3D::setUniformMatrix2x3fv(GLint location,
2028 GLsizei count,
2029 GLboolean transpose,
2030 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002031{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002032 setUniformMatrixfvInternal<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002033}
2034
Jamie Madill334d6152015-10-22 14:00:28 -04002035void ProgramD3D::setUniformMatrix3x2fv(GLint location,
2036 GLsizei count,
2037 GLboolean transpose,
2038 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002039{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002040 setUniformMatrixfvInternal<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002041}
2042
Jamie Madill334d6152015-10-22 14:00:28 -04002043void ProgramD3D::setUniformMatrix2x4fv(GLint location,
2044 GLsizei count,
2045 GLboolean transpose,
2046 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002047{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002048 setUniformMatrixfvInternal<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049}
2050
Jamie Madill334d6152015-10-22 14:00:28 -04002051void ProgramD3D::setUniformMatrix4x2fv(GLint location,
2052 GLsizei count,
2053 GLboolean transpose,
2054 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002055{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002056 setUniformMatrixfvInternal<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002057}
2058
Jamie Madill334d6152015-10-22 14:00:28 -04002059void ProgramD3D::setUniformMatrix3x4fv(GLint location,
2060 GLsizei count,
2061 GLboolean transpose,
2062 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002063{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002064 setUniformMatrixfvInternal<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002065}
2066
Jamie Madill334d6152015-10-22 14:00:28 -04002067void ProgramD3D::setUniformMatrix4x3fv(GLint location,
2068 GLsizei count,
2069 GLboolean transpose,
2070 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002071{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002072 setUniformMatrixfvInternal<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002073}
2074
2075void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
2076{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002077 setUniformInternal(location, count, v, GL_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002078}
2079
2080void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2081{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002082 setUniformInternal(location, count, v, GL_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002083}
2084
2085void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2086{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002087 setUniformInternal(location, count, v, GL_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002088}
2089
2090void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2091{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002092 setUniformInternal(location, count, v, GL_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002093}
2094
2095void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2096{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002097 setUniformInternal(location, count, v, GL_UNSIGNED_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002098}
2099
2100void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2101{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002102 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002103}
2104
2105void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2106{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002107 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108}
2109
2110void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2111{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002112 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002113}
2114
Jamie Madillf4141212017-12-12 15:08:07 -05002115void ProgramD3D::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04002116{
Jamie Madillf4141212017-12-12 15:08:07 -05002117 mRenderer->onDirtyUniformBlockBinding(uniformBlockIndex);
Geoff Lang5d124a62015-09-15 13:03:27 -04002118}
2119
Jamie Madillbd044ed2017-06-05 12:59:21 -04002120void ProgramD3D::defineUniformsAndAssignRegisters(const gl::Context *context)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002121{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002122 D3DUniformMap uniformMap;
Jiawei Shao385b3e02018-03-21 09:43:28 +08002123 for (gl::ShaderType shaderType : gl::AllShaderTypes())
Jamie Madill417df922017-01-12 09:23:07 -05002124 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08002125 gl::Shader *shader = mState.getAttachedShader(shaderType);
2126 if (shader)
Jamie Madillfb536032015-09-11 13:19:49 -04002127 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08002128 for (const sh::Uniform &uniform : shader->getUniforms(context))
Xinghua Caob1239382016-12-13 15:07:05 +08002129 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08002130 if (uniform.active)
2131 {
2132 defineUniformBase(shader, uniform, &uniformMap);
2133 }
Xinghua Caob1239382016-12-13 15:07:05 +08002134 }
Jamie Madillfb536032015-09-11 13:19:49 -04002135 }
2136 }
2137
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002138 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002139 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002140 {
2141 if (!glUniform.isInDefaultBlock())
2142 continue;
2143
Olli Etuahod2551232017-10-26 20:03:33 +03002144 std::string name = glUniform.name;
2145 if (glUniform.isArray())
2146 {
2147 // In the program state, array uniform names include [0] as in the program resource
2148 // spec. Here we don't include it.
2149 // TODO(oetuaho@nvidia.com): consider using the same uniform naming here as in the GL
2150 // layer.
2151 ASSERT(angle::EndsWith(name, "[0]"));
2152 name.resize(name.length() - 3);
2153 }
2154 auto mapEntry = uniformMap.find(name);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002155 ASSERT(mapEntry != uniformMap.end());
2156 mD3DUniforms.push_back(mapEntry->second);
2157 }
2158
Jamie Madill62d31cb2015-09-11 13:25:51 -04002159 assignAllSamplerRegisters();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002160 assignAllImageRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04002161 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04002162}
2163
Jamie Madill91445bc2015-09-23 16:47:53 -04002164void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002165 const sh::Uniform &uniform,
2166 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04002167{
Xinghua Cao26143fd2017-11-01 18:19:05 +08002168 // Samplers get their registers assigned in assignAllSamplerRegisters, and images get their
2169 // registers assigned in assignAllImageRegisters.
2170 if (gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04002171 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002172 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::Texture, nullptr,
2173 uniformMap);
2174 return;
2175 }
2176 else if (gl::IsImageType(uniform.type))
2177 {
2178 if (uniform.readonly)
2179 {
2180 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::Texture,
2181 nullptr, uniformMap);
2182 }
2183 else
2184 {
2185 defineUniform(shader->getType(), uniform, uniform.name,
2186 HLSLRegisterType::UnorderedAccessView, nullptr, uniformMap);
2187 }
2188 mImageBindingMap[uniform.name] = uniform.binding;
2189 return;
2190 }
2191 else if (uniform.isBuiltIn())
2192 {
2193 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::None, nullptr,
2194 uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04002195 return;
2196 }
2197
Jamie Madill91445bc2015-09-23 16:47:53 -04002198 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
Jamie Madill91445bc2015-09-23 16:47:53 -04002199 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
Xinghua Cao26143fd2017-11-01 18:19:05 +08002200 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04002201 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002202 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002203
Xinghua Cao26143fd2017-11-01 18:19:05 +08002204 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::None, &encoder,
2205 uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002206}
2207
Jamie Madill62d31cb2015-09-11 13:25:51 -04002208D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
2209{
2210 for (D3DUniform *d3dUniform : mD3DUniforms)
2211 {
2212 if (d3dUniform->name == name)
2213 {
2214 return d3dUniform;
2215 }
2216 }
2217
2218 return nullptr;
2219}
2220
Jiawei Shao385b3e02018-03-21 09:43:28 +08002221void ProgramD3D::defineStructUniformFields(gl::ShaderType shaderType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002222 const std::vector<sh::ShaderVariable> &fields,
2223 const std::string &namePrefix,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002224 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002225 sh::HLSLBlockEncoder *encoder,
2226 D3DUniformMap *uniformMap)
2227{
2228 if (encoder)
2229 encoder->enterAggregateType();
2230
2231 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
2232 {
2233 const sh::ShaderVariable &field = fields[fieldIndex];
2234 const std::string &fieldFullName = (namePrefix + "." + field.name);
2235
2236 // Samplers get their registers assigned in assignAllSamplerRegisters.
2237 // Also they couldn't use the same encoder as the rest of the struct, since they are
2238 // extracted out of the struct by the shader translator.
2239 if (gl::IsSamplerType(field.type))
2240 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002241 defineUniform(shaderType, field, fieldFullName, regType, nullptr, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002242 }
2243 else
2244 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002245 defineUniform(shaderType, field, fieldFullName, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002246 }
2247 }
2248
2249 if (encoder)
2250 encoder->exitAggregateType();
2251}
2252
Jiawei Shao385b3e02018-03-21 09:43:28 +08002253void ProgramD3D::defineArrayOfStructsUniformFields(gl::ShaderType shaderType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002254 const sh::ShaderVariable &uniform,
2255 unsigned int arrayNestingIndex,
2256 const std::string &prefix,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002257 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002258 sh::HLSLBlockEncoder *encoder,
2259 D3DUniformMap *uniformMap)
2260{
2261 // Nested arrays are processed starting from outermost (arrayNestingIndex 0u) and ending at the
2262 // innermost.
2263 const unsigned int currentArraySize = uniform.getNestedArraySize(arrayNestingIndex);
2264 for (unsigned int arrayElement = 0u; arrayElement < currentArraySize; ++arrayElement)
2265 {
2266 const std::string &elementString = prefix + ArrayString(arrayElement);
2267 if (arrayNestingIndex + 1u < uniform.arraySizes.size())
2268 {
2269 defineArrayOfStructsUniformFields(shaderType, uniform, arrayNestingIndex + 1u,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002270 elementString, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002271 }
2272 else
2273 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002274 defineStructUniformFields(shaderType, uniform.fields, elementString, regType, encoder,
Olli Etuaho465835d2017-09-26 13:34:10 +03002275 uniformMap);
2276 }
2277 }
2278}
2279
Jiawei Shao385b3e02018-03-21 09:43:28 +08002280void ProgramD3D::defineArrayUniformElements(gl::ShaderType shaderType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002281 const sh::ShaderVariable &uniform,
2282 const std::string &fullName,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002283 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002284 sh::HLSLBlockEncoder *encoder,
2285 D3DUniformMap *uniformMap)
2286{
2287 if (encoder)
2288 encoder->enterAggregateType();
2289
2290 sh::ShaderVariable uniformElement = uniform;
2291 uniformElement.arraySizes.pop_back();
2292 for (unsigned int arrayIndex = 0u; arrayIndex < uniform.getOutermostArraySize(); ++arrayIndex)
2293 {
2294 std::string elementFullName = fullName + ArrayString(arrayIndex);
Xinghua Cao26143fd2017-11-01 18:19:05 +08002295 defineUniform(shaderType, uniformElement, elementFullName, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002296 }
2297
2298 if (encoder)
2299 encoder->exitAggregateType();
2300}
2301
Jiawei Shao385b3e02018-03-21 09:43:28 +08002302void ProgramD3D::defineUniform(gl::ShaderType shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002303 const sh::ShaderVariable &uniform,
2304 const std::string &fullName,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002305 const HLSLRegisterType regType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002306 sh::HLSLBlockEncoder *encoder,
2307 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002308{
2309 if (uniform.isStruct())
2310 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002311 if (uniform.isArray())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002312 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002313 defineArrayOfStructsUniformFields(shaderType, uniform, 0u, fullName, regType, encoder,
Olli Etuaho465835d2017-09-26 13:34:10 +03002314 uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002315 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002316 else
2317 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002318 defineStructUniformFields(shaderType, uniform.fields, fullName, regType, encoder,
2319 uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002320 }
2321 return;
2322 }
2323 if (uniform.isArrayOfArrays())
2324 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002325 defineArrayUniformElements(shaderType, uniform, fullName, regType, encoder, uniformMap);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002326 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002327 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002328
2329 // Not a struct. Arrays are treated as aggregate types.
2330 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002331 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002332 encoder->enterAggregateType();
2333 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002334
Jamie Madill62d31cb2015-09-11 13:25:51 -04002335 // Advance the uniform offset, to track registers allocation for structs
2336 sh::BlockMemberInfo blockInfo =
Olli Etuaho465835d2017-09-26 13:34:10 +03002337 encoder ? encoder->encodeType(uniform.type, uniform.arraySizes, false)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002338 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002339
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002340 auto uniformMapEntry = uniformMap->find(fullName);
2341 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002342
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002343 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002344 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002345 d3dUniform = uniformMapEntry->second;
2346 }
2347 else
2348 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002349 d3dUniform = new D3DUniform(uniform.type, regType, fullName, uniform.arraySizes, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002350 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002351 }
2352
2353 if (encoder)
2354 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002355 d3dUniform->registerElement =
2356 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002357 unsigned int reg =
2358 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jiawei Shao385b3e02018-03-21 09:43:28 +08002359
2360 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002361 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08002362 case gl::ShaderType::Fragment:
2363 d3dUniform->psRegisterIndex = reg;
2364 break;
2365 case gl::ShaderType::Vertex:
2366 d3dUniform->vsRegisterIndex = reg;
2367 break;
2368 case gl::ShaderType::Compute:
2369 d3dUniform->csRegisterIndex = reg;
2370 break;
2371 default:
2372 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002373 }
Jamie Madillfb536032015-09-11 13:19:49 -04002374
2375 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002376 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002377 {
2378 encoder->exitAggregateType();
2379 }
2380 }
2381}
2382
Jamie Madill134f93d2017-08-31 17:11:00 -04002383// Assume count is already clamped.
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002384template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002385void ProgramD3D::setUniformImpl(const gl::VariableLocation &locationInfo,
Jamie Madill134f93d2017-08-31 17:11:00 -04002386 GLsizei count,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002387 const T *v,
2388 uint8_t *targetData,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002389 GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002390{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002391 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
Jamie Madill33bb7c42017-09-09 23:32:51 -04002392 const int components = targetUniform->typeInfo.componentCount;
Olli Etuaho1734e172017-10-27 15:30:27 +03002393 const unsigned int arrayElementOffset = locationInfo.arrayIndex;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002394
Jamie Madill33bb7c42017-09-09 23:32:51 -04002395 if (targetUniform->typeInfo.type == uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002396 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002397 T *dest = reinterpret_cast<T *>(targetData) + arrayElementOffset * 4;
Jamie Madill33bb7c42017-09-09 23:32:51 -04002398 const T *source = v;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002399
Jamie Madill33bb7c42017-09-09 23:32:51 -04002400 for (GLint i = 0; i < count; i++, dest += 4, source += components)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002401 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002402 memcpy(dest, source, components * sizeof(T));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002403 }
2404 }
Jamie Madill33bb7c42017-09-09 23:32:51 -04002405 else
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002406 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002407 ASSERT(targetUniform->typeInfo.type == gl::VariableBoolVectorType(uniformType));
Olli Etuahoc8538042017-09-27 11:20:15 +03002408 GLint *boolParams = reinterpret_cast<GLint *>(targetData) + arrayElementOffset * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002409
Jamie Madill134f93d2017-08-31 17:11:00 -04002410 for (GLint i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002411 {
Jamie Madill334d6152015-10-22 14:00:28 -04002412 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002413 const T *source = v + (i * components);
2414
2415 for (int c = 0; c < components; c++)
2416 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002417 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002418 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002419 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002420 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002421}
2422
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002423template <typename T>
Jamie Madill33bb7c42017-09-09 23:32:51 -04002424void ProgramD3D::setUniformInternal(GLint location, GLsizei count, const T *v, GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002425{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002426 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2427 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
2428
Jamie Madill33bb7c42017-09-09 23:32:51 -04002429 if (targetUniform->typeInfo.isSampler)
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002430 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002431 ASSERT(uniformType == GL_INT);
Jamie Madill80823cc2017-09-14 15:46:21 -04002432 size_t size = count * sizeof(T);
Jamie Madillacf2f3a2017-11-21 19:22:44 -05002433 GLint *dest = &targetUniform->mSamplerData[locationInfo.arrayIndex];
Jamie Madill80823cc2017-09-14 15:46:21 -04002434 if (memcmp(dest, v, size) != 0)
2435 {
2436 memcpy(dest, v, size);
2437 mDirtySamplerMapping = true;
2438 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002439 return;
2440 }
2441
2442 if (targetUniform->vsData)
2443 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002444 setUniformImpl(locationInfo, count, v, targetUniform->vsData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002445 mVertexUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002446 }
2447
2448 if (targetUniform->psData)
2449 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002450 setUniformImpl(locationInfo, count, v, targetUniform->psData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002451 mFragmentUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002452 }
2453
2454 if (targetUniform->csData)
2455 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002456 setUniformImpl(locationInfo, count, v, targetUniform->csData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002457 mComputeUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002458 }
2459}
2460
2461template <int cols, int rows>
Jamie Madill80823cc2017-09-14 15:46:21 -04002462bool ProgramD3D::setUniformMatrixfvImpl(GLint location,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002463 GLsizei countIn,
2464 GLboolean transpose,
2465 const GLfloat *value,
2466 uint8_t *targetData,
2467 GLenum targetUniformType)
2468{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002469 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002470
Olli Etuaho465835d2017-09-26 13:34:10 +03002471 unsigned int elementCount = targetUniform->getArraySizeProduct();
Olli Etuaho1734e172017-10-27 15:30:27 +03002472 unsigned int arrayElementOffset = mState.getUniformLocations()[location].arrayIndex;
Olli Etuahoc8538042017-09-27 11:20:15 +03002473 unsigned int count =
2474 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002475
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002476 const unsigned int targetMatrixStride = (4 * rows);
Olli Etuahoc8538042017-09-27 11:20:15 +03002477 GLfloat *target = reinterpret_cast<GLfloat *>(
2478 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002479
Jamie Madill80823cc2017-09-14 15:46:21 -04002480 bool dirty = false;
2481
Jamie Madill62d31cb2015-09-11 13:25:51 -04002482 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002483 {
2484 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2485 if (transpose == GL_FALSE)
2486 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002487 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002488 }
2489 else
2490 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002491 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002492 }
2493 target += targetMatrixStride;
2494 value += cols * rows;
2495 }
Jamie Madill80823cc2017-09-14 15:46:21 -04002496
2497 return dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002498}
2499
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002500template <int cols, int rows>
2501void ProgramD3D::setUniformMatrixfvInternal(GLint location,
2502 GLsizei countIn,
2503 GLboolean transpose,
2504 const GLfloat *value,
2505 GLenum targetUniformType)
2506{
2507 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
2508
2509 if (targetUniform->vsData)
2510 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002511 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2512 targetUniform->vsData, targetUniformType))
2513 {
2514 mVertexUniformsDirty = true;
2515 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002516 }
2517
2518 if (targetUniform->psData)
2519 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002520 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2521 targetUniform->psData, targetUniformType))
2522 {
2523 mFragmentUniformsDirty = true;
2524 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002525 }
2526
2527 if (targetUniform->csData)
2528 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002529 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2530 targetUniform->csData, targetUniformType))
2531 {
2532 mComputeUniformsDirty = true;
2533 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002534 }
2535}
2536
Jamie Madill62d31cb2015-09-11 13:25:51 -04002537void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002538{
Olli Etuaho465835d2017-09-26 13:34:10 +03002539 for (size_t uniformIndex = 0; uniformIndex < mD3DUniforms.size(); ++uniformIndex)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002540 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002541 if (mD3DUniforms[uniformIndex]->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002542 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002543 assignSamplerRegisters(uniformIndex);
Jamie Madillfb536032015-09-11 13:19:49 -04002544 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002545 }
2546}
2547
Olli Etuaho465835d2017-09-26 13:34:10 +03002548void ProgramD3D::assignSamplerRegisters(size_t uniformIndex)
Jamie Madillfb536032015-09-11 13:19:49 -04002549{
Olli Etuaho465835d2017-09-26 13:34:10 +03002550 D3DUniform *d3dUniform = mD3DUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002551 ASSERT(d3dUniform->isSampler());
Olli Etuaho465835d2017-09-26 13:34:10 +03002552 // If the uniform is an array of arrays, then we have separate entries for each inner array in
2553 // mD3DUniforms. However, the sampler register info is stored in the shader only for the
2554 // outermost array.
2555 std::vector<unsigned int> subscripts;
2556 const std::string baseName = gl::ParseResourceName(d3dUniform->name, &subscripts);
2557 unsigned int registerOffset = mState.getUniforms()[uniformIndex].flattenedOffsetInParentArrays *
2558 d3dUniform->getArraySizeProduct();
2559
Jiawei Shao385b3e02018-03-21 09:43:28 +08002560 const gl::Shader *computeShader = mState.getAttachedShader(gl::ShaderType::Compute);
Xinghua Caob1239382016-12-13 15:07:05 +08002561 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002562 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08002563 const ShaderD3D *computeShaderD3D =
2564 GetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Compute));
Olli Etuaho465835d2017-09-26 13:34:10 +03002565 ASSERT(computeShaderD3D->hasUniform(baseName));
2566 d3dUniform->csRegisterIndex =
2567 computeShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002568 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002569 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->typeInfo,
2570 d3dUniform->getArraySizeProduct(), mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002571 }
Xinghua Caob1239382016-12-13 15:07:05 +08002572 else
Jamie Madillfb536032015-09-11 13:19:49 -04002573 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08002574 const ShaderD3D *vertexShaderD3D =
2575 GetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Vertex));
Xinghua Caob1239382016-12-13 15:07:05 +08002576 const ShaderD3D *fragmentShaderD3D =
Jiawei Shao385b3e02018-03-21 09:43:28 +08002577 GetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Fragment));
Olli Etuaho465835d2017-09-26 13:34:10 +03002578 ASSERT(vertexShaderD3D->hasUniform(baseName) || fragmentShaderD3D->hasUniform(baseName));
2579 if (vertexShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002580 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002581 d3dUniform->vsRegisterIndex =
2582 vertexShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002583 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002584 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->typeInfo,
2585 d3dUniform->getArraySizeProduct(), mSamplersVS,
2586 &mUsedVertexSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002587 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002588 if (fragmentShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002589 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002590 d3dUniform->psRegisterIndex =
2591 fragmentShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002592 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002593 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->typeInfo,
2594 d3dUniform->getArraySizeProduct(), mSamplersPS, &mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002595 }
Jamie Madillfb536032015-09-11 13:19:49 -04002596 }
2597}
2598
Jamie Madill62d31cb2015-09-11 13:25:51 -04002599// static
2600void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002601 const gl::UniformTypeInfo &typeInfo,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002602 unsigned int samplerCount,
2603 std::vector<Sampler> &outSamplers,
2604 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002605{
2606 unsigned int samplerIndex = startSamplerIndex;
2607
2608 do
2609 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002610 ASSERT(samplerIndex < outSamplers.size());
2611 Sampler *sampler = &outSamplers[samplerIndex];
2612 sampler->active = true;
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002613 sampler->textureType = gl::FromGLenum<gl::TextureType>(typeInfo.textureType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002614 sampler->logicalTextureUnit = 0;
2615 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002616 samplerIndex++;
2617 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002618}
2619
Xinghua Cao26143fd2017-11-01 18:19:05 +08002620void ProgramD3D::assignAllImageRegisters()
2621{
2622 for (size_t uniformIndex = 0; uniformIndex < mD3DUniforms.size(); ++uniformIndex)
2623 {
2624 if (mD3DUniforms[uniformIndex]->isImage())
2625 {
2626 assignImageRegisters(uniformIndex);
2627 }
2628 }
2629}
2630
2631void ProgramD3D::assignImageRegisters(size_t uniformIndex)
2632{
2633 D3DUniform *d3dUniform = mD3DUniforms[uniformIndex];
2634 ASSERT(d3dUniform->isImage());
2635 // If the uniform is an array of arrays, then we have separate entries for each inner array in
2636 // mD3DUniforms. However, the image register info is stored in the shader only for the
2637 // outermost array.
2638 std::vector<unsigned int> subscripts;
2639 const std::string baseName = gl::ParseResourceName(d3dUniform->name, &subscripts);
2640 unsigned int registerOffset = mState.getUniforms()[uniformIndex].flattenedOffsetInParentArrays *
2641 d3dUniform->getArraySizeProduct();
2642
Jiawei Shao385b3e02018-03-21 09:43:28 +08002643 const gl::Shader *computeShader = mState.getAttachedShader(gl::ShaderType::Compute);
Xinghua Cao26143fd2017-11-01 18:19:05 +08002644 if (computeShader)
2645 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08002646 const ShaderD3D *computeShaderD3D =
2647 GetImplAs<ShaderD3D>(mState.getAttachedShader(gl::ShaderType::Compute));
Xinghua Cao26143fd2017-11-01 18:19:05 +08002648 ASSERT(computeShaderD3D->hasUniform(baseName));
2649 d3dUniform->csRegisterIndex =
2650 computeShaderD3D->getUniformRegister(baseName) + registerOffset;
2651 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2652 auto bindingIter = mImageBindingMap.find(baseName);
2653 ASSERT(bindingIter != mImageBindingMap.end());
2654 if (d3dUniform->regType == HLSLRegisterType::Texture)
2655 {
2656 AssignImages(d3dUniform->csRegisterIndex, bindingIter->second,
2657 d3dUniform->getArraySizeProduct(), mReadonlyImagesCS,
2658 &mUsedComputeReadonlyImageRange);
2659 }
2660 else if (d3dUniform->regType == HLSLRegisterType::UnorderedAccessView)
2661 {
2662 AssignImages(d3dUniform->csRegisterIndex, bindingIter->second,
2663 d3dUniform->getArraySizeProduct(), mImagesCS, &mUsedComputeImageRange);
2664 }
2665 else
2666 {
2667 UNREACHABLE();
2668 }
2669 }
2670 else
2671 {
2672 // TODO(xinghua.cao@intel.com): Implement image variables in vertex shader and pixel shader.
2673 UNIMPLEMENTED();
2674 }
2675}
2676
2677// static
2678void ProgramD3D::AssignImages(unsigned int startImageIndex,
2679 int startLogicalImageUnit,
2680 unsigned int imageCount,
2681 std::vector<Image> &outImages,
2682 GLuint *outUsedRange)
2683{
2684 unsigned int imageIndex = startImageIndex;
2685 // If declare without a binding qualifier, any uniform image variable (include all elements of
2686 // unbound image array) shoud be bound to unit zero.
2687 if (startLogicalImageUnit == -1)
2688 {
2689 ASSERT(imageIndex < outImages.size());
2690 Image *image = &outImages[imageIndex];
2691 image->active = true;
2692 image->logicalImageUnit = 0;
2693 *outUsedRange = std::max(imageIndex + 1, *outUsedRange);
2694 return;
2695 }
2696
2697 unsigned int logcalImageUnit = startLogicalImageUnit;
2698 do
2699 {
2700 ASSERT(imageIndex < outImages.size());
2701 Image *image = &outImages[imageIndex];
2702 image->active = true;
2703 image->logicalImageUnit = logcalImageUnit;
2704 *outUsedRange = std::max(imageIndex + 1, *outUsedRange);
2705 imageIndex++;
2706 logcalImageUnit++;
2707 } while (imageIndex < startImageIndex + imageCount);
2708}
2709
Brandon Jonesc9610c52014-08-25 17:02:59 -07002710void ProgramD3D::reset()
2711{
Xinghua Caob1239382016-12-13 15:07:05 +08002712 mVertexExecutables.clear();
2713 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002714
Xinghua Caob1239382016-12-13 15:07:05 +08002715 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002716 {
Xinghua Caob1239382016-12-13 15:07:05 +08002717 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002718 }
Brandon Joneseb994362014-09-24 10:27:28 -07002719
Xinghua Caob1239382016-12-13 15:07:05 +08002720 mComputeExecutable.reset(nullptr);
2721
Brandon Jones22502d52014-08-29 16:58:36 -07002722 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002723 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002724
2725 mPixelHLSL.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002726 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
2727 mUsesFragDepth = false;
Martin Radev41ac68e2017-06-06 12:16:58 +03002728 mHasANGLEMultiviewEnabled = false;
2729 mUsesViewID = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002730 mPixelShaderKey.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002731 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002732 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002733
Jamie Madill62d31cb2015-09-11 13:25:51 -04002734 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002735 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002736
Xinghua Caob1239382016-12-13 15:07:05 +08002737 mVertexUniformStorage.reset(nullptr);
2738 mFragmentUniformStorage.reset(nullptr);
2739 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002740
2741 mSamplersPS.clear();
2742 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002743 mSamplersCS.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002744 mImagesCS.clear();
2745 mReadonlyImagesCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002746
Xinghua Cao26143fd2017-11-01 18:19:05 +08002747 mUsedVertexSamplerRange = 0;
2748 mUsedPixelSamplerRange = 0;
2749 mUsedComputeSamplerRange = 0;
2750 mDirtySamplerMapping = true;
2751 mUsedComputeImageRange = 0;
2752 mUsedComputeReadonlyImageRange = 0;
Jamie Madill437d2662014-12-05 14:23:35 -05002753
Jamie Madill8047c0d2016-03-07 13:02:12 -05002754 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002755
Jamie Madill9fc36822015-11-18 13:08:07 -05002756 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002757
2758 mGeometryShaderPreamble.clear();
Jamie Madill561ed3a2017-08-31 16:48:09 -04002759
Jamie Madill4148fd72017-09-14 15:46:20 -04002760 dirtyAllUniforms();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002761
2762 mCachedPixelExecutableIndex.reset();
2763 mCachedVertexExecutableIndex.reset();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002764}
2765
Geoff Lang7dd2e102014-11-10 15:19:26 -05002766unsigned int ProgramD3D::getSerial() const
2767{
2768 return mSerial;
2769}
2770
2771unsigned int ProgramD3D::issueSerial()
2772{
2773 return mCurrentSerial++;
2774}
2775
Jamie Madillbd044ed2017-06-05 12:59:21 -04002776void ProgramD3D::initAttribLocationsToD3DSemantic(const gl::Context *context)
Jamie Madill63805b42015-08-25 13:17:39 -04002777{
Jiawei Shao385b3e02018-03-21 09:43:28 +08002778 gl::Shader *vertexShader = mState.getAttachedShader(gl::ShaderType::Vertex);
Jamie Madill63805b42015-08-25 13:17:39 -04002779 ASSERT(vertexShader != nullptr);
2780
2781 // Init semantic index
Jamie Madill34ca4f52017-06-13 11:49:39 -04002782 int semanticIndex = 0;
2783 for (const sh::Attribute &attribute : vertexShader->getActiveAttributes(context))
Jamie Madill63805b42015-08-25 13:17:39 -04002784 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002785 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill34ca4f52017-06-13 11:49:39 -04002786 GLuint location = mState.getAttributeLocation(attribute.name);
2787 ASSERT(location != std::numeric_limits<GLuint>::max());
Jamie Madill63805b42015-08-25 13:17:39 -04002788
Jamie Madill8047c0d2016-03-07 13:02:12 -05002789 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002790 {
Jamie Madill34ca4f52017-06-13 11:49:39 -04002791 mAttribLocationToD3DSemantic[location + reg] = semanticIndex++;
Jamie Madill63805b42015-08-25 13:17:39 -04002792 }
2793 }
Jamie Madill437d2662014-12-05 14:23:35 -05002794}
2795
Jamie Madilla779b612017-07-24 11:46:05 -04002796void ProgramD3D::updateCachedInputLayout(Serial associatedSerial, const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002797{
Jamie Madilla779b612017-07-24 11:46:05 -04002798 if (mCurrentVertexArrayStateSerial == associatedSerial)
2799 {
2800 return;
2801 }
2802
2803 mCurrentVertexArrayStateSerial = associatedSerial;
Jamie Madillbd136f92015-08-10 14:51:37 -04002804 mCachedInputLayout.clear();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002805
Jamie Madilld3dfda22015-07-06 08:28:49 -04002806 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002807
Jamie Madill6de51852017-04-12 09:53:01 -04002808 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002809 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002810 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002811
Jamie Madill8047c0d2016-03-07 13:02:12 -05002812 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002813 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002814 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002815 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002816 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002817 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002818 mCachedInputLayout[d3dSemantic] =
2819 GetVertexFormatType(vertexAttributes[locationIndex],
2820 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002821 }
2822 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002823
2824 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04002825
2826 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002827}
2828
2829void ProgramD3D::updateCachedOutputLayout(const gl::Context *context,
2830 const gl::Framebuffer *framebuffer)
2831{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002832 mPixelShaderOutputLayoutCache.clear();
2833
2834 FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(framebuffer);
2835 const auto &colorbuffers = fboD3D->getColorAttachmentsForRender(context);
2836
2837 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
2838 {
2839 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
2840
2841 if (colorbuffer)
2842 {
2843 auto binding = colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0
2844 : colorbuffer->getBinding();
2845 mPixelShaderOutputLayoutCache.push_back(binding);
2846 }
2847 else
2848 {
2849 mPixelShaderOutputLayoutCache.push_back(GL_NONE);
2850 }
2851 }
2852
2853 updateCachedPixelExecutableIndex();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002854}
2855
Jamie Madill192745a2016-12-22 15:58:21 -05002856void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2857 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002858{
Jamie Madill9fc36822015-11-18 13:08:07 -05002859 const std::string &varyingSemantic =
2860 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2861
Jamie Madillccdf74b2015-08-18 10:46:12 -04002862 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002863 mStreamOutVaryings.clear();
2864
Jamie Madill48ef11b2016-04-27 15:21:52 -04002865 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002866 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2867 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002868 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002869 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2870 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002871 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002872 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002873 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002874 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2875 builtins.glPosition.index, 4, outputSlot));
2876 }
2877 }
2878 else if (tfVaryingName == "gl_FragCoord")
2879 {
2880 if (builtins.glFragCoord.enabled)
2881 {
2882 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2883 builtins.glFragCoord.index, 4, outputSlot));
2884 }
2885 }
2886 else if (tfVaryingName == "gl_PointSize")
2887 {
2888 if (builtins.glPointSize.enabled)
2889 {
2890 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2891 }
2892 }
2893 else
2894 {
Jamie Madill192745a2016-12-22 15:58:21 -05002895 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002896 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002897 const auto &varying = *registerInfo.packedVarying->varying;
2898 GLenum transposedType = gl::TransposeMatrixType(varying.type);
jchen108225e732017-11-14 16:29:03 +08002899 int componentCount = gl::VariableColumnCount(transposedType);
2900 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill55c25d02015-11-18 13:08:08 -05002901
Jamie Madill9fc36822015-11-18 13:08:07 -05002902 // There can be more than one register assigned to a particular varying, and each
2903 // register needs its own stream out entry.
jchen108225e732017-11-14 16:29:03 +08002904 if (registerInfo.tfVaryingName() == tfVaryingName)
Jamie Madill9fc36822015-11-18 13:08:07 -05002905 {
2906 mStreamOutVaryings.push_back(D3DVarying(
2907 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2908 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002909 }
2910 }
2911 }
2912}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002913
2914D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2915{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002916 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002917}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002918
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002919const D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location) const
2920{
2921 return mD3DUniforms[mState.getUniformLocations()[location].index];
2922}
2923
Sami Väisänen46eaa942016-06-29 10:26:37 +03002924void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2925 GLenum genMode,
2926 GLint components,
2927 const GLfloat *coeffs)
2928{
2929 UNREACHABLE();
2930}
2931
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002932bool ProgramD3D::hasVertexExecutableForCachedInputLayout()
2933{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002934 return mCachedVertexExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002935}
2936
2937bool ProgramD3D::hasGeometryExecutableForPrimitiveType(GLenum drawMode)
2938{
2939 if (!usesGeometryShader(drawMode))
2940 {
2941 // No shader necessary mean we have the required (null) executable.
2942 return true;
2943 }
2944
2945 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
2946 return mGeometryExecutables[geometryShaderType].get() != nullptr;
2947}
2948
2949bool ProgramD3D::hasPixelExecutableForCachedOutputLayout()
2950{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002951 return mCachedPixelExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002952}
2953
Jamie Madill54164b02017-08-28 15:17:37 -04002954template <typename DestT>
2955void ProgramD3D::getUniformInternal(GLint location, DestT *dataOut) const
2956{
2957 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2958 const gl::LinkedUniform &uniform = mState.getUniforms()[locationInfo.index];
2959
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002960 const D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Olli Etuaho1734e172017-10-27 15:30:27 +03002961 const uint8_t *srcPointer = targetUniform->getDataPtrToElement(locationInfo.arrayIndex);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002962
2963 if (gl::IsMatrixType(uniform.type))
2964 {
2965 GetMatrixUniform(gl::VariableColumnCount(uniform.type), gl::VariableRowCount(uniform.type),
2966 dataOut, reinterpret_cast<const DestT *>(srcPointer));
2967 }
2968 else
2969 {
2970 memcpy(dataOut, srcPointer, uniform.getElementSize());
2971 }
Jamie Madill54164b02017-08-28 15:17:37 -04002972}
2973
2974void ProgramD3D::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
2975{
2976 getUniformInternal(location, params);
2977}
2978
2979void ProgramD3D::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
2980{
2981 getUniformInternal(location, params);
2982}
2983
2984void ProgramD3D::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
2985{
2986 getUniformInternal(location, params);
2987}
2988
Jamie Madill0e7f1732017-09-09 23:32:50 -04002989void ProgramD3D::updateCachedVertexExecutableIndex()
2990{
2991 mCachedVertexExecutableIndex.reset();
2992 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
2993 {
2994 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
2995 {
2996 mCachedVertexExecutableIndex = executableIndex;
2997 break;
2998 }
2999 }
3000}
3001
3002void ProgramD3D::updateCachedPixelExecutableIndex()
3003{
3004 mCachedPixelExecutableIndex.reset();
3005 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
3006 {
3007 if (mPixelExecutables[executableIndex]->matchesSignature(mPixelShaderOutputLayoutCache))
3008 {
3009 mCachedPixelExecutableIndex = executableIndex;
3010 break;
3011 }
3012 }
3013}
3014
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003015void ProgramD3D::linkResources(const gl::Context *context,
3016 const gl::ProgramLinkedResources &resources)
3017{
3018 UniformBlockInfo uniformBlockInfo;
Jiawei Shao385b3e02018-03-21 09:43:28 +08003019 for (gl::ShaderType shaderType : gl::AllShaderTypes())
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003020 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08003021 gl::Shader *shader = mState.getAttachedShader(shaderType);
3022 if (shader)
3023 {
3024 uniformBlockInfo.getShaderBlockInfo(context, shader);
3025 }
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003026 }
3027
3028 // Gather interface block info.
3029 auto getUniformBlockSize = [&uniformBlockInfo](const std::string &name,
3030 const std::string &mappedName, size_t *sizeOut) {
3031 return uniformBlockInfo.getBlockSize(name, mappedName, sizeOut);
3032 };
3033
3034 auto getUniformBlockMemberInfo = [&uniformBlockInfo](const std::string &name,
3035 const std::string &mappedName,
3036 sh::BlockMemberInfo *infoOut) {
3037 return uniformBlockInfo.getBlockMemberInfo(name, mappedName, infoOut);
3038 };
3039
3040 resources.uniformBlockLinker.linkBlocks(getUniformBlockSize, getUniformBlockMemberInfo);
3041 initializeUniformBlocks();
3042
3043 // TODO(jiajia.qin@intel.com): Determine correct shader storage block info.
3044 auto getShaderStorageBlockSize = [](const std::string &name, const std::string &mappedName,
3045 size_t *sizeOut) {
3046 *sizeOut = 0;
3047 return true;
3048 };
3049
3050 auto getShaderStorageBlockMemberInfo =
3051 [](const std::string &name, const std::string &mappedName, sh::BlockMemberInfo *infoOut) {
3052 *infoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
3053 return true;
3054 };
3055
3056 resources.shaderStorageBlockLinker.linkBlocks(getShaderStorageBlockSize,
3057 getShaderStorageBlockMemberInfo);
3058}
3059
Jamie Madill8047c0d2016-03-07 13:02:12 -05003060} // namespace rx