blob: 18a6586e0805de9a6b3c6ed7f6c6707d8105831a [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 {
Xinghua Cao37584b32017-12-01 11:04:03 +0800675 case gl::SHADER_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;
Xinghua Cao37584b32017-12-01 11:04:03 +0800682 case gl::SHADER_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;
Xinghua Cao37584b32017-12-01 11:04:03 +0800689 case gl::SHADER_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 {
Xinghua Cao37584b32017-12-01 11:04:03 +0800716 case gl::SHADER_FRAGMENT:
Jamie Madill334d6152015-10-22 14:00:28 -0400717 ASSERT(samplerIndex < mSamplersPS.size());
718 ASSERT(mSamplersPS[samplerIndex].active);
719 return mSamplersPS[samplerIndex].textureType;
Xinghua Cao37584b32017-12-01 11:04:03 +0800720 case gl::SHADER_VERTEX:
Jamie Madill334d6152015-10-22 14:00:28 -0400721 ASSERT(samplerIndex < mSamplersVS.size());
722 ASSERT(mSamplersVS[samplerIndex].active);
723 return mSamplersVS[samplerIndex].textureType;
Xinghua Cao37584b32017-12-01 11:04:03 +0800724 case gl::SHADER_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 {
Xinghua Cao37584b32017-12-01 11:04:03 +0800739 case gl::SHADER_FRAGMENT:
Jamie Madill334d6152015-10-22 14:00:28 -0400740 return mUsedPixelSamplerRange;
Xinghua Cao37584b32017-12-01 11:04:03 +0800741 case gl::SHADER_VERTEX:
Jamie Madill334d6152015-10-22 14:00:28 -0400742 return mUsedVertexSamplerRange;
Xinghua Cao37584b32017-12-01 11:04:03 +0800743 case gl::SHADER_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 {
829 case gl::SHADER_COMPUTE:
830 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 {
857 case gl::SHADER_COMPUTE:
858 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,
1052 gl::SHADER_VERTEX, mStreamOutVaryings, separateAttribs,
Jamie Madillb0a838b2016-11-13 20:02:12 -05001053 &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,
1087 gl::SHADER_FRAGMENT, mStreamOutVaryings,
1088 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,
Yunchao He85072e82017-11-14 15:43:28 +08001116 gl::SHADER_GEOMETRY, mStreamOutVaryings,
1117 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,
Yunchao He85072e82017-11-14 15:43:28 +08001137 gl::SHADER_COMPUTE, std::vector<D3DVarying>(), false,
Xinghua Caob1239382016-12-13 15:07:05 +08001138 &computeExecutable));
1139
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(
Yunchao He85072e82017-11-14 15:43:28 +08001362 *currentInfoLog, finalPixelHLSL, gl::SHADER_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(
Yunchao He85072e82017-11-14 15:43:28 +08001403 *currentInfoLog, finalVertexHLSL, gl::SHADER_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(
Yunchao He85072e82017-11-14 15:43:28 +08001460 *currentInfoLog, geometryHLSL, gl::SHADER_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{
1527 GetDefaultInputLayoutFromShader(context, mState.getAttachedVertexShader(), &mCachedInputLayout);
1528 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001529 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001530}
1531
Jamie Madill01074252016-11-28 15:55:51 -05001532class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1533{
1534 public:
1535 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1536 gl::Error run() override
1537 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001538 mProgram->updateCachedOutputLayoutFromShader();
Jamie Madill01074252016-11-28 15:55:51 -05001539
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001540 ANGLE_TRY(mProgram->getPixelExecutableForCachedOutputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001541
1542 return gl::NoError();
1543 }
1544};
1545
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001546void ProgramD3D::updateCachedOutputLayoutFromShader()
1547{
1548 GetDefaultOutputLayoutFromShader(mPixelShaderKey, &mPixelShaderOutputLayoutCache);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001549 updateCachedPixelExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001550}
1551
Jamie Madill01074252016-11-28 15:55:51 -05001552class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1553{
1554 public:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001555 GetGeometryExecutableTask(ProgramD3D *program, const gl::Context *context)
1556 : GetExecutableTask(program), mContext(context)
Jamie Madill01074252016-11-28 15:55:51 -05001557 {
1558 }
1559
1560 gl::Error run() override
1561 {
1562 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1563 // D3D11.
1564 if (mProgram->usesGeometryShader(GL_POINTS))
1565 {
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001566 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContext, GL_POINTS, &mResult,
1567 &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001568 }
1569
1570 return gl::NoError();
1571 }
1572
1573 private:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001574 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001575};
1576
Xinghua Cao73badc02017-03-29 19:14:53 +08001577gl::Error ProgramD3D::getComputeExecutable(ShaderExecutableD3D **outExecutable)
1578{
1579 if (outExecutable)
1580 {
1581 *outExecutable = mComputeExecutable.get();
1582 }
1583
1584 return gl::NoError();
1585}
1586
Jamie Madill9cf9e872017-06-05 12:59:25 -04001587gl::LinkResult ProgramD3D::compileProgramExecutables(const gl::Context *context,
1588 gl::InfoLog &infoLog)
Jamie Madill01074252016-11-28 15:55:51 -05001589{
1590 // Ensure the compiler is initialized to avoid race conditions.
1591 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1592
1593 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1594
Jamie Madillbd044ed2017-06-05 12:59:21 -04001595 GetVertexExecutableTask vertexTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001596 GetPixelExecutableTask pixelTask(this);
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001597 GetGeometryExecutableTask geometryTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001598
1599 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1600 workerPool->postWorkerTask(&pixelTask),
1601 workerPool->postWorkerTask(&geometryTask)}};
1602
1603 WaitableEvent::WaitMany(&waitEvents);
1604
Jiawei Shao02f15232017-12-27 10:10:28 +08001605 if (!vertexTask.getInfoLog().empty())
1606 {
1607 infoLog << vertexTask.getInfoLog().str();
1608 }
1609 if (!pixelTask.getInfoLog().empty())
1610 {
1611 infoLog << pixelTask.getInfoLog().str();
1612 }
1613 if (!geometryTask.getInfoLog().empty())
1614 {
1615 infoLog << geometryTask.getInfoLog().str();
1616 }
Jamie Madill01074252016-11-28 15:55:51 -05001617
1618 ANGLE_TRY(vertexTask.getError());
1619 ANGLE_TRY(pixelTask.getError());
1620 ANGLE_TRY(geometryTask.getError());
1621
1622 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1623 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1624 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1625
Jamie Madill48ef11b2016-04-27 15:21:52 -04001626 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001627
1628 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001629 {
Jamie Madill334d6152015-10-22 14:00:28 -04001630 // Geometry shaders are currently only used internally, so there is no corresponding shader
1631 // object at the interface level. For now the geometry shader debug info is prepended to
1632 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001633 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001634 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001635 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1636 }
1637
1638 if (defaultVertexExecutable)
1639 {
1640 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1641 }
1642
1643 if (defaultPixelExecutable)
1644 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001645 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001646 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001647 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1648 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001649
Jamie Madillb0a838b2016-11-13 20:02:12 -05001650 return (defaultVertexExecutable && defaultPixelExecutable &&
1651 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001652}
1653
Jamie Madill9cf9e872017-06-05 12:59:25 -04001654gl::LinkResult ProgramD3D::compileComputeExecutable(const gl::Context *context,
1655 gl::InfoLog &infoLog)
Xinghua Caob1239382016-12-13 15:07:05 +08001656{
1657 // Ensure the compiler is initialized to avoid race conditions.
1658 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1659
Jamie Madillbd044ed2017-06-05 12:59:21 -04001660 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(context, mState);
Xinghua Caob1239382016-12-13 15:07:05 +08001661
1662 ShaderExecutableD3D *computeExecutable = nullptr;
Yunchao He85072e82017-11-14 15:43:28 +08001663 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, gl::SHADER_COMPUTE,
Xinghua Caob1239382016-12-13 15:07:05 +08001664 std::vector<D3DVarying>(), false,
1665 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1666
1667 if (computeExecutable == nullptr)
1668 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001669 ERR() << "Error compiling dynamic compute executable:" << std::endl
1670 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001671 }
1672 else
1673 {
1674 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1675 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1676 mComputeExecutable.reset(computeExecutable);
1677 }
1678
1679 return mComputeExecutable.get() != nullptr;
1680}
1681
Jamie Madill9cf9e872017-06-05 12:59:25 -04001682gl::LinkResult ProgramD3D::link(const gl::Context *context,
Jamie Madillc9727f32017-11-07 12:37:07 -05001683 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -04001684 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001685{
Jamie Madillc564c072017-06-01 12:45:42 -04001686 const auto &data = context->getContextState();
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001687
Jamie Madill62d31cb2015-09-11 13:25:51 -04001688 reset();
1689
Jamie Madillbd044ed2017-06-05 12:59:21 -04001690 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001691 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001692 {
Xinghua Caob1239382016-12-13 15:07:05 +08001693 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
Xinghua Cao26143fd2017-11-01 18:19:05 +08001694 mImagesCS.resize(data.getCaps().maxImageUnits);
1695 mReadonlyImagesCS.resize(data.getCaps().maxImageUnits);
Xinghua Caob1239382016-12-13 15:07:05 +08001696
Jamie Madillbd044ed2017-06-05 12:59:21 -04001697 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001698
Jamie Madill9cf9e872017-06-05 12:59:25 -04001699 gl::LinkResult result = compileComputeExecutable(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001700 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001701 {
Xinghua Caob1239382016-12-13 15:07:05 +08001702 infoLog << result.getError().getMessage();
1703 return result;
1704 }
1705 else if (!result.getResult())
1706 {
1707 infoLog << "Failed to create D3D compute shader.";
1708 return result;
1709 }
Xinghua Caob1239382016-12-13 15:07:05 +08001710 }
1711 else
1712 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001713 gl::Shader *vertexShader = mState.getAttachedVertexShader();
1714 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001715
1716 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1717 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1718
1719 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1720 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1721
1722 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1723 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1724
1725 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1726 {
1727 if (fragmentShaderD3D->usesFrontFacing())
1728 {
1729 infoLog << "The current renderer doesn't support gl_FrontFacing";
1730 return false;
1731 }
1732 }
1733
Xinghua Caob1239382016-12-13 15:07:05 +08001734 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madillc9727f32017-11-07 12:37:07 -05001735 BuiltinVaryingsD3D builtins(metadata, resources.varyingPacking);
Xinghua Caob1239382016-12-13 15:07:05 +08001736
Jamie Madillc9727f32017-11-07 12:37:07 -05001737 mDynamicHLSL->generateShaderLinkHLSL(context, mState, metadata, resources.varyingPacking,
1738 builtins, &mPixelHLSL, &mVertexHLSL);
Xinghua Caob1239382016-12-13 15:07:05 +08001739
1740 mUsesPointSize = vertexShaderD3D->usesPointSize();
1741 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
Xinghua Cao26143fd2017-11-01 18:19:05 +08001742 mUsesFragDepth = metadata.usesFragDepth();
Martin Radev41ac68e2017-06-06 12:16:58 +03001743 mUsesViewID = metadata.usesViewID();
1744 mHasANGLEMultiviewEnabled = metadata.hasANGLEMultiviewEnabled();
Xinghua Caob1239382016-12-13 15:07:05 +08001745
1746 // Cache if we use flat shading
Jamie Madillbd044ed2017-06-05 12:59:21 -04001747 mUsesFlatInterpolation =
Jiawei Shao3d404882017-10-16 13:30:48 +08001748 (FindFlatInterpolationVarying(fragmentShader->getInputVaryings(context)) ||
1749 FindFlatInterpolationVarying(vertexShader->getOutputVaryings(context)));
Xinghua Caob1239382016-12-13 15:07:05 +08001750
1751 if (mRenderer->getMajorShaderModel() >= 4)
1752 {
Martin Radev41ac68e2017-06-06 12:16:58 +03001753 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(
Jamie Madillc9727f32017-11-07 12:37:07 -05001754 resources.varyingPacking, builtins, mHasANGLEMultiviewEnabled,
Martin Radevc1d4e552017-08-21 12:01:10 +03001755 metadata.canSelectViewInVertexShader());
Xinghua Caob1239382016-12-13 15:07:05 +08001756 }
1757
Jamie Madillbd044ed2017-06-05 12:59:21 -04001758 initAttribLocationsToD3DSemantic(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001759
Jamie Madillbd044ed2017-06-05 12:59:21 -04001760 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001761
Yunchao He85072e82017-11-14 15:43:28 +08001762 gatherTransformFeedbackVaryings(resources.varyingPacking, builtins[gl::SHADER_VERTEX]);
Xinghua Caob1239382016-12-13 15:07:05 +08001763
Jamie Madill9cf9e872017-06-05 12:59:25 -04001764 gl::LinkResult result = compileProgramExecutables(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001765 if (result.isError())
1766 {
1767 infoLog << result.getError().getMessage();
1768 return result;
1769 }
1770 else if (!result.getResult())
1771 {
1772 infoLog << "Failed to create D3D shaders.";
1773 return result;
1774 }
Austin Kinross02df7962015-07-01 10:03:42 -07001775 }
1776
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001777 linkResources(context, resources);
1778
Jamie Madillb0a838b2016-11-13 20:02:12 -05001779 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001780}
1781
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001782GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001783{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001784 // TODO(jmadill): Do something useful here?
1785 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001786}
1787
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001788void ProgramD3D::initializeUniformBlocks()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001789{
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001790 if (mState.getUniformBlocks().empty())
Jamie Madill51f522f2016-12-21 15:10:55 -05001791 {
1792 return;
1793 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001794
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001795 ASSERT(mD3DUniformBlocks.empty());
1796
Jamie Madill62d31cb2015-09-11 13:25:51 -04001797 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001798 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1799 const ShaderD3D *fragmentShaderD3D =
1800 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1801 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001802
Jiajia Qin729b2c62017-08-14 09:36:11 +08001803 for (const gl::InterfaceBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001804 {
1805 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1806
Jamie Madill4a3c2342015-10-08 12:58:45 -04001807 D3DUniformBlock d3dUniformBlock;
1808
Olli Etuaho107c7242018-03-20 15:45:35 +02001809 if (uniformBlock.vertexActive)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001810 {
Xinghua Caob1239382016-12-13 15:07:05 +08001811 ASSERT(vertexShaderD3D != nullptr);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001812 unsigned int baseRegister = vertexShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001813 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001814 }
1815
Olli Etuaho107c7242018-03-20 15:45:35 +02001816 if (uniformBlock.fragmentActive)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001817 {
Xinghua Caob1239382016-12-13 15:07:05 +08001818 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001819 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001820 fragmentShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001821 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001822 }
1823
Olli Etuaho107c7242018-03-20 15:45:35 +02001824 if (uniformBlock.computeActive)
Xinghua Caob1239382016-12-13 15:07:05 +08001825 {
1826 ASSERT(computeShaderD3D != nullptr);
1827 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001828 computeShaderD3D->getUniformBlockRegister(uniformBlock.name);
Xinghua Caob1239382016-12-13 15:07:05 +08001829 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1830 }
1831
Jamie Madill4a3c2342015-10-08 12:58:45 -04001832 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001833 }
1834}
1835
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001836void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001837{
1838 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001839 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001840 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001841 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001842 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001843 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001844 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001845 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001846 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001847 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001848 vertexRegisters = std::max(vertexRegisters,
1849 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001850 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001851 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001852 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001853 fragmentRegisters = std::max(
1854 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001855 }
Xinghua Caob1239382016-12-13 15:07:05 +08001856 if (d3dUniform->isReferencedByComputeShader())
1857 {
1858 computeRegisters = std::max(
1859 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1860 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001861 }
1862 }
1863
Xinghua Caob1239382016-12-13 15:07:05 +08001864 mVertexUniformStorage =
1865 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1866 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1867 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1868 mComputeUniformStorage =
1869 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001870
1871 // Iterate the uniforms again to assign data pointers to default block uniforms.
1872 for (D3DUniform *d3dUniform : mD3DUniforms)
1873 {
1874 if (d3dUniform->isSampler())
1875 {
Olli Etuaho465835d2017-09-26 13:34:10 +03001876 d3dUniform->mSamplerData.resize(d3dUniform->getArraySizeProduct(), 0);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001877 continue;
1878 }
1879
1880 if (d3dUniform->isReferencedByVertexShader())
1881 {
1882 d3dUniform->vsData = mVertexUniformStorage->getDataPointer(d3dUniform->vsRegisterIndex,
1883 d3dUniform->registerElement);
1884 }
1885
1886 if (d3dUniform->isReferencedByFragmentShader())
1887 {
1888 d3dUniform->psData = mFragmentUniformStorage->getDataPointer(
1889 d3dUniform->psRegisterIndex, d3dUniform->registerElement);
1890 }
1891
1892 if (d3dUniform->isReferencedByComputeShader())
1893 {
1894 d3dUniform->csData = mComputeUniformStorage->getDataPointer(
1895 d3dUniform->csRegisterIndex, d3dUniform->registerElement);
1896 }
1897 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001898}
1899
Jamie Madilld63961d2017-09-12 15:22:57 -04001900void ProgramD3D::updateUniformBufferCache(const gl::Caps &caps,
1901 unsigned int reservedVertex,
1902 unsigned int reservedFragment)
Brandon Jones18bd4102014-09-22 14:21:44 -07001903{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001904 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001905 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001906 return;
Jamie Madill4a3c2342015-10-08 12:58:45 -04001907 }
1908
Jamie Madill03260fa2015-06-22 13:57:22 -04001909 mVertexUBOCache.clear();
1910 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001911
Jamie Madill4a3c2342015-10-08 12:58:45 -04001912 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001913 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001914 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001915 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001916 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001917
Brandon Jones18bd4102014-09-22 14:21:44 -07001918 // Unnecessary to apply an unreferenced standard or shared UBO
Olli Etuaho107c7242018-03-20 15:45:35 +02001919 if (!uniformBlock.vertexActive() && !uniformBlock.fragmentActive())
Brandon Jones18bd4102014-09-22 14:21:44 -07001920 {
1921 continue;
1922 }
1923
Olli Etuaho107c7242018-03-20 15:45:35 +02001924 if (uniformBlock.vertexActive())
Brandon Jones18bd4102014-09-22 14:21:44 -07001925 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001926 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedVertex;
1927 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001928
Jamie Madill969194d2015-07-20 14:36:56 -04001929 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001930 {
1931 mVertexUBOCache.resize(registerIndex + 1, -1);
1932 }
1933
1934 ASSERT(mVertexUBOCache[registerIndex] == -1);
1935 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001936 }
1937
Olli Etuaho107c7242018-03-20 15:45:35 +02001938 if (uniformBlock.fragmentActive())
Brandon Jones18bd4102014-09-22 14:21:44 -07001939 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001940 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedFragment;
1941 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001942
1943 if (mFragmentUBOCache.size() <= registerIndex)
1944 {
1945 mFragmentUBOCache.resize(registerIndex + 1, -1);
1946 }
1947
1948 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1949 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001950 }
1951 }
Jamie Madilld63961d2017-09-12 15:22:57 -04001952}
Brandon Jones18bd4102014-09-22 14:21:44 -07001953
Jamie Madilld63961d2017-09-12 15:22:57 -04001954const std::vector<GLint> &ProgramD3D::getVertexUniformBufferCache() const
1955{
1956 return mVertexUBOCache;
1957}
1958
1959const std::vector<GLint> &ProgramD3D::getFragmentUniformBufferCache() const
1960{
1961 return mFragmentUBOCache;
Brandon Jones18bd4102014-09-22 14:21:44 -07001962}
1963
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001965{
Jamie Madill4148fd72017-09-14 15:46:20 -04001966 mVertexUniformsDirty = true;
1967 mFragmentUniformsDirty = true;
1968 mComputeUniformsDirty = true;
1969}
1970
1971void ProgramD3D::markUniformsClean()
1972{
1973 mVertexUniformsDirty = false;
1974 mFragmentUniformsDirty = false;
1975 mComputeUniformsDirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001976}
1977
Jamie Madill334d6152015-10-22 14:00:28 -04001978void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001979{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001980 setUniformInternal(location, count, v, GL_FLOAT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001981}
1982
1983void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1984{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001985 setUniformInternal(location, count, v, GL_FLOAT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001986}
1987
1988void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1989{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001990 setUniformInternal(location, count, v, GL_FLOAT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001991}
1992
1993void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1994{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001995 setUniformInternal(location, count, v, GL_FLOAT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001996}
1997
Jamie Madill334d6152015-10-22 14:00:28 -04001998void ProgramD3D::setUniformMatrix2fv(GLint location,
1999 GLsizei count,
2000 GLboolean transpose,
2001 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002002{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002003 setUniformMatrixfvInternal<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002004}
2005
Jamie Madill334d6152015-10-22 14:00:28 -04002006void ProgramD3D::setUniformMatrix3fv(GLint location,
2007 GLsizei count,
2008 GLboolean transpose,
2009 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002010{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002011 setUniformMatrixfvInternal<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002012}
2013
Jamie Madill334d6152015-10-22 14:00:28 -04002014void ProgramD3D::setUniformMatrix4fv(GLint location,
2015 GLsizei count,
2016 GLboolean transpose,
2017 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002019 setUniformMatrixfvInternal<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002020}
2021
Jamie Madill334d6152015-10-22 14:00:28 -04002022void ProgramD3D::setUniformMatrix2x3fv(GLint location,
2023 GLsizei count,
2024 GLboolean transpose,
2025 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002026{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002027 setUniformMatrixfvInternal<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002028}
2029
Jamie Madill334d6152015-10-22 14:00:28 -04002030void ProgramD3D::setUniformMatrix3x2fv(GLint location,
2031 GLsizei count,
2032 GLboolean transpose,
2033 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002034{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002035 setUniformMatrixfvInternal<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002036}
2037
Jamie Madill334d6152015-10-22 14:00:28 -04002038void ProgramD3D::setUniformMatrix2x4fv(GLint location,
2039 GLsizei count,
2040 GLboolean transpose,
2041 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002042{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002043 setUniformMatrixfvInternal<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002044}
2045
Jamie Madill334d6152015-10-22 14:00:28 -04002046void ProgramD3D::setUniformMatrix4x2fv(GLint location,
2047 GLsizei count,
2048 GLboolean transpose,
2049 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002050{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002051 setUniformMatrixfvInternal<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002052}
2053
Jamie Madill334d6152015-10-22 14:00:28 -04002054void ProgramD3D::setUniformMatrix3x4fv(GLint location,
2055 GLsizei count,
2056 GLboolean transpose,
2057 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002058{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002059 setUniformMatrixfvInternal<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002060}
2061
Jamie Madill334d6152015-10-22 14:00:28 -04002062void ProgramD3D::setUniformMatrix4x3fv(GLint location,
2063 GLsizei count,
2064 GLboolean transpose,
2065 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002066{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002067 setUniformMatrixfvInternal<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002068}
2069
2070void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
2071{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002072 setUniformInternal(location, count, v, GL_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002073}
2074
2075void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2076{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002077 setUniformInternal(location, count, v, GL_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002078}
2079
2080void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2081{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002082 setUniformInternal(location, count, v, GL_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002083}
2084
2085void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2086{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002087 setUniformInternal(location, count, v, GL_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002088}
2089
2090void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2091{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002092 setUniformInternal(location, count, v, GL_UNSIGNED_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002093}
2094
2095void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2096{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002097 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002098}
2099
2100void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2101{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002102 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002103}
2104
2105void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2106{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002107 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108}
2109
Jamie Madillf4141212017-12-12 15:08:07 -05002110void ProgramD3D::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04002111{
Jamie Madillf4141212017-12-12 15:08:07 -05002112 mRenderer->onDirtyUniformBlockBinding(uniformBlockIndex);
Geoff Lang5d124a62015-09-15 13:03:27 -04002113}
2114
Jamie Madillbd044ed2017-06-05 12:59:21 -04002115void ProgramD3D::defineUniformsAndAssignRegisters(const gl::Context *context)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002116{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002117 D3DUniformMap uniformMap;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002118 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08002119 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05002120 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002121 for (const sh::Uniform &computeUniform : computeShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04002122 {
Olli Etuaho107c7242018-03-20 15:45:35 +02002123 if (computeUniform.active)
Xinghua Caob1239382016-12-13 15:07:05 +08002124 {
2125 defineUniformBase(computeShader, computeUniform, &uniformMap);
2126 }
Jamie Madillfb536032015-09-11 13:19:49 -04002127 }
2128 }
Xinghua Caob1239382016-12-13 15:07:05 +08002129 else
Jamie Madillfb536032015-09-11 13:19:49 -04002130 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002131 gl::Shader *vertexShader = mState.getAttachedVertexShader();
2132 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04002133 {
Olli Etuaho107c7242018-03-20 15:45:35 +02002134 if (vertexUniform.active)
Xinghua Caob1239382016-12-13 15:07:05 +08002135 {
2136 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
2137 }
2138 }
2139
Jamie Madillbd044ed2017-06-05 12:59:21 -04002140 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
2141 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms(context))
Xinghua Caob1239382016-12-13 15:07:05 +08002142 {
Olli Etuaho107c7242018-03-20 15:45:35 +02002143 if (fragmentUniform.active)
Xinghua Caob1239382016-12-13 15:07:05 +08002144 {
2145 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
2146 }
Jamie Madillfb536032015-09-11 13:19:49 -04002147 }
2148 }
2149
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002150 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002151 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002152 {
2153 if (!glUniform.isInDefaultBlock())
2154 continue;
2155
Olli Etuahod2551232017-10-26 20:03:33 +03002156 std::string name = glUniform.name;
2157 if (glUniform.isArray())
2158 {
2159 // In the program state, array uniform names include [0] as in the program resource
2160 // spec. Here we don't include it.
2161 // TODO(oetuaho@nvidia.com): consider using the same uniform naming here as in the GL
2162 // layer.
2163 ASSERT(angle::EndsWith(name, "[0]"));
2164 name.resize(name.length() - 3);
2165 }
2166 auto mapEntry = uniformMap.find(name);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002167 ASSERT(mapEntry != uniformMap.end());
2168 mD3DUniforms.push_back(mapEntry->second);
2169 }
2170
Jamie Madill62d31cb2015-09-11 13:25:51 -04002171 assignAllSamplerRegisters();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002172 assignAllImageRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04002173 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04002174}
2175
Jamie Madill91445bc2015-09-23 16:47:53 -04002176void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002177 const sh::Uniform &uniform,
2178 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04002179{
Xinghua Cao26143fd2017-11-01 18:19:05 +08002180 // Samplers get their registers assigned in assignAllSamplerRegisters, and images get their
2181 // registers assigned in assignAllImageRegisters.
2182 if (gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04002183 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002184 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::Texture, nullptr,
2185 uniformMap);
2186 return;
2187 }
2188 else if (gl::IsImageType(uniform.type))
2189 {
2190 if (uniform.readonly)
2191 {
2192 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::Texture,
2193 nullptr, uniformMap);
2194 }
2195 else
2196 {
2197 defineUniform(shader->getType(), uniform, uniform.name,
2198 HLSLRegisterType::UnorderedAccessView, nullptr, uniformMap);
2199 }
2200 mImageBindingMap[uniform.name] = uniform.binding;
2201 return;
2202 }
2203 else if (uniform.isBuiltIn())
2204 {
2205 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::None, nullptr,
2206 uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04002207 return;
2208 }
2209
Jamie Madill91445bc2015-09-23 16:47:53 -04002210 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
Jamie Madill91445bc2015-09-23 16:47:53 -04002211 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
Xinghua Cao26143fd2017-11-01 18:19:05 +08002212 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04002213 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002214 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002215
Xinghua Cao26143fd2017-11-01 18:19:05 +08002216 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::None, &encoder,
2217 uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002218}
2219
Jamie Madill62d31cb2015-09-11 13:25:51 -04002220D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
2221{
2222 for (D3DUniform *d3dUniform : mD3DUniforms)
2223 {
2224 if (d3dUniform->name == name)
2225 {
2226 return d3dUniform;
2227 }
2228 }
2229
2230 return nullptr;
2231}
2232
Olli Etuaho465835d2017-09-26 13:34:10 +03002233void ProgramD3D::defineStructUniformFields(GLenum shaderType,
2234 const std::vector<sh::ShaderVariable> &fields,
2235 const std::string &namePrefix,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002236 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002237 sh::HLSLBlockEncoder *encoder,
2238 D3DUniformMap *uniformMap)
2239{
2240 if (encoder)
2241 encoder->enterAggregateType();
2242
2243 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
2244 {
2245 const sh::ShaderVariable &field = fields[fieldIndex];
2246 const std::string &fieldFullName = (namePrefix + "." + field.name);
2247
2248 // Samplers get their registers assigned in assignAllSamplerRegisters.
2249 // Also they couldn't use the same encoder as the rest of the struct, since they are
2250 // extracted out of the struct by the shader translator.
2251 if (gl::IsSamplerType(field.type))
2252 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002253 defineUniform(shaderType, field, fieldFullName, regType, nullptr, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002254 }
2255 else
2256 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002257 defineUniform(shaderType, field, fieldFullName, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002258 }
2259 }
2260
2261 if (encoder)
2262 encoder->exitAggregateType();
2263}
2264
2265void ProgramD3D::defineArrayOfStructsUniformFields(GLenum shaderType,
2266 const sh::ShaderVariable &uniform,
2267 unsigned int arrayNestingIndex,
2268 const std::string &prefix,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002269 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002270 sh::HLSLBlockEncoder *encoder,
2271 D3DUniformMap *uniformMap)
2272{
2273 // Nested arrays are processed starting from outermost (arrayNestingIndex 0u) and ending at the
2274 // innermost.
2275 const unsigned int currentArraySize = uniform.getNestedArraySize(arrayNestingIndex);
2276 for (unsigned int arrayElement = 0u; arrayElement < currentArraySize; ++arrayElement)
2277 {
2278 const std::string &elementString = prefix + ArrayString(arrayElement);
2279 if (arrayNestingIndex + 1u < uniform.arraySizes.size())
2280 {
2281 defineArrayOfStructsUniformFields(shaderType, uniform, arrayNestingIndex + 1u,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002282 elementString, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002283 }
2284 else
2285 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002286 defineStructUniformFields(shaderType, uniform.fields, elementString, regType, encoder,
Olli Etuaho465835d2017-09-26 13:34:10 +03002287 uniformMap);
2288 }
2289 }
2290}
2291
2292void ProgramD3D::defineArrayUniformElements(GLenum shaderType,
2293 const sh::ShaderVariable &uniform,
2294 const std::string &fullName,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002295 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002296 sh::HLSLBlockEncoder *encoder,
2297 D3DUniformMap *uniformMap)
2298{
2299 if (encoder)
2300 encoder->enterAggregateType();
2301
2302 sh::ShaderVariable uniformElement = uniform;
2303 uniformElement.arraySizes.pop_back();
2304 for (unsigned int arrayIndex = 0u; arrayIndex < uniform.getOutermostArraySize(); ++arrayIndex)
2305 {
2306 std::string elementFullName = fullName + ArrayString(arrayIndex);
Xinghua Cao26143fd2017-11-01 18:19:05 +08002307 defineUniform(shaderType, uniformElement, elementFullName, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002308 }
2309
2310 if (encoder)
2311 encoder->exitAggregateType();
2312}
2313
Jamie Madill91445bc2015-09-23 16:47:53 -04002314void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002315 const sh::ShaderVariable &uniform,
2316 const std::string &fullName,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002317 const HLSLRegisterType regType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002318 sh::HLSLBlockEncoder *encoder,
2319 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002320{
2321 if (uniform.isStruct())
2322 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002323 if (uniform.isArray())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002324 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002325 defineArrayOfStructsUniformFields(shaderType, uniform, 0u, fullName, regType, encoder,
Olli Etuaho465835d2017-09-26 13:34:10 +03002326 uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002327 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002328 else
2329 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002330 defineStructUniformFields(shaderType, uniform.fields, fullName, regType, encoder,
2331 uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002332 }
2333 return;
2334 }
2335 if (uniform.isArrayOfArrays())
2336 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002337 defineArrayUniformElements(shaderType, uniform, fullName, regType, encoder, uniformMap);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002338 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002339 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002340
2341 // Not a struct. Arrays are treated as aggregate types.
2342 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002343 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002344 encoder->enterAggregateType();
2345 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002346
Jamie Madill62d31cb2015-09-11 13:25:51 -04002347 // Advance the uniform offset, to track registers allocation for structs
2348 sh::BlockMemberInfo blockInfo =
Olli Etuaho465835d2017-09-26 13:34:10 +03002349 encoder ? encoder->encodeType(uniform.type, uniform.arraySizes, false)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002350 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002351
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002352 auto uniformMapEntry = uniformMap->find(fullName);
2353 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002354
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002355 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002356 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002357 d3dUniform = uniformMapEntry->second;
2358 }
2359 else
2360 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002361 d3dUniform = new D3DUniform(uniform.type, regType, fullName, uniform.arraySizes, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002362 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002363 }
2364
2365 if (encoder)
2366 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002367 d3dUniform->registerElement =
2368 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002369 unsigned int reg =
2370 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002371 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002372 {
2373 d3dUniform->psRegisterIndex = reg;
2374 }
Xinghua Caob1239382016-12-13 15:07:05 +08002375 else if (shaderType == GL_VERTEX_SHADER)
2376 {
2377 d3dUniform->vsRegisterIndex = reg;
2378 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002379 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002380 {
Xinghua Caob1239382016-12-13 15:07:05 +08002381 ASSERT(shaderType == GL_COMPUTE_SHADER);
2382 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002383 }
Jamie Madillfb536032015-09-11 13:19:49 -04002384
2385 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002386 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002387 {
2388 encoder->exitAggregateType();
2389 }
2390 }
2391}
2392
Jamie Madill134f93d2017-08-31 17:11:00 -04002393// Assume count is already clamped.
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002394template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002395void ProgramD3D::setUniformImpl(const gl::VariableLocation &locationInfo,
Jamie Madill134f93d2017-08-31 17:11:00 -04002396 GLsizei count,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002397 const T *v,
2398 uint8_t *targetData,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002399 GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002400{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002401 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
Jamie Madill33bb7c42017-09-09 23:32:51 -04002402 const int components = targetUniform->typeInfo.componentCount;
Olli Etuaho1734e172017-10-27 15:30:27 +03002403 const unsigned int arrayElementOffset = locationInfo.arrayIndex;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002404
Jamie Madill33bb7c42017-09-09 23:32:51 -04002405 if (targetUniform->typeInfo.type == uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002406 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002407 T *dest = reinterpret_cast<T *>(targetData) + arrayElementOffset * 4;
Jamie Madill33bb7c42017-09-09 23:32:51 -04002408 const T *source = v;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002409
Jamie Madill33bb7c42017-09-09 23:32:51 -04002410 for (GLint i = 0; i < count; i++, dest += 4, source += components)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002411 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002412 memcpy(dest, source, components * sizeof(T));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002413 }
2414 }
Jamie Madill33bb7c42017-09-09 23:32:51 -04002415 else
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002416 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002417 ASSERT(targetUniform->typeInfo.type == gl::VariableBoolVectorType(uniformType));
Olli Etuahoc8538042017-09-27 11:20:15 +03002418 GLint *boolParams = reinterpret_cast<GLint *>(targetData) + arrayElementOffset * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002419
Jamie Madill134f93d2017-08-31 17:11:00 -04002420 for (GLint i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002421 {
Jamie Madill334d6152015-10-22 14:00:28 -04002422 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002423 const T *source = v + (i * components);
2424
2425 for (int c = 0; c < components; c++)
2426 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002427 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002428 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002429 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002430 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002431}
2432
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002433template <typename T>
Jamie Madill33bb7c42017-09-09 23:32:51 -04002434void ProgramD3D::setUniformInternal(GLint location, GLsizei count, const T *v, GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002435{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002436 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2437 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
2438
Jamie Madill33bb7c42017-09-09 23:32:51 -04002439 if (targetUniform->typeInfo.isSampler)
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002440 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002441 ASSERT(uniformType == GL_INT);
Jamie Madill80823cc2017-09-14 15:46:21 -04002442 size_t size = count * sizeof(T);
Jamie Madillacf2f3a2017-11-21 19:22:44 -05002443 GLint *dest = &targetUniform->mSamplerData[locationInfo.arrayIndex];
Jamie Madill80823cc2017-09-14 15:46:21 -04002444 if (memcmp(dest, v, size) != 0)
2445 {
2446 memcpy(dest, v, size);
2447 mDirtySamplerMapping = true;
2448 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002449 return;
2450 }
2451
2452 if (targetUniform->vsData)
2453 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002454 setUniformImpl(locationInfo, count, v, targetUniform->vsData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002455 mVertexUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002456 }
2457
2458 if (targetUniform->psData)
2459 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002460 setUniformImpl(locationInfo, count, v, targetUniform->psData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002461 mFragmentUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002462 }
2463
2464 if (targetUniform->csData)
2465 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002466 setUniformImpl(locationInfo, count, v, targetUniform->csData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002467 mComputeUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002468 }
2469}
2470
2471template <int cols, int rows>
Jamie Madill80823cc2017-09-14 15:46:21 -04002472bool ProgramD3D::setUniformMatrixfvImpl(GLint location,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002473 GLsizei countIn,
2474 GLboolean transpose,
2475 const GLfloat *value,
2476 uint8_t *targetData,
2477 GLenum targetUniformType)
2478{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002479 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002480
Olli Etuaho465835d2017-09-26 13:34:10 +03002481 unsigned int elementCount = targetUniform->getArraySizeProduct();
Olli Etuaho1734e172017-10-27 15:30:27 +03002482 unsigned int arrayElementOffset = mState.getUniformLocations()[location].arrayIndex;
Olli Etuahoc8538042017-09-27 11:20:15 +03002483 unsigned int count =
2484 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002485
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002486 const unsigned int targetMatrixStride = (4 * rows);
Olli Etuahoc8538042017-09-27 11:20:15 +03002487 GLfloat *target = reinterpret_cast<GLfloat *>(
2488 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002489
Jamie Madill80823cc2017-09-14 15:46:21 -04002490 bool dirty = false;
2491
Jamie Madill62d31cb2015-09-11 13:25:51 -04002492 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002493 {
2494 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2495 if (transpose == GL_FALSE)
2496 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002497 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002498 }
2499 else
2500 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002501 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002502 }
2503 target += targetMatrixStride;
2504 value += cols * rows;
2505 }
Jamie Madill80823cc2017-09-14 15:46:21 -04002506
2507 return dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002508}
2509
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002510template <int cols, int rows>
2511void ProgramD3D::setUniformMatrixfvInternal(GLint location,
2512 GLsizei countIn,
2513 GLboolean transpose,
2514 const GLfloat *value,
2515 GLenum targetUniformType)
2516{
2517 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
2518
2519 if (targetUniform->vsData)
2520 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002521 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2522 targetUniform->vsData, targetUniformType))
2523 {
2524 mVertexUniformsDirty = true;
2525 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002526 }
2527
2528 if (targetUniform->psData)
2529 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002530 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2531 targetUniform->psData, targetUniformType))
2532 {
2533 mFragmentUniformsDirty = true;
2534 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002535 }
2536
2537 if (targetUniform->csData)
2538 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002539 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2540 targetUniform->csData, targetUniformType))
2541 {
2542 mComputeUniformsDirty = true;
2543 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002544 }
2545}
2546
Jamie Madill62d31cb2015-09-11 13:25:51 -04002547void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002548{
Olli Etuaho465835d2017-09-26 13:34:10 +03002549 for (size_t uniformIndex = 0; uniformIndex < mD3DUniforms.size(); ++uniformIndex)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002550 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002551 if (mD3DUniforms[uniformIndex]->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002552 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002553 assignSamplerRegisters(uniformIndex);
Jamie Madillfb536032015-09-11 13:19:49 -04002554 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002555 }
2556}
2557
Olli Etuaho465835d2017-09-26 13:34:10 +03002558void ProgramD3D::assignSamplerRegisters(size_t uniformIndex)
Jamie Madillfb536032015-09-11 13:19:49 -04002559{
Olli Etuaho465835d2017-09-26 13:34:10 +03002560 D3DUniform *d3dUniform = mD3DUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002561 ASSERT(d3dUniform->isSampler());
Olli Etuaho465835d2017-09-26 13:34:10 +03002562 // If the uniform is an array of arrays, then we have separate entries for each inner array in
2563 // mD3DUniforms. However, the sampler register info is stored in the shader only for the
2564 // outermost array.
2565 std::vector<unsigned int> subscripts;
2566 const std::string baseName = gl::ParseResourceName(d3dUniform->name, &subscripts);
2567 unsigned int registerOffset = mState.getUniforms()[uniformIndex].flattenedOffsetInParentArrays *
2568 d3dUniform->getArraySizeProduct();
2569
Xinghua Caob1239382016-12-13 15:07:05 +08002570 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2571 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002572 {
Xinghua Caob1239382016-12-13 15:07:05 +08002573 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Olli Etuaho465835d2017-09-26 13:34:10 +03002574 ASSERT(computeShaderD3D->hasUniform(baseName));
2575 d3dUniform->csRegisterIndex =
2576 computeShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002577 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002578 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->typeInfo,
2579 d3dUniform->getArraySizeProduct(), mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002580 }
Xinghua Caob1239382016-12-13 15:07:05 +08002581 else
Jamie Madillfb536032015-09-11 13:19:49 -04002582 {
Xinghua Caob1239382016-12-13 15:07:05 +08002583 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2584 const ShaderD3D *fragmentShaderD3D =
2585 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho465835d2017-09-26 13:34:10 +03002586 ASSERT(vertexShaderD3D->hasUniform(baseName) || fragmentShaderD3D->hasUniform(baseName));
2587 if (vertexShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002588 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002589 d3dUniform->vsRegisterIndex =
2590 vertexShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002591 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002592 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->typeInfo,
2593 d3dUniform->getArraySizeProduct(), mSamplersVS,
2594 &mUsedVertexSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002595 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002596 if (fragmentShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002597 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002598 d3dUniform->psRegisterIndex =
2599 fragmentShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002600 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002601 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->typeInfo,
2602 d3dUniform->getArraySizeProduct(), mSamplersPS, &mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002603 }
Jamie Madillfb536032015-09-11 13:19:49 -04002604 }
2605}
2606
Jamie Madill62d31cb2015-09-11 13:25:51 -04002607// static
2608void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002609 const gl::UniformTypeInfo &typeInfo,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002610 unsigned int samplerCount,
2611 std::vector<Sampler> &outSamplers,
2612 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002613{
2614 unsigned int samplerIndex = startSamplerIndex;
2615
2616 do
2617 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002618 ASSERT(samplerIndex < outSamplers.size());
2619 Sampler *sampler = &outSamplers[samplerIndex];
2620 sampler->active = true;
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002621 sampler->textureType = gl::FromGLenum<gl::TextureType>(typeInfo.textureType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002622 sampler->logicalTextureUnit = 0;
2623 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002624 samplerIndex++;
2625 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002626}
2627
Xinghua Cao26143fd2017-11-01 18:19:05 +08002628void ProgramD3D::assignAllImageRegisters()
2629{
2630 for (size_t uniformIndex = 0; uniformIndex < mD3DUniforms.size(); ++uniformIndex)
2631 {
2632 if (mD3DUniforms[uniformIndex]->isImage())
2633 {
2634 assignImageRegisters(uniformIndex);
2635 }
2636 }
2637}
2638
2639void ProgramD3D::assignImageRegisters(size_t uniformIndex)
2640{
2641 D3DUniform *d3dUniform = mD3DUniforms[uniformIndex];
2642 ASSERT(d3dUniform->isImage());
2643 // If the uniform is an array of arrays, then we have separate entries for each inner array in
2644 // mD3DUniforms. However, the image register info is stored in the shader only for the
2645 // outermost array.
2646 std::vector<unsigned int> subscripts;
2647 const std::string baseName = gl::ParseResourceName(d3dUniform->name, &subscripts);
2648 unsigned int registerOffset = mState.getUniforms()[uniformIndex].flattenedOffsetInParentArrays *
2649 d3dUniform->getArraySizeProduct();
2650
2651 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2652 if (computeShader)
2653 {
2654 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2655 ASSERT(computeShaderD3D->hasUniform(baseName));
2656 d3dUniform->csRegisterIndex =
2657 computeShaderD3D->getUniformRegister(baseName) + registerOffset;
2658 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2659 auto bindingIter = mImageBindingMap.find(baseName);
2660 ASSERT(bindingIter != mImageBindingMap.end());
2661 if (d3dUniform->regType == HLSLRegisterType::Texture)
2662 {
2663 AssignImages(d3dUniform->csRegisterIndex, bindingIter->second,
2664 d3dUniform->getArraySizeProduct(), mReadonlyImagesCS,
2665 &mUsedComputeReadonlyImageRange);
2666 }
2667 else if (d3dUniform->regType == HLSLRegisterType::UnorderedAccessView)
2668 {
2669 AssignImages(d3dUniform->csRegisterIndex, bindingIter->second,
2670 d3dUniform->getArraySizeProduct(), mImagesCS, &mUsedComputeImageRange);
2671 }
2672 else
2673 {
2674 UNREACHABLE();
2675 }
2676 }
2677 else
2678 {
2679 // TODO(xinghua.cao@intel.com): Implement image variables in vertex shader and pixel shader.
2680 UNIMPLEMENTED();
2681 }
2682}
2683
2684// static
2685void ProgramD3D::AssignImages(unsigned int startImageIndex,
2686 int startLogicalImageUnit,
2687 unsigned int imageCount,
2688 std::vector<Image> &outImages,
2689 GLuint *outUsedRange)
2690{
2691 unsigned int imageIndex = startImageIndex;
2692 // If declare without a binding qualifier, any uniform image variable (include all elements of
2693 // unbound image array) shoud be bound to unit zero.
2694 if (startLogicalImageUnit == -1)
2695 {
2696 ASSERT(imageIndex < outImages.size());
2697 Image *image = &outImages[imageIndex];
2698 image->active = true;
2699 image->logicalImageUnit = 0;
2700 *outUsedRange = std::max(imageIndex + 1, *outUsedRange);
2701 return;
2702 }
2703
2704 unsigned int logcalImageUnit = startLogicalImageUnit;
2705 do
2706 {
2707 ASSERT(imageIndex < outImages.size());
2708 Image *image = &outImages[imageIndex];
2709 image->active = true;
2710 image->logicalImageUnit = logcalImageUnit;
2711 *outUsedRange = std::max(imageIndex + 1, *outUsedRange);
2712 imageIndex++;
2713 logcalImageUnit++;
2714 } while (imageIndex < startImageIndex + imageCount);
2715}
2716
Brandon Jonesc9610c52014-08-25 17:02:59 -07002717void ProgramD3D::reset()
2718{
Xinghua Caob1239382016-12-13 15:07:05 +08002719 mVertexExecutables.clear();
2720 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002721
Xinghua Caob1239382016-12-13 15:07:05 +08002722 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002723 {
Xinghua Caob1239382016-12-13 15:07:05 +08002724 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002725 }
Brandon Joneseb994362014-09-24 10:27:28 -07002726
Xinghua Caob1239382016-12-13 15:07:05 +08002727 mComputeExecutable.reset(nullptr);
2728
Brandon Jones22502d52014-08-29 16:58:36 -07002729 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002730 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002731
2732 mPixelHLSL.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002733 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
2734 mUsesFragDepth = false;
Martin Radev41ac68e2017-06-06 12:16:58 +03002735 mHasANGLEMultiviewEnabled = false;
2736 mUsesViewID = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002737 mPixelShaderKey.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002738 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002739 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002740
Jamie Madill62d31cb2015-09-11 13:25:51 -04002741 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002742 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002743
Xinghua Caob1239382016-12-13 15:07:05 +08002744 mVertexUniformStorage.reset(nullptr);
2745 mFragmentUniformStorage.reset(nullptr);
2746 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002747
2748 mSamplersPS.clear();
2749 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002750 mSamplersCS.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002751 mImagesCS.clear();
2752 mReadonlyImagesCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002753
Xinghua Cao26143fd2017-11-01 18:19:05 +08002754 mUsedVertexSamplerRange = 0;
2755 mUsedPixelSamplerRange = 0;
2756 mUsedComputeSamplerRange = 0;
2757 mDirtySamplerMapping = true;
2758 mUsedComputeImageRange = 0;
2759 mUsedComputeReadonlyImageRange = 0;
Jamie Madill437d2662014-12-05 14:23:35 -05002760
Jamie Madill8047c0d2016-03-07 13:02:12 -05002761 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002762
Jamie Madill9fc36822015-11-18 13:08:07 -05002763 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002764
2765 mGeometryShaderPreamble.clear();
Jamie Madill561ed3a2017-08-31 16:48:09 -04002766
Jamie Madill4148fd72017-09-14 15:46:20 -04002767 dirtyAllUniforms();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002768
2769 mCachedPixelExecutableIndex.reset();
2770 mCachedVertexExecutableIndex.reset();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002771}
2772
Geoff Lang7dd2e102014-11-10 15:19:26 -05002773unsigned int ProgramD3D::getSerial() const
2774{
2775 return mSerial;
2776}
2777
2778unsigned int ProgramD3D::issueSerial()
2779{
2780 return mCurrentSerial++;
2781}
2782
Jamie Madillbd044ed2017-06-05 12:59:21 -04002783void ProgramD3D::initAttribLocationsToD3DSemantic(const gl::Context *context)
Jamie Madill63805b42015-08-25 13:17:39 -04002784{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002785 gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002786 ASSERT(vertexShader != nullptr);
2787
2788 // Init semantic index
Jamie Madill34ca4f52017-06-13 11:49:39 -04002789 int semanticIndex = 0;
2790 for (const sh::Attribute &attribute : vertexShader->getActiveAttributes(context))
Jamie Madill63805b42015-08-25 13:17:39 -04002791 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002792 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill34ca4f52017-06-13 11:49:39 -04002793 GLuint location = mState.getAttributeLocation(attribute.name);
2794 ASSERT(location != std::numeric_limits<GLuint>::max());
Jamie Madill63805b42015-08-25 13:17:39 -04002795
Jamie Madill8047c0d2016-03-07 13:02:12 -05002796 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002797 {
Jamie Madill34ca4f52017-06-13 11:49:39 -04002798 mAttribLocationToD3DSemantic[location + reg] = semanticIndex++;
Jamie Madill63805b42015-08-25 13:17:39 -04002799 }
2800 }
Jamie Madill437d2662014-12-05 14:23:35 -05002801}
2802
Jamie Madilla779b612017-07-24 11:46:05 -04002803void ProgramD3D::updateCachedInputLayout(Serial associatedSerial, const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002804{
Jamie Madilla779b612017-07-24 11:46:05 -04002805 if (mCurrentVertexArrayStateSerial == associatedSerial)
2806 {
2807 return;
2808 }
2809
2810 mCurrentVertexArrayStateSerial = associatedSerial;
Jamie Madillbd136f92015-08-10 14:51:37 -04002811 mCachedInputLayout.clear();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002812
Jamie Madilld3dfda22015-07-06 08:28:49 -04002813 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002814
Jamie Madill6de51852017-04-12 09:53:01 -04002815 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002816 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002817 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002818
Jamie Madill8047c0d2016-03-07 13:02:12 -05002819 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002820 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002821 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002822 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002823 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002824 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002825 mCachedInputLayout[d3dSemantic] =
2826 GetVertexFormatType(vertexAttributes[locationIndex],
2827 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002828 }
2829 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002830
2831 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04002832
2833 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002834}
2835
2836void ProgramD3D::updateCachedOutputLayout(const gl::Context *context,
2837 const gl::Framebuffer *framebuffer)
2838{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002839 mPixelShaderOutputLayoutCache.clear();
2840
2841 FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(framebuffer);
2842 const auto &colorbuffers = fboD3D->getColorAttachmentsForRender(context);
2843
2844 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
2845 {
2846 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
2847
2848 if (colorbuffer)
2849 {
2850 auto binding = colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0
2851 : colorbuffer->getBinding();
2852 mPixelShaderOutputLayoutCache.push_back(binding);
2853 }
2854 else
2855 {
2856 mPixelShaderOutputLayoutCache.push_back(GL_NONE);
2857 }
2858 }
2859
2860 updateCachedPixelExecutableIndex();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002861}
2862
Jamie Madill192745a2016-12-22 15:58:21 -05002863void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2864 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002865{
Jamie Madill9fc36822015-11-18 13:08:07 -05002866 const std::string &varyingSemantic =
2867 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2868
Jamie Madillccdf74b2015-08-18 10:46:12 -04002869 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002870 mStreamOutVaryings.clear();
2871
Jamie Madill48ef11b2016-04-27 15:21:52 -04002872 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002873 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2874 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002875 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002876 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2877 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002878 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002879 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002880 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002881 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2882 builtins.glPosition.index, 4, outputSlot));
2883 }
2884 }
2885 else if (tfVaryingName == "gl_FragCoord")
2886 {
2887 if (builtins.glFragCoord.enabled)
2888 {
2889 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2890 builtins.glFragCoord.index, 4, outputSlot));
2891 }
2892 }
2893 else if (tfVaryingName == "gl_PointSize")
2894 {
2895 if (builtins.glPointSize.enabled)
2896 {
2897 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2898 }
2899 }
2900 else
2901 {
Jamie Madill192745a2016-12-22 15:58:21 -05002902 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002903 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002904 const auto &varying = *registerInfo.packedVarying->varying;
2905 GLenum transposedType = gl::TransposeMatrixType(varying.type);
jchen108225e732017-11-14 16:29:03 +08002906 int componentCount = gl::VariableColumnCount(transposedType);
2907 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill55c25d02015-11-18 13:08:08 -05002908
Jamie Madill9fc36822015-11-18 13:08:07 -05002909 // There can be more than one register assigned to a particular varying, and each
2910 // register needs its own stream out entry.
jchen108225e732017-11-14 16:29:03 +08002911 if (registerInfo.tfVaryingName() == tfVaryingName)
Jamie Madill9fc36822015-11-18 13:08:07 -05002912 {
2913 mStreamOutVaryings.push_back(D3DVarying(
2914 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2915 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002916 }
2917 }
2918 }
2919}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002920
2921D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2922{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002923 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002924}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002925
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002926const D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location) const
2927{
2928 return mD3DUniforms[mState.getUniformLocations()[location].index];
2929}
2930
Sami Väisänen46eaa942016-06-29 10:26:37 +03002931void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2932 GLenum genMode,
2933 GLint components,
2934 const GLfloat *coeffs)
2935{
2936 UNREACHABLE();
2937}
2938
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002939bool ProgramD3D::hasVertexExecutableForCachedInputLayout()
2940{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002941 return mCachedVertexExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002942}
2943
2944bool ProgramD3D::hasGeometryExecutableForPrimitiveType(GLenum drawMode)
2945{
2946 if (!usesGeometryShader(drawMode))
2947 {
2948 // No shader necessary mean we have the required (null) executable.
2949 return true;
2950 }
2951
2952 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
2953 return mGeometryExecutables[geometryShaderType].get() != nullptr;
2954}
2955
2956bool ProgramD3D::hasPixelExecutableForCachedOutputLayout()
2957{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002958 return mCachedPixelExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002959}
2960
Jamie Madill54164b02017-08-28 15:17:37 -04002961template <typename DestT>
2962void ProgramD3D::getUniformInternal(GLint location, DestT *dataOut) const
2963{
2964 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2965 const gl::LinkedUniform &uniform = mState.getUniforms()[locationInfo.index];
2966
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002967 const D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Olli Etuaho1734e172017-10-27 15:30:27 +03002968 const uint8_t *srcPointer = targetUniform->getDataPtrToElement(locationInfo.arrayIndex);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002969
2970 if (gl::IsMatrixType(uniform.type))
2971 {
2972 GetMatrixUniform(gl::VariableColumnCount(uniform.type), gl::VariableRowCount(uniform.type),
2973 dataOut, reinterpret_cast<const DestT *>(srcPointer));
2974 }
2975 else
2976 {
2977 memcpy(dataOut, srcPointer, uniform.getElementSize());
2978 }
Jamie Madill54164b02017-08-28 15:17:37 -04002979}
2980
2981void ProgramD3D::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
2982{
2983 getUniformInternal(location, params);
2984}
2985
2986void ProgramD3D::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
2987{
2988 getUniformInternal(location, params);
2989}
2990
2991void ProgramD3D::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
2992{
2993 getUniformInternal(location, params);
2994}
2995
Jamie Madill0e7f1732017-09-09 23:32:50 -04002996void ProgramD3D::updateCachedVertexExecutableIndex()
2997{
2998 mCachedVertexExecutableIndex.reset();
2999 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
3000 {
3001 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
3002 {
3003 mCachedVertexExecutableIndex = executableIndex;
3004 break;
3005 }
3006 }
3007}
3008
3009void ProgramD3D::updateCachedPixelExecutableIndex()
3010{
3011 mCachedPixelExecutableIndex.reset();
3012 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
3013 {
3014 if (mPixelExecutables[executableIndex]->matchesSignature(mPixelShaderOutputLayoutCache))
3015 {
3016 mCachedPixelExecutableIndex = executableIndex;
3017 break;
3018 }
3019 }
3020}
3021
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003022void ProgramD3D::linkResources(const gl::Context *context,
3023 const gl::ProgramLinkedResources &resources)
3024{
3025 UniformBlockInfo uniformBlockInfo;
3026
3027 if (mState.getAttachedVertexShader())
3028 {
3029 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedVertexShader());
3030 }
3031
3032 if (mState.getAttachedFragmentShader())
3033 {
3034 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedFragmentShader());
3035 }
3036
3037 if (mState.getAttachedComputeShader())
3038 {
3039 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedComputeShader());
3040 }
3041
3042 // Gather interface block info.
3043 auto getUniformBlockSize = [&uniformBlockInfo](const std::string &name,
3044 const std::string &mappedName, size_t *sizeOut) {
3045 return uniformBlockInfo.getBlockSize(name, mappedName, sizeOut);
3046 };
3047
3048 auto getUniformBlockMemberInfo = [&uniformBlockInfo](const std::string &name,
3049 const std::string &mappedName,
3050 sh::BlockMemberInfo *infoOut) {
3051 return uniformBlockInfo.getBlockMemberInfo(name, mappedName, infoOut);
3052 };
3053
3054 resources.uniformBlockLinker.linkBlocks(getUniformBlockSize, getUniformBlockMemberInfo);
3055 initializeUniformBlocks();
3056
3057 // TODO(jiajia.qin@intel.com): Determine correct shader storage block info.
3058 auto getShaderStorageBlockSize = [](const std::string &name, const std::string &mappedName,
3059 size_t *sizeOut) {
3060 *sizeOut = 0;
3061 return true;
3062 };
3063
3064 auto getShaderStorageBlockMemberInfo =
3065 [](const std::string &name, const std::string &mappedName, sh::BlockMemberInfo *infoOut) {
3066 *infoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
3067 return true;
3068 };
3069
3070 resources.shaderStorageBlockLinker.linkBlocks(getShaderStorageBlockSize,
3071 getShaderStorageBlockMemberInfo);
3072}
3073
Jamie Madill8047c0d2016-03-07 13:02:12 -05003074} // namespace rx