blob: be1660f773a958241965be71ad7f29d0ab82fbcb [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 {
224 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
225 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{
237 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
238
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
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700597ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
598{
599}
600
Xinghua Cao26143fd2017-11-01 18:19:05 +0800601ProgramD3D::Image::Image() : active(false), logicalImageUnit(0)
602{
603}
604
Geoff Lang7dd2e102014-11-10 15:19:26 -0500605unsigned int ProgramD3D::mCurrentSerial = 1;
606
Jamie Madill48ef11b2016-04-27 15:21:52 -0400607ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
608 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700609 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800610 mDynamicHLSL(nullptr),
611 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
612 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700613 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400614 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800615 mVertexUniformStorage(nullptr),
616 mFragmentUniformStorage(nullptr),
617 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700618 mUsedVertexSamplerRange(0),
619 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800620 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700621 mDirtySamplerMapping(true),
Xinghua Cao26143fd2017-11-01 18:19:05 +0800622 mUsedComputeImageRange(0),
623 mUsedComputeReadonlyImageRange(0),
Jamie Madill561ed3a2017-08-31 16:48:09 -0400624 mSerial(issueSerial()),
Jamie Madill4148fd72017-09-14 15:46:20 -0400625 mVertexUniformsDirty(true),
626 mFragmentUniformsDirty(true),
627 mComputeUniformsDirty(true)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700628{
Brandon Joneseb994362014-09-24 10:27:28 -0700629 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700630}
631
632ProgramD3D::~ProgramD3D()
633{
634 reset();
635 SafeDelete(mDynamicHLSL);
636}
637
Brandon Jones44151a92014-09-10 11:32:25 -0700638bool ProgramD3D::usesPointSpriteEmulation() const
639{
640 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
641}
642
Martin Radev41ac68e2017-06-06 12:16:58 +0300643bool ProgramD3D::usesGeometryShaderForPointSpriteEmulation() const
644{
645 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
646}
647
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400648bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700649{
Martin Radevc1d4e552017-08-21 12:01:10 +0300650 if (mHasANGLEMultiviewEnabled && !mRenderer->canSelectViewInVertexShader())
Martin Radev41ac68e2017-06-06 12:16:58 +0300651 {
652 return true;
653 }
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400654 if (drawMode != GL_POINTS)
655 {
656 return mUsesFlatInterpolation;
657 }
Martin Radev41ac68e2017-06-06 12:16:58 +0300658 return usesGeometryShaderForPointSpriteEmulation();
Cooper Partine6664f02015-01-09 16:22:24 -0800659}
660
661bool ProgramD3D::usesInstancedPointSpriteEmulation() const
662{
663 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700664}
665
Xinghua Cao37584b32017-12-01 11:04:03 +0800666GLint ProgramD3D::getSamplerMapping(gl::ShaderType type,
Jamie Madill334d6152015-10-22 14:00:28 -0400667 unsigned int samplerIndex,
668 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700669{
670 GLint logicalTextureUnit = -1;
671
672 switch (type)
673 {
Xinghua Cao37584b32017-12-01 11:04:03 +0800674 case gl::SHADER_FRAGMENT:
Jamie Madill334d6152015-10-22 14:00:28 -0400675 ASSERT(samplerIndex < caps.maxTextureImageUnits);
676 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
677 {
678 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
679 }
680 break;
Xinghua Cao37584b32017-12-01 11:04:03 +0800681 case gl::SHADER_VERTEX:
Jamie Madill334d6152015-10-22 14:00:28 -0400682 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
683 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
684 {
685 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
686 }
687 break;
Xinghua Cao37584b32017-12-01 11:04:03 +0800688 case gl::SHADER_COMPUTE:
Xinghua Caob1239382016-12-13 15:07:05 +0800689 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
690 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
691 {
692 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
693 }
694 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400695 default:
696 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700697 }
698
Jamie Madill334d6152015-10-22 14:00:28 -0400699 if (logicalTextureUnit >= 0 &&
700 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700701 {
702 return logicalTextureUnit;
703 }
704
705 return -1;
706}
707
708// Returns the texture type for a given Direct3D 9 sampler type and
709// index (0-15 for the pixel shader and 0-3 for the vertex shader).
Xinghua Cao37584b32017-12-01 11:04:03 +0800710GLenum ProgramD3D::getSamplerTextureType(gl::ShaderType type, unsigned int samplerIndex) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700711{
712 switch (type)
713 {
Xinghua Cao37584b32017-12-01 11:04:03 +0800714 case gl::SHADER_FRAGMENT:
Jamie Madill334d6152015-10-22 14:00:28 -0400715 ASSERT(samplerIndex < mSamplersPS.size());
716 ASSERT(mSamplersPS[samplerIndex].active);
717 return mSamplersPS[samplerIndex].textureType;
Xinghua Cao37584b32017-12-01 11:04:03 +0800718 case gl::SHADER_VERTEX:
Jamie Madill334d6152015-10-22 14:00:28 -0400719 ASSERT(samplerIndex < mSamplersVS.size());
720 ASSERT(mSamplersVS[samplerIndex].active);
721 return mSamplersVS[samplerIndex].textureType;
Xinghua Cao37584b32017-12-01 11:04:03 +0800722 case gl::SHADER_COMPUTE:
Xinghua Caob1239382016-12-13 15:07:05 +0800723 ASSERT(samplerIndex < mSamplersCS.size());
724 ASSERT(mSamplersCS[samplerIndex].active);
725 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400726 default:
727 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700728 }
729
730 return GL_TEXTURE_2D;
731}
732
Xinghua Cao37584b32017-12-01 11:04:03 +0800733GLuint ProgramD3D::getUsedSamplerRange(gl::ShaderType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700734{
735 switch (type)
736 {
Xinghua Cao37584b32017-12-01 11:04:03 +0800737 case gl::SHADER_FRAGMENT:
Jamie Madill334d6152015-10-22 14:00:28 -0400738 return mUsedPixelSamplerRange;
Xinghua Cao37584b32017-12-01 11:04:03 +0800739 case gl::SHADER_VERTEX:
Jamie Madill334d6152015-10-22 14:00:28 -0400740 return mUsedVertexSamplerRange;
Xinghua Cao37584b32017-12-01 11:04:03 +0800741 case gl::SHADER_COMPUTE:
Xinghua Caob1239382016-12-13 15:07:05 +0800742 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400743 default:
744 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200745 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700746 }
747}
748
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400749ProgramD3D::SamplerMapping ProgramD3D::updateSamplerMapping()
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700750{
751 if (!mDirtySamplerMapping)
752 {
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400753 return SamplerMapping::WasClean;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700754 }
755
756 mDirtySamplerMapping = false;
757
758 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400759 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700760 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400761 if (!d3dUniform->isSampler())
762 continue;
763
Olli Etuaho465835d2017-09-26 13:34:10 +0300764 int count = d3dUniform->getArraySizeProduct();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400765
766 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700767 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400768 unsigned int firstIndex = d3dUniform->psRegisterIndex;
769
770 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700771 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400772 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700773
Jamie Madill62d31cb2015-09-11 13:25:51 -0400774 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700775 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400776 ASSERT(mSamplersPS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400777 mSamplersPS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700778 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400779 }
780 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700781
Jamie Madill62d31cb2015-09-11 13:25:51 -0400782 if (d3dUniform->isReferencedByVertexShader())
783 {
784 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
785
786 for (int i = 0; i < count; i++)
787 {
788 unsigned int samplerIndex = firstIndex + i;
789
790 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700791 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400792 ASSERT(mSamplersVS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400793 mSamplersVS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700794 }
795 }
796 }
Xinghua Caob1239382016-12-13 15:07:05 +0800797
798 if (d3dUniform->isReferencedByComputeShader())
799 {
800 unsigned int firstIndex = d3dUniform->csRegisterIndex;
801
802 for (int i = 0; i < count; i++)
803 {
804 unsigned int samplerIndex = firstIndex + i;
805
806 if (samplerIndex < mSamplersCS.size())
807 {
808 ASSERT(mSamplersCS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400809 mSamplersCS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Xinghua Caob1239382016-12-13 15:07:05 +0800810 }
811 }
812 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700813 }
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400814
815 return SamplerMapping::WasDirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700816}
817
Xinghua Cao26143fd2017-11-01 18:19:05 +0800818GLint ProgramD3D::getImageMapping(gl::ShaderType type,
819 unsigned int imageIndex,
820 bool readonly,
821 const gl::Caps &caps) const
822{
823 GLint logicalImageUnit = -1;
824 ASSERT(imageIndex < caps.maxImageUnits);
825 switch (type)
826 {
827 case gl::SHADER_COMPUTE:
828 if (readonly && imageIndex < mReadonlyImagesCS.size() &&
829 mReadonlyImagesCS[imageIndex].active)
830 {
831 logicalImageUnit = mReadonlyImagesCS[imageIndex].logicalImageUnit;
832 }
833 else if (imageIndex < mImagesCS.size() && mImagesCS[imageIndex].active)
834 {
835 logicalImageUnit = mImagesCS[imageIndex].logicalImageUnit;
836 }
837 break;
838 // TODO(xinghua.cao@intel.com): add image mapping for vertex shader and pixel shader.
839 default:
840 UNREACHABLE();
841 }
842
843 if (logicalImageUnit >= 0 && logicalImageUnit < static_cast<GLint>(caps.maxImageUnits))
844 {
845 return logicalImageUnit;
846 }
847
848 return -1;
849}
850
851GLuint ProgramD3D::getUsedImageRange(gl::ShaderType type, bool readonly) const
852{
853 switch (type)
854 {
855 case gl::SHADER_COMPUTE:
856 return readonly ? mUsedComputeReadonlyImageRange : mUsedComputeImageRange;
857 // TODO(xinghua.cao@intel.com): add image range of vertex shader and pixel shader.
858 default:
859 UNREACHABLE();
860 return 0u;
861 }
862}
863
Jamie Madill9cf9e872017-06-05 12:59:25 -0400864gl::LinkResult ProgramD3D::load(const gl::Context *context,
865 gl::InfoLog &infoLog,
866 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700867{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500868 // TODO(jmadill): Use Renderer from contextImpl.
869
Jamie Madill62d31cb2015-09-11 13:25:51 -0400870 reset();
871
Jamie Madill334d6152015-10-22 14:00:28 -0400872 DeviceIdentifier binaryDeviceIdentifier = {0};
873 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
874 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700875
876 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
877 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
878 {
879 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500880 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700881 }
882
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500883 int compileFlags = stream->readInt<int>();
884 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
885 {
Jamie Madillf6113162015-05-07 11:49:21 -0400886 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500887 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500888 }
889
Jamie Madill8047c0d2016-03-07 13:02:12 -0500890 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400891 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500892 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400893 }
894
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700895 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
896 for (unsigned int i = 0; i < psSamplerCount; ++i)
897 {
898 Sampler sampler;
899 stream->readBool(&sampler.active);
900 stream->readInt(&sampler.logicalTextureUnit);
901 stream->readInt(&sampler.textureType);
902 mSamplersPS.push_back(sampler);
903 }
904 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
905 for (unsigned int i = 0; i < vsSamplerCount; ++i)
906 {
907 Sampler sampler;
908 stream->readBool(&sampler.active);
909 stream->readInt(&sampler.logicalTextureUnit);
910 stream->readInt(&sampler.textureType);
911 mSamplersVS.push_back(sampler);
912 }
913
Xinghua Caob1239382016-12-13 15:07:05 +0800914 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
915 for (unsigned int i = 0; i < csSamplerCount; ++i)
916 {
917 Sampler sampler;
918 stream->readBool(&sampler.active);
919 stream->readInt(&sampler.logicalTextureUnit);
920 stream->readInt(&sampler.textureType);
921 mSamplersCS.push_back(sampler);
922 }
923
Xinghua Cao26143fd2017-11-01 18:19:05 +0800924 const unsigned int csImageCount = stream->readInt<unsigned int>();
925 for (unsigned int i = 0; i < csImageCount; ++i)
926 {
927 Image image;
928 stream->readBool(&image.active);
929 stream->readInt(&image.logicalImageUnit);
930 mImagesCS.push_back(image);
931 }
932
933 const unsigned int csReadonlyImageCount = stream->readInt<unsigned int>();
934 for (unsigned int i = 0; i < csReadonlyImageCount; ++i)
935 {
936 Image image;
937 stream->readBool(&image.active);
938 stream->readInt(&image.logicalImageUnit);
939 mReadonlyImagesCS.push_back(image);
940 }
941
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700942 stream->readInt(&mUsedVertexSamplerRange);
943 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800944 stream->readInt(&mUsedComputeSamplerRange);
Xinghua Cao26143fd2017-11-01 18:19:05 +0800945 stream->readInt(&mUsedComputeImageRange);
946 stream->readInt(&mUsedComputeReadonlyImageRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700947
948 const unsigned int uniformCount = stream->readInt<unsigned int>();
949 if (stream->error())
950 {
Jamie Madillf6113162015-05-07 11:49:21 -0400951 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500952 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700953 }
954
Jamie Madill48ef11b2016-04-27 15:21:52 -0400955 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400956 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700957 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
958 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400959 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700960
Jamie Madill62d31cb2015-09-11 13:25:51 -0400961 D3DUniform *d3dUniform =
Xinghua Cao26143fd2017-11-01 18:19:05 +0800962 new D3DUniform(linkedUniform.type, HLSLRegisterType::None, linkedUniform.name,
963 linkedUniform.arraySizes, linkedUniform.isInDefaultBlock());
964 stream->readInt<HLSLRegisterType>(&d3dUniform->regType);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400965 stream->readInt(&d3dUniform->psRegisterIndex);
966 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800967 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400968 stream->readInt(&d3dUniform->registerCount);
969 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700970
Jamie Madill62d31cb2015-09-11 13:25:51 -0400971 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700972 }
973
Jamie Madill4a3c2342015-10-08 12:58:45 -0400974 const unsigned int blockCount = stream->readInt<unsigned int>();
975 if (stream->error())
976 {
977 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500978 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400979 }
980
981 ASSERT(mD3DUniformBlocks.empty());
982 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
983 {
984 D3DUniformBlock uniformBlock;
985 stream->readInt(&uniformBlock.psRegisterIndex);
986 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800987 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400988 mD3DUniformBlocks.push_back(uniformBlock);
989 }
990
Jamie Madill9fc36822015-11-18 13:08:07 -0500991 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
992 mStreamOutVaryings.resize(streamOutVaryingCount);
993 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700994 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500995 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700996
Jamie Madill28afae52015-11-09 15:07:57 -0500997 stream->readString(&varying->semanticName);
998 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500999 stream->readInt(&varying->componentCount);
1000 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001001 }
1002
Brandon Jones22502d52014-08-29 16:58:36 -07001003 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001004 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001005 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001006 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001007 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001008 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001009 stream->readBool(&mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +03001010 stream->readBool(&mHasANGLEMultiviewEnabled);
1011 stream->readBool(&mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -07001012 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001013 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001014
1015 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
1016 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -04001017 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
1018 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001019 {
1020 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
1021 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
1022 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
1023 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
1024 }
1025
Jamie Madill4e31ad52015-10-29 10:32:57 -04001026 stream->readString(&mGeometryShaderPreamble);
1027
Jamie Madill334d6152015-10-22 14:00:28 -04001028 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -07001029
Jamie Madillb0a838b2016-11-13 20:02:12 -05001030 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
1031
Brandon Joneseb994362014-09-24 10:27:28 -07001032 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -04001033 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
1034 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001035 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001036 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04001037 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -07001038
Jamie Madilld3dfda22015-07-06 08:28:49 -04001039 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001040 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -04001041 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -07001042 }
1043
Jamie Madill334d6152015-10-22 14:00:28 -04001044 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -07001045 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -04001046
Jamie Madillada9ecc2015-08-17 12:53:37 -04001047 ShaderExecutableD3D *shaderExecutable = nullptr;
1048
Yunchao He85072e82017-11-14 15:43:28 +08001049 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
1050 gl::SHADER_VERTEX, mStreamOutVaryings, separateAttribs,
Jamie Madillb0a838b2016-11-13 20:02:12 -05001051 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001052
Brandon Joneseb994362014-09-24 10:27:28 -07001053 if (!shaderExecutable)
1054 {
Jamie Madillf6113162015-05-07 11:49:21 -04001055 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001056 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001057 }
1058
1059 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -04001060 VertexExecutable::Signature signature;
1061 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -07001062
1063 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +08001064 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
1065 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001066
1067 stream->skip(vertexShaderSize);
1068 }
1069
1070 const size_t pixelShaderCount = stream->readInt<unsigned int>();
1071 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
1072 {
1073 const size_t outputCount = stream->readInt<unsigned int>();
1074 std::vector<GLenum> outputs(outputCount);
1075 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
1076 {
1077 stream->readInt(&outputs[outputIndex]);
1078 }
1079
Jamie Madill334d6152015-10-22 14:00:28 -04001080 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -07001081 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -04001082 ShaderExecutableD3D *shaderExecutable = nullptr;
1083
Yunchao He85072e82017-11-14 15:43:28 +08001084 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize,
1085 gl::SHADER_FRAGMENT, mStreamOutVaryings,
1086 separateAttribs, &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001087
1088 if (!shaderExecutable)
1089 {
Jamie Madillf6113162015-05-07 11:49:21 -04001090 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001091 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001092 }
1093
1094 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +08001095 mPixelExecutables.push_back(
1096 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001097
1098 stream->skip(pixelShaderSize);
1099 }
1100
Jamie Madill4e31ad52015-10-29 10:32:57 -04001101 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
1102 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -07001103 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001104 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
1105 if (geometryShaderSize == 0)
1106 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001107 continue;
1108 }
1109
Brandon Joneseb994362014-09-24 10:27:28 -07001110 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001111
Xinghua Caob1239382016-12-13 15:07:05 +08001112 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -05001113 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
Yunchao He85072e82017-11-14 15:43:28 +08001114 gl::SHADER_GEOMETRY, mStreamOutVaryings,
1115 separateAttribs, &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001116
Xinghua Caob1239382016-12-13 15:07:05 +08001117 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001118 {
Jamie Madillf6113162015-05-07 11:49:21 -04001119 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001120 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001121 }
Xinghua Caob1239382016-12-13 15:07:05 +08001122
1123 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
1124
Brandon Joneseb994362014-09-24 10:27:28 -07001125 stream->skip(geometryShaderSize);
1126 }
1127
Xinghua Caob1239382016-12-13 15:07:05 +08001128 unsigned int computeShaderSize = stream->readInt<unsigned int>();
1129 if (computeShaderSize > 0)
1130 {
1131 const unsigned char *computeShaderFunction = binary + stream->offset();
1132
1133 ShaderExecutableD3D *computeExecutable = nullptr;
1134 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
Yunchao He85072e82017-11-14 15:43:28 +08001135 gl::SHADER_COMPUTE, std::vector<D3DVarying>(), false,
Xinghua Caob1239382016-12-13 15:07:05 +08001136 &computeExecutable));
1137
1138 if (!computeExecutable)
1139 {
1140 infoLog << "Could not create compute shader.";
1141 return false;
1142 }
1143
1144 mComputeExecutable.reset(computeExecutable);
1145 }
1146
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001147 initializeUniformStorage();
1148
Jamie Madillb0a838b2016-11-13 20:02:12 -05001149 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001150}
1151
Jamie Madill27a60632017-06-30 15:12:01 -04001152void ProgramD3D::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -07001153{
Austin Kinross137b1512015-06-17 16:14:53 -07001154 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -04001155 // When we load the binary again later, we can validate the device identifier before trying to
1156 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -07001157 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -04001158 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
1159 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -07001160
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001161 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1162
Jamie Madill8047c0d2016-03-07 13:02:12 -05001163 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -04001164 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05001165 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -04001166 }
1167
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001168 stream->writeInt(mSamplersPS.size());
1169 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1170 {
1171 stream->writeInt(mSamplersPS[i].active);
1172 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
1173 stream->writeInt(mSamplersPS[i].textureType);
1174 }
1175
1176 stream->writeInt(mSamplersVS.size());
1177 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1178 {
1179 stream->writeInt(mSamplersVS[i].active);
1180 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1181 stream->writeInt(mSamplersVS[i].textureType);
1182 }
1183
Xinghua Caob1239382016-12-13 15:07:05 +08001184 stream->writeInt(mSamplersCS.size());
1185 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
1186 {
1187 stream->writeInt(mSamplersCS[i].active);
1188 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
1189 stream->writeInt(mSamplersCS[i].textureType);
1190 }
1191
Xinghua Cao26143fd2017-11-01 18:19:05 +08001192 stream->writeInt(mImagesCS.size());
1193 for (unsigned int i = 0; i < mImagesCS.size(); ++i)
1194 {
1195 stream->writeInt(mImagesCS[i].active);
1196 stream->writeInt(mImagesCS[i].logicalImageUnit);
1197 }
1198
1199 stream->writeInt(mReadonlyImagesCS.size());
1200 for (unsigned int i = 0; i < mReadonlyImagesCS.size(); ++i)
1201 {
1202 stream->writeInt(mReadonlyImagesCS[i].active);
1203 stream->writeInt(mReadonlyImagesCS[i].logicalImageUnit);
1204 }
1205
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001206 stream->writeInt(mUsedVertexSamplerRange);
1207 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001208 stream->writeInt(mUsedComputeSamplerRange);
Xinghua Cao26143fd2017-11-01 18:19:05 +08001209 stream->writeInt(mUsedComputeImageRange);
1210 stream->writeInt(mUsedComputeReadonlyImageRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001211
Jamie Madill62d31cb2015-09-11 13:25:51 -04001212 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001213 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001214 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001215 // Type, name and arraySize are redundant, so aren't stored in the binary.
Xinghua Cao26143fd2017-11-01 18:19:05 +08001216 stream->writeInt(static_cast<unsigned int>(uniform->regType));
Jamie Madille2e406c2016-06-02 13:04:10 -04001217 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1218 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001219 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001220 stream->writeInt(uniform->registerCount);
1221 stream->writeInt(uniform->registerElement);
1222 }
1223
1224 stream->writeInt(mD3DUniformBlocks.size());
1225 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1226 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001227 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1228 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001229 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001230 }
1231
Jamie Madill9fc36822015-11-18 13:08:07 -05001232 stream->writeInt(mStreamOutVaryings.size());
1233 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001234 {
Brandon Joneseb994362014-09-24 10:27:28 -07001235 stream->writeString(varying.semanticName);
1236 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001237 stream->writeInt(varying.componentCount);
1238 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001239 }
1240
Brandon Jones22502d52014-08-29 16:58:36 -07001241 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001242 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001243 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001244 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001245 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001246 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001247 stream->writeInt(mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +03001248 stream->writeInt(mHasANGLEMultiviewEnabled);
1249 stream->writeInt(mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -07001250 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001251 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001252
Brandon Joneseb994362014-09-24 10:27:28 -07001253 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001254 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001255 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1256 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001257 {
Brandon Joneseb994362014-09-24 10:27:28 -07001258 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001259 stream->writeInt(variable.type);
1260 stream->writeString(variable.name);
1261 stream->writeString(variable.source);
1262 stream->writeInt(variable.outputIndex);
1263 }
1264
Jamie Madill4e31ad52015-10-29 10:32:57 -04001265 stream->writeString(mGeometryShaderPreamble);
1266
Brandon Joneseb994362014-09-24 10:27:28 -07001267 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001268 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1269 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001270 {
Xinghua Caob1239382016-12-13 15:07:05 +08001271 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001272
Jamie Madilld3dfda22015-07-06 08:28:49 -04001273 const auto &inputLayout = vertexExecutable->inputs();
1274 stream->writeInt(inputLayout.size());
1275
1276 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001277 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001278 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001279 }
1280
1281 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1282 stream->writeInt(vertexShaderSize);
1283
1284 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1285 stream->writeBytes(vertexBlob, vertexShaderSize);
1286 }
1287
1288 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001289 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1290 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001291 {
Xinghua Caob1239382016-12-13 15:07:05 +08001292 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001293
1294 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1295 stream->writeInt(outputs.size());
1296 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1297 {
1298 stream->writeInt(outputs[outputIndex]);
1299 }
1300
1301 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1302 stream->writeInt(pixelShaderSize);
1303
1304 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1305 stream->writeBytes(pixelBlob, pixelShaderSize);
1306 }
1307
Xinghua Caob1239382016-12-13 15:07:05 +08001308 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001309 {
Xinghua Caob1239382016-12-13 15:07:05 +08001310 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001311 {
1312 stream->writeInt(0);
1313 continue;
1314 }
1315
Xinghua Caob1239382016-12-13 15:07:05 +08001316 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001317 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001318 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1319 }
1320
1321 if (mComputeExecutable)
1322 {
1323 size_t computeShaderSize = mComputeExecutable->getLength();
1324 stream->writeInt(computeShaderSize);
1325 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1326 }
1327 else
1328 {
1329 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001330 }
Brandon Jones22502d52014-08-29 16:58:36 -07001331}
1332
Geoff Langc5629752015-12-07 16:29:04 -05001333void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1334{
1335}
1336
Yunchao He61afff12017-03-14 15:34:03 +08001337void ProgramD3D::setSeparable(bool /* separable */)
1338{
1339}
1340
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001341gl::Error ProgramD3D::getPixelExecutableForCachedOutputLayout(ShaderExecutableD3D **outExecutable,
1342 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001343{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001344 if (mCachedPixelExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001345 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001346 *outExecutable = mPixelExecutables[mCachedPixelExecutableIndex.value()]->shaderExecutable();
1347 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001348 }
1349
Jamie Madill334d6152015-10-22 14:00:28 -04001350 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001351 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, mPixelShaderOutputLayoutCache);
Brandon Jones22502d52014-08-29 16:58:36 -07001352
1353 // Generate new pixel executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001354 ShaderExecutableD3D *pixelExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001355
1356 gl::InfoLog tempInfoLog;
1357 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1358
Jamie Madill01074252016-11-28 15:55:51 -05001359 ANGLE_TRY(mRenderer->compileToExecutable(
Yunchao He85072e82017-11-14 15:43:28 +08001360 *currentInfoLog, finalPixelHLSL, gl::SHADER_FRAGMENT, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001361 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001362 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001363
Jamie Madill97399232014-12-23 12:31:15 -05001364 if (pixelExecutable)
1365 {
Xinghua Caob1239382016-12-13 15:07:05 +08001366 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001367 new PixelExecutable(mPixelShaderOutputLayoutCache, pixelExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001368 mCachedPixelExecutableIndex = mPixelExecutables.size() - 1;
Jamie Madill97399232014-12-23 12:31:15 -05001369 }
1370 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001371 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001372 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1373 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001374 }
Brandon Jones22502d52014-08-29 16:58:36 -07001375
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001376 *outExecutable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001377 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001378}
1379
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001380gl::Error ProgramD3D::getVertexExecutableForCachedInputLayout(ShaderExecutableD3D **outExectuable,
1381 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001382{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001383 if (mCachedVertexExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001384 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001385 *outExectuable =
1386 mVertexExecutables[mCachedVertexExecutableIndex.value()]->shaderExecutable();
1387 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001388 }
1389
Brandon Jones22502d52014-08-29 16:58:36 -07001390 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001391 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001392 mVertexHLSL, mCachedInputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001393
1394 // Generate new vertex executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001395 ShaderExecutableD3D *vertexExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001396
1397 gl::InfoLog tempInfoLog;
1398 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1399
Jamie Madill01074252016-11-28 15:55:51 -05001400 ANGLE_TRY(mRenderer->compileToExecutable(
Yunchao He85072e82017-11-14 15:43:28 +08001401 *currentInfoLog, finalVertexHLSL, gl::SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001402 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001403 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001404
Jamie Madill97399232014-12-23 12:31:15 -05001405 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001406 {
Xinghua Caob1239382016-12-13 15:07:05 +08001407 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001408 new VertexExecutable(mCachedInputLayout, mCachedVertexSignature, vertexExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001409 mCachedVertexExecutableIndex = mVertexExecutables.size() - 1;
Brandon Joneseb994362014-09-24 10:27:28 -07001410 }
Jamie Madill97399232014-12-23 12:31:15 -05001411 else if (!infoLog)
1412 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001413 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1414 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001415 }
Brandon Jones22502d52014-08-29 16:58:36 -07001416
Geoff Langb543aff2014-09-30 14:52:54 -04001417 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001418 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001419}
1420
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001421gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Context *context,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001422 GLenum drawMode,
1423 ShaderExecutableD3D **outExecutable,
1424 gl::InfoLog *infoLog)
1425{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001426 if (outExecutable)
1427 {
1428 *outExecutable = nullptr;
1429 }
1430
Austin Kinross88829e82016-01-12 13:04:41 -08001431 // Return a null shader if the current rendering doesn't use a geometry shader
1432 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001433 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001434 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001435 }
1436
Jamie Madill4e31ad52015-10-29 10:32:57 -04001437 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1438
Xinghua Caob1239382016-12-13 15:07:05 +08001439 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001440 {
1441 if (outExecutable)
1442 {
Xinghua Caob1239382016-12-13 15:07:05 +08001443 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001444 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001445 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001446 }
1447
1448 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001449 context, geometryShaderType, mState, mRenderer->presentPathFastEnabled(),
Martin Radevc1d4e552017-08-21 12:01:10 +03001450 mHasANGLEMultiviewEnabled, mRenderer->canSelectViewInVertexShader(),
1451 usesGeometryShaderForPointSpriteEmulation(), mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001452
1453 gl::InfoLog tempInfoLog;
1454 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1455
Xinghua Caob1239382016-12-13 15:07:05 +08001456 ShaderExecutableD3D *geometryExecutable = nullptr;
1457 gl::Error error = mRenderer->compileToExecutable(
Yunchao He85072e82017-11-14 15:43:28 +08001458 *currentInfoLog, geometryHLSL, gl::SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001459 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001460 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001461
1462 if (!infoLog && error.isError())
1463 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001464 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1465 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001466 }
1467
Xinghua Caob1239382016-12-13 15:07:05 +08001468 if (geometryExecutable != nullptr)
1469 {
1470 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1471 }
1472
Jamie Madill4e31ad52015-10-29 10:32:57 -04001473 if (outExecutable)
1474 {
Xinghua Caob1239382016-12-13 15:07:05 +08001475 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001476 }
1477 return error;
1478}
1479
Jamie Madill01074252016-11-28 15:55:51 -05001480class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001481{
Jamie Madill01074252016-11-28 15:55:51 -05001482 public:
1483 GetExecutableTask(ProgramD3D *program)
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001484 : mProgram(program), mError(gl::NoError()), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001485 {
Brandon Joneseb994362014-09-24 10:27:28 -07001486 }
1487
Jamie Madill01074252016-11-28 15:55:51 -05001488 virtual gl::Error run() = 0;
1489
1490 void operator()() override { mError = run(); }
1491
1492 const gl::Error &getError() const { return mError; }
1493 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1494 ShaderExecutableD3D *getResult() { return mResult; }
1495
1496 protected:
1497 ProgramD3D *mProgram;
1498 gl::Error mError;
1499 gl::InfoLog mInfoLog;
1500 ShaderExecutableD3D *mResult;
1501};
1502
1503class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1504{
1505 public:
Jamie Madillbd044ed2017-06-05 12:59:21 -04001506 GetVertexExecutableTask(ProgramD3D *program, const gl::Context *context)
1507 : GetExecutableTask(program), mContext(context)
1508 {
1509 }
Jamie Madill01074252016-11-28 15:55:51 -05001510 gl::Error run() override
1511 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001512 mProgram->updateCachedInputLayoutFromShader(mContext);
Jamie Madill01074252016-11-28 15:55:51 -05001513
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001514 ANGLE_TRY(mProgram->getVertexExecutableForCachedInputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001515
1516 return gl::NoError();
1517 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001518
1519 private:
1520 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001521};
1522
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001523void ProgramD3D::updateCachedInputLayoutFromShader(const gl::Context *context)
1524{
1525 GetDefaultInputLayoutFromShader(context, mState.getAttachedVertexShader(), &mCachedInputLayout);
1526 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001527 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001528}
1529
Jamie Madill01074252016-11-28 15:55:51 -05001530class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1531{
1532 public:
1533 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1534 gl::Error run() override
1535 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001536 mProgram->updateCachedOutputLayoutFromShader();
Jamie Madill01074252016-11-28 15:55:51 -05001537
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001538 ANGLE_TRY(mProgram->getPixelExecutableForCachedOutputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001539
1540 return gl::NoError();
1541 }
1542};
1543
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001544void ProgramD3D::updateCachedOutputLayoutFromShader()
1545{
1546 GetDefaultOutputLayoutFromShader(mPixelShaderKey, &mPixelShaderOutputLayoutCache);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001547 updateCachedPixelExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001548}
1549
Jamie Madill01074252016-11-28 15:55:51 -05001550class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1551{
1552 public:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001553 GetGeometryExecutableTask(ProgramD3D *program, const gl::Context *context)
1554 : GetExecutableTask(program), mContext(context)
Jamie Madill01074252016-11-28 15:55:51 -05001555 {
1556 }
1557
1558 gl::Error run() override
1559 {
1560 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1561 // D3D11.
1562 if (mProgram->usesGeometryShader(GL_POINTS))
1563 {
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001564 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContext, GL_POINTS, &mResult,
1565 &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001566 }
1567
1568 return gl::NoError();
1569 }
1570
1571 private:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001572 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001573};
1574
Xinghua Cao73badc02017-03-29 19:14:53 +08001575gl::Error ProgramD3D::getComputeExecutable(ShaderExecutableD3D **outExecutable)
1576{
1577 if (outExecutable)
1578 {
1579 *outExecutable = mComputeExecutable.get();
1580 }
1581
1582 return gl::NoError();
1583}
1584
Jamie Madill9cf9e872017-06-05 12:59:25 -04001585gl::LinkResult ProgramD3D::compileProgramExecutables(const gl::Context *context,
1586 gl::InfoLog &infoLog)
Jamie Madill01074252016-11-28 15:55:51 -05001587{
1588 // Ensure the compiler is initialized to avoid race conditions.
1589 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1590
1591 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1592
Jamie Madillbd044ed2017-06-05 12:59:21 -04001593 GetVertexExecutableTask vertexTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001594 GetPixelExecutableTask pixelTask(this);
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001595 GetGeometryExecutableTask geometryTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001596
1597 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1598 workerPool->postWorkerTask(&pixelTask),
1599 workerPool->postWorkerTask(&geometryTask)}};
1600
1601 WaitableEvent::WaitMany(&waitEvents);
1602
Jiawei Shao02f15232017-12-27 10:10:28 +08001603 if (!vertexTask.getInfoLog().empty())
1604 {
1605 infoLog << vertexTask.getInfoLog().str();
1606 }
1607 if (!pixelTask.getInfoLog().empty())
1608 {
1609 infoLog << pixelTask.getInfoLog().str();
1610 }
1611 if (!geometryTask.getInfoLog().empty())
1612 {
1613 infoLog << geometryTask.getInfoLog().str();
1614 }
Jamie Madill01074252016-11-28 15:55:51 -05001615
1616 ANGLE_TRY(vertexTask.getError());
1617 ANGLE_TRY(pixelTask.getError());
1618 ANGLE_TRY(geometryTask.getError());
1619
1620 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1621 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1622 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1623
Jamie Madill48ef11b2016-04-27 15:21:52 -04001624 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001625
1626 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001627 {
Jamie Madill334d6152015-10-22 14:00:28 -04001628 // Geometry shaders are currently only used internally, so there is no corresponding shader
1629 // object at the interface level. For now the geometry shader debug info is prepended to
1630 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001631 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001632 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001633 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1634 }
1635
1636 if (defaultVertexExecutable)
1637 {
1638 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1639 }
1640
1641 if (defaultPixelExecutable)
1642 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001643 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001644 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001645 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1646 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001647
Jamie Madillb0a838b2016-11-13 20:02:12 -05001648 return (defaultVertexExecutable && defaultPixelExecutable &&
1649 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001650}
1651
Jamie Madill9cf9e872017-06-05 12:59:25 -04001652gl::LinkResult ProgramD3D::compileComputeExecutable(const gl::Context *context,
1653 gl::InfoLog &infoLog)
Xinghua Caob1239382016-12-13 15:07:05 +08001654{
1655 // Ensure the compiler is initialized to avoid race conditions.
1656 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1657
Jamie Madillbd044ed2017-06-05 12:59:21 -04001658 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(context, mState);
Xinghua Caob1239382016-12-13 15:07:05 +08001659
1660 ShaderExecutableD3D *computeExecutable = nullptr;
Yunchao He85072e82017-11-14 15:43:28 +08001661 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, gl::SHADER_COMPUTE,
Xinghua Caob1239382016-12-13 15:07:05 +08001662 std::vector<D3DVarying>(), false,
1663 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1664
1665 if (computeExecutable == nullptr)
1666 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001667 ERR() << "Error compiling dynamic compute executable:" << std::endl
1668 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001669 }
1670 else
1671 {
1672 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1673 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1674 mComputeExecutable.reset(computeExecutable);
1675 }
1676
1677 return mComputeExecutable.get() != nullptr;
1678}
1679
Jamie Madill9cf9e872017-06-05 12:59:25 -04001680gl::LinkResult ProgramD3D::link(const gl::Context *context,
Jamie Madillc9727f32017-11-07 12:37:07 -05001681 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -04001682 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001683{
Jamie Madillc564c072017-06-01 12:45:42 -04001684 const auto &data = context->getContextState();
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001685
Jamie Madill62d31cb2015-09-11 13:25:51 -04001686 reset();
1687
Jamie Madillbd044ed2017-06-05 12:59:21 -04001688 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001689 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001690 {
Xinghua Caob1239382016-12-13 15:07:05 +08001691 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
Xinghua Cao26143fd2017-11-01 18:19:05 +08001692 mImagesCS.resize(data.getCaps().maxImageUnits);
1693 mReadonlyImagesCS.resize(data.getCaps().maxImageUnits);
Xinghua Caob1239382016-12-13 15:07:05 +08001694
Jamie Madillbd044ed2017-06-05 12:59:21 -04001695 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001696
Jamie Madill9cf9e872017-06-05 12:59:25 -04001697 gl::LinkResult result = compileComputeExecutable(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001698 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001699 {
Xinghua Caob1239382016-12-13 15:07:05 +08001700 infoLog << result.getError().getMessage();
1701 return result;
1702 }
1703 else if (!result.getResult())
1704 {
1705 infoLog << "Failed to create D3D compute shader.";
1706 return result;
1707 }
Xinghua Caob1239382016-12-13 15:07:05 +08001708 }
1709 else
1710 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001711 gl::Shader *vertexShader = mState.getAttachedVertexShader();
1712 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001713
1714 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1715 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1716
1717 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1718 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1719
1720 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1721 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1722
1723 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1724 {
1725 if (fragmentShaderD3D->usesFrontFacing())
1726 {
1727 infoLog << "The current renderer doesn't support gl_FrontFacing";
1728 return false;
1729 }
1730 }
1731
Xinghua Caob1239382016-12-13 15:07:05 +08001732 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madillc9727f32017-11-07 12:37:07 -05001733 BuiltinVaryingsD3D builtins(metadata, resources.varyingPacking);
Xinghua Caob1239382016-12-13 15:07:05 +08001734
Jamie Madillc9727f32017-11-07 12:37:07 -05001735 mDynamicHLSL->generateShaderLinkHLSL(context, mState, metadata, resources.varyingPacking,
1736 builtins, &mPixelHLSL, &mVertexHLSL);
Xinghua Caob1239382016-12-13 15:07:05 +08001737
1738 mUsesPointSize = vertexShaderD3D->usesPointSize();
1739 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
Xinghua Cao26143fd2017-11-01 18:19:05 +08001740 mUsesFragDepth = metadata.usesFragDepth();
Martin Radev41ac68e2017-06-06 12:16:58 +03001741 mUsesViewID = metadata.usesViewID();
1742 mHasANGLEMultiviewEnabled = metadata.hasANGLEMultiviewEnabled();
Xinghua Caob1239382016-12-13 15:07:05 +08001743
1744 // Cache if we use flat shading
Jamie Madillbd044ed2017-06-05 12:59:21 -04001745 mUsesFlatInterpolation =
Jiawei Shao3d404882017-10-16 13:30:48 +08001746 (FindFlatInterpolationVarying(fragmentShader->getInputVaryings(context)) ||
1747 FindFlatInterpolationVarying(vertexShader->getOutputVaryings(context)));
Xinghua Caob1239382016-12-13 15:07:05 +08001748
1749 if (mRenderer->getMajorShaderModel() >= 4)
1750 {
Martin Radev41ac68e2017-06-06 12:16:58 +03001751 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(
Jamie Madillc9727f32017-11-07 12:37:07 -05001752 resources.varyingPacking, builtins, mHasANGLEMultiviewEnabled,
Martin Radevc1d4e552017-08-21 12:01:10 +03001753 metadata.canSelectViewInVertexShader());
Xinghua Caob1239382016-12-13 15:07:05 +08001754 }
1755
Jamie Madillbd044ed2017-06-05 12:59:21 -04001756 initAttribLocationsToD3DSemantic(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001757
Jamie Madillbd044ed2017-06-05 12:59:21 -04001758 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001759
Yunchao He85072e82017-11-14 15:43:28 +08001760 gatherTransformFeedbackVaryings(resources.varyingPacking, builtins[gl::SHADER_VERTEX]);
Xinghua Caob1239382016-12-13 15:07:05 +08001761
Jamie Madill9cf9e872017-06-05 12:59:25 -04001762 gl::LinkResult result = compileProgramExecutables(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001763 if (result.isError())
1764 {
1765 infoLog << result.getError().getMessage();
1766 return result;
1767 }
1768 else if (!result.getResult())
1769 {
1770 infoLog << "Failed to create D3D shaders.";
1771 return result;
1772 }
Austin Kinross02df7962015-07-01 10:03:42 -07001773 }
1774
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001775 linkResources(context, resources);
1776
Jamie Madillb0a838b2016-11-13 20:02:12 -05001777 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001778}
1779
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001780GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001781{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001782 // TODO(jmadill): Do something useful here?
1783 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001784}
1785
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001786void ProgramD3D::initializeUniformBlocks()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001787{
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001788 if (mState.getUniformBlocks().empty())
Jamie Madill51f522f2016-12-21 15:10:55 -05001789 {
1790 return;
1791 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001792
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001793 ASSERT(mD3DUniformBlocks.empty());
1794
Jamie Madill62d31cb2015-09-11 13:25:51 -04001795 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001796 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1797 const ShaderD3D *fragmentShaderD3D =
1798 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1799 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001800
Jiajia Qin729b2c62017-08-14 09:36:11 +08001801 for (const gl::InterfaceBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001802 {
1803 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1804
Jamie Madill4a3c2342015-10-08 12:58:45 -04001805 D3DUniformBlock d3dUniformBlock;
1806
Jamie Madill62d31cb2015-09-11 13:25:51 -04001807 if (uniformBlock.vertexStaticUse)
1808 {
Xinghua Caob1239382016-12-13 15:07:05 +08001809 ASSERT(vertexShaderD3D != nullptr);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001810 unsigned int baseRegister = vertexShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001811 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001812 }
1813
1814 if (uniformBlock.fragmentStaticUse)
1815 {
Xinghua Caob1239382016-12-13 15:07:05 +08001816 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001817 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001818 fragmentShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001819 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001820 }
1821
Xinghua Caob1239382016-12-13 15:07:05 +08001822 if (uniformBlock.computeStaticUse)
1823 {
1824 ASSERT(computeShaderD3D != nullptr);
1825 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001826 computeShaderD3D->getUniformBlockRegister(uniformBlock.name);
Xinghua Caob1239382016-12-13 15:07:05 +08001827 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1828 }
1829
Jamie Madill4a3c2342015-10-08 12:58:45 -04001830 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001831 }
1832}
1833
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001834void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001835{
1836 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001837 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001838 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001839 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001840 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001841 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001842 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001843 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001844 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001845 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001846 vertexRegisters = std::max(vertexRegisters,
1847 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001848 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001849 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001850 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001851 fragmentRegisters = std::max(
1852 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001853 }
Xinghua Caob1239382016-12-13 15:07:05 +08001854 if (d3dUniform->isReferencedByComputeShader())
1855 {
1856 computeRegisters = std::max(
1857 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1858 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001859 }
1860 }
1861
Xinghua Caob1239382016-12-13 15:07:05 +08001862 mVertexUniformStorage =
1863 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1864 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1865 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1866 mComputeUniformStorage =
1867 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001868
1869 // Iterate the uniforms again to assign data pointers to default block uniforms.
1870 for (D3DUniform *d3dUniform : mD3DUniforms)
1871 {
1872 if (d3dUniform->isSampler())
1873 {
Olli Etuaho465835d2017-09-26 13:34:10 +03001874 d3dUniform->mSamplerData.resize(d3dUniform->getArraySizeProduct(), 0);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001875 continue;
1876 }
1877
1878 if (d3dUniform->isReferencedByVertexShader())
1879 {
1880 d3dUniform->vsData = mVertexUniformStorage->getDataPointer(d3dUniform->vsRegisterIndex,
1881 d3dUniform->registerElement);
1882 }
1883
1884 if (d3dUniform->isReferencedByFragmentShader())
1885 {
1886 d3dUniform->psData = mFragmentUniformStorage->getDataPointer(
1887 d3dUniform->psRegisterIndex, d3dUniform->registerElement);
1888 }
1889
1890 if (d3dUniform->isReferencedByComputeShader())
1891 {
1892 d3dUniform->csData = mComputeUniformStorage->getDataPointer(
1893 d3dUniform->csRegisterIndex, d3dUniform->registerElement);
1894 }
1895 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001896}
1897
Jamie Madilld63961d2017-09-12 15:22:57 -04001898void ProgramD3D::updateUniformBufferCache(const gl::Caps &caps,
1899 unsigned int reservedVertex,
1900 unsigned int reservedFragment)
Brandon Jones18bd4102014-09-22 14:21:44 -07001901{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001902 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001903 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001904 return;
Jamie Madill4a3c2342015-10-08 12:58:45 -04001905 }
1906
Jamie Madill03260fa2015-06-22 13:57:22 -04001907 mVertexUBOCache.clear();
1908 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001909
Jamie Madill4a3c2342015-10-08 12:58:45 -04001910 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001911 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001912 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001913 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001914 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001915
Brandon Jones18bd4102014-09-22 14:21:44 -07001916 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001917 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001918 {
1919 continue;
1920 }
1921
Jamie Madill4a3c2342015-10-08 12:58:45 -04001922 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001923 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001924 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedVertex;
1925 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001926
Jamie Madill969194d2015-07-20 14:36:56 -04001927 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001928 {
1929 mVertexUBOCache.resize(registerIndex + 1, -1);
1930 }
1931
1932 ASSERT(mVertexUBOCache[registerIndex] == -1);
1933 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001934 }
1935
Jamie Madill4a3c2342015-10-08 12:58:45 -04001936 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001937 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001938 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedFragment;
1939 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001940
1941 if (mFragmentUBOCache.size() <= registerIndex)
1942 {
1943 mFragmentUBOCache.resize(registerIndex + 1, -1);
1944 }
1945
1946 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1947 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001948 }
1949 }
Jamie Madilld63961d2017-09-12 15:22:57 -04001950}
Brandon Jones18bd4102014-09-22 14:21:44 -07001951
Jamie Madilld63961d2017-09-12 15:22:57 -04001952const std::vector<GLint> &ProgramD3D::getVertexUniformBufferCache() const
1953{
1954 return mVertexUBOCache;
1955}
1956
1957const std::vector<GLint> &ProgramD3D::getFragmentUniformBufferCache() const
1958{
1959 return mFragmentUBOCache;
Brandon Jones18bd4102014-09-22 14:21:44 -07001960}
1961
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001962void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001963{
Jamie Madill4148fd72017-09-14 15:46:20 -04001964 mVertexUniformsDirty = true;
1965 mFragmentUniformsDirty = true;
1966 mComputeUniformsDirty = true;
1967}
1968
1969void ProgramD3D::markUniformsClean()
1970{
1971 mVertexUniformsDirty = false;
1972 mFragmentUniformsDirty = false;
1973 mComputeUniformsDirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001974}
1975
Jamie Madill334d6152015-10-22 14:00:28 -04001976void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001977{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001978 setUniformInternal(location, count, v, GL_FLOAT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001979}
1980
1981void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1982{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001983 setUniformInternal(location, count, v, GL_FLOAT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001984}
1985
1986void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1987{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001988 setUniformInternal(location, count, v, GL_FLOAT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001989}
1990
1991void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1992{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001993 setUniformInternal(location, count, v, GL_FLOAT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001994}
1995
Jamie Madill334d6152015-10-22 14:00:28 -04001996void ProgramD3D::setUniformMatrix2fv(GLint location,
1997 GLsizei count,
1998 GLboolean transpose,
1999 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002000{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002001 setUniformMatrixfvInternal<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002002}
2003
Jamie Madill334d6152015-10-22 14:00:28 -04002004void ProgramD3D::setUniformMatrix3fv(GLint location,
2005 GLsizei count,
2006 GLboolean transpose,
2007 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002008{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002009 setUniformMatrixfvInternal<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002010}
2011
Jamie Madill334d6152015-10-22 14:00:28 -04002012void ProgramD3D::setUniformMatrix4fv(GLint location,
2013 GLsizei count,
2014 GLboolean transpose,
2015 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002016{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002017 setUniformMatrixfvInternal<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018}
2019
Jamie Madill334d6152015-10-22 14:00:28 -04002020void ProgramD3D::setUniformMatrix2x3fv(GLint location,
2021 GLsizei count,
2022 GLboolean transpose,
2023 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002024{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002025 setUniformMatrixfvInternal<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002026}
2027
Jamie Madill334d6152015-10-22 14:00:28 -04002028void ProgramD3D::setUniformMatrix3x2fv(GLint location,
2029 GLsizei count,
2030 GLboolean transpose,
2031 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002032{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002033 setUniformMatrixfvInternal<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002034}
2035
Jamie Madill334d6152015-10-22 14:00:28 -04002036void ProgramD3D::setUniformMatrix2x4fv(GLint location,
2037 GLsizei count,
2038 GLboolean transpose,
2039 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002040{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002041 setUniformMatrixfvInternal<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002042}
2043
Jamie Madill334d6152015-10-22 14:00:28 -04002044void ProgramD3D::setUniformMatrix4x2fv(GLint location,
2045 GLsizei count,
2046 GLboolean transpose,
2047 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002048{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002049 setUniformMatrixfvInternal<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002050}
2051
Jamie Madill334d6152015-10-22 14:00:28 -04002052void ProgramD3D::setUniformMatrix3x4fv(GLint location,
2053 GLsizei count,
2054 GLboolean transpose,
2055 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002056{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002057 setUniformMatrixfvInternal<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002058}
2059
Jamie Madill334d6152015-10-22 14:00:28 -04002060void ProgramD3D::setUniformMatrix4x3fv(GLint location,
2061 GLsizei count,
2062 GLboolean transpose,
2063 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002064{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002065 setUniformMatrixfvInternal<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002066}
2067
2068void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
2069{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002070 setUniformInternal(location, count, v, GL_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002071}
2072
2073void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2074{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002075 setUniformInternal(location, count, v, GL_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002076}
2077
2078void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2079{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002080 setUniformInternal(location, count, v, GL_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002081}
2082
2083void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2084{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002085 setUniformInternal(location, count, v, GL_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002086}
2087
2088void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2089{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002090 setUniformInternal(location, count, v, GL_UNSIGNED_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002091}
2092
2093void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2094{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002095 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002096}
2097
2098void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2099{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002100 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002101}
2102
2103void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2104{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002105 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002106}
2107
Jamie Madillf4141212017-12-12 15:08:07 -05002108void ProgramD3D::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04002109{
Jamie Madillf4141212017-12-12 15:08:07 -05002110 mRenderer->onDirtyUniformBlockBinding(uniformBlockIndex);
Geoff Lang5d124a62015-09-15 13:03:27 -04002111}
2112
Jamie Madillbd044ed2017-06-05 12:59:21 -04002113void ProgramD3D::defineUniformsAndAssignRegisters(const gl::Context *context)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002114{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002115 D3DUniformMap uniformMap;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002116 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08002117 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05002118 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002119 for (const sh::Uniform &computeUniform : computeShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04002120 {
Xinghua Caob1239382016-12-13 15:07:05 +08002121 if (computeUniform.staticUse)
2122 {
2123 defineUniformBase(computeShader, computeUniform, &uniformMap);
2124 }
Jamie Madillfb536032015-09-11 13:19:49 -04002125 }
2126 }
Xinghua Caob1239382016-12-13 15:07:05 +08002127 else
Jamie Madillfb536032015-09-11 13:19:49 -04002128 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002129 gl::Shader *vertexShader = mState.getAttachedVertexShader();
2130 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04002131 {
Xinghua Caob1239382016-12-13 15:07:05 +08002132 if (vertexUniform.staticUse)
2133 {
2134 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
2135 }
2136 }
2137
Jamie Madillbd044ed2017-06-05 12:59:21 -04002138 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
2139 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms(context))
Xinghua Caob1239382016-12-13 15:07:05 +08002140 {
2141 if (fragmentUniform.staticUse)
2142 {
2143 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
2144 }
Jamie Madillfb536032015-09-11 13:19:49 -04002145 }
2146 }
2147
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002148 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002149 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002150 {
2151 if (!glUniform.isInDefaultBlock())
2152 continue;
2153
Olli Etuahod2551232017-10-26 20:03:33 +03002154 std::string name = glUniform.name;
2155 if (glUniform.isArray())
2156 {
2157 // In the program state, array uniform names include [0] as in the program resource
2158 // spec. Here we don't include it.
2159 // TODO(oetuaho@nvidia.com): consider using the same uniform naming here as in the GL
2160 // layer.
2161 ASSERT(angle::EndsWith(name, "[0]"));
2162 name.resize(name.length() - 3);
2163 }
2164 auto mapEntry = uniformMap.find(name);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002165 ASSERT(mapEntry != uniformMap.end());
2166 mD3DUniforms.push_back(mapEntry->second);
2167 }
2168
Jamie Madill62d31cb2015-09-11 13:25:51 -04002169 assignAllSamplerRegisters();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002170 assignAllImageRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04002171 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04002172}
2173
Jamie Madill91445bc2015-09-23 16:47:53 -04002174void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002175 const sh::Uniform &uniform,
2176 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04002177{
Xinghua Cao26143fd2017-11-01 18:19:05 +08002178 // Samplers get their registers assigned in assignAllSamplerRegisters, and images get their
2179 // registers assigned in assignAllImageRegisters.
2180 if (gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04002181 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002182 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::Texture, nullptr,
2183 uniformMap);
2184 return;
2185 }
2186 else if (gl::IsImageType(uniform.type))
2187 {
2188 if (uniform.readonly)
2189 {
2190 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::Texture,
2191 nullptr, uniformMap);
2192 }
2193 else
2194 {
2195 defineUniform(shader->getType(), uniform, uniform.name,
2196 HLSLRegisterType::UnorderedAccessView, nullptr, uniformMap);
2197 }
2198 mImageBindingMap[uniform.name] = uniform.binding;
2199 return;
2200 }
2201 else if (uniform.isBuiltIn())
2202 {
2203 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::None, nullptr,
2204 uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04002205 return;
2206 }
2207
Jamie Madill91445bc2015-09-23 16:47:53 -04002208 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
Jamie Madill91445bc2015-09-23 16:47:53 -04002209 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
Xinghua Cao26143fd2017-11-01 18:19:05 +08002210 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04002211 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002212 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002213
Xinghua Cao26143fd2017-11-01 18:19:05 +08002214 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::None, &encoder,
2215 uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002216}
2217
Jamie Madill62d31cb2015-09-11 13:25:51 -04002218D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
2219{
2220 for (D3DUniform *d3dUniform : mD3DUniforms)
2221 {
2222 if (d3dUniform->name == name)
2223 {
2224 return d3dUniform;
2225 }
2226 }
2227
2228 return nullptr;
2229}
2230
Olli Etuaho465835d2017-09-26 13:34:10 +03002231void ProgramD3D::defineStructUniformFields(GLenum shaderType,
2232 const std::vector<sh::ShaderVariable> &fields,
2233 const std::string &namePrefix,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002234 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002235 sh::HLSLBlockEncoder *encoder,
2236 D3DUniformMap *uniformMap)
2237{
2238 if (encoder)
2239 encoder->enterAggregateType();
2240
2241 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
2242 {
2243 const sh::ShaderVariable &field = fields[fieldIndex];
2244 const std::string &fieldFullName = (namePrefix + "." + field.name);
2245
2246 // Samplers get their registers assigned in assignAllSamplerRegisters.
2247 // Also they couldn't use the same encoder as the rest of the struct, since they are
2248 // extracted out of the struct by the shader translator.
2249 if (gl::IsSamplerType(field.type))
2250 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002251 defineUniform(shaderType, field, fieldFullName, regType, nullptr, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002252 }
2253 else
2254 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002255 defineUniform(shaderType, field, fieldFullName, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002256 }
2257 }
2258
2259 if (encoder)
2260 encoder->exitAggregateType();
2261}
2262
2263void ProgramD3D::defineArrayOfStructsUniformFields(GLenum shaderType,
2264 const sh::ShaderVariable &uniform,
2265 unsigned int arrayNestingIndex,
2266 const std::string &prefix,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002267 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002268 sh::HLSLBlockEncoder *encoder,
2269 D3DUniformMap *uniformMap)
2270{
2271 // Nested arrays are processed starting from outermost (arrayNestingIndex 0u) and ending at the
2272 // innermost.
2273 const unsigned int currentArraySize = uniform.getNestedArraySize(arrayNestingIndex);
2274 for (unsigned int arrayElement = 0u; arrayElement < currentArraySize; ++arrayElement)
2275 {
2276 const std::string &elementString = prefix + ArrayString(arrayElement);
2277 if (arrayNestingIndex + 1u < uniform.arraySizes.size())
2278 {
2279 defineArrayOfStructsUniformFields(shaderType, uniform, arrayNestingIndex + 1u,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002280 elementString, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002281 }
2282 else
2283 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002284 defineStructUniformFields(shaderType, uniform.fields, elementString, regType, encoder,
Olli Etuaho465835d2017-09-26 13:34:10 +03002285 uniformMap);
2286 }
2287 }
2288}
2289
2290void ProgramD3D::defineArrayUniformElements(GLenum shaderType,
2291 const sh::ShaderVariable &uniform,
2292 const std::string &fullName,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002293 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002294 sh::HLSLBlockEncoder *encoder,
2295 D3DUniformMap *uniformMap)
2296{
2297 if (encoder)
2298 encoder->enterAggregateType();
2299
2300 sh::ShaderVariable uniformElement = uniform;
2301 uniformElement.arraySizes.pop_back();
2302 for (unsigned int arrayIndex = 0u; arrayIndex < uniform.getOutermostArraySize(); ++arrayIndex)
2303 {
2304 std::string elementFullName = fullName + ArrayString(arrayIndex);
Xinghua Cao26143fd2017-11-01 18:19:05 +08002305 defineUniform(shaderType, uniformElement, elementFullName, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002306 }
2307
2308 if (encoder)
2309 encoder->exitAggregateType();
2310}
2311
Jamie Madill91445bc2015-09-23 16:47:53 -04002312void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002313 const sh::ShaderVariable &uniform,
2314 const std::string &fullName,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002315 const HLSLRegisterType regType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002316 sh::HLSLBlockEncoder *encoder,
2317 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002318{
2319 if (uniform.isStruct())
2320 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002321 if (uniform.isArray())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002322 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002323 defineArrayOfStructsUniformFields(shaderType, uniform, 0u, fullName, regType, encoder,
Olli Etuaho465835d2017-09-26 13:34:10 +03002324 uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002325 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002326 else
2327 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002328 defineStructUniformFields(shaderType, uniform.fields, fullName, regType, encoder,
2329 uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002330 }
2331 return;
2332 }
2333 if (uniform.isArrayOfArrays())
2334 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002335 defineArrayUniformElements(shaderType, uniform, fullName, regType, encoder, uniformMap);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002336 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002337 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002338
2339 // Not a struct. Arrays are treated as aggregate types.
2340 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002341 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002342 encoder->enterAggregateType();
2343 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002344
Jamie Madill62d31cb2015-09-11 13:25:51 -04002345 // Advance the uniform offset, to track registers allocation for structs
2346 sh::BlockMemberInfo blockInfo =
Olli Etuaho465835d2017-09-26 13:34:10 +03002347 encoder ? encoder->encodeType(uniform.type, uniform.arraySizes, false)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002348 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002349
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002350 auto uniformMapEntry = uniformMap->find(fullName);
2351 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002352
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002353 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002354 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002355 d3dUniform = uniformMapEntry->second;
2356 }
2357 else
2358 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002359 d3dUniform = new D3DUniform(uniform.type, regType, fullName, uniform.arraySizes, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002360 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002361 }
2362
2363 if (encoder)
2364 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002365 d3dUniform->registerElement =
2366 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002367 unsigned int reg =
2368 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002369 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002370 {
2371 d3dUniform->psRegisterIndex = reg;
2372 }
Xinghua Caob1239382016-12-13 15:07:05 +08002373 else if (shaderType == GL_VERTEX_SHADER)
2374 {
2375 d3dUniform->vsRegisterIndex = reg;
2376 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002377 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002378 {
Xinghua Caob1239382016-12-13 15:07:05 +08002379 ASSERT(shaderType == GL_COMPUTE_SHADER);
2380 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002381 }
Jamie Madillfb536032015-09-11 13:19:49 -04002382
2383 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002384 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002385 {
2386 encoder->exitAggregateType();
2387 }
2388 }
2389}
2390
Jamie Madill134f93d2017-08-31 17:11:00 -04002391// Assume count is already clamped.
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002392template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002393void ProgramD3D::setUniformImpl(const gl::VariableLocation &locationInfo,
Jamie Madill134f93d2017-08-31 17:11:00 -04002394 GLsizei count,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002395 const T *v,
2396 uint8_t *targetData,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002397 GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002398{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002399 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
Jamie Madill33bb7c42017-09-09 23:32:51 -04002400 const int components = targetUniform->typeInfo.componentCount;
Olli Etuaho1734e172017-10-27 15:30:27 +03002401 const unsigned int arrayElementOffset = locationInfo.arrayIndex;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002402
Jamie Madill33bb7c42017-09-09 23:32:51 -04002403 if (targetUniform->typeInfo.type == uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002404 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002405 T *dest = reinterpret_cast<T *>(targetData) + arrayElementOffset * 4;
Jamie Madill33bb7c42017-09-09 23:32:51 -04002406 const T *source = v;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002407
Jamie Madill33bb7c42017-09-09 23:32:51 -04002408 for (GLint i = 0; i < count; i++, dest += 4, source += components)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002409 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002410 memcpy(dest, source, components * sizeof(T));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002411 }
2412 }
Jamie Madill33bb7c42017-09-09 23:32:51 -04002413 else
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002414 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002415 ASSERT(targetUniform->typeInfo.type == gl::VariableBoolVectorType(uniformType));
Olli Etuahoc8538042017-09-27 11:20:15 +03002416 GLint *boolParams = reinterpret_cast<GLint *>(targetData) + arrayElementOffset * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002417
Jamie Madill134f93d2017-08-31 17:11:00 -04002418 for (GLint i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002419 {
Jamie Madill334d6152015-10-22 14:00:28 -04002420 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002421 const T *source = v + (i * components);
2422
2423 for (int c = 0; c < components; c++)
2424 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002425 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002426 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002427 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002428 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002429}
2430
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002431template <typename T>
Jamie Madill33bb7c42017-09-09 23:32:51 -04002432void ProgramD3D::setUniformInternal(GLint location, GLsizei count, const T *v, GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002433{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002434 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2435 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
2436
Jamie Madill33bb7c42017-09-09 23:32:51 -04002437 if (targetUniform->typeInfo.isSampler)
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002438 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002439 ASSERT(uniformType == GL_INT);
Jamie Madill80823cc2017-09-14 15:46:21 -04002440 size_t size = count * sizeof(T);
Jamie Madillacf2f3a2017-11-21 19:22:44 -05002441 GLint *dest = &targetUniform->mSamplerData[locationInfo.arrayIndex];
Jamie Madill80823cc2017-09-14 15:46:21 -04002442 if (memcmp(dest, v, size) != 0)
2443 {
2444 memcpy(dest, v, size);
2445 mDirtySamplerMapping = true;
2446 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002447 return;
2448 }
2449
2450 if (targetUniform->vsData)
2451 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002452 setUniformImpl(locationInfo, count, v, targetUniform->vsData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002453 mVertexUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002454 }
2455
2456 if (targetUniform->psData)
2457 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002458 setUniformImpl(locationInfo, count, v, targetUniform->psData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002459 mFragmentUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002460 }
2461
2462 if (targetUniform->csData)
2463 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002464 setUniformImpl(locationInfo, count, v, targetUniform->csData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002465 mComputeUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002466 }
2467}
2468
2469template <int cols, int rows>
Jamie Madill80823cc2017-09-14 15:46:21 -04002470bool ProgramD3D::setUniformMatrixfvImpl(GLint location,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002471 GLsizei countIn,
2472 GLboolean transpose,
2473 const GLfloat *value,
2474 uint8_t *targetData,
2475 GLenum targetUniformType)
2476{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002477 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002478
Olli Etuaho465835d2017-09-26 13:34:10 +03002479 unsigned int elementCount = targetUniform->getArraySizeProduct();
Olli Etuaho1734e172017-10-27 15:30:27 +03002480 unsigned int arrayElementOffset = mState.getUniformLocations()[location].arrayIndex;
Olli Etuahoc8538042017-09-27 11:20:15 +03002481 unsigned int count =
2482 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002483
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002484 const unsigned int targetMatrixStride = (4 * rows);
Olli Etuahoc8538042017-09-27 11:20:15 +03002485 GLfloat *target = reinterpret_cast<GLfloat *>(
2486 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002487
Jamie Madill80823cc2017-09-14 15:46:21 -04002488 bool dirty = false;
2489
Jamie Madill62d31cb2015-09-11 13:25:51 -04002490 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002491 {
2492 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2493 if (transpose == GL_FALSE)
2494 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002495 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002496 }
2497 else
2498 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002499 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002500 }
2501 target += targetMatrixStride;
2502 value += cols * rows;
2503 }
Jamie Madill80823cc2017-09-14 15:46:21 -04002504
2505 return dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002506}
2507
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002508template <int cols, int rows>
2509void ProgramD3D::setUniformMatrixfvInternal(GLint location,
2510 GLsizei countIn,
2511 GLboolean transpose,
2512 const GLfloat *value,
2513 GLenum targetUniformType)
2514{
2515 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
2516
2517 if (targetUniform->vsData)
2518 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002519 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2520 targetUniform->vsData, targetUniformType))
2521 {
2522 mVertexUniformsDirty = true;
2523 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002524 }
2525
2526 if (targetUniform->psData)
2527 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002528 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2529 targetUniform->psData, targetUniformType))
2530 {
2531 mFragmentUniformsDirty = true;
2532 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002533 }
2534
2535 if (targetUniform->csData)
2536 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002537 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2538 targetUniform->csData, targetUniformType))
2539 {
2540 mComputeUniformsDirty = true;
2541 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002542 }
2543}
2544
Jamie Madill62d31cb2015-09-11 13:25:51 -04002545void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002546{
Olli Etuaho465835d2017-09-26 13:34:10 +03002547 for (size_t uniformIndex = 0; uniformIndex < mD3DUniforms.size(); ++uniformIndex)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002548 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002549 if (mD3DUniforms[uniformIndex]->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002550 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002551 assignSamplerRegisters(uniformIndex);
Jamie Madillfb536032015-09-11 13:19:49 -04002552 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002553 }
2554}
2555
Olli Etuaho465835d2017-09-26 13:34:10 +03002556void ProgramD3D::assignSamplerRegisters(size_t uniformIndex)
Jamie Madillfb536032015-09-11 13:19:49 -04002557{
Olli Etuaho465835d2017-09-26 13:34:10 +03002558 D3DUniform *d3dUniform = mD3DUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002559 ASSERT(d3dUniform->isSampler());
Olli Etuaho465835d2017-09-26 13:34:10 +03002560 // If the uniform is an array of arrays, then we have separate entries for each inner array in
2561 // mD3DUniforms. However, the sampler register info is stored in the shader only for the
2562 // outermost array.
2563 std::vector<unsigned int> subscripts;
2564 const std::string baseName = gl::ParseResourceName(d3dUniform->name, &subscripts);
2565 unsigned int registerOffset = mState.getUniforms()[uniformIndex].flattenedOffsetInParentArrays *
2566 d3dUniform->getArraySizeProduct();
2567
Xinghua Caob1239382016-12-13 15:07:05 +08002568 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2569 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002570 {
Xinghua Caob1239382016-12-13 15:07:05 +08002571 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Olli Etuaho465835d2017-09-26 13:34:10 +03002572 ASSERT(computeShaderD3D->hasUniform(baseName));
2573 d3dUniform->csRegisterIndex =
2574 computeShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002575 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002576 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->typeInfo,
2577 d3dUniform->getArraySizeProduct(), mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002578 }
Xinghua Caob1239382016-12-13 15:07:05 +08002579 else
Jamie Madillfb536032015-09-11 13:19:49 -04002580 {
Xinghua Caob1239382016-12-13 15:07:05 +08002581 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2582 const ShaderD3D *fragmentShaderD3D =
2583 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho465835d2017-09-26 13:34:10 +03002584 ASSERT(vertexShaderD3D->hasUniform(baseName) || fragmentShaderD3D->hasUniform(baseName));
2585 if (vertexShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002586 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002587 d3dUniform->vsRegisterIndex =
2588 vertexShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002589 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002590 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->typeInfo,
2591 d3dUniform->getArraySizeProduct(), mSamplersVS,
2592 &mUsedVertexSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002593 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002594 if (fragmentShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002595 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002596 d3dUniform->psRegisterIndex =
2597 fragmentShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002598 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002599 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->typeInfo,
2600 d3dUniform->getArraySizeProduct(), mSamplersPS, &mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002601 }
Jamie Madillfb536032015-09-11 13:19:49 -04002602 }
2603}
2604
Jamie Madill62d31cb2015-09-11 13:25:51 -04002605// static
2606void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002607 const gl::UniformTypeInfo &typeInfo,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002608 unsigned int samplerCount,
2609 std::vector<Sampler> &outSamplers,
2610 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002611{
2612 unsigned int samplerIndex = startSamplerIndex;
2613
2614 do
2615 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002616 ASSERT(samplerIndex < outSamplers.size());
2617 Sampler *sampler = &outSamplers[samplerIndex];
2618 sampler->active = true;
Xinghua Cao26143fd2017-11-01 18:19:05 +08002619 sampler->textureType = typeInfo.textureType;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002620 sampler->logicalTextureUnit = 0;
2621 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002622 samplerIndex++;
2623 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002624}
2625
Xinghua Cao26143fd2017-11-01 18:19:05 +08002626void ProgramD3D::assignAllImageRegisters()
2627{
2628 for (size_t uniformIndex = 0; uniformIndex < mD3DUniforms.size(); ++uniformIndex)
2629 {
2630 if (mD3DUniforms[uniformIndex]->isImage())
2631 {
2632 assignImageRegisters(uniformIndex);
2633 }
2634 }
2635}
2636
2637void ProgramD3D::assignImageRegisters(size_t uniformIndex)
2638{
2639 D3DUniform *d3dUniform = mD3DUniforms[uniformIndex];
2640 ASSERT(d3dUniform->isImage());
2641 // If the uniform is an array of arrays, then we have separate entries for each inner array in
2642 // mD3DUniforms. However, the image register info is stored in the shader only for the
2643 // outermost array.
2644 std::vector<unsigned int> subscripts;
2645 const std::string baseName = gl::ParseResourceName(d3dUniform->name, &subscripts);
2646 unsigned int registerOffset = mState.getUniforms()[uniformIndex].flattenedOffsetInParentArrays *
2647 d3dUniform->getArraySizeProduct();
2648
2649 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2650 if (computeShader)
2651 {
2652 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2653 ASSERT(computeShaderD3D->hasUniform(baseName));
2654 d3dUniform->csRegisterIndex =
2655 computeShaderD3D->getUniformRegister(baseName) + registerOffset;
2656 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2657 auto bindingIter = mImageBindingMap.find(baseName);
2658 ASSERT(bindingIter != mImageBindingMap.end());
2659 if (d3dUniform->regType == HLSLRegisterType::Texture)
2660 {
2661 AssignImages(d3dUniform->csRegisterIndex, bindingIter->second,
2662 d3dUniform->getArraySizeProduct(), mReadonlyImagesCS,
2663 &mUsedComputeReadonlyImageRange);
2664 }
2665 else if (d3dUniform->regType == HLSLRegisterType::UnorderedAccessView)
2666 {
2667 AssignImages(d3dUniform->csRegisterIndex, bindingIter->second,
2668 d3dUniform->getArraySizeProduct(), mImagesCS, &mUsedComputeImageRange);
2669 }
2670 else
2671 {
2672 UNREACHABLE();
2673 }
2674 }
2675 else
2676 {
2677 // TODO(xinghua.cao@intel.com): Implement image variables in vertex shader and pixel shader.
2678 UNIMPLEMENTED();
2679 }
2680}
2681
2682// static
2683void ProgramD3D::AssignImages(unsigned int startImageIndex,
2684 int startLogicalImageUnit,
2685 unsigned int imageCount,
2686 std::vector<Image> &outImages,
2687 GLuint *outUsedRange)
2688{
2689 unsigned int imageIndex = startImageIndex;
2690 // If declare without a binding qualifier, any uniform image variable (include all elements of
2691 // unbound image array) shoud be bound to unit zero.
2692 if (startLogicalImageUnit == -1)
2693 {
2694 ASSERT(imageIndex < outImages.size());
2695 Image *image = &outImages[imageIndex];
2696 image->active = true;
2697 image->logicalImageUnit = 0;
2698 *outUsedRange = std::max(imageIndex + 1, *outUsedRange);
2699 return;
2700 }
2701
2702 unsigned int logcalImageUnit = startLogicalImageUnit;
2703 do
2704 {
2705 ASSERT(imageIndex < outImages.size());
2706 Image *image = &outImages[imageIndex];
2707 image->active = true;
2708 image->logicalImageUnit = logcalImageUnit;
2709 *outUsedRange = std::max(imageIndex + 1, *outUsedRange);
2710 imageIndex++;
2711 logcalImageUnit++;
2712 } while (imageIndex < startImageIndex + imageCount);
2713}
2714
Brandon Jonesc9610c52014-08-25 17:02:59 -07002715void ProgramD3D::reset()
2716{
Xinghua Caob1239382016-12-13 15:07:05 +08002717 mVertexExecutables.clear();
2718 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002719
Xinghua Caob1239382016-12-13 15:07:05 +08002720 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002721 {
Xinghua Caob1239382016-12-13 15:07:05 +08002722 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002723 }
Brandon Joneseb994362014-09-24 10:27:28 -07002724
Xinghua Caob1239382016-12-13 15:07:05 +08002725 mComputeExecutable.reset(nullptr);
2726
Brandon Jones22502d52014-08-29 16:58:36 -07002727 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002728 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002729
2730 mPixelHLSL.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002731 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
2732 mUsesFragDepth = false;
Martin Radev41ac68e2017-06-06 12:16:58 +03002733 mHasANGLEMultiviewEnabled = false;
2734 mUsesViewID = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002735 mPixelShaderKey.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002736 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002737 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002738
Jamie Madill62d31cb2015-09-11 13:25:51 -04002739 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002740 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002741
Xinghua Caob1239382016-12-13 15:07:05 +08002742 mVertexUniformStorage.reset(nullptr);
2743 mFragmentUniformStorage.reset(nullptr);
2744 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002745
2746 mSamplersPS.clear();
2747 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002748 mSamplersCS.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002749 mImagesCS.clear();
2750 mReadonlyImagesCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002751
Xinghua Cao26143fd2017-11-01 18:19:05 +08002752 mUsedVertexSamplerRange = 0;
2753 mUsedPixelSamplerRange = 0;
2754 mUsedComputeSamplerRange = 0;
2755 mDirtySamplerMapping = true;
2756 mUsedComputeImageRange = 0;
2757 mUsedComputeReadonlyImageRange = 0;
Jamie Madill437d2662014-12-05 14:23:35 -05002758
Jamie Madill8047c0d2016-03-07 13:02:12 -05002759 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002760
Jamie Madill9fc36822015-11-18 13:08:07 -05002761 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002762
2763 mGeometryShaderPreamble.clear();
Jamie Madill561ed3a2017-08-31 16:48:09 -04002764
Jamie Madill4148fd72017-09-14 15:46:20 -04002765 dirtyAllUniforms();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002766
2767 mCachedPixelExecutableIndex.reset();
2768 mCachedVertexExecutableIndex.reset();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002769}
2770
Geoff Lang7dd2e102014-11-10 15:19:26 -05002771unsigned int ProgramD3D::getSerial() const
2772{
2773 return mSerial;
2774}
2775
2776unsigned int ProgramD3D::issueSerial()
2777{
2778 return mCurrentSerial++;
2779}
2780
Jamie Madillbd044ed2017-06-05 12:59:21 -04002781void ProgramD3D::initAttribLocationsToD3DSemantic(const gl::Context *context)
Jamie Madill63805b42015-08-25 13:17:39 -04002782{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002783 gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002784 ASSERT(vertexShader != nullptr);
2785
2786 // Init semantic index
Jamie Madill34ca4f52017-06-13 11:49:39 -04002787 int semanticIndex = 0;
2788 for (const sh::Attribute &attribute : vertexShader->getActiveAttributes(context))
Jamie Madill63805b42015-08-25 13:17:39 -04002789 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002790 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill34ca4f52017-06-13 11:49:39 -04002791 GLuint location = mState.getAttributeLocation(attribute.name);
2792 ASSERT(location != std::numeric_limits<GLuint>::max());
Jamie Madill63805b42015-08-25 13:17:39 -04002793
Jamie Madill8047c0d2016-03-07 13:02:12 -05002794 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002795 {
Jamie Madill34ca4f52017-06-13 11:49:39 -04002796 mAttribLocationToD3DSemantic[location + reg] = semanticIndex++;
Jamie Madill63805b42015-08-25 13:17:39 -04002797 }
2798 }
Jamie Madill437d2662014-12-05 14:23:35 -05002799}
2800
Jamie Madilla779b612017-07-24 11:46:05 -04002801void ProgramD3D::updateCachedInputLayout(Serial associatedSerial, const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002802{
Jamie Madilla779b612017-07-24 11:46:05 -04002803 if (mCurrentVertexArrayStateSerial == associatedSerial)
2804 {
2805 return;
2806 }
2807
2808 mCurrentVertexArrayStateSerial = associatedSerial;
Jamie Madillbd136f92015-08-10 14:51:37 -04002809 mCachedInputLayout.clear();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002810
Jamie Madilld3dfda22015-07-06 08:28:49 -04002811 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002812
Jamie Madill6de51852017-04-12 09:53:01 -04002813 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002814 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002815 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002816
Jamie Madill8047c0d2016-03-07 13:02:12 -05002817 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002818 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002819 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002820 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002821 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002822 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002823 mCachedInputLayout[d3dSemantic] =
2824 GetVertexFormatType(vertexAttributes[locationIndex],
2825 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002826 }
2827 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002828
2829 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04002830
2831 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002832}
2833
2834void ProgramD3D::updateCachedOutputLayout(const gl::Context *context,
2835 const gl::Framebuffer *framebuffer)
2836{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002837 mPixelShaderOutputLayoutCache.clear();
2838
2839 FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(framebuffer);
2840 const auto &colorbuffers = fboD3D->getColorAttachmentsForRender(context);
2841
2842 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
2843 {
2844 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
2845
2846 if (colorbuffer)
2847 {
2848 auto binding = colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0
2849 : colorbuffer->getBinding();
2850 mPixelShaderOutputLayoutCache.push_back(binding);
2851 }
2852 else
2853 {
2854 mPixelShaderOutputLayoutCache.push_back(GL_NONE);
2855 }
2856 }
2857
2858 updateCachedPixelExecutableIndex();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002859}
2860
Jamie Madill192745a2016-12-22 15:58:21 -05002861void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2862 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002863{
Jamie Madill9fc36822015-11-18 13:08:07 -05002864 const std::string &varyingSemantic =
2865 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2866
Jamie Madillccdf74b2015-08-18 10:46:12 -04002867 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002868 mStreamOutVaryings.clear();
2869
Jamie Madill48ef11b2016-04-27 15:21:52 -04002870 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002871 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2872 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002873 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002874 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2875 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002876 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002877 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002878 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002879 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2880 builtins.glPosition.index, 4, outputSlot));
2881 }
2882 }
2883 else if (tfVaryingName == "gl_FragCoord")
2884 {
2885 if (builtins.glFragCoord.enabled)
2886 {
2887 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2888 builtins.glFragCoord.index, 4, outputSlot));
2889 }
2890 }
2891 else if (tfVaryingName == "gl_PointSize")
2892 {
2893 if (builtins.glPointSize.enabled)
2894 {
2895 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2896 }
2897 }
2898 else
2899 {
Jamie Madill192745a2016-12-22 15:58:21 -05002900 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002901 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002902 const auto &varying = *registerInfo.packedVarying->varying;
2903 GLenum transposedType = gl::TransposeMatrixType(varying.type);
jchen108225e732017-11-14 16:29:03 +08002904 int componentCount = gl::VariableColumnCount(transposedType);
2905 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill55c25d02015-11-18 13:08:08 -05002906
Jamie Madill9fc36822015-11-18 13:08:07 -05002907 // There can be more than one register assigned to a particular varying, and each
2908 // register needs its own stream out entry.
jchen108225e732017-11-14 16:29:03 +08002909 if (registerInfo.tfVaryingName() == tfVaryingName)
Jamie Madill9fc36822015-11-18 13:08:07 -05002910 {
2911 mStreamOutVaryings.push_back(D3DVarying(
2912 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2913 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002914 }
2915 }
2916 }
2917}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002918
2919D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2920{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002921 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002922}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002923
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002924const D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location) const
2925{
2926 return mD3DUniforms[mState.getUniformLocations()[location].index];
2927}
2928
Sami Väisänen46eaa942016-06-29 10:26:37 +03002929void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2930 GLenum genMode,
2931 GLint components,
2932 const GLfloat *coeffs)
2933{
2934 UNREACHABLE();
2935}
2936
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002937bool ProgramD3D::hasVertexExecutableForCachedInputLayout()
2938{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002939 return mCachedVertexExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002940}
2941
2942bool ProgramD3D::hasGeometryExecutableForPrimitiveType(GLenum drawMode)
2943{
2944 if (!usesGeometryShader(drawMode))
2945 {
2946 // No shader necessary mean we have the required (null) executable.
2947 return true;
2948 }
2949
2950 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
2951 return mGeometryExecutables[geometryShaderType].get() != nullptr;
2952}
2953
2954bool ProgramD3D::hasPixelExecutableForCachedOutputLayout()
2955{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002956 return mCachedPixelExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002957}
2958
Jamie Madill54164b02017-08-28 15:17:37 -04002959template <typename DestT>
2960void ProgramD3D::getUniformInternal(GLint location, DestT *dataOut) const
2961{
2962 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2963 const gl::LinkedUniform &uniform = mState.getUniforms()[locationInfo.index];
2964
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002965 const D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Olli Etuaho1734e172017-10-27 15:30:27 +03002966 const uint8_t *srcPointer = targetUniform->getDataPtrToElement(locationInfo.arrayIndex);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002967
2968 if (gl::IsMatrixType(uniform.type))
2969 {
2970 GetMatrixUniform(gl::VariableColumnCount(uniform.type), gl::VariableRowCount(uniform.type),
2971 dataOut, reinterpret_cast<const DestT *>(srcPointer));
2972 }
2973 else
2974 {
2975 memcpy(dataOut, srcPointer, uniform.getElementSize());
2976 }
Jamie Madill54164b02017-08-28 15:17:37 -04002977}
2978
2979void ProgramD3D::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
2980{
2981 getUniformInternal(location, params);
2982}
2983
2984void ProgramD3D::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
2985{
2986 getUniformInternal(location, params);
2987}
2988
2989void ProgramD3D::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
2990{
2991 getUniformInternal(location, params);
2992}
2993
Jamie Madill0e7f1732017-09-09 23:32:50 -04002994void ProgramD3D::updateCachedVertexExecutableIndex()
2995{
2996 mCachedVertexExecutableIndex.reset();
2997 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
2998 {
2999 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
3000 {
3001 mCachedVertexExecutableIndex = executableIndex;
3002 break;
3003 }
3004 }
3005}
3006
3007void ProgramD3D::updateCachedPixelExecutableIndex()
3008{
3009 mCachedPixelExecutableIndex.reset();
3010 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
3011 {
3012 if (mPixelExecutables[executableIndex]->matchesSignature(mPixelShaderOutputLayoutCache))
3013 {
3014 mCachedPixelExecutableIndex = executableIndex;
3015 break;
3016 }
3017 }
3018}
3019
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003020void ProgramD3D::linkResources(const gl::Context *context,
3021 const gl::ProgramLinkedResources &resources)
3022{
3023 UniformBlockInfo uniformBlockInfo;
3024
3025 if (mState.getAttachedVertexShader())
3026 {
3027 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedVertexShader());
3028 }
3029
3030 if (mState.getAttachedFragmentShader())
3031 {
3032 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedFragmentShader());
3033 }
3034
3035 if (mState.getAttachedComputeShader())
3036 {
3037 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedComputeShader());
3038 }
3039
3040 // Gather interface block info.
3041 auto getUniformBlockSize = [&uniformBlockInfo](const std::string &name,
3042 const std::string &mappedName, size_t *sizeOut) {
3043 return uniformBlockInfo.getBlockSize(name, mappedName, sizeOut);
3044 };
3045
3046 auto getUniformBlockMemberInfo = [&uniformBlockInfo](const std::string &name,
3047 const std::string &mappedName,
3048 sh::BlockMemberInfo *infoOut) {
3049 return uniformBlockInfo.getBlockMemberInfo(name, mappedName, infoOut);
3050 };
3051
3052 resources.uniformBlockLinker.linkBlocks(getUniformBlockSize, getUniformBlockMemberInfo);
3053 initializeUniformBlocks();
3054
3055 // TODO(jiajia.qin@intel.com): Determine correct shader storage block info.
3056 auto getShaderStorageBlockSize = [](const std::string &name, const std::string &mappedName,
3057 size_t *sizeOut) {
3058 *sizeOut = 0;
3059 return true;
3060 };
3061
3062 auto getShaderStorageBlockMemberInfo =
3063 [](const std::string &name, const std::string &mappedName, sh::BlockMemberInfo *infoOut) {
3064 *infoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
3065 return true;
3066 };
3067
3068 resources.shaderStorageBlockLinker.linkBlocks(getShaderStorageBlockSize,
3069 getShaderStorageBlockMemberInfo);
3070}
3071
Jamie Madill8047c0d2016-03-07 13:02:12 -05003072} // namespace rx