blob: 80ae30be863a8cccd955f92c3ce3c72b21ba9d7c [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
1732 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1733 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1734 // intelligently, but D3D9 assumes one semantic per register.
1735 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madillc9727f32017-11-07 12:37:07 -05001736 resources.varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Xinghua Caob1239382016-12-13 15:07:05 +08001737 {
1738 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001739 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001740 }
Xinghua Caob1239382016-12-13 15:07:05 +08001741
1742 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madillc9727f32017-11-07 12:37:07 -05001743 BuiltinVaryingsD3D builtins(metadata, resources.varyingPacking);
Xinghua Caob1239382016-12-13 15:07:05 +08001744
Jamie Madillc9727f32017-11-07 12:37:07 -05001745 mDynamicHLSL->generateShaderLinkHLSL(context, mState, metadata, resources.varyingPacking,
1746 builtins, &mPixelHLSL, &mVertexHLSL);
Xinghua Caob1239382016-12-13 15:07:05 +08001747
1748 mUsesPointSize = vertexShaderD3D->usesPointSize();
1749 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
Xinghua Cao26143fd2017-11-01 18:19:05 +08001750 mUsesFragDepth = metadata.usesFragDepth();
Martin Radev41ac68e2017-06-06 12:16:58 +03001751 mUsesViewID = metadata.usesViewID();
1752 mHasANGLEMultiviewEnabled = metadata.hasANGLEMultiviewEnabled();
Xinghua Caob1239382016-12-13 15:07:05 +08001753
1754 // Cache if we use flat shading
Jamie Madillbd044ed2017-06-05 12:59:21 -04001755 mUsesFlatInterpolation =
Jiawei Shao3d404882017-10-16 13:30:48 +08001756 (FindFlatInterpolationVarying(fragmentShader->getInputVaryings(context)) ||
1757 FindFlatInterpolationVarying(vertexShader->getOutputVaryings(context)));
Xinghua Caob1239382016-12-13 15:07:05 +08001758
1759 if (mRenderer->getMajorShaderModel() >= 4)
1760 {
Martin Radev41ac68e2017-06-06 12:16:58 +03001761 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(
Jamie Madillc9727f32017-11-07 12:37:07 -05001762 resources.varyingPacking, builtins, mHasANGLEMultiviewEnabled,
Martin Radevc1d4e552017-08-21 12:01:10 +03001763 metadata.canSelectViewInVertexShader());
Xinghua Caob1239382016-12-13 15:07:05 +08001764 }
1765
Jamie Madillbd044ed2017-06-05 12:59:21 -04001766 initAttribLocationsToD3DSemantic(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001767
Jamie Madillbd044ed2017-06-05 12:59:21 -04001768 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001769
Yunchao He85072e82017-11-14 15:43:28 +08001770 gatherTransformFeedbackVaryings(resources.varyingPacking, builtins[gl::SHADER_VERTEX]);
Xinghua Caob1239382016-12-13 15:07:05 +08001771
Jamie Madill9cf9e872017-06-05 12:59:25 -04001772 gl::LinkResult result = compileProgramExecutables(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001773 if (result.isError())
1774 {
1775 infoLog << result.getError().getMessage();
1776 return result;
1777 }
1778 else if (!result.getResult())
1779 {
1780 infoLog << "Failed to create D3D shaders.";
1781 return result;
1782 }
Austin Kinross02df7962015-07-01 10:03:42 -07001783 }
1784
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001785 linkResources(context, resources);
1786
Jamie Madillb0a838b2016-11-13 20:02:12 -05001787 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001788}
1789
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001790GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001791{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001792 // TODO(jmadill): Do something useful here?
1793 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001794}
1795
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001796void ProgramD3D::initializeUniformBlocks()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001797{
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001798 if (mState.getUniformBlocks().empty())
Jamie Madill51f522f2016-12-21 15:10:55 -05001799 {
1800 return;
1801 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001802
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001803 ASSERT(mD3DUniformBlocks.empty());
1804
Jamie Madill62d31cb2015-09-11 13:25:51 -04001805 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001806 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1807 const ShaderD3D *fragmentShaderD3D =
1808 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1809 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001810
Jiajia Qin729b2c62017-08-14 09:36:11 +08001811 for (const gl::InterfaceBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001812 {
1813 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1814
Jamie Madill4a3c2342015-10-08 12:58:45 -04001815 D3DUniformBlock d3dUniformBlock;
1816
Jamie Madill62d31cb2015-09-11 13:25:51 -04001817 if (uniformBlock.vertexStaticUse)
1818 {
Xinghua Caob1239382016-12-13 15:07:05 +08001819 ASSERT(vertexShaderD3D != nullptr);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001820 unsigned int baseRegister = vertexShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001821 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001822 }
1823
1824 if (uniformBlock.fragmentStaticUse)
1825 {
Xinghua Caob1239382016-12-13 15:07:05 +08001826 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001827 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001828 fragmentShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001829 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001830 }
1831
Xinghua Caob1239382016-12-13 15:07:05 +08001832 if (uniformBlock.computeStaticUse)
1833 {
1834 ASSERT(computeShaderD3D != nullptr);
1835 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001836 computeShaderD3D->getUniformBlockRegister(uniformBlock.name);
Xinghua Caob1239382016-12-13 15:07:05 +08001837 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1838 }
1839
Jamie Madill4a3c2342015-10-08 12:58:45 -04001840 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001841 }
1842}
1843
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001844void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001845{
1846 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001847 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001848 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001849 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001850 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001851 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001852 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001853 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001854 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001855 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001856 vertexRegisters = std::max(vertexRegisters,
1857 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001858 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001859 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001860 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001861 fragmentRegisters = std::max(
1862 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001863 }
Xinghua Caob1239382016-12-13 15:07:05 +08001864 if (d3dUniform->isReferencedByComputeShader())
1865 {
1866 computeRegisters = std::max(
1867 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1868 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001869 }
1870 }
1871
Xinghua Caob1239382016-12-13 15:07:05 +08001872 mVertexUniformStorage =
1873 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1874 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1875 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1876 mComputeUniformStorage =
1877 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001878
1879 // Iterate the uniforms again to assign data pointers to default block uniforms.
1880 for (D3DUniform *d3dUniform : mD3DUniforms)
1881 {
1882 if (d3dUniform->isSampler())
1883 {
Olli Etuaho465835d2017-09-26 13:34:10 +03001884 d3dUniform->mSamplerData.resize(d3dUniform->getArraySizeProduct(), 0);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001885 continue;
1886 }
1887
1888 if (d3dUniform->isReferencedByVertexShader())
1889 {
1890 d3dUniform->vsData = mVertexUniformStorage->getDataPointer(d3dUniform->vsRegisterIndex,
1891 d3dUniform->registerElement);
1892 }
1893
1894 if (d3dUniform->isReferencedByFragmentShader())
1895 {
1896 d3dUniform->psData = mFragmentUniformStorage->getDataPointer(
1897 d3dUniform->psRegisterIndex, d3dUniform->registerElement);
1898 }
1899
1900 if (d3dUniform->isReferencedByComputeShader())
1901 {
1902 d3dUniform->csData = mComputeUniformStorage->getDataPointer(
1903 d3dUniform->csRegisterIndex, d3dUniform->registerElement);
1904 }
1905 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001906}
1907
Jamie Madilld63961d2017-09-12 15:22:57 -04001908void ProgramD3D::updateUniformBufferCache(const gl::Caps &caps,
1909 unsigned int reservedVertex,
1910 unsigned int reservedFragment)
Brandon Jones18bd4102014-09-22 14:21:44 -07001911{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001912 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001913 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001914 return;
Jamie Madill4a3c2342015-10-08 12:58:45 -04001915 }
1916
Jamie Madill03260fa2015-06-22 13:57:22 -04001917 mVertexUBOCache.clear();
1918 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001919
Jamie Madill4a3c2342015-10-08 12:58:45 -04001920 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001922 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001923 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001924 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001925
Brandon Jones18bd4102014-09-22 14:21:44 -07001926 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001927 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001928 {
1929 continue;
1930 }
1931
Jamie Madill4a3c2342015-10-08 12:58:45 -04001932 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001933 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001934 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedVertex;
1935 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001936
Jamie Madill969194d2015-07-20 14:36:56 -04001937 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001938 {
1939 mVertexUBOCache.resize(registerIndex + 1, -1);
1940 }
1941
1942 ASSERT(mVertexUBOCache[registerIndex] == -1);
1943 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001944 }
1945
Jamie Madill4a3c2342015-10-08 12:58:45 -04001946 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001947 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001948 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedFragment;
1949 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001950
1951 if (mFragmentUBOCache.size() <= registerIndex)
1952 {
1953 mFragmentUBOCache.resize(registerIndex + 1, -1);
1954 }
1955
1956 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1957 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001958 }
1959 }
Jamie Madilld63961d2017-09-12 15:22:57 -04001960}
Brandon Jones18bd4102014-09-22 14:21:44 -07001961
Jamie Madilld63961d2017-09-12 15:22:57 -04001962const std::vector<GLint> &ProgramD3D::getVertexUniformBufferCache() const
1963{
1964 return mVertexUBOCache;
1965}
1966
1967const std::vector<GLint> &ProgramD3D::getFragmentUniformBufferCache() const
1968{
1969 return mFragmentUBOCache;
Brandon Jones18bd4102014-09-22 14:21:44 -07001970}
1971
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001972void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001973{
Jamie Madill4148fd72017-09-14 15:46:20 -04001974 mVertexUniformsDirty = true;
1975 mFragmentUniformsDirty = true;
1976 mComputeUniformsDirty = true;
1977}
1978
1979void ProgramD3D::markUniformsClean()
1980{
1981 mVertexUniformsDirty = false;
1982 mFragmentUniformsDirty = false;
1983 mComputeUniformsDirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001984}
1985
Jamie Madill334d6152015-10-22 14:00:28 -04001986void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001987{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001988 setUniformInternal(location, count, v, GL_FLOAT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001989}
1990
1991void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1992{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001993 setUniformInternal(location, count, v, GL_FLOAT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001994}
1995
1996void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1997{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001998 setUniformInternal(location, count, v, GL_FLOAT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001999}
2000
2001void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
2002{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002003 setUniformInternal(location, count, v, GL_FLOAT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002004}
2005
Jamie Madill334d6152015-10-22 14:00:28 -04002006void ProgramD3D::setUniformMatrix2fv(GLint location,
2007 GLsizei count,
2008 GLboolean transpose,
2009 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002010{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002011 setUniformMatrixfvInternal<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002012}
2013
Jamie Madill334d6152015-10-22 14:00:28 -04002014void ProgramD3D::setUniformMatrix3fv(GLint location,
2015 GLsizei count,
2016 GLboolean transpose,
2017 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002018{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002019 setUniformMatrixfvInternal<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002020}
2021
Jamie Madill334d6152015-10-22 14:00:28 -04002022void ProgramD3D::setUniformMatrix4fv(GLint location,
2023 GLsizei count,
2024 GLboolean transpose,
2025 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002026{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002027 setUniformMatrixfvInternal<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002028}
2029
Jamie Madill334d6152015-10-22 14:00:28 -04002030void ProgramD3D::setUniformMatrix2x3fv(GLint location,
2031 GLsizei count,
2032 GLboolean transpose,
2033 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002034{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002035 setUniformMatrixfvInternal<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002036}
2037
Jamie Madill334d6152015-10-22 14:00:28 -04002038void ProgramD3D::setUniformMatrix3x2fv(GLint location,
2039 GLsizei count,
2040 GLboolean transpose,
2041 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002042{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002043 setUniformMatrixfvInternal<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002044}
2045
Jamie Madill334d6152015-10-22 14:00:28 -04002046void ProgramD3D::setUniformMatrix2x4fv(GLint location,
2047 GLsizei count,
2048 GLboolean transpose,
2049 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002050{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002051 setUniformMatrixfvInternal<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002052}
2053
Jamie Madill334d6152015-10-22 14:00:28 -04002054void ProgramD3D::setUniformMatrix4x2fv(GLint location,
2055 GLsizei count,
2056 GLboolean transpose,
2057 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002058{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002059 setUniformMatrixfvInternal<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002060}
2061
Jamie Madill334d6152015-10-22 14:00:28 -04002062void ProgramD3D::setUniformMatrix3x4fv(GLint location,
2063 GLsizei count,
2064 GLboolean transpose,
2065 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002066{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002067 setUniformMatrixfvInternal<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002068}
2069
Jamie Madill334d6152015-10-22 14:00:28 -04002070void ProgramD3D::setUniformMatrix4x3fv(GLint location,
2071 GLsizei count,
2072 GLboolean transpose,
2073 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002074{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002075 setUniformMatrixfvInternal<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002076}
2077
2078void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
2079{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002080 setUniformInternal(location, count, v, GL_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002081}
2082
2083void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2084{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002085 setUniformInternal(location, count, v, GL_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002086}
2087
2088void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2089{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002090 setUniformInternal(location, count, v, GL_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002091}
2092
2093void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2094{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002095 setUniformInternal(location, count, v, GL_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002096}
2097
2098void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2099{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002100 setUniformInternal(location, count, v, GL_UNSIGNED_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002101}
2102
2103void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2104{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002105 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002106}
2107
2108void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2109{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002110 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002111}
2112
2113void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2114{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002115 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002116}
2117
Jamie Madillf4141212017-12-12 15:08:07 -05002118void ProgramD3D::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04002119{
Jamie Madillf4141212017-12-12 15:08:07 -05002120 mRenderer->onDirtyUniformBlockBinding(uniformBlockIndex);
Geoff Lang5d124a62015-09-15 13:03:27 -04002121}
2122
Jamie Madillbd044ed2017-06-05 12:59:21 -04002123void ProgramD3D::defineUniformsAndAssignRegisters(const gl::Context *context)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002124{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002125 D3DUniformMap uniformMap;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002126 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08002127 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05002128 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002129 for (const sh::Uniform &computeUniform : computeShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04002130 {
Xinghua Caob1239382016-12-13 15:07:05 +08002131 if (computeUniform.staticUse)
2132 {
2133 defineUniformBase(computeShader, computeUniform, &uniformMap);
2134 }
Jamie Madillfb536032015-09-11 13:19:49 -04002135 }
2136 }
Xinghua Caob1239382016-12-13 15:07:05 +08002137 else
Jamie Madillfb536032015-09-11 13:19:49 -04002138 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002139 gl::Shader *vertexShader = mState.getAttachedVertexShader();
2140 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04002141 {
Xinghua Caob1239382016-12-13 15:07:05 +08002142 if (vertexUniform.staticUse)
2143 {
2144 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
2145 }
2146 }
2147
Jamie Madillbd044ed2017-06-05 12:59:21 -04002148 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
2149 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms(context))
Xinghua Caob1239382016-12-13 15:07:05 +08002150 {
2151 if (fragmentUniform.staticUse)
2152 {
2153 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
2154 }
Jamie Madillfb536032015-09-11 13:19:49 -04002155 }
2156 }
2157
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002158 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002159 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002160 {
2161 if (!glUniform.isInDefaultBlock())
2162 continue;
2163
Olli Etuahod2551232017-10-26 20:03:33 +03002164 std::string name = glUniform.name;
2165 if (glUniform.isArray())
2166 {
2167 // In the program state, array uniform names include [0] as in the program resource
2168 // spec. Here we don't include it.
2169 // TODO(oetuaho@nvidia.com): consider using the same uniform naming here as in the GL
2170 // layer.
2171 ASSERT(angle::EndsWith(name, "[0]"));
2172 name.resize(name.length() - 3);
2173 }
2174 auto mapEntry = uniformMap.find(name);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002175 ASSERT(mapEntry != uniformMap.end());
2176 mD3DUniforms.push_back(mapEntry->second);
2177 }
2178
Jamie Madill62d31cb2015-09-11 13:25:51 -04002179 assignAllSamplerRegisters();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002180 assignAllImageRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04002181 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04002182}
2183
Jamie Madill91445bc2015-09-23 16:47:53 -04002184void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002185 const sh::Uniform &uniform,
2186 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04002187{
Xinghua Cao26143fd2017-11-01 18:19:05 +08002188 // Samplers get their registers assigned in assignAllSamplerRegisters, and images get their
2189 // registers assigned in assignAllImageRegisters.
2190 if (gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04002191 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002192 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::Texture, nullptr,
2193 uniformMap);
2194 return;
2195 }
2196 else if (gl::IsImageType(uniform.type))
2197 {
2198 if (uniform.readonly)
2199 {
2200 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::Texture,
2201 nullptr, uniformMap);
2202 }
2203 else
2204 {
2205 defineUniform(shader->getType(), uniform, uniform.name,
2206 HLSLRegisterType::UnorderedAccessView, nullptr, uniformMap);
2207 }
2208 mImageBindingMap[uniform.name] = uniform.binding;
2209 return;
2210 }
2211 else if (uniform.isBuiltIn())
2212 {
2213 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::None, nullptr,
2214 uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04002215 return;
2216 }
2217
Jamie Madill91445bc2015-09-23 16:47:53 -04002218 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
Jamie Madill91445bc2015-09-23 16:47:53 -04002219 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
Xinghua Cao26143fd2017-11-01 18:19:05 +08002220 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04002221 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002222 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002223
Xinghua Cao26143fd2017-11-01 18:19:05 +08002224 defineUniform(shader->getType(), uniform, uniform.name, HLSLRegisterType::None, &encoder,
2225 uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002226}
2227
Jamie Madill62d31cb2015-09-11 13:25:51 -04002228D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
2229{
2230 for (D3DUniform *d3dUniform : mD3DUniforms)
2231 {
2232 if (d3dUniform->name == name)
2233 {
2234 return d3dUniform;
2235 }
2236 }
2237
2238 return nullptr;
2239}
2240
Olli Etuaho465835d2017-09-26 13:34:10 +03002241void ProgramD3D::defineStructUniformFields(GLenum shaderType,
2242 const std::vector<sh::ShaderVariable> &fields,
2243 const std::string &namePrefix,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002244 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002245 sh::HLSLBlockEncoder *encoder,
2246 D3DUniformMap *uniformMap)
2247{
2248 if (encoder)
2249 encoder->enterAggregateType();
2250
2251 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
2252 {
2253 const sh::ShaderVariable &field = fields[fieldIndex];
2254 const std::string &fieldFullName = (namePrefix + "." + field.name);
2255
2256 // Samplers get their registers assigned in assignAllSamplerRegisters.
2257 // Also they couldn't use the same encoder as the rest of the struct, since they are
2258 // extracted out of the struct by the shader translator.
2259 if (gl::IsSamplerType(field.type))
2260 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002261 defineUniform(shaderType, field, fieldFullName, regType, nullptr, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002262 }
2263 else
2264 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002265 defineUniform(shaderType, field, fieldFullName, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002266 }
2267 }
2268
2269 if (encoder)
2270 encoder->exitAggregateType();
2271}
2272
2273void ProgramD3D::defineArrayOfStructsUniformFields(GLenum shaderType,
2274 const sh::ShaderVariable &uniform,
2275 unsigned int arrayNestingIndex,
2276 const std::string &prefix,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002277 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002278 sh::HLSLBlockEncoder *encoder,
2279 D3DUniformMap *uniformMap)
2280{
2281 // Nested arrays are processed starting from outermost (arrayNestingIndex 0u) and ending at the
2282 // innermost.
2283 const unsigned int currentArraySize = uniform.getNestedArraySize(arrayNestingIndex);
2284 for (unsigned int arrayElement = 0u; arrayElement < currentArraySize; ++arrayElement)
2285 {
2286 const std::string &elementString = prefix + ArrayString(arrayElement);
2287 if (arrayNestingIndex + 1u < uniform.arraySizes.size())
2288 {
2289 defineArrayOfStructsUniformFields(shaderType, uniform, arrayNestingIndex + 1u,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002290 elementString, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002291 }
2292 else
2293 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002294 defineStructUniformFields(shaderType, uniform.fields, elementString, regType, encoder,
Olli Etuaho465835d2017-09-26 13:34:10 +03002295 uniformMap);
2296 }
2297 }
2298}
2299
2300void ProgramD3D::defineArrayUniformElements(GLenum shaderType,
2301 const sh::ShaderVariable &uniform,
2302 const std::string &fullName,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002303 const HLSLRegisterType regType,
Olli Etuaho465835d2017-09-26 13:34:10 +03002304 sh::HLSLBlockEncoder *encoder,
2305 D3DUniformMap *uniformMap)
2306{
2307 if (encoder)
2308 encoder->enterAggregateType();
2309
2310 sh::ShaderVariable uniformElement = uniform;
2311 uniformElement.arraySizes.pop_back();
2312 for (unsigned int arrayIndex = 0u; arrayIndex < uniform.getOutermostArraySize(); ++arrayIndex)
2313 {
2314 std::string elementFullName = fullName + ArrayString(arrayIndex);
Xinghua Cao26143fd2017-11-01 18:19:05 +08002315 defineUniform(shaderType, uniformElement, elementFullName, regType, encoder, uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002316 }
2317
2318 if (encoder)
2319 encoder->exitAggregateType();
2320}
2321
Jamie Madill91445bc2015-09-23 16:47:53 -04002322void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002323 const sh::ShaderVariable &uniform,
2324 const std::string &fullName,
Xinghua Cao26143fd2017-11-01 18:19:05 +08002325 const HLSLRegisterType regType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002326 sh::HLSLBlockEncoder *encoder,
2327 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002328{
2329 if (uniform.isStruct())
2330 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002331 if (uniform.isArray())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002332 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002333 defineArrayOfStructsUniformFields(shaderType, uniform, 0u, fullName, regType, encoder,
Olli Etuaho465835d2017-09-26 13:34:10 +03002334 uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002335 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002336 else
2337 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002338 defineStructUniformFields(shaderType, uniform.fields, fullName, regType, encoder,
2339 uniformMap);
Olli Etuaho465835d2017-09-26 13:34:10 +03002340 }
2341 return;
2342 }
2343 if (uniform.isArrayOfArrays())
2344 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002345 defineArrayUniformElements(shaderType, uniform, fullName, regType, encoder, uniformMap);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002346 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002347 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002348
2349 // Not a struct. Arrays are treated as aggregate types.
2350 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002351 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002352 encoder->enterAggregateType();
2353 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002354
Jamie Madill62d31cb2015-09-11 13:25:51 -04002355 // Advance the uniform offset, to track registers allocation for structs
2356 sh::BlockMemberInfo blockInfo =
Olli Etuaho465835d2017-09-26 13:34:10 +03002357 encoder ? encoder->encodeType(uniform.type, uniform.arraySizes, false)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002358 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002359
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002360 auto uniformMapEntry = uniformMap->find(fullName);
2361 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002362
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002363 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002364 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002365 d3dUniform = uniformMapEntry->second;
2366 }
2367 else
2368 {
Xinghua Cao26143fd2017-11-01 18:19:05 +08002369 d3dUniform = new D3DUniform(uniform.type, regType, fullName, uniform.arraySizes, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002370 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002371 }
2372
2373 if (encoder)
2374 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002375 d3dUniform->registerElement =
2376 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002377 unsigned int reg =
2378 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002379 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002380 {
2381 d3dUniform->psRegisterIndex = reg;
2382 }
Xinghua Caob1239382016-12-13 15:07:05 +08002383 else if (shaderType == GL_VERTEX_SHADER)
2384 {
2385 d3dUniform->vsRegisterIndex = reg;
2386 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002387 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002388 {
Xinghua Caob1239382016-12-13 15:07:05 +08002389 ASSERT(shaderType == GL_COMPUTE_SHADER);
2390 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002391 }
Jamie Madillfb536032015-09-11 13:19:49 -04002392
2393 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002394 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002395 {
2396 encoder->exitAggregateType();
2397 }
2398 }
2399}
2400
Jamie Madill134f93d2017-08-31 17:11:00 -04002401// Assume count is already clamped.
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002402template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002403void ProgramD3D::setUniformImpl(const gl::VariableLocation &locationInfo,
Jamie Madill134f93d2017-08-31 17:11:00 -04002404 GLsizei count,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002405 const T *v,
2406 uint8_t *targetData,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002407 GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002408{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002409 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
Jamie Madill33bb7c42017-09-09 23:32:51 -04002410 const int components = targetUniform->typeInfo.componentCount;
Olli Etuaho1734e172017-10-27 15:30:27 +03002411 const unsigned int arrayElementOffset = locationInfo.arrayIndex;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002412
Jamie Madill33bb7c42017-09-09 23:32:51 -04002413 if (targetUniform->typeInfo.type == uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002414 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002415 T *dest = reinterpret_cast<T *>(targetData) + arrayElementOffset * 4;
Jamie Madill33bb7c42017-09-09 23:32:51 -04002416 const T *source = v;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002417
Jamie Madill33bb7c42017-09-09 23:32:51 -04002418 for (GLint i = 0; i < count; i++, dest += 4, source += components)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002419 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002420 memcpy(dest, source, components * sizeof(T));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002421 }
2422 }
Jamie Madill33bb7c42017-09-09 23:32:51 -04002423 else
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002424 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002425 ASSERT(targetUniform->typeInfo.type == gl::VariableBoolVectorType(uniformType));
Olli Etuahoc8538042017-09-27 11:20:15 +03002426 GLint *boolParams = reinterpret_cast<GLint *>(targetData) + arrayElementOffset * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002427
Jamie Madill134f93d2017-08-31 17:11:00 -04002428 for (GLint i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002429 {
Jamie Madill334d6152015-10-22 14:00:28 -04002430 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002431 const T *source = v + (i * components);
2432
2433 for (int c = 0; c < components; c++)
2434 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002435 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002436 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002437 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002438 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002439}
2440
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002441template <typename T>
Jamie Madill33bb7c42017-09-09 23:32:51 -04002442void ProgramD3D::setUniformInternal(GLint location, GLsizei count, const T *v, GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002443{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002444 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2445 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
2446
Jamie Madill33bb7c42017-09-09 23:32:51 -04002447 if (targetUniform->typeInfo.isSampler)
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002448 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002449 ASSERT(uniformType == GL_INT);
Jamie Madill80823cc2017-09-14 15:46:21 -04002450 size_t size = count * sizeof(T);
Jamie Madillacf2f3a2017-11-21 19:22:44 -05002451 GLint *dest = &targetUniform->mSamplerData[locationInfo.arrayIndex];
Jamie Madill80823cc2017-09-14 15:46:21 -04002452 if (memcmp(dest, v, size) != 0)
2453 {
2454 memcpy(dest, v, size);
2455 mDirtySamplerMapping = true;
2456 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002457 return;
2458 }
2459
2460 if (targetUniform->vsData)
2461 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002462 setUniformImpl(locationInfo, count, v, targetUniform->vsData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002463 mVertexUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002464 }
2465
2466 if (targetUniform->psData)
2467 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002468 setUniformImpl(locationInfo, count, v, targetUniform->psData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002469 mFragmentUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002470 }
2471
2472 if (targetUniform->csData)
2473 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002474 setUniformImpl(locationInfo, count, v, targetUniform->csData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002475 mComputeUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002476 }
2477}
2478
2479template <int cols, int rows>
Jamie Madill80823cc2017-09-14 15:46:21 -04002480bool ProgramD3D::setUniformMatrixfvImpl(GLint location,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002481 GLsizei countIn,
2482 GLboolean transpose,
2483 const GLfloat *value,
2484 uint8_t *targetData,
2485 GLenum targetUniformType)
2486{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002487 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002488
Olli Etuaho465835d2017-09-26 13:34:10 +03002489 unsigned int elementCount = targetUniform->getArraySizeProduct();
Olli Etuaho1734e172017-10-27 15:30:27 +03002490 unsigned int arrayElementOffset = mState.getUniformLocations()[location].arrayIndex;
Olli Etuahoc8538042017-09-27 11:20:15 +03002491 unsigned int count =
2492 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002493
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002494 const unsigned int targetMatrixStride = (4 * rows);
Olli Etuahoc8538042017-09-27 11:20:15 +03002495 GLfloat *target = reinterpret_cast<GLfloat *>(
2496 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002497
Jamie Madill80823cc2017-09-14 15:46:21 -04002498 bool dirty = false;
2499
Jamie Madill62d31cb2015-09-11 13:25:51 -04002500 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002501 {
2502 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2503 if (transpose == GL_FALSE)
2504 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002505 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002506 }
2507 else
2508 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002509 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002510 }
2511 target += targetMatrixStride;
2512 value += cols * rows;
2513 }
Jamie Madill80823cc2017-09-14 15:46:21 -04002514
2515 return dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002516}
2517
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002518template <int cols, int rows>
2519void ProgramD3D::setUniformMatrixfvInternal(GLint location,
2520 GLsizei countIn,
2521 GLboolean transpose,
2522 const GLfloat *value,
2523 GLenum targetUniformType)
2524{
2525 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
2526
2527 if (targetUniform->vsData)
2528 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002529 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2530 targetUniform->vsData, targetUniformType))
2531 {
2532 mVertexUniformsDirty = true;
2533 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002534 }
2535
2536 if (targetUniform->psData)
2537 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002538 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2539 targetUniform->psData, targetUniformType))
2540 {
2541 mFragmentUniformsDirty = true;
2542 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002543 }
2544
2545 if (targetUniform->csData)
2546 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002547 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2548 targetUniform->csData, targetUniformType))
2549 {
2550 mComputeUniformsDirty = true;
2551 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002552 }
2553}
2554
Jamie Madill62d31cb2015-09-11 13:25:51 -04002555void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002556{
Olli Etuaho465835d2017-09-26 13:34:10 +03002557 for (size_t uniformIndex = 0; uniformIndex < mD3DUniforms.size(); ++uniformIndex)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002558 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002559 if (mD3DUniforms[uniformIndex]->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002560 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002561 assignSamplerRegisters(uniformIndex);
Jamie Madillfb536032015-09-11 13:19:49 -04002562 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002563 }
2564}
2565
Olli Etuaho465835d2017-09-26 13:34:10 +03002566void ProgramD3D::assignSamplerRegisters(size_t uniformIndex)
Jamie Madillfb536032015-09-11 13:19:49 -04002567{
Olli Etuaho465835d2017-09-26 13:34:10 +03002568 D3DUniform *d3dUniform = mD3DUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002569 ASSERT(d3dUniform->isSampler());
Olli Etuaho465835d2017-09-26 13:34:10 +03002570 // If the uniform is an array of arrays, then we have separate entries for each inner array in
2571 // mD3DUniforms. However, the sampler register info is stored in the shader only for the
2572 // outermost array.
2573 std::vector<unsigned int> subscripts;
2574 const std::string baseName = gl::ParseResourceName(d3dUniform->name, &subscripts);
2575 unsigned int registerOffset = mState.getUniforms()[uniformIndex].flattenedOffsetInParentArrays *
2576 d3dUniform->getArraySizeProduct();
2577
Xinghua Caob1239382016-12-13 15:07:05 +08002578 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2579 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002580 {
Xinghua Caob1239382016-12-13 15:07:05 +08002581 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Olli Etuaho465835d2017-09-26 13:34:10 +03002582 ASSERT(computeShaderD3D->hasUniform(baseName));
2583 d3dUniform->csRegisterIndex =
2584 computeShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002585 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002586 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->typeInfo,
2587 d3dUniform->getArraySizeProduct(), mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002588 }
Xinghua Caob1239382016-12-13 15:07:05 +08002589 else
Jamie Madillfb536032015-09-11 13:19:49 -04002590 {
Xinghua Caob1239382016-12-13 15:07:05 +08002591 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2592 const ShaderD3D *fragmentShaderD3D =
2593 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho465835d2017-09-26 13:34:10 +03002594 ASSERT(vertexShaderD3D->hasUniform(baseName) || fragmentShaderD3D->hasUniform(baseName));
2595 if (vertexShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002596 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002597 d3dUniform->vsRegisterIndex =
2598 vertexShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002599 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002600 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->typeInfo,
2601 d3dUniform->getArraySizeProduct(), mSamplersVS,
2602 &mUsedVertexSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002603 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002604 if (fragmentShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002605 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002606 d3dUniform->psRegisterIndex =
2607 fragmentShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002608 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002609 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->typeInfo,
2610 d3dUniform->getArraySizeProduct(), mSamplersPS, &mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002611 }
Jamie Madillfb536032015-09-11 13:19:49 -04002612 }
2613}
2614
Jamie Madill62d31cb2015-09-11 13:25:51 -04002615// static
2616void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002617 const gl::UniformTypeInfo &typeInfo,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002618 unsigned int samplerCount,
2619 std::vector<Sampler> &outSamplers,
2620 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002621{
2622 unsigned int samplerIndex = startSamplerIndex;
2623
2624 do
2625 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002626 ASSERT(samplerIndex < outSamplers.size());
2627 Sampler *sampler = &outSamplers[samplerIndex];
2628 sampler->active = true;
Xinghua Cao26143fd2017-11-01 18:19:05 +08002629 sampler->textureType = typeInfo.textureType;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002630 sampler->logicalTextureUnit = 0;
2631 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002632 samplerIndex++;
2633 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002634}
2635
Xinghua Cao26143fd2017-11-01 18:19:05 +08002636void ProgramD3D::assignAllImageRegisters()
2637{
2638 for (size_t uniformIndex = 0; uniformIndex < mD3DUniforms.size(); ++uniformIndex)
2639 {
2640 if (mD3DUniforms[uniformIndex]->isImage())
2641 {
2642 assignImageRegisters(uniformIndex);
2643 }
2644 }
2645}
2646
2647void ProgramD3D::assignImageRegisters(size_t uniformIndex)
2648{
2649 D3DUniform *d3dUniform = mD3DUniforms[uniformIndex];
2650 ASSERT(d3dUniform->isImage());
2651 // If the uniform is an array of arrays, then we have separate entries for each inner array in
2652 // mD3DUniforms. However, the image register info is stored in the shader only for the
2653 // outermost array.
2654 std::vector<unsigned int> subscripts;
2655 const std::string baseName = gl::ParseResourceName(d3dUniform->name, &subscripts);
2656 unsigned int registerOffset = mState.getUniforms()[uniformIndex].flattenedOffsetInParentArrays *
2657 d3dUniform->getArraySizeProduct();
2658
2659 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2660 if (computeShader)
2661 {
2662 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
2663 ASSERT(computeShaderD3D->hasUniform(baseName));
2664 d3dUniform->csRegisterIndex =
2665 computeShaderD3D->getUniformRegister(baseName) + registerOffset;
2666 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
2667 auto bindingIter = mImageBindingMap.find(baseName);
2668 ASSERT(bindingIter != mImageBindingMap.end());
2669 if (d3dUniform->regType == HLSLRegisterType::Texture)
2670 {
2671 AssignImages(d3dUniform->csRegisterIndex, bindingIter->second,
2672 d3dUniform->getArraySizeProduct(), mReadonlyImagesCS,
2673 &mUsedComputeReadonlyImageRange);
2674 }
2675 else if (d3dUniform->regType == HLSLRegisterType::UnorderedAccessView)
2676 {
2677 AssignImages(d3dUniform->csRegisterIndex, bindingIter->second,
2678 d3dUniform->getArraySizeProduct(), mImagesCS, &mUsedComputeImageRange);
2679 }
2680 else
2681 {
2682 UNREACHABLE();
2683 }
2684 }
2685 else
2686 {
2687 // TODO(xinghua.cao@intel.com): Implement image variables in vertex shader and pixel shader.
2688 UNIMPLEMENTED();
2689 }
2690}
2691
2692// static
2693void ProgramD3D::AssignImages(unsigned int startImageIndex,
2694 int startLogicalImageUnit,
2695 unsigned int imageCount,
2696 std::vector<Image> &outImages,
2697 GLuint *outUsedRange)
2698{
2699 unsigned int imageIndex = startImageIndex;
2700 // If declare without a binding qualifier, any uniform image variable (include all elements of
2701 // unbound image array) shoud be bound to unit zero.
2702 if (startLogicalImageUnit == -1)
2703 {
2704 ASSERT(imageIndex < outImages.size());
2705 Image *image = &outImages[imageIndex];
2706 image->active = true;
2707 image->logicalImageUnit = 0;
2708 *outUsedRange = std::max(imageIndex + 1, *outUsedRange);
2709 return;
2710 }
2711
2712 unsigned int logcalImageUnit = startLogicalImageUnit;
2713 do
2714 {
2715 ASSERT(imageIndex < outImages.size());
2716 Image *image = &outImages[imageIndex];
2717 image->active = true;
2718 image->logicalImageUnit = logcalImageUnit;
2719 *outUsedRange = std::max(imageIndex + 1, *outUsedRange);
2720 imageIndex++;
2721 logcalImageUnit++;
2722 } while (imageIndex < startImageIndex + imageCount);
2723}
2724
Brandon Jonesc9610c52014-08-25 17:02:59 -07002725void ProgramD3D::reset()
2726{
Xinghua Caob1239382016-12-13 15:07:05 +08002727 mVertexExecutables.clear();
2728 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002729
Xinghua Caob1239382016-12-13 15:07:05 +08002730 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002731 {
Xinghua Caob1239382016-12-13 15:07:05 +08002732 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002733 }
Brandon Joneseb994362014-09-24 10:27:28 -07002734
Xinghua Caob1239382016-12-13 15:07:05 +08002735 mComputeExecutable.reset(nullptr);
2736
Brandon Jones22502d52014-08-29 16:58:36 -07002737 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002738 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002739
2740 mPixelHLSL.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002741 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
2742 mUsesFragDepth = false;
Martin Radev41ac68e2017-06-06 12:16:58 +03002743 mHasANGLEMultiviewEnabled = false;
2744 mUsesViewID = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002745 mPixelShaderKey.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002746 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002747 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002748
Jamie Madill62d31cb2015-09-11 13:25:51 -04002749 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002750 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002751
Xinghua Caob1239382016-12-13 15:07:05 +08002752 mVertexUniformStorage.reset(nullptr);
2753 mFragmentUniformStorage.reset(nullptr);
2754 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002755
2756 mSamplersPS.clear();
2757 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002758 mSamplersCS.clear();
Xinghua Cao26143fd2017-11-01 18:19:05 +08002759 mImagesCS.clear();
2760 mReadonlyImagesCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002761
Xinghua Cao26143fd2017-11-01 18:19:05 +08002762 mUsedVertexSamplerRange = 0;
2763 mUsedPixelSamplerRange = 0;
2764 mUsedComputeSamplerRange = 0;
2765 mDirtySamplerMapping = true;
2766 mUsedComputeImageRange = 0;
2767 mUsedComputeReadonlyImageRange = 0;
Jamie Madill437d2662014-12-05 14:23:35 -05002768
Jamie Madill8047c0d2016-03-07 13:02:12 -05002769 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002770
Jamie Madill9fc36822015-11-18 13:08:07 -05002771 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002772
2773 mGeometryShaderPreamble.clear();
Jamie Madill561ed3a2017-08-31 16:48:09 -04002774
Jamie Madill4148fd72017-09-14 15:46:20 -04002775 dirtyAllUniforms();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002776
2777 mCachedPixelExecutableIndex.reset();
2778 mCachedVertexExecutableIndex.reset();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002779}
2780
Geoff Lang7dd2e102014-11-10 15:19:26 -05002781unsigned int ProgramD3D::getSerial() const
2782{
2783 return mSerial;
2784}
2785
2786unsigned int ProgramD3D::issueSerial()
2787{
2788 return mCurrentSerial++;
2789}
2790
Jamie Madillbd044ed2017-06-05 12:59:21 -04002791void ProgramD3D::initAttribLocationsToD3DSemantic(const gl::Context *context)
Jamie Madill63805b42015-08-25 13:17:39 -04002792{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002793 gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002794 ASSERT(vertexShader != nullptr);
2795
2796 // Init semantic index
Jamie Madill34ca4f52017-06-13 11:49:39 -04002797 int semanticIndex = 0;
2798 for (const sh::Attribute &attribute : vertexShader->getActiveAttributes(context))
Jamie Madill63805b42015-08-25 13:17:39 -04002799 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002800 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill34ca4f52017-06-13 11:49:39 -04002801 GLuint location = mState.getAttributeLocation(attribute.name);
2802 ASSERT(location != std::numeric_limits<GLuint>::max());
Jamie Madill63805b42015-08-25 13:17:39 -04002803
Jamie Madill8047c0d2016-03-07 13:02:12 -05002804 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002805 {
Jamie Madill34ca4f52017-06-13 11:49:39 -04002806 mAttribLocationToD3DSemantic[location + reg] = semanticIndex++;
Jamie Madill63805b42015-08-25 13:17:39 -04002807 }
2808 }
Jamie Madill437d2662014-12-05 14:23:35 -05002809}
2810
Jamie Madilla779b612017-07-24 11:46:05 -04002811void ProgramD3D::updateCachedInputLayout(Serial associatedSerial, const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002812{
Jamie Madilla779b612017-07-24 11:46:05 -04002813 if (mCurrentVertexArrayStateSerial == associatedSerial)
2814 {
2815 return;
2816 }
2817
2818 mCurrentVertexArrayStateSerial = associatedSerial;
Jamie Madillbd136f92015-08-10 14:51:37 -04002819 mCachedInputLayout.clear();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002820
Jamie Madilld3dfda22015-07-06 08:28:49 -04002821 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002822
Jamie Madill6de51852017-04-12 09:53:01 -04002823 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002824 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002825 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002826
Jamie Madill8047c0d2016-03-07 13:02:12 -05002827 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002828 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002829 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002830 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002831 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002832 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002833 mCachedInputLayout[d3dSemantic] =
2834 GetVertexFormatType(vertexAttributes[locationIndex],
2835 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002836 }
2837 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002838
2839 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04002840
2841 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002842}
2843
2844void ProgramD3D::updateCachedOutputLayout(const gl::Context *context,
2845 const gl::Framebuffer *framebuffer)
2846{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002847 mPixelShaderOutputLayoutCache.clear();
2848
2849 FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(framebuffer);
2850 const auto &colorbuffers = fboD3D->getColorAttachmentsForRender(context);
2851
2852 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
2853 {
2854 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
2855
2856 if (colorbuffer)
2857 {
2858 auto binding = colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0
2859 : colorbuffer->getBinding();
2860 mPixelShaderOutputLayoutCache.push_back(binding);
2861 }
2862 else
2863 {
2864 mPixelShaderOutputLayoutCache.push_back(GL_NONE);
2865 }
2866 }
2867
2868 updateCachedPixelExecutableIndex();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002869}
2870
Jamie Madill192745a2016-12-22 15:58:21 -05002871void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2872 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002873{
Jamie Madill9fc36822015-11-18 13:08:07 -05002874 const std::string &varyingSemantic =
2875 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2876
Jamie Madillccdf74b2015-08-18 10:46:12 -04002877 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002878 mStreamOutVaryings.clear();
2879
Jamie Madill48ef11b2016-04-27 15:21:52 -04002880 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002881 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2882 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002883 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002884 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2885 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002886 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002887 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002888 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002889 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2890 builtins.glPosition.index, 4, outputSlot));
2891 }
2892 }
2893 else if (tfVaryingName == "gl_FragCoord")
2894 {
2895 if (builtins.glFragCoord.enabled)
2896 {
2897 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2898 builtins.glFragCoord.index, 4, outputSlot));
2899 }
2900 }
2901 else if (tfVaryingName == "gl_PointSize")
2902 {
2903 if (builtins.glPointSize.enabled)
2904 {
2905 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2906 }
2907 }
2908 else
2909 {
Jamie Madill192745a2016-12-22 15:58:21 -05002910 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002911 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002912 const auto &varying = *registerInfo.packedVarying->varying;
2913 GLenum transposedType = gl::TransposeMatrixType(varying.type);
jchen108225e732017-11-14 16:29:03 +08002914 int componentCount = gl::VariableColumnCount(transposedType);
2915 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill55c25d02015-11-18 13:08:08 -05002916
Jamie Madill9fc36822015-11-18 13:08:07 -05002917 // There can be more than one register assigned to a particular varying, and each
2918 // register needs its own stream out entry.
jchen108225e732017-11-14 16:29:03 +08002919 if (registerInfo.tfVaryingName() == tfVaryingName)
Jamie Madill9fc36822015-11-18 13:08:07 -05002920 {
2921 mStreamOutVaryings.push_back(D3DVarying(
2922 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2923 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002924 }
2925 }
2926 }
2927}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002928
2929D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2930{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002931 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002932}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002933
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002934const D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location) const
2935{
2936 return mD3DUniforms[mState.getUniformLocations()[location].index];
2937}
2938
Sami Väisänen46eaa942016-06-29 10:26:37 +03002939void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2940 GLenum genMode,
2941 GLint components,
2942 const GLfloat *coeffs)
2943{
2944 UNREACHABLE();
2945}
2946
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002947bool ProgramD3D::hasVertexExecutableForCachedInputLayout()
2948{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002949 return mCachedVertexExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002950}
2951
2952bool ProgramD3D::hasGeometryExecutableForPrimitiveType(GLenum drawMode)
2953{
2954 if (!usesGeometryShader(drawMode))
2955 {
2956 // No shader necessary mean we have the required (null) executable.
2957 return true;
2958 }
2959
2960 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
2961 return mGeometryExecutables[geometryShaderType].get() != nullptr;
2962}
2963
2964bool ProgramD3D::hasPixelExecutableForCachedOutputLayout()
2965{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002966 return mCachedPixelExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002967}
2968
Jamie Madill54164b02017-08-28 15:17:37 -04002969template <typename DestT>
2970void ProgramD3D::getUniformInternal(GLint location, DestT *dataOut) const
2971{
2972 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2973 const gl::LinkedUniform &uniform = mState.getUniforms()[locationInfo.index];
2974
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002975 const D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Olli Etuaho1734e172017-10-27 15:30:27 +03002976 const uint8_t *srcPointer = targetUniform->getDataPtrToElement(locationInfo.arrayIndex);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002977
2978 if (gl::IsMatrixType(uniform.type))
2979 {
2980 GetMatrixUniform(gl::VariableColumnCount(uniform.type), gl::VariableRowCount(uniform.type),
2981 dataOut, reinterpret_cast<const DestT *>(srcPointer));
2982 }
2983 else
2984 {
2985 memcpy(dataOut, srcPointer, uniform.getElementSize());
2986 }
Jamie Madill54164b02017-08-28 15:17:37 -04002987}
2988
2989void ProgramD3D::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
2990{
2991 getUniformInternal(location, params);
2992}
2993
2994void ProgramD3D::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
2995{
2996 getUniformInternal(location, params);
2997}
2998
2999void ProgramD3D::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
3000{
3001 getUniformInternal(location, params);
3002}
3003
Jamie Madill0e7f1732017-09-09 23:32:50 -04003004void ProgramD3D::updateCachedVertexExecutableIndex()
3005{
3006 mCachedVertexExecutableIndex.reset();
3007 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
3008 {
3009 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
3010 {
3011 mCachedVertexExecutableIndex = executableIndex;
3012 break;
3013 }
3014 }
3015}
3016
3017void ProgramD3D::updateCachedPixelExecutableIndex()
3018{
3019 mCachedPixelExecutableIndex.reset();
3020 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
3021 {
3022 if (mPixelExecutables[executableIndex]->matchesSignature(mPixelShaderOutputLayoutCache))
3023 {
3024 mCachedPixelExecutableIndex = executableIndex;
3025 break;
3026 }
3027 }
3028}
3029
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003030void ProgramD3D::linkResources(const gl::Context *context,
3031 const gl::ProgramLinkedResources &resources)
3032{
3033 UniformBlockInfo uniformBlockInfo;
3034
3035 if (mState.getAttachedVertexShader())
3036 {
3037 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedVertexShader());
3038 }
3039
3040 if (mState.getAttachedFragmentShader())
3041 {
3042 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedFragmentShader());
3043 }
3044
3045 if (mState.getAttachedComputeShader())
3046 {
3047 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedComputeShader());
3048 }
3049
3050 // Gather interface block info.
3051 auto getUniformBlockSize = [&uniformBlockInfo](const std::string &name,
3052 const std::string &mappedName, size_t *sizeOut) {
3053 return uniformBlockInfo.getBlockSize(name, mappedName, sizeOut);
3054 };
3055
3056 auto getUniformBlockMemberInfo = [&uniformBlockInfo](const std::string &name,
3057 const std::string &mappedName,
3058 sh::BlockMemberInfo *infoOut) {
3059 return uniformBlockInfo.getBlockMemberInfo(name, mappedName, infoOut);
3060 };
3061
3062 resources.uniformBlockLinker.linkBlocks(getUniformBlockSize, getUniformBlockMemberInfo);
3063 initializeUniformBlocks();
3064
3065 // TODO(jiajia.qin@intel.com): Determine correct shader storage block info.
3066 auto getShaderStorageBlockSize = [](const std::string &name, const std::string &mappedName,
3067 size_t *sizeOut) {
3068 *sizeOut = 0;
3069 return true;
3070 };
3071
3072 auto getShaderStorageBlockMemberInfo =
3073 [](const std::string &name, const std::string &mappedName, sh::BlockMemberInfo *infoOut) {
3074 *infoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
3075 return true;
3076 };
3077
3078 resources.shaderStorageBlockLinker.linkBlocks(getShaderStorageBlockSize,
3079 getShaderStorageBlockMemberInfo);
3080}
3081
Jamie Madill8047c0d2016-03-07 13:02:12 -05003082} // namespace rx