blob: d40f0b28995f65f305d97b25b1ddde5448572904 [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/RendererD3D.h"
27#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050028#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050029#include "libANGLE/renderer/d3d/VertexDataManager.h"
Geoff Lang22072132014-11-20 15:15:01 -050030
Jamie Madill01074252016-11-28 15:55:51 -050031using namespace angle;
32
Brandon Jonesc9610c52014-08-25 17:02:59 -070033namespace rx
34{
35
Brandon Joneseb994362014-09-24 10:27:28 -070036namespace
37{
38
Jamie Madill4c19a8a2017-07-24 11:46:06 -040039void GetDefaultInputLayoutFromShader(const gl::Context *context,
40 gl::Shader *vertexShader,
41 gl::InputLayout *inputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070042{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040043 inputLayoutOut->clear();
44
Jamie Madillbd044ed2017-06-05 12:59:21 -040045 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes(context))
Brandon Joneseb994362014-09-24 10:27:28 -070046 {
Brandon Joneseb994362014-09-24 10:27:28 -070047 if (shaderAttr.type != GL_NONE)
48 {
49 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
50
Jamie Madilld3dfda22015-07-06 08:28:49 -040051 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040052 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070053 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040054 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040055 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040056 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040057 gl::VertexFormatType defaultType =
58 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070059
Jamie Madill4c19a8a2017-07-24 11:46:06 -040060 inputLayoutOut->push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070061 }
62 }
63 }
64}
65
Jamie Madill4c19a8a2017-07-24 11:46:06 -040066void GetDefaultOutputLayoutFromShader(
67 const std::vector<PixelShaderOutputVariable> &shaderOutputVars,
68 std::vector<GLenum> *outputLayoutOut)
Brandon Joneseb994362014-09-24 10:27:28 -070069{
Jamie Madill4c19a8a2017-07-24 11:46:06 -040070 outputLayoutOut->clear();
Brandon Joneseb994362014-09-24 10:27:28 -070071
Jamie Madillb4463142014-12-19 14:56:54 -050072 if (!shaderOutputVars.empty())
73 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -040074 outputLayoutOut->push_back(GL_COLOR_ATTACHMENT0 +
75 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050076 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -040077}
Brandon Joneseb994362014-09-24 10:27:28 -070078
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080079template <typename T, int cols, int rows>
80bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -040081{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080082 constexpr int targetWidth = 4;
83 constexpr int targetHeight = rows;
84 constexpr int srcWidth = rows;
85 constexpr int srcHeight = cols;
86
87 constexpr int copyWidth = std::min(targetHeight, srcWidth);
88 constexpr int copyHeight = std::min(targetWidth, srcHeight);
89
90 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -040091
92 for (int x = 0; x < copyWidth; x++)
93 {
94 for (int y = 0; y < copyHeight; y++)
95 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -080096 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -040097 }
98 }
99
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800100 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
101 {
102 return false;
103 }
104
105 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
106 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400107}
108
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800109template <typename T, int cols, int rows>
110bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400111{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800112 constexpr int targetWidth = 4;
113 constexpr int targetHeight = rows;
114 constexpr int srcWidth = cols;
115 constexpr int srcHeight = rows;
116
117 constexpr int copyWidth = std::min(targetWidth, srcWidth);
118 constexpr int copyHeight = std::min(targetHeight, srcHeight);
119
120 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400121
122 for (int y = 0; y < copyHeight; y++)
123 {
124 for (int x = 0; x < copyWidth; x++)
125 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800126 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400127 }
128 }
129
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800130 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
131 {
132 return false;
133 }
134
135 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
136 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400137}
138
Jamie Madill4e31ad52015-10-29 10:32:57 -0400139gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
140{
141 switch (drawMode)
142 {
143 // Uses the point sprite geometry shader.
144 case GL_POINTS:
145 return gl::PRIMITIVE_POINTS;
146
147 // All line drawing uses the same geometry shader.
148 case GL_LINES:
149 case GL_LINE_STRIP:
150 case GL_LINE_LOOP:
151 return gl::PRIMITIVE_LINES;
152
153 // The triangle fan primitive is emulated with strips in D3D11.
154 case GL_TRIANGLES:
155 case GL_TRIANGLE_FAN:
156 return gl::PRIMITIVE_TRIANGLES;
157
158 // Special case for triangle strips.
159 case GL_TRIANGLE_STRIP:
160 return gl::PRIMITIVE_TRIANGLE_STRIP;
161
162 default:
163 UNREACHABLE();
164 return gl::PRIMITIVE_TYPE_MAX;
165 }
166}
167
Jamie Madill192745a2016-12-22 15:58:21 -0500168bool FindFlatInterpolationVarying(const std::vector<sh::Varying> &varyings)
169{
170 // Note: this assumes nested structs can only be packed with one interpolation.
171 for (const auto &varying : varyings)
172 {
173 if (varying.interpolation == sh::INTERPOLATION_FLAT)
174 {
175 return true;
176 }
177 }
178
179 return false;
180}
181
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400182// Helper method to de-tranpose a matrix uniform for an API query.
183void GetMatrixUniform(GLint columns, GLint rows, GLfloat *dataOut, const GLfloat *source)
184{
185 for (GLint col = 0; col < columns; ++col)
186 {
187 for (GLint row = 0; row < rows; ++row)
188 {
189 GLfloat *outptr = dataOut + ((col * rows) + row);
190 const GLfloat *inptr = source + ((row * 4) + col);
191 *outptr = *inptr;
192 }
193 }
194}
195
196template <typename NonFloatT>
197void GetMatrixUniform(GLint columns, GLint rows, NonFloatT *dataOut, const NonFloatT *source)
198{
199 UNREACHABLE();
200}
201
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500202class UniformBlockInfo final : angle::NonCopyable
203{
204 public:
205 UniformBlockInfo() {}
206
207 void getShaderBlockInfo(const gl::Context *context, gl::Shader *shader);
208
209 bool getBlockSize(const std::string &name, const std::string &mappedName, size_t *sizeOut);
210 bool getBlockMemberInfo(const std::string &name,
211 const std::string &mappedName,
212 sh::BlockMemberInfo *infoOut);
213
214 private:
215 size_t getBlockInfo(const sh::InterfaceBlock &interfaceBlock);
216
217 std::map<std::string, size_t> mBlockSizes;
218 sh::BlockLayoutMap mBlockLayout;
219};
220
221void UniformBlockInfo::getShaderBlockInfo(const gl::Context *context, gl::Shader *shader)
222{
223 for (const sh::InterfaceBlock &interfaceBlock : shader->getUniformBlocks(context))
224 {
225 if (!interfaceBlock.staticUse && interfaceBlock.layout == sh::BLOCKLAYOUT_PACKED)
226 continue;
227
228 if (mBlockSizes.count(interfaceBlock.name) > 0)
229 continue;
230
231 size_t dataSize = getBlockInfo(interfaceBlock);
232 mBlockSizes[interfaceBlock.name] = dataSize;
233 }
234}
235
236size_t UniformBlockInfo::getBlockInfo(const sh::InterfaceBlock &interfaceBlock)
237{
238 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
239
240 // define member uniforms
241 sh::Std140BlockEncoder std140Encoder;
242 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED, false);
243 sh::BlockLayoutEncoder *encoder = nullptr;
244
245 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STD140)
246 {
247 encoder = &std140Encoder;
248 }
249 else
250 {
251 encoder = &hlslEncoder;
252 }
253
254 sh::GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
Olli Etuaho3de27032017-11-30 12:16:47 +0200255 &mBlockLayout);
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500256
257 return encoder->getBlockSize();
258}
259
260bool UniformBlockInfo::getBlockSize(const std::string &name,
261 const std::string &mappedName,
262 size_t *sizeOut)
263{
264 size_t nameLengthWithoutArrayIndex;
265 gl::ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
266 std::string baseName = name.substr(0u, nameLengthWithoutArrayIndex);
267 auto sizeIter = mBlockSizes.find(baseName);
268 if (sizeIter == mBlockSizes.end())
269 {
270 *sizeOut = 0;
271 return false;
272 }
273
274 *sizeOut = sizeIter->second;
275 return true;
276};
277
278bool UniformBlockInfo::getBlockMemberInfo(const std::string &name,
279 const std::string &mappedName,
280 sh::BlockMemberInfo *infoOut)
281{
282 auto infoIter = mBlockLayout.find(name);
283 if (infoIter == mBlockLayout.end())
284 {
285 *infoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
286 return false;
287 }
288
289 *infoOut = infoIter->second;
290 return true;
291};
292
Jamie Madillada9ecc2015-08-17 12:53:37 -0400293} // anonymous namespace
294
Jamie Madill28afae52015-11-09 15:07:57 -0500295// D3DUniform Implementation
296
Jamie Madill33bb7c42017-09-09 23:32:51 -0400297D3DUniform::D3DUniform(GLenum type,
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),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400307 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400308 psRegisterIndex(GL_INVALID_INDEX),
Xinghua Caob1239382016-12-13 15:07:05 +0800309 csRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400310 registerCount(0),
311 registerElement(0)
312{
313 // We use data storage for default block uniforms to cache values that are sent to D3D during
314 // rendering
315 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
316 if (defaultBlock)
317 {
Jamie Madillcc2ed612017-03-14 15:59:00 -0400318 // Use the row count as register count, will work for non-square matrices.
Olli Etuaho465835d2017-09-26 13:34:10 +0300319 registerCount = typeInfo.rowCount * getArraySizeProduct();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400320 }
321}
322
323D3DUniform::~D3DUniform()
324{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400325}
326
Olli Etuaho465835d2017-09-26 13:34:10 +0300327unsigned int D3DUniform::getArraySizeProduct() const
328{
329 return gl::ArraySizeProduct(arraySizes);
330}
331
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400332const uint8_t *D3DUniform::getDataPtrToElement(size_t elementIndex) const
333{
Olli Etuaho465835d2017-09-26 13:34:10 +0300334 ASSERT((!isArray() && elementIndex == 0) ||
335 (isArray() && elementIndex < getArraySizeProduct()));
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400336
337 if (isSampler())
338 {
339 return reinterpret_cast<const uint8_t *>(&mSamplerData[elementIndex]);
340 }
341
Jamie Madill33bb7c42017-09-09 23:32:51 -0400342 return firstNonNullData() + (elementIndex > 0 ? (typeInfo.internalSize * elementIndex) : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400343}
344
345bool D3DUniform::isSampler() const
346{
Jamie Madill33bb7c42017-09-09 23:32:51 -0400347 return typeInfo.isSampler;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400348}
349
350bool D3DUniform::isReferencedByVertexShader() const
351{
352 return vsRegisterIndex != GL_INVALID_INDEX;
353}
354
355bool D3DUniform::isReferencedByFragmentShader() const
356{
357 return psRegisterIndex != GL_INVALID_INDEX;
358}
359
Xinghua Caob1239382016-12-13 15:07:05 +0800360bool D3DUniform::isReferencedByComputeShader() const
361{
362 return csRegisterIndex != GL_INVALID_INDEX;
363}
364
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400365const uint8_t *D3DUniform::firstNonNullData() const
366{
367 ASSERT(vsData || psData || csData || !mSamplerData.empty());
368
369 if (!mSamplerData.empty())
370 {
371 return reinterpret_cast<const uint8_t *>(mSamplerData.data());
372 }
373
374 return vsData ? vsData : (psData ? psData : csData);
375}
376
Jamie Madill28afae52015-11-09 15:07:57 -0500377// D3DVarying Implementation
378
Jamie Madill9fc36822015-11-18 13:08:07 -0500379D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500380{
381}
382
Jamie Madill9fc36822015-11-18 13:08:07 -0500383D3DVarying::D3DVarying(const std::string &semanticNameIn,
384 unsigned int semanticIndexIn,
385 unsigned int componentCountIn,
386 unsigned int outputSlotIn)
387 : semanticName(semanticNameIn),
388 semanticIndex(semanticIndexIn),
389 componentCount(componentCountIn),
390 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500391{
392}
393
Jamie Madille39a3f02015-11-17 20:42:15 -0500394// ProgramD3DMetadata Implementation
395
Jamie Madillc9bde922016-07-24 17:58:50 -0400396ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500397 const ShaderD3D *vertexShader,
398 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400399 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
400 mShaderModelSuffix(renderer->getShaderModelSuffix()),
401 mUsesInstancedPointSpriteEmulation(
402 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
403 mUsesViewScale(renderer->presentPathFastEnabled()),
Martin Radev41ac68e2017-06-06 12:16:58 +0300404 mHasANGLEMultiviewEnabled(vertexShader->hasANGLEMultiviewEnabled()),
405 mUsesViewID(fragmentShader->usesViewID()),
Martin Radevc1d4e552017-08-21 12:01:10 +0300406 mCanSelectViewInVertexShader(renderer->canSelectViewInVertexShader()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500407 mVertexShader(vertexShader),
408 mFragmentShader(fragmentShader)
409{
410}
411
412int ProgramD3DMetadata::getRendererMajorShaderModel() const
413{
414 return mRendererMajorShaderModel;
415}
416
Jamie Madill9082b982016-04-27 15:21:51 -0400417bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500418{
Corentin Wallezc084de12017-06-05 14:28:52 -0700419 return (mFragmentShader->usesFragColor() && mFragmentShader->usesMultipleRenderTargets() &&
420 data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500421}
422
Jamie Madill48ef11b2016-04-27 15:21:52 -0400423bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500424{
Jamie Madill63286672015-11-24 13:00:08 -0500425 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500426}
427
428bool ProgramD3DMetadata::usesPointCoord() const
429{
430 return mFragmentShader->usesPointCoord();
431}
432
433bool ProgramD3DMetadata::usesFragCoord() const
434{
435 return mFragmentShader->usesFragCoord();
436}
437
438bool ProgramD3DMetadata::usesPointSize() const
439{
440 return mVertexShader->usesPointSize();
441}
442
443bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
444{
Jamie Madillc9bde922016-07-24 17:58:50 -0400445 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
446 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500447}
448
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800449bool ProgramD3DMetadata::usesViewScale() const
450{
451 return mUsesViewScale;
452}
453
Martin Radev41ac68e2017-06-06 12:16:58 +0300454bool ProgramD3DMetadata::hasANGLEMultiviewEnabled() const
455{
456 return mHasANGLEMultiviewEnabled;
457}
458
459bool ProgramD3DMetadata::usesViewID() const
460{
461 return mUsesViewID;
462}
463
Martin Radevc1d4e552017-08-21 12:01:10 +0300464bool ProgramD3DMetadata::canSelectViewInVertexShader() const
465{
466 return mCanSelectViewInVertexShader;
467}
468
Jamie Madille39a3f02015-11-17 20:42:15 -0500469bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
470{
Jamie Madillc9bde922016-07-24 17:58:50 -0400471 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500472 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400473 // Even with a geometry shader, the app can render triangles or lines and reference
474 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
475 // simplicity, we always add this to the vertex shader when the fragment shader
476 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500477 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
478 usesInsertedPointCoordValue();
479}
480
481bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
482{
483 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
484 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
485 // the vertex shader in this case. This saves us 1 output vector.
486 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
487}
488
489bool ProgramD3DMetadata::usesSystemValuePointSize() const
490{
491 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
492}
493
494bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
495{
496 return mFragmentShader->usesMultipleRenderTargets();
497}
498
499GLint ProgramD3DMetadata::getMajorShaderVersion() const
500{
501 return mVertexShader->getData().getShaderVersion();
502}
503
504const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
505{
506 return mFragmentShader;
507}
508
Jamie Madill28afae52015-11-09 15:07:57 -0500509// ProgramD3D Implementation
510
Jamie Madilld3dfda22015-07-06 08:28:49 -0400511ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
512 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500513 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400514 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700515{
Brandon Joneseb994362014-09-24 10:27:28 -0700516}
517
518ProgramD3D::VertexExecutable::~VertexExecutable()
519{
520 SafeDelete(mShaderExecutable);
521}
522
Jamie Madilld3dfda22015-07-06 08:28:49 -0400523// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500524ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
525 GLenum type)
526{
527 switch (type)
528 {
529 case GL_INT:
530 return HLSLAttribType::SIGNED_INT;
531 case GL_UNSIGNED_INT:
532 return HLSLAttribType::UNSIGNED_INT;
533 case GL_SIGNED_NORMALIZED:
534 case GL_UNSIGNED_NORMALIZED:
535 case GL_FLOAT:
536 return HLSLAttribType::FLOAT;
537 default:
538 UNREACHABLE();
539 return HLSLAttribType::FLOAT;
540 }
541}
542
543// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400544void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
545 const gl::InputLayout &inputLayout,
546 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700547{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500548 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400549
550 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700551 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400552 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500553 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
554 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400555
Jamie Madillbdec2f42016-03-02 16:35:32 -0500556 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
557 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
558 continue;
559
560 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
561 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700562 }
Brandon Joneseb994362014-09-24 10:27:28 -0700563}
564
Jamie Madilld3dfda22015-07-06 08:28:49 -0400565bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
566{
Jamie Madillbd136f92015-08-10 14:51:37 -0400567 size_t limit = std::max(mSignature.size(), signature.size());
568 for (size_t index = 0; index < limit; ++index)
569 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500570 // treat undefined indexes as FLOAT
571 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
572 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400573 if (a != b)
574 return false;
575 }
576
577 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400578}
579
580ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
581 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400582 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700583{
584}
585
586ProgramD3D::PixelExecutable::~PixelExecutable()
587{
588 SafeDelete(mShaderExecutable);
589}
590
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700591ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
592{
593}
594
Geoff Lang7dd2e102014-11-10 15:19:26 -0500595unsigned int ProgramD3D::mCurrentSerial = 1;
596
Jamie Madill48ef11b2016-04-27 15:21:52 -0400597ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
598 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700599 mRenderer(renderer),
Xinghua Caob1239382016-12-13 15:07:05 +0800600 mDynamicHLSL(nullptr),
601 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX),
602 mComputeExecutable(nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700603 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400604 mUsesFlatInterpolation(false),
Xinghua Caob1239382016-12-13 15:07:05 +0800605 mVertexUniformStorage(nullptr),
606 mFragmentUniformStorage(nullptr),
607 mComputeUniformStorage(nullptr),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700608 mUsedVertexSamplerRange(0),
609 mUsedPixelSamplerRange(0),
Xinghua Caob1239382016-12-13 15:07:05 +0800610 mUsedComputeSamplerRange(0),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700611 mDirtySamplerMapping(true),
Jamie Madill561ed3a2017-08-31 16:48:09 -0400612 mSerial(issueSerial()),
Jamie Madill4148fd72017-09-14 15:46:20 -0400613 mVertexUniformsDirty(true),
614 mFragmentUniformsDirty(true),
615 mComputeUniformsDirty(true)
Brandon Jonesc9610c52014-08-25 17:02:59 -0700616{
Brandon Joneseb994362014-09-24 10:27:28 -0700617 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700618}
619
620ProgramD3D::~ProgramD3D()
621{
622 reset();
623 SafeDelete(mDynamicHLSL);
624}
625
Brandon Jones44151a92014-09-10 11:32:25 -0700626bool ProgramD3D::usesPointSpriteEmulation() const
627{
628 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
629}
630
Martin Radev41ac68e2017-06-06 12:16:58 +0300631bool ProgramD3D::usesGeometryShaderForPointSpriteEmulation() const
632{
633 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
634}
635
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400636bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700637{
Martin Radevc1d4e552017-08-21 12:01:10 +0300638 if (mHasANGLEMultiviewEnabled && !mRenderer->canSelectViewInVertexShader())
Martin Radev41ac68e2017-06-06 12:16:58 +0300639 {
640 return true;
641 }
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400642 if (drawMode != GL_POINTS)
643 {
644 return mUsesFlatInterpolation;
645 }
Martin Radev41ac68e2017-06-06 12:16:58 +0300646 return usesGeometryShaderForPointSpriteEmulation();
Cooper Partine6664f02015-01-09 16:22:24 -0800647}
648
649bool ProgramD3D::usesInstancedPointSpriteEmulation() const
650{
651 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700652}
653
Xinghua Cao37584b32017-12-01 11:04:03 +0800654GLint ProgramD3D::getSamplerMapping(gl::ShaderType type,
Jamie Madill334d6152015-10-22 14:00:28 -0400655 unsigned int samplerIndex,
656 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700657{
658 GLint logicalTextureUnit = -1;
659
660 switch (type)
661 {
Xinghua Cao37584b32017-12-01 11:04:03 +0800662 case gl::SHADER_FRAGMENT:
Jamie Madill334d6152015-10-22 14:00:28 -0400663 ASSERT(samplerIndex < caps.maxTextureImageUnits);
664 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
665 {
666 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
667 }
668 break;
Xinghua Cao37584b32017-12-01 11:04:03 +0800669 case gl::SHADER_VERTEX:
Jamie Madill334d6152015-10-22 14:00:28 -0400670 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
671 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
672 {
673 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
674 }
675 break;
Xinghua Cao37584b32017-12-01 11:04:03 +0800676 case gl::SHADER_COMPUTE:
Xinghua Caob1239382016-12-13 15:07:05 +0800677 ASSERT(samplerIndex < caps.maxComputeTextureImageUnits);
678 if (samplerIndex < mSamplersCS.size() && mSamplersCS[samplerIndex].active)
679 {
680 logicalTextureUnit = mSamplersCS[samplerIndex].logicalTextureUnit;
681 }
682 break;
Jamie Madill334d6152015-10-22 14:00:28 -0400683 default:
684 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685 }
686
Jamie Madill334d6152015-10-22 14:00:28 -0400687 if (logicalTextureUnit >= 0 &&
688 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689 {
690 return logicalTextureUnit;
691 }
692
693 return -1;
694}
695
696// Returns the texture type for a given Direct3D 9 sampler type and
697// index (0-15 for the pixel shader and 0-3 for the vertex shader).
Xinghua Cao37584b32017-12-01 11:04:03 +0800698GLenum ProgramD3D::getSamplerTextureType(gl::ShaderType type, unsigned int samplerIndex) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700699{
700 switch (type)
701 {
Xinghua Cao37584b32017-12-01 11:04:03 +0800702 case gl::SHADER_FRAGMENT:
Jamie Madill334d6152015-10-22 14:00:28 -0400703 ASSERT(samplerIndex < mSamplersPS.size());
704 ASSERT(mSamplersPS[samplerIndex].active);
705 return mSamplersPS[samplerIndex].textureType;
Xinghua Cao37584b32017-12-01 11:04:03 +0800706 case gl::SHADER_VERTEX:
Jamie Madill334d6152015-10-22 14:00:28 -0400707 ASSERT(samplerIndex < mSamplersVS.size());
708 ASSERT(mSamplersVS[samplerIndex].active);
709 return mSamplersVS[samplerIndex].textureType;
Xinghua Cao37584b32017-12-01 11:04:03 +0800710 case gl::SHADER_COMPUTE:
Xinghua Caob1239382016-12-13 15:07:05 +0800711 ASSERT(samplerIndex < mSamplersCS.size());
712 ASSERT(mSamplersCS[samplerIndex].active);
713 return mSamplersCS[samplerIndex].textureType;
Jamie Madill334d6152015-10-22 14:00:28 -0400714 default:
715 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700716 }
717
718 return GL_TEXTURE_2D;
719}
720
Xinghua Cao37584b32017-12-01 11:04:03 +0800721GLuint ProgramD3D::getUsedSamplerRange(gl::ShaderType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700722{
723 switch (type)
724 {
Xinghua Cao37584b32017-12-01 11:04:03 +0800725 case gl::SHADER_FRAGMENT:
Jamie Madill334d6152015-10-22 14:00:28 -0400726 return mUsedPixelSamplerRange;
Xinghua Cao37584b32017-12-01 11:04:03 +0800727 case gl::SHADER_VERTEX:
Jamie Madill334d6152015-10-22 14:00:28 -0400728 return mUsedVertexSamplerRange;
Xinghua Cao37584b32017-12-01 11:04:03 +0800729 case gl::SHADER_COMPUTE:
Xinghua Caob1239382016-12-13 15:07:05 +0800730 return mUsedComputeSamplerRange;
Jamie Madill334d6152015-10-22 14:00:28 -0400731 default:
732 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200733 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700734 }
735}
736
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400737ProgramD3D::SamplerMapping ProgramD3D::updateSamplerMapping()
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700738{
739 if (!mDirtySamplerMapping)
740 {
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400741 return SamplerMapping::WasClean;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700742 }
743
744 mDirtySamplerMapping = false;
745
746 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400747 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700748 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400749 if (!d3dUniform->isSampler())
750 continue;
751
Olli Etuaho465835d2017-09-26 13:34:10 +0300752 int count = d3dUniform->getArraySizeProduct();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400753
754 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700755 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400756 unsigned int firstIndex = d3dUniform->psRegisterIndex;
757
758 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700759 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400760 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700761
Jamie Madill62d31cb2015-09-11 13:25:51 -0400762 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700763 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400764 ASSERT(mSamplersPS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400765 mSamplersPS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700766 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400767 }
768 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700769
Jamie Madill62d31cb2015-09-11 13:25:51 -0400770 if (d3dUniform->isReferencedByVertexShader())
771 {
772 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
773
774 for (int i = 0; i < count; i++)
775 {
776 unsigned int samplerIndex = firstIndex + i;
777
778 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700779 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400780 ASSERT(mSamplersVS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400781 mSamplersVS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700782 }
783 }
784 }
Xinghua Caob1239382016-12-13 15:07:05 +0800785
786 if (d3dUniform->isReferencedByComputeShader())
787 {
788 unsigned int firstIndex = d3dUniform->csRegisterIndex;
789
790 for (int i = 0; i < count; i++)
791 {
792 unsigned int samplerIndex = firstIndex + i;
793
794 if (samplerIndex < mSamplersCS.size())
795 {
796 ASSERT(mSamplersCS[samplerIndex].active);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -0400797 mSamplersCS[samplerIndex].logicalTextureUnit = d3dUniform->mSamplerData[i];
Xinghua Caob1239382016-12-13 15:07:05 +0800798 }
799 }
800 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700801 }
Jamie Madillaf4ffe02017-09-09 23:32:48 -0400802
803 return SamplerMapping::WasDirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700804}
805
Jamie Madill9cf9e872017-06-05 12:59:25 -0400806gl::LinkResult ProgramD3D::load(const gl::Context *context,
807 gl::InfoLog &infoLog,
808 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700809{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500810 // TODO(jmadill): Use Renderer from contextImpl.
811
Jamie Madill62d31cb2015-09-11 13:25:51 -0400812 reset();
813
Jamie Madill334d6152015-10-22 14:00:28 -0400814 DeviceIdentifier binaryDeviceIdentifier = {0};
815 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
816 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700817
818 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
819 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
820 {
821 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500822 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700823 }
824
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500825 int compileFlags = stream->readInt<int>();
826 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
827 {
Jamie Madillf6113162015-05-07 11:49:21 -0400828 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500829 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500830 }
831
Jamie Madill8047c0d2016-03-07 13:02:12 -0500832 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400833 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500834 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400835 }
836
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700837 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
838 for (unsigned int i = 0; i < psSamplerCount; ++i)
839 {
840 Sampler sampler;
841 stream->readBool(&sampler.active);
842 stream->readInt(&sampler.logicalTextureUnit);
843 stream->readInt(&sampler.textureType);
844 mSamplersPS.push_back(sampler);
845 }
846 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
847 for (unsigned int i = 0; i < vsSamplerCount; ++i)
848 {
849 Sampler sampler;
850 stream->readBool(&sampler.active);
851 stream->readInt(&sampler.logicalTextureUnit);
852 stream->readInt(&sampler.textureType);
853 mSamplersVS.push_back(sampler);
854 }
855
Xinghua Caob1239382016-12-13 15:07:05 +0800856 const unsigned int csSamplerCount = stream->readInt<unsigned int>();
857 for (unsigned int i = 0; i < csSamplerCount; ++i)
858 {
859 Sampler sampler;
860 stream->readBool(&sampler.active);
861 stream->readInt(&sampler.logicalTextureUnit);
862 stream->readInt(&sampler.textureType);
863 mSamplersCS.push_back(sampler);
864 }
865
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700866 stream->readInt(&mUsedVertexSamplerRange);
867 stream->readInt(&mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +0800868 stream->readInt(&mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700869
870 const unsigned int uniformCount = stream->readInt<unsigned int>();
871 if (stream->error())
872 {
Jamie Madillf6113162015-05-07 11:49:21 -0400873 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500874 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700875 }
876
Jamie Madill48ef11b2016-04-27 15:21:52 -0400877 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400878 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700879 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
880 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400881 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700882
Jamie Madill62d31cb2015-09-11 13:25:51 -0400883 D3DUniform *d3dUniform =
Olli Etuaho465835d2017-09-26 13:34:10 +0300884 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySizes,
Jamie Madill62d31cb2015-09-11 13:25:51 -0400885 linkedUniform.isInDefaultBlock());
886 stream->readInt(&d3dUniform->psRegisterIndex);
887 stream->readInt(&d3dUniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800888 stream->readInt(&d3dUniform->csRegisterIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400889 stream->readInt(&d3dUniform->registerCount);
890 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700891
Jamie Madill62d31cb2015-09-11 13:25:51 -0400892 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700893 }
894
Jamie Madill4a3c2342015-10-08 12:58:45 -0400895 const unsigned int blockCount = stream->readInt<unsigned int>();
896 if (stream->error())
897 {
898 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500899 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400900 }
901
902 ASSERT(mD3DUniformBlocks.empty());
903 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
904 {
905 D3DUniformBlock uniformBlock;
906 stream->readInt(&uniformBlock.psRegisterIndex);
907 stream->readInt(&uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +0800908 stream->readInt(&uniformBlock.csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400909 mD3DUniformBlocks.push_back(uniformBlock);
910 }
911
Jamie Madill9fc36822015-11-18 13:08:07 -0500912 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
913 mStreamOutVaryings.resize(streamOutVaryingCount);
914 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700915 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500916 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700917
Jamie Madill28afae52015-11-09 15:07:57 -0500918 stream->readString(&varying->semanticName);
919 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500920 stream->readInt(&varying->componentCount);
921 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700922 }
923
Brandon Jones22502d52014-08-29 16:58:36 -0700924 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400925 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500926 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700927 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400928 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500929 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700930 stream->readBool(&mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +0300931 stream->readBool(&mHasANGLEMultiviewEnabled);
932 stream->readBool(&mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -0700933 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400934 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700935
936 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
937 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400938 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
939 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700940 {
941 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
942 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
943 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
944 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
945 }
946
Jamie Madill4e31ad52015-10-29 10:32:57 -0400947 stream->readString(&mGeometryShaderPreamble);
948
Jamie Madill334d6152015-10-22 14:00:28 -0400949 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700950
Jamie Madillb0a838b2016-11-13 20:02:12 -0500951 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
952
Brandon Joneseb994362014-09-24 10:27:28 -0700953 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400954 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
955 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700956 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400957 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400958 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700959
Jamie Madilld3dfda22015-07-06 08:28:49 -0400960 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700961 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400962 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700963 }
964
Jamie Madill334d6152015-10-22 14:00:28 -0400965 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700966 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400967
Jamie Madillada9ecc2015-08-17 12:53:37 -0400968 ShaderExecutableD3D *shaderExecutable = nullptr;
969
Yunchao He85072e82017-11-14 15:43:28 +0800970 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize,
971 gl::SHADER_VERTEX, mStreamOutVaryings, separateAttribs,
Jamie Madillb0a838b2016-11-13 20:02:12 -0500972 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400973
Brandon Joneseb994362014-09-24 10:27:28 -0700974 if (!shaderExecutable)
975 {
Jamie Madillf6113162015-05-07 11:49:21 -0400976 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500977 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700978 }
979
980 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400981 VertexExecutable::Signature signature;
982 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700983
984 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +0800985 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
986 new VertexExecutable(inputLayout, signature, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -0700987
988 stream->skip(vertexShaderSize);
989 }
990
991 const size_t pixelShaderCount = stream->readInt<unsigned int>();
992 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
993 {
994 const size_t outputCount = stream->readInt<unsigned int>();
995 std::vector<GLenum> outputs(outputCount);
996 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
997 {
998 stream->readInt(&outputs[outputIndex]);
999 }
1000
Jamie Madill334d6152015-10-22 14:00:28 -04001001 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -07001002 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -04001003 ShaderExecutableD3D *shaderExecutable = nullptr;
1004
Yunchao He85072e82017-11-14 15:43:28 +08001005 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize,
1006 gl::SHADER_FRAGMENT, mStreamOutVaryings,
1007 separateAttribs, &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001008
1009 if (!shaderExecutable)
1010 {
Jamie Madillf6113162015-05-07 11:49:21 -04001011 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001012 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001013 }
1014
1015 // add new binary
Xinghua Caob1239382016-12-13 15:07:05 +08001016 mPixelExecutables.push_back(
1017 std::unique_ptr<PixelExecutable>(new PixelExecutable(outputs, shaderExecutable)));
Brandon Joneseb994362014-09-24 10:27:28 -07001018
1019 stream->skip(pixelShaderSize);
1020 }
1021
Jamie Madill4e31ad52015-10-29 10:32:57 -04001022 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
1023 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -07001024 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001025 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
1026 if (geometryShaderSize == 0)
1027 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001028 continue;
1029 }
1030
Brandon Joneseb994362014-09-24 10:27:28 -07001031 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001032
Xinghua Caob1239382016-12-13 15:07:05 +08001033 ShaderExecutableD3D *geometryExecutable = nullptr;
Jamie Madillb0a838b2016-11-13 20:02:12 -05001034 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
Yunchao He85072e82017-11-14 15:43:28 +08001035 gl::SHADER_GEOMETRY, mStreamOutVaryings,
1036 separateAttribs, &geometryExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001037
Xinghua Caob1239382016-12-13 15:07:05 +08001038 if (!geometryExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001039 {
Jamie Madillf6113162015-05-07 11:49:21 -04001040 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001041 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001042 }
Xinghua Caob1239382016-12-13 15:07:05 +08001043
1044 mGeometryExecutables[geometryExeIndex].reset(geometryExecutable);
1045
Brandon Joneseb994362014-09-24 10:27:28 -07001046 stream->skip(geometryShaderSize);
1047 }
1048
Xinghua Caob1239382016-12-13 15:07:05 +08001049 unsigned int computeShaderSize = stream->readInt<unsigned int>();
1050 if (computeShaderSize > 0)
1051 {
1052 const unsigned char *computeShaderFunction = binary + stream->offset();
1053
1054 ShaderExecutableD3D *computeExecutable = nullptr;
1055 ANGLE_TRY(mRenderer->loadExecutable(computeShaderFunction, computeShaderSize,
Yunchao He85072e82017-11-14 15:43:28 +08001056 gl::SHADER_COMPUTE, std::vector<D3DVarying>(), false,
Xinghua Caob1239382016-12-13 15:07:05 +08001057 &computeExecutable));
1058
1059 if (!computeExecutable)
1060 {
1061 infoLog << "Could not create compute shader.";
1062 return false;
1063 }
1064
1065 mComputeExecutable.reset(computeExecutable);
1066 }
1067
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001068 initializeUniformStorage();
1069
Jamie Madillb0a838b2016-11-13 20:02:12 -05001070 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001071}
1072
Jamie Madill27a60632017-06-30 15:12:01 -04001073void ProgramD3D::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -07001074{
Austin Kinross137b1512015-06-17 16:14:53 -07001075 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -04001076 // When we load the binary again later, we can validate the device identifier before trying to
1077 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -07001078 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -04001079 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
1080 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -07001081
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001082 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1083
Jamie Madill8047c0d2016-03-07 13:02:12 -05001084 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -04001085 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05001086 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -04001087 }
1088
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001089 stream->writeInt(mSamplersPS.size());
1090 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1091 {
1092 stream->writeInt(mSamplersPS[i].active);
1093 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
1094 stream->writeInt(mSamplersPS[i].textureType);
1095 }
1096
1097 stream->writeInt(mSamplersVS.size());
1098 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1099 {
1100 stream->writeInt(mSamplersVS[i].active);
1101 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1102 stream->writeInt(mSamplersVS[i].textureType);
1103 }
1104
Xinghua Caob1239382016-12-13 15:07:05 +08001105 stream->writeInt(mSamplersCS.size());
1106 for (unsigned int i = 0; i < mSamplersCS.size(); ++i)
1107 {
1108 stream->writeInt(mSamplersCS[i].active);
1109 stream->writeInt(mSamplersCS[i].logicalTextureUnit);
1110 stream->writeInt(mSamplersCS[i].textureType);
1111 }
1112
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001113 stream->writeInt(mUsedVertexSamplerRange);
1114 stream->writeInt(mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08001115 stream->writeInt(mUsedComputeSamplerRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001116
Jamie Madill62d31cb2015-09-11 13:25:51 -04001117 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001118 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001119 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001120 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001121 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1122 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001123 stream->writeIntOrNegOne(uniform->csRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001124 stream->writeInt(uniform->registerCount);
1125 stream->writeInt(uniform->registerElement);
1126 }
1127
1128 stream->writeInt(mD3DUniformBlocks.size());
1129 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1130 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001131 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1132 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Xinghua Caob1239382016-12-13 15:07:05 +08001133 stream->writeIntOrNegOne(uniformBlock.csRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001134 }
1135
Jamie Madill9fc36822015-11-18 13:08:07 -05001136 stream->writeInt(mStreamOutVaryings.size());
1137 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001138 {
Brandon Joneseb994362014-09-24 10:27:28 -07001139 stream->writeString(varying.semanticName);
1140 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001141 stream->writeInt(varying.componentCount);
1142 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001143 }
1144
Brandon Jones22502d52014-08-29 16:58:36 -07001145 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001146 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001147 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001148 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001149 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001150 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001151 stream->writeInt(mUsesFragDepth);
Martin Radev41ac68e2017-06-06 12:16:58 +03001152 stream->writeInt(mHasANGLEMultiviewEnabled);
1153 stream->writeInt(mUsesViewID);
Brandon Jones44151a92014-09-10 11:32:25 -07001154 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001155 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001156
Brandon Joneseb994362014-09-24 10:27:28 -07001157 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001158 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001159 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1160 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001161 {
Brandon Joneseb994362014-09-24 10:27:28 -07001162 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001163 stream->writeInt(variable.type);
1164 stream->writeString(variable.name);
1165 stream->writeString(variable.source);
1166 stream->writeInt(variable.outputIndex);
1167 }
1168
Jamie Madill4e31ad52015-10-29 10:32:57 -04001169 stream->writeString(mGeometryShaderPreamble);
1170
Brandon Joneseb994362014-09-24 10:27:28 -07001171 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001172 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1173 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001174 {
Xinghua Caob1239382016-12-13 15:07:05 +08001175 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001176
Jamie Madilld3dfda22015-07-06 08:28:49 -04001177 const auto &inputLayout = vertexExecutable->inputs();
1178 stream->writeInt(inputLayout.size());
1179
1180 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001181 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001182 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001183 }
1184
1185 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1186 stream->writeInt(vertexShaderSize);
1187
1188 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1189 stream->writeBytes(vertexBlob, vertexShaderSize);
1190 }
1191
1192 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001193 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1194 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001195 {
Xinghua Caob1239382016-12-13 15:07:05 +08001196 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex].get();
Brandon Joneseb994362014-09-24 10:27:28 -07001197
1198 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1199 stream->writeInt(outputs.size());
1200 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1201 {
1202 stream->writeInt(outputs[outputIndex]);
1203 }
1204
1205 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1206 stream->writeInt(pixelShaderSize);
1207
1208 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1209 stream->writeBytes(pixelBlob, pixelShaderSize);
1210 }
1211
Xinghua Caob1239382016-12-13 15:07:05 +08001212 for (auto const &geometryExecutable : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001213 {
Xinghua Caob1239382016-12-13 15:07:05 +08001214 if (!geometryExecutable)
Jamie Madill4e31ad52015-10-29 10:32:57 -04001215 {
1216 stream->writeInt(0);
1217 continue;
1218 }
1219
Xinghua Caob1239382016-12-13 15:07:05 +08001220 size_t geometryShaderSize = geometryExecutable->getLength();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001221 stream->writeInt(geometryShaderSize);
Xinghua Caob1239382016-12-13 15:07:05 +08001222 stream->writeBytes(geometryExecutable->getFunction(), geometryShaderSize);
1223 }
1224
1225 if (mComputeExecutable)
1226 {
1227 size_t computeShaderSize = mComputeExecutable->getLength();
1228 stream->writeInt(computeShaderSize);
1229 stream->writeBytes(mComputeExecutable->getFunction(), computeShaderSize);
1230 }
1231 else
1232 {
1233 stream->writeInt(0);
Brandon Joneseb994362014-09-24 10:27:28 -07001234 }
Brandon Jones22502d52014-08-29 16:58:36 -07001235}
1236
Geoff Langc5629752015-12-07 16:29:04 -05001237void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1238{
1239}
1240
Yunchao He61afff12017-03-14 15:34:03 +08001241void ProgramD3D::setSeparable(bool /* separable */)
1242{
1243}
1244
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001245gl::Error ProgramD3D::getPixelExecutableForCachedOutputLayout(ShaderExecutableD3D **outExecutable,
1246 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001247{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001248 if (mCachedPixelExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001249 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001250 *outExecutable = mPixelExecutables[mCachedPixelExecutableIndex.value()]->shaderExecutable();
1251 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001252 }
1253
Jamie Madill334d6152015-10-22 14:00:28 -04001254 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001255 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, mPixelShaderOutputLayoutCache);
Brandon Jones22502d52014-08-29 16:58:36 -07001256
1257 // Generate new pixel executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001258 ShaderExecutableD3D *pixelExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001259
1260 gl::InfoLog tempInfoLog;
1261 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1262
Jamie Madill01074252016-11-28 15:55:51 -05001263 ANGLE_TRY(mRenderer->compileToExecutable(
Yunchao He85072e82017-11-14 15:43:28 +08001264 *currentInfoLog, finalPixelHLSL, gl::SHADER_FRAGMENT, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001265 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001266 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001267
Jamie Madill97399232014-12-23 12:31:15 -05001268 if (pixelExecutable)
1269 {
Xinghua Caob1239382016-12-13 15:07:05 +08001270 mPixelExecutables.push_back(std::unique_ptr<PixelExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001271 new PixelExecutable(mPixelShaderOutputLayoutCache, pixelExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001272 mCachedPixelExecutableIndex = mPixelExecutables.size() - 1;
Jamie Madill97399232014-12-23 12:31:15 -05001273 }
1274 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001275 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001276 ERR() << "Error compiling dynamic pixel executable:" << std::endl
1277 << tempInfoLog.str() << std::endl;
Brandon Joneseb994362014-09-24 10:27:28 -07001278 }
Brandon Jones22502d52014-08-29 16:58:36 -07001279
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001280 *outExecutable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001281 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001282}
1283
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001284gl::Error ProgramD3D::getVertexExecutableForCachedInputLayout(ShaderExecutableD3D **outExectuable,
1285 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001286{
Jamie Madill0e7f1732017-09-09 23:32:50 -04001287 if (mCachedVertexExecutableIndex.valid())
Brandon Joneseb994362014-09-24 10:27:28 -07001288 {
Jamie Madill0e7f1732017-09-09 23:32:50 -04001289 *outExectuable =
1290 mVertexExecutables[mCachedVertexExecutableIndex.value()]->shaderExecutable();
1291 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001292 }
1293
Brandon Jones22502d52014-08-29 16:58:36 -07001294 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001295 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001296 mVertexHLSL, mCachedInputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001297
1298 // Generate new vertex executable
Yunchao Hed7297bf2017-04-19 15:27:10 +08001299 ShaderExecutableD3D *vertexExecutable = nullptr;
Jamie Madill97399232014-12-23 12:31:15 -05001300
1301 gl::InfoLog tempInfoLog;
1302 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1303
Jamie Madill01074252016-11-28 15:55:51 -05001304 ANGLE_TRY(mRenderer->compileToExecutable(
Yunchao He85072e82017-11-14 15:43:28 +08001305 *currentInfoLog, finalVertexHLSL, gl::SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001306 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001307 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001308
Jamie Madill97399232014-12-23 12:31:15 -05001309 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001310 {
Xinghua Caob1239382016-12-13 15:07:05 +08001311 mVertexExecutables.push_back(std::unique_ptr<VertexExecutable>(
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001312 new VertexExecutable(mCachedInputLayout, mCachedVertexSignature, vertexExecutable)));
Jamie Madill0e7f1732017-09-09 23:32:50 -04001313 mCachedVertexExecutableIndex = mVertexExecutables.size() - 1;
Brandon Joneseb994362014-09-24 10:27:28 -07001314 }
Jamie Madill97399232014-12-23 12:31:15 -05001315 else if (!infoLog)
1316 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001317 ERR() << "Error compiling dynamic vertex executable:" << std::endl
1318 << tempInfoLog.str() << std::endl;
Jamie Madill97399232014-12-23 12:31:15 -05001319 }
Brandon Jones22502d52014-08-29 16:58:36 -07001320
Geoff Langb543aff2014-09-30 14:52:54 -04001321 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001322 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001323}
1324
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001325gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::Context *context,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001326 GLenum drawMode,
1327 ShaderExecutableD3D **outExecutable,
1328 gl::InfoLog *infoLog)
1329{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001330 if (outExecutable)
1331 {
1332 *outExecutable = nullptr;
1333 }
1334
Austin Kinross88829e82016-01-12 13:04:41 -08001335 // Return a null shader if the current rendering doesn't use a geometry shader
1336 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001337 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001338 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001339 }
1340
Jamie Madill4e31ad52015-10-29 10:32:57 -04001341 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1342
Xinghua Caob1239382016-12-13 15:07:05 +08001343 if (mGeometryExecutables[geometryShaderType])
Jamie Madill4e31ad52015-10-29 10:32:57 -04001344 {
1345 if (outExecutable)
1346 {
Xinghua Caob1239382016-12-13 15:07:05 +08001347 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001348 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001349 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001350 }
1351
1352 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001353 context, geometryShaderType, mState, mRenderer->presentPathFastEnabled(),
Martin Radevc1d4e552017-08-21 12:01:10 +03001354 mHasANGLEMultiviewEnabled, mRenderer->canSelectViewInVertexShader(),
1355 usesGeometryShaderForPointSpriteEmulation(), mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001356
1357 gl::InfoLog tempInfoLog;
1358 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1359
Xinghua Caob1239382016-12-13 15:07:05 +08001360 ShaderExecutableD3D *geometryExecutable = nullptr;
1361 gl::Error error = mRenderer->compileToExecutable(
Yunchao He85072e82017-11-14 15:43:28 +08001362 *currentInfoLog, geometryHLSL, gl::SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001363 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
Xinghua Caob1239382016-12-13 15:07:05 +08001364 angle::CompilerWorkaroundsD3D(), &geometryExecutable);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001365
1366 if (!infoLog && error.isError())
1367 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001368 ERR() << "Error compiling dynamic geometry executable:" << std::endl
1369 << tempInfoLog.str() << std::endl;
Jamie Madill4e31ad52015-10-29 10:32:57 -04001370 }
1371
Xinghua Caob1239382016-12-13 15:07:05 +08001372 if (geometryExecutable != nullptr)
1373 {
1374 mGeometryExecutables[geometryShaderType].reset(geometryExecutable);
1375 }
1376
Jamie Madill4e31ad52015-10-29 10:32:57 -04001377 if (outExecutable)
1378 {
Xinghua Caob1239382016-12-13 15:07:05 +08001379 *outExecutable = mGeometryExecutables[geometryShaderType].get();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001380 }
1381 return error;
1382}
1383
Jamie Madill01074252016-11-28 15:55:51 -05001384class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001385{
Jamie Madill01074252016-11-28 15:55:51 -05001386 public:
1387 GetExecutableTask(ProgramD3D *program)
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001388 : mProgram(program), mError(gl::NoError()), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001389 {
Brandon Joneseb994362014-09-24 10:27:28 -07001390 }
1391
Jamie Madill01074252016-11-28 15:55:51 -05001392 virtual gl::Error run() = 0;
1393
1394 void operator()() override { mError = run(); }
1395
1396 const gl::Error &getError() const { return mError; }
1397 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1398 ShaderExecutableD3D *getResult() { return mResult; }
1399
1400 protected:
1401 ProgramD3D *mProgram;
1402 gl::Error mError;
1403 gl::InfoLog mInfoLog;
1404 ShaderExecutableD3D *mResult;
1405};
1406
1407class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1408{
1409 public:
Jamie Madillbd044ed2017-06-05 12:59:21 -04001410 GetVertexExecutableTask(ProgramD3D *program, const gl::Context *context)
1411 : GetExecutableTask(program), mContext(context)
1412 {
1413 }
Jamie Madill01074252016-11-28 15:55:51 -05001414 gl::Error run() override
1415 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001416 mProgram->updateCachedInputLayoutFromShader(mContext);
Jamie Madill01074252016-11-28 15:55:51 -05001417
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001418 ANGLE_TRY(mProgram->getVertexExecutableForCachedInputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001419
1420 return gl::NoError();
1421 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001422
1423 private:
1424 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001425};
1426
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001427void ProgramD3D::updateCachedInputLayoutFromShader(const gl::Context *context)
1428{
1429 GetDefaultInputLayoutFromShader(context, mState.getAttachedVertexShader(), &mCachedInputLayout);
1430 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001431 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001432}
1433
Jamie Madill01074252016-11-28 15:55:51 -05001434class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1435{
1436 public:
1437 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1438 gl::Error run() override
1439 {
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001440 mProgram->updateCachedOutputLayoutFromShader();
Jamie Madill01074252016-11-28 15:55:51 -05001441
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001442 ANGLE_TRY(mProgram->getPixelExecutableForCachedOutputLayout(&mResult, &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001443
1444 return gl::NoError();
1445 }
1446};
1447
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001448void ProgramD3D::updateCachedOutputLayoutFromShader()
1449{
1450 GetDefaultOutputLayoutFromShader(mPixelShaderKey, &mPixelShaderOutputLayoutCache);
Jamie Madill0e7f1732017-09-09 23:32:50 -04001451 updateCachedPixelExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04001452}
1453
Jamie Madill01074252016-11-28 15:55:51 -05001454class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1455{
1456 public:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001457 GetGeometryExecutableTask(ProgramD3D *program, const gl::Context *context)
1458 : GetExecutableTask(program), mContext(context)
Jamie Madill01074252016-11-28 15:55:51 -05001459 {
1460 }
1461
1462 gl::Error run() override
1463 {
1464 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1465 // D3D11.
1466 if (mProgram->usesGeometryShader(GL_POINTS))
1467 {
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001468 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContext, GL_POINTS, &mResult,
1469 &mInfoLog));
Jamie Madill01074252016-11-28 15:55:51 -05001470 }
1471
1472 return gl::NoError();
1473 }
1474
1475 private:
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001476 const gl::Context *mContext;
Jamie Madill01074252016-11-28 15:55:51 -05001477};
1478
Xinghua Cao73badc02017-03-29 19:14:53 +08001479gl::Error ProgramD3D::getComputeExecutable(ShaderExecutableD3D **outExecutable)
1480{
1481 if (outExecutable)
1482 {
1483 *outExecutable = mComputeExecutable.get();
1484 }
1485
1486 return gl::NoError();
1487}
1488
Jamie Madill9cf9e872017-06-05 12:59:25 -04001489gl::LinkResult ProgramD3D::compileProgramExecutables(const gl::Context *context,
1490 gl::InfoLog &infoLog)
Jamie Madill01074252016-11-28 15:55:51 -05001491{
1492 // Ensure the compiler is initialized to avoid race conditions.
1493 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1494
1495 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1496
Jamie Madillbd044ed2017-06-05 12:59:21 -04001497 GetVertexExecutableTask vertexTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001498 GetPixelExecutableTask pixelTask(this);
Jamie Madillc9fed8d2017-09-12 15:23:00 -04001499 GetGeometryExecutableTask geometryTask(this, context);
Jamie Madill01074252016-11-28 15:55:51 -05001500
1501 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1502 workerPool->postWorkerTask(&pixelTask),
1503 workerPool->postWorkerTask(&geometryTask)}};
1504
1505 WaitableEvent::WaitMany(&waitEvents);
1506
1507 infoLog << vertexTask.getInfoLog().str();
1508 infoLog << pixelTask.getInfoLog().str();
1509 infoLog << geometryTask.getInfoLog().str();
1510
1511 ANGLE_TRY(vertexTask.getError());
1512 ANGLE_TRY(pixelTask.getError());
1513 ANGLE_TRY(geometryTask.getError());
1514
1515 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1516 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1517 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1518
Jamie Madill48ef11b2016-04-27 15:21:52 -04001519 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001520
1521 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001522 {
Jamie Madill334d6152015-10-22 14:00:28 -04001523 // Geometry shaders are currently only used internally, so there is no corresponding shader
1524 // object at the interface level. For now the geometry shader debug info is prepended to
1525 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001526 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001527 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001528 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1529 }
1530
1531 if (defaultVertexExecutable)
1532 {
1533 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1534 }
1535
1536 if (defaultPixelExecutable)
1537 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001538 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001539 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001540 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1541 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001542
Jamie Madillb0a838b2016-11-13 20:02:12 -05001543 return (defaultVertexExecutable && defaultPixelExecutable &&
1544 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001545}
1546
Jamie Madill9cf9e872017-06-05 12:59:25 -04001547gl::LinkResult ProgramD3D::compileComputeExecutable(const gl::Context *context,
1548 gl::InfoLog &infoLog)
Xinghua Caob1239382016-12-13 15:07:05 +08001549{
1550 // Ensure the compiler is initialized to avoid race conditions.
1551 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1552
Jamie Madillbd044ed2017-06-05 12:59:21 -04001553 std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(context, mState);
Xinghua Caob1239382016-12-13 15:07:05 +08001554
1555 ShaderExecutableD3D *computeExecutable = nullptr;
Yunchao He85072e82017-11-14 15:43:28 +08001556 ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, gl::SHADER_COMPUTE,
Xinghua Caob1239382016-12-13 15:07:05 +08001557 std::vector<D3DVarying>(), false,
1558 angle::CompilerWorkaroundsD3D(), &computeExecutable));
1559
1560 if (computeExecutable == nullptr)
1561 {
Yuly Novikovd73f8522017-01-13 17:48:57 -05001562 ERR() << "Error compiling dynamic compute executable:" << std::endl
1563 << infoLog.str() << std::endl;
Xinghua Caob1239382016-12-13 15:07:05 +08001564 }
1565 else
1566 {
1567 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
1568 computeShaderD3D->appendDebugInfo(computeExecutable->getDebugInfo());
1569 mComputeExecutable.reset(computeExecutable);
1570 }
1571
1572 return mComputeExecutable.get() != nullptr;
1573}
1574
Jamie Madill9cf9e872017-06-05 12:59:25 -04001575gl::LinkResult ProgramD3D::link(const gl::Context *context,
Jamie Madillc9727f32017-11-07 12:37:07 -05001576 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -04001577 gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001578{
Jamie Madillc564c072017-06-01 12:45:42 -04001579 const auto &data = context->getContextState();
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001580
Jamie Madill62d31cb2015-09-11 13:25:51 -04001581 reset();
1582
Jamie Madillbd044ed2017-06-05 12:59:21 -04001583 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001584 if (computeShader)
Austin Kinross02df7962015-07-01 10:03:42 -07001585 {
Xinghua Caob1239382016-12-13 15:07:05 +08001586 mSamplersCS.resize(data.getCaps().maxComputeTextureImageUnits);
1587
Jamie Madillbd044ed2017-06-05 12:59:21 -04001588 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001589
Jamie Madill9cf9e872017-06-05 12:59:25 -04001590 gl::LinkResult result = compileComputeExecutable(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001591 if (result.isError())
Austin Kinross02df7962015-07-01 10:03:42 -07001592 {
Xinghua Caob1239382016-12-13 15:07:05 +08001593 infoLog << result.getError().getMessage();
1594 return result;
1595 }
1596 else if (!result.getResult())
1597 {
1598 infoLog << "Failed to create D3D compute shader.";
1599 return result;
1600 }
Xinghua Caob1239382016-12-13 15:07:05 +08001601 }
1602 else
1603 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001604 gl::Shader *vertexShader = mState.getAttachedVertexShader();
1605 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Xinghua Caob1239382016-12-13 15:07:05 +08001606
1607 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1608 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1609
1610 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1611 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
1612
1613 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
1614 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1615
1616 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
1617 {
1618 if (fragmentShaderD3D->usesFrontFacing())
1619 {
1620 infoLog << "The current renderer doesn't support gl_FrontFacing";
1621 return false;
1622 }
1623 }
1624
1625 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1626 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1627 // intelligently, but D3D9 assumes one semantic per register.
1628 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madillc9727f32017-11-07 12:37:07 -05001629 resources.varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Xinghua Caob1239382016-12-13 15:07:05 +08001630 {
1631 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001632 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001633 }
Xinghua Caob1239382016-12-13 15:07:05 +08001634
1635 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madillc9727f32017-11-07 12:37:07 -05001636 BuiltinVaryingsD3D builtins(metadata, resources.varyingPacking);
Xinghua Caob1239382016-12-13 15:07:05 +08001637
Jamie Madillc9727f32017-11-07 12:37:07 -05001638 mDynamicHLSL->generateShaderLinkHLSL(context, mState, metadata, resources.varyingPacking,
1639 builtins, &mPixelHLSL, &mVertexHLSL);
Xinghua Caob1239382016-12-13 15:07:05 +08001640
1641 mUsesPointSize = vertexShaderD3D->usesPointSize();
1642 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1643 mUsesFragDepth = metadata.usesFragDepth();
Martin Radev41ac68e2017-06-06 12:16:58 +03001644 mUsesViewID = metadata.usesViewID();
1645 mHasANGLEMultiviewEnabled = metadata.hasANGLEMultiviewEnabled();
Xinghua Caob1239382016-12-13 15:07:05 +08001646
1647 // Cache if we use flat shading
Jamie Madillbd044ed2017-06-05 12:59:21 -04001648 mUsesFlatInterpolation =
Jiawei Shao3d404882017-10-16 13:30:48 +08001649 (FindFlatInterpolationVarying(fragmentShader->getInputVaryings(context)) ||
1650 FindFlatInterpolationVarying(vertexShader->getOutputVaryings(context)));
Xinghua Caob1239382016-12-13 15:07:05 +08001651
1652 if (mRenderer->getMajorShaderModel() >= 4)
1653 {
Martin Radev41ac68e2017-06-06 12:16:58 +03001654 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(
Jamie Madillc9727f32017-11-07 12:37:07 -05001655 resources.varyingPacking, builtins, mHasANGLEMultiviewEnabled,
Martin Radevc1d4e552017-08-21 12:01:10 +03001656 metadata.canSelectViewInVertexShader());
Xinghua Caob1239382016-12-13 15:07:05 +08001657 }
1658
Jamie Madillbd044ed2017-06-05 12:59:21 -04001659 initAttribLocationsToD3DSemantic(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001660
Jamie Madillbd044ed2017-06-05 12:59:21 -04001661 defineUniformsAndAssignRegisters(context);
Xinghua Caob1239382016-12-13 15:07:05 +08001662
Yunchao He85072e82017-11-14 15:43:28 +08001663 gatherTransformFeedbackVaryings(resources.varyingPacking, builtins[gl::SHADER_VERTEX]);
Xinghua Caob1239382016-12-13 15:07:05 +08001664
Jamie Madill9cf9e872017-06-05 12:59:25 -04001665 gl::LinkResult result = compileProgramExecutables(context, infoLog);
Xinghua Caob1239382016-12-13 15:07:05 +08001666 if (result.isError())
1667 {
1668 infoLog << result.getError().getMessage();
1669 return result;
1670 }
1671 else if (!result.getResult())
1672 {
1673 infoLog << "Failed to create D3D shaders.";
1674 return result;
1675 }
Austin Kinross02df7962015-07-01 10:03:42 -07001676 }
1677
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001678 linkResources(context, resources);
1679
Jamie Madillb0a838b2016-11-13 20:02:12 -05001680 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001681}
1682
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001683GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001684{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001685 // TODO(jmadill): Do something useful here?
1686 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001687}
1688
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001689void ProgramD3D::initializeUniformBlocks()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001690{
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001691 if (mState.getUniformBlocks().empty())
Jamie Madill51f522f2016-12-21 15:10:55 -05001692 {
1693 return;
1694 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001695
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001696 ASSERT(mD3DUniformBlocks.empty());
1697
Jamie Madill62d31cb2015-09-11 13:25:51 -04001698 // Assign registers and update sizes.
Xinghua Caob1239382016-12-13 15:07:05 +08001699 const ShaderD3D *vertexShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1700 const ShaderD3D *fragmentShaderD3D =
1701 SafeGetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
1702 const ShaderD3D *computeShaderD3D = SafeGetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001703
Jiajia Qin729b2c62017-08-14 09:36:11 +08001704 for (const gl::InterfaceBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001705 {
1706 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1707
Jamie Madill4a3c2342015-10-08 12:58:45 -04001708 D3DUniformBlock d3dUniformBlock;
1709
Jamie Madill62d31cb2015-09-11 13:25:51 -04001710 if (uniformBlock.vertexStaticUse)
1711 {
Xinghua Caob1239382016-12-13 15:07:05 +08001712 ASSERT(vertexShaderD3D != nullptr);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001713 unsigned int baseRegister = vertexShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001714 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001715 }
1716
1717 if (uniformBlock.fragmentStaticUse)
1718 {
Xinghua Caob1239382016-12-13 15:07:05 +08001719 ASSERT(fragmentShaderD3D != nullptr);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001720 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001721 fragmentShaderD3D->getUniformBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001722 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001723 }
1724
Xinghua Caob1239382016-12-13 15:07:05 +08001725 if (uniformBlock.computeStaticUse)
1726 {
1727 ASSERT(computeShaderD3D != nullptr);
1728 unsigned int baseRegister =
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001729 computeShaderD3D->getUniformBlockRegister(uniformBlock.name);
Xinghua Caob1239382016-12-13 15:07:05 +08001730 d3dUniformBlock.csRegisterIndex = baseRegister + uniformBlockElement;
1731 }
1732
Jamie Madill4a3c2342015-10-08 12:58:45 -04001733 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001734 }
1735}
1736
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001737void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001738{
1739 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001740 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001741 unsigned int fragmentRegisters = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08001742 unsigned int computeRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001743 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001744 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001745 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001746 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001747 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001748 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001749 vertexRegisters = std::max(vertexRegisters,
1750 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001751 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001752 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001753 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001754 fragmentRegisters = std::max(
1755 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001756 }
Xinghua Caob1239382016-12-13 15:07:05 +08001757 if (d3dUniform->isReferencedByComputeShader())
1758 {
1759 computeRegisters = std::max(
1760 computeRegisters, d3dUniform->csRegisterIndex + d3dUniform->registerCount);
1761 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001762 }
1763 }
1764
Xinghua Caob1239382016-12-13 15:07:05 +08001765 mVertexUniformStorage =
1766 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(vertexRegisters * 16u));
1767 mFragmentUniformStorage = std::unique_ptr<UniformStorageD3D>(
1768 mRenderer->createUniformStorage(fragmentRegisters * 16u));
1769 mComputeUniformStorage =
1770 std::unique_ptr<UniformStorageD3D>(mRenderer->createUniformStorage(computeRegisters * 16u));
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001771
1772 // Iterate the uniforms again to assign data pointers to default block uniforms.
1773 for (D3DUniform *d3dUniform : mD3DUniforms)
1774 {
1775 if (d3dUniform->isSampler())
1776 {
Olli Etuaho465835d2017-09-26 13:34:10 +03001777 d3dUniform->mSamplerData.resize(d3dUniform->getArraySizeProduct(), 0);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001778 continue;
1779 }
1780
1781 if (d3dUniform->isReferencedByVertexShader())
1782 {
1783 d3dUniform->vsData = mVertexUniformStorage->getDataPointer(d3dUniform->vsRegisterIndex,
1784 d3dUniform->registerElement);
1785 }
1786
1787 if (d3dUniform->isReferencedByFragmentShader())
1788 {
1789 d3dUniform->psData = mFragmentUniformStorage->getDataPointer(
1790 d3dUniform->psRegisterIndex, d3dUniform->registerElement);
1791 }
1792
1793 if (d3dUniform->isReferencedByComputeShader())
1794 {
1795 d3dUniform->csData = mComputeUniformStorage->getDataPointer(
1796 d3dUniform->csRegisterIndex, d3dUniform->registerElement);
1797 }
1798 }
Brandon Jonesc9610c52014-08-25 17:02:59 -07001799}
1800
Jamie Madilld63961d2017-09-12 15:22:57 -04001801void ProgramD3D::updateUniformBufferCache(const gl::Caps &caps,
1802 unsigned int reservedVertex,
1803 unsigned int reservedFragment)
Brandon Jones18bd4102014-09-22 14:21:44 -07001804{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001805 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001806 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001807 return;
Jamie Madill4a3c2342015-10-08 12:58:45 -04001808 }
1809
Jamie Madill03260fa2015-06-22 13:57:22 -04001810 mVertexUBOCache.clear();
1811 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001812
Jamie Madill4a3c2342015-10-08 12:58:45 -04001813 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001814 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001815 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001816 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001817 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001818
Brandon Jones18bd4102014-09-22 14:21:44 -07001819 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001820 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001821 {
1822 continue;
1823 }
1824
Jamie Madill4a3c2342015-10-08 12:58:45 -04001825 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001826 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001827 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedVertex;
1828 ASSERT(registerIndex < caps.maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001829
Jamie Madill969194d2015-07-20 14:36:56 -04001830 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001831 {
1832 mVertexUBOCache.resize(registerIndex + 1, -1);
1833 }
1834
1835 ASSERT(mVertexUBOCache[registerIndex] == -1);
1836 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001837 }
1838
Jamie Madill4a3c2342015-10-08 12:58:45 -04001839 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001840 {
Jamie Madilld63961d2017-09-12 15:22:57 -04001841 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedFragment;
1842 ASSERT(registerIndex < caps.maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001843
1844 if (mFragmentUBOCache.size() <= registerIndex)
1845 {
1846 mFragmentUBOCache.resize(registerIndex + 1, -1);
1847 }
1848
1849 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1850 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001851 }
1852 }
Jamie Madilld63961d2017-09-12 15:22:57 -04001853}
Brandon Jones18bd4102014-09-22 14:21:44 -07001854
Jamie Madilld63961d2017-09-12 15:22:57 -04001855const std::vector<GLint> &ProgramD3D::getVertexUniformBufferCache() const
1856{
1857 return mVertexUBOCache;
1858}
1859
1860const std::vector<GLint> &ProgramD3D::getFragmentUniformBufferCache() const
1861{
1862 return mFragmentUBOCache;
Brandon Jones18bd4102014-09-22 14:21:44 -07001863}
1864
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001865void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001866{
Jamie Madill4148fd72017-09-14 15:46:20 -04001867 mVertexUniformsDirty = true;
1868 mFragmentUniformsDirty = true;
1869 mComputeUniformsDirty = true;
1870}
1871
1872void ProgramD3D::markUniformsClean()
1873{
1874 mVertexUniformsDirty = false;
1875 mFragmentUniformsDirty = false;
1876 mComputeUniformsDirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001877}
1878
Jamie Madill334d6152015-10-22 14:00:28 -04001879void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001880{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001881 setUniformInternal(location, count, v, GL_FLOAT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001882}
1883
1884void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1885{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001886 setUniformInternal(location, count, v, GL_FLOAT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001887}
1888
1889void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1890{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001891 setUniformInternal(location, count, v, GL_FLOAT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001892}
1893
1894void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1895{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001896 setUniformInternal(location, count, v, GL_FLOAT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001897}
1898
Jamie Madill334d6152015-10-22 14:00:28 -04001899void ProgramD3D::setUniformMatrix2fv(GLint location,
1900 GLsizei count,
1901 GLboolean transpose,
1902 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001903{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001904 setUniformMatrixfvInternal<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001905}
1906
Jamie Madill334d6152015-10-22 14:00:28 -04001907void ProgramD3D::setUniformMatrix3fv(GLint location,
1908 GLsizei count,
1909 GLboolean transpose,
1910 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001911{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001912 setUniformMatrixfvInternal<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001913}
1914
Jamie Madill334d6152015-10-22 14:00:28 -04001915void ProgramD3D::setUniformMatrix4fv(GLint location,
1916 GLsizei count,
1917 GLboolean transpose,
1918 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001919{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001920 setUniformMatrixfvInternal<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001921}
1922
Jamie Madill334d6152015-10-22 14:00:28 -04001923void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1924 GLsizei count,
1925 GLboolean transpose,
1926 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001928 setUniformMatrixfvInternal<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001929}
1930
Jamie Madill334d6152015-10-22 14:00:28 -04001931void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1932 GLsizei count,
1933 GLboolean transpose,
1934 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001935{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001936 setUniformMatrixfvInternal<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001937}
1938
Jamie Madill334d6152015-10-22 14:00:28 -04001939void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1940 GLsizei count,
1941 GLboolean transpose,
1942 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001944 setUniformMatrixfvInternal<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001945}
1946
Jamie Madill334d6152015-10-22 14:00:28 -04001947void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1948 GLsizei count,
1949 GLboolean transpose,
1950 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001951{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001952 setUniformMatrixfvInternal<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001953}
1954
Jamie Madill334d6152015-10-22 14:00:28 -04001955void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1956 GLsizei count,
1957 GLboolean transpose,
1958 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001959{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001960 setUniformMatrixfvInternal<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001961}
1962
Jamie Madill334d6152015-10-22 14:00:28 -04001963void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1964 GLsizei count,
1965 GLboolean transpose,
1966 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001967{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001968 setUniformMatrixfvInternal<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001969}
1970
1971void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1972{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001973 setUniformInternal(location, count, v, GL_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001974}
1975
1976void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1977{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001978 setUniformInternal(location, count, v, GL_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001979}
1980
1981void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1982{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001983 setUniformInternal(location, count, v, GL_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001984}
1985
1986void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1987{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001988 setUniformInternal(location, count, v, GL_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001989}
1990
1991void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1992{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001993 setUniformInternal(location, count, v, GL_UNSIGNED_INT);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001994}
1995
1996void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1997{
Jamie Madill33bb7c42017-09-09 23:32:51 -04001998 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC2);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001999}
2000
2001void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2002{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002003 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC3);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002004}
2005
2006void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2007{
Jamie Madill33bb7c42017-09-09 23:32:51 -04002008 setUniformInternal(location, count, v, GL_UNSIGNED_INT_VEC4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002009}
2010
Jamie Madill4a3c2342015-10-08 12:58:45 -04002011void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
2012 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04002013{
2014}
2015
Jamie Madillbd044ed2017-06-05 12:59:21 -04002016void ProgramD3D::defineUniformsAndAssignRegisters(const gl::Context *context)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002017{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002018 D3DUniformMap uniformMap;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002019 gl::Shader *computeShader = mState.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +08002020 if (computeShader)
Jamie Madill417df922017-01-12 09:23:07 -05002021 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002022 for (const sh::Uniform &computeUniform : computeShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04002023 {
Xinghua Caob1239382016-12-13 15:07:05 +08002024 if (computeUniform.staticUse)
2025 {
2026 defineUniformBase(computeShader, computeUniform, &uniformMap);
2027 }
Jamie Madillfb536032015-09-11 13:19:49 -04002028 }
2029 }
Xinghua Caob1239382016-12-13 15:07:05 +08002030 else
Jamie Madillfb536032015-09-11 13:19:49 -04002031 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002032 gl::Shader *vertexShader = mState.getAttachedVertexShader();
2033 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms(context))
Jamie Madillfb536032015-09-11 13:19:49 -04002034 {
Xinghua Caob1239382016-12-13 15:07:05 +08002035 if (vertexUniform.staticUse)
2036 {
2037 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
2038 }
2039 }
2040
Jamie Madillbd044ed2017-06-05 12:59:21 -04002041 gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
2042 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms(context))
Xinghua Caob1239382016-12-13 15:07:05 +08002043 {
2044 if (fragmentUniform.staticUse)
2045 {
2046 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
2047 }
Jamie Madillfb536032015-09-11 13:19:49 -04002048 }
2049 }
2050
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002051 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002052 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002053 {
2054 if (!glUniform.isInDefaultBlock())
2055 continue;
2056
Olli Etuahod2551232017-10-26 20:03:33 +03002057 std::string name = glUniform.name;
2058 if (glUniform.isArray())
2059 {
2060 // In the program state, array uniform names include [0] as in the program resource
2061 // spec. Here we don't include it.
2062 // TODO(oetuaho@nvidia.com): consider using the same uniform naming here as in the GL
2063 // layer.
2064 ASSERT(angle::EndsWith(name, "[0]"));
2065 name.resize(name.length() - 3);
2066 }
2067 auto mapEntry = uniformMap.find(name);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002068 ASSERT(mapEntry != uniformMap.end());
2069 mD3DUniforms.push_back(mapEntry->second);
2070 }
2071
Jamie Madill62d31cb2015-09-11 13:25:51 -04002072 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04002073 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04002074}
2075
Jamie Madill91445bc2015-09-23 16:47:53 -04002076void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002077 const sh::Uniform &uniform,
2078 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04002079{
Olli Etuaho96963162016-03-21 11:54:33 +02002080 // Samplers get their registers assigned in assignAllSamplerRegisters.
2081 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04002082 {
Jamie Madill91445bc2015-09-23 16:47:53 -04002083 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04002084 return;
2085 }
2086
Jamie Madill91445bc2015-09-23 16:47:53 -04002087 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
2088
2089 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
2090 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Jamie Madillcc2ed612017-03-14 15:59:00 -04002091 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType), true);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002092 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002093
Jamie Madill91445bc2015-09-23 16:47:53 -04002094 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002095}
2096
Jamie Madill62d31cb2015-09-11 13:25:51 -04002097D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
2098{
2099 for (D3DUniform *d3dUniform : mD3DUniforms)
2100 {
2101 if (d3dUniform->name == name)
2102 {
2103 return d3dUniform;
2104 }
2105 }
2106
2107 return nullptr;
2108}
2109
Olli Etuaho465835d2017-09-26 13:34:10 +03002110void ProgramD3D::defineStructUniformFields(GLenum shaderType,
2111 const std::vector<sh::ShaderVariable> &fields,
2112 const std::string &namePrefix,
2113 sh::HLSLBlockEncoder *encoder,
2114 D3DUniformMap *uniformMap)
2115{
2116 if (encoder)
2117 encoder->enterAggregateType();
2118
2119 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
2120 {
2121 const sh::ShaderVariable &field = fields[fieldIndex];
2122 const std::string &fieldFullName = (namePrefix + "." + field.name);
2123
2124 // Samplers get their registers assigned in assignAllSamplerRegisters.
2125 // Also they couldn't use the same encoder as the rest of the struct, since they are
2126 // extracted out of the struct by the shader translator.
2127 if (gl::IsSamplerType(field.type))
2128 {
2129 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
2130 }
2131 else
2132 {
2133 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
2134 }
2135 }
2136
2137 if (encoder)
2138 encoder->exitAggregateType();
2139}
2140
2141void ProgramD3D::defineArrayOfStructsUniformFields(GLenum shaderType,
2142 const sh::ShaderVariable &uniform,
2143 unsigned int arrayNestingIndex,
2144 const std::string &prefix,
2145 sh::HLSLBlockEncoder *encoder,
2146 D3DUniformMap *uniformMap)
2147{
2148 // Nested arrays are processed starting from outermost (arrayNestingIndex 0u) and ending at the
2149 // innermost.
2150 const unsigned int currentArraySize = uniform.getNestedArraySize(arrayNestingIndex);
2151 for (unsigned int arrayElement = 0u; arrayElement < currentArraySize; ++arrayElement)
2152 {
2153 const std::string &elementString = prefix + ArrayString(arrayElement);
2154 if (arrayNestingIndex + 1u < uniform.arraySizes.size())
2155 {
2156 defineArrayOfStructsUniformFields(shaderType, uniform, arrayNestingIndex + 1u,
2157 elementString, encoder, uniformMap);
2158 }
2159 else
2160 {
2161 defineStructUniformFields(shaderType, uniform.fields, elementString, encoder,
2162 uniformMap);
2163 }
2164 }
2165}
2166
2167void ProgramD3D::defineArrayUniformElements(GLenum shaderType,
2168 const sh::ShaderVariable &uniform,
2169 const std::string &fullName,
2170 sh::HLSLBlockEncoder *encoder,
2171 D3DUniformMap *uniformMap)
2172{
2173 if (encoder)
2174 encoder->enterAggregateType();
2175
2176 sh::ShaderVariable uniformElement = uniform;
2177 uniformElement.arraySizes.pop_back();
2178 for (unsigned int arrayIndex = 0u; arrayIndex < uniform.getOutermostArraySize(); ++arrayIndex)
2179 {
2180 std::string elementFullName = fullName + ArrayString(arrayIndex);
2181 defineUniform(shaderType, uniformElement, elementFullName, encoder, uniformMap);
2182 }
2183
2184 if (encoder)
2185 encoder->exitAggregateType();
2186}
2187
Jamie Madill91445bc2015-09-23 16:47:53 -04002188void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002189 const sh::ShaderVariable &uniform,
2190 const std::string &fullName,
2191 sh::HLSLBlockEncoder *encoder,
2192 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002193{
2194 if (uniform.isStruct())
2195 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002196 if (uniform.isArray())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002197 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002198 defineArrayOfStructsUniformFields(shaderType, uniform, 0u, fullName, encoder,
2199 uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002200 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002201 else
2202 {
2203 defineStructUniformFields(shaderType, uniform.fields, fullName, encoder, uniformMap);
2204 }
2205 return;
2206 }
2207 if (uniform.isArrayOfArrays())
2208 {
2209 defineArrayUniformElements(shaderType, uniform, fullName, encoder, uniformMap);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002210 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002211 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002212
2213 // Not a struct. Arrays are treated as aggregate types.
2214 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002215 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002216 encoder->enterAggregateType();
2217 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002218
Jamie Madill62d31cb2015-09-11 13:25:51 -04002219 // Advance the uniform offset, to track registers allocation for structs
2220 sh::BlockMemberInfo blockInfo =
Olli Etuaho465835d2017-09-26 13:34:10 +03002221 encoder ? encoder->encodeType(uniform.type, uniform.arraySizes, false)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002222 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002223
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002224 auto uniformMapEntry = uniformMap->find(fullName);
2225 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002226
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002227 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002228 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002229 d3dUniform = uniformMapEntry->second;
2230 }
2231 else
2232 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002233 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySizes, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002234 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002235 }
2236
2237 if (encoder)
2238 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002239 d3dUniform->registerElement =
2240 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002241 unsigned int reg =
2242 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002243 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002244 {
2245 d3dUniform->psRegisterIndex = reg;
2246 }
Xinghua Caob1239382016-12-13 15:07:05 +08002247 else if (shaderType == GL_VERTEX_SHADER)
2248 {
2249 d3dUniform->vsRegisterIndex = reg;
2250 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002251 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002252 {
Xinghua Caob1239382016-12-13 15:07:05 +08002253 ASSERT(shaderType == GL_COMPUTE_SHADER);
2254 d3dUniform->csRegisterIndex = reg;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002255 }
Jamie Madillfb536032015-09-11 13:19:49 -04002256
2257 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002258 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002259 {
2260 encoder->exitAggregateType();
2261 }
2262 }
2263}
2264
Jamie Madill134f93d2017-08-31 17:11:00 -04002265// Assume count is already clamped.
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002266template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002267void ProgramD3D::setUniformImpl(const gl::VariableLocation &locationInfo,
Jamie Madill134f93d2017-08-31 17:11:00 -04002268 GLsizei count,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002269 const T *v,
2270 uint8_t *targetData,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002271 GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002272{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002273 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
Jamie Madill33bb7c42017-09-09 23:32:51 -04002274 const int components = targetUniform->typeInfo.componentCount;
Olli Etuaho1734e172017-10-27 15:30:27 +03002275 const unsigned int arrayElementOffset = locationInfo.arrayIndex;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002276
Jamie Madill33bb7c42017-09-09 23:32:51 -04002277 if (targetUniform->typeInfo.type == uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002278 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002279 T *dest = reinterpret_cast<T *>(targetData) + arrayElementOffset * 4;
Jamie Madill33bb7c42017-09-09 23:32:51 -04002280 const T *source = v;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002281
Jamie Madill33bb7c42017-09-09 23:32:51 -04002282 for (GLint i = 0; i < count; i++, dest += 4, source += components)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002283 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002284 memcpy(dest, source, components * sizeof(T));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002285 }
2286 }
Jamie Madill33bb7c42017-09-09 23:32:51 -04002287 else
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002288 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002289 ASSERT(targetUniform->typeInfo.type == gl::VariableBoolVectorType(uniformType));
Olli Etuahoc8538042017-09-27 11:20:15 +03002290 GLint *boolParams = reinterpret_cast<GLint *>(targetData) + arrayElementOffset * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002291
Jamie Madill134f93d2017-08-31 17:11:00 -04002292 for (GLint i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002293 {
Jamie Madill334d6152015-10-22 14:00:28 -04002294 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002295 const T *source = v + (i * components);
2296
2297 for (int c = 0; c < components; c++)
2298 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002299 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002300 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002301 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002302 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002303}
2304
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002305template <typename T>
Jamie Madill33bb7c42017-09-09 23:32:51 -04002306void ProgramD3D::setUniformInternal(GLint location, GLsizei count, const T *v, GLenum uniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002307{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002308 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2309 D3DUniform *targetUniform = mD3DUniforms[locationInfo.index];
2310
Jamie Madill33bb7c42017-09-09 23:32:51 -04002311 if (targetUniform->typeInfo.isSampler)
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002312 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002313 ASSERT(uniformType == GL_INT);
Jamie Madill80823cc2017-09-14 15:46:21 -04002314 size_t size = count * sizeof(T);
Jamie Madillacf2f3a2017-11-21 19:22:44 -05002315 GLint *dest = &targetUniform->mSamplerData[locationInfo.arrayIndex];
Jamie Madill80823cc2017-09-14 15:46:21 -04002316 if (memcmp(dest, v, size) != 0)
2317 {
2318 memcpy(dest, v, size);
2319 mDirtySamplerMapping = true;
2320 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002321 return;
2322 }
2323
2324 if (targetUniform->vsData)
2325 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002326 setUniformImpl(locationInfo, count, v, targetUniform->vsData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002327 mVertexUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002328 }
2329
2330 if (targetUniform->psData)
2331 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002332 setUniformImpl(locationInfo, count, v, targetUniform->psData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002333 mFragmentUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002334 }
2335
2336 if (targetUniform->csData)
2337 {
Jamie Madill33bb7c42017-09-09 23:32:51 -04002338 setUniformImpl(locationInfo, count, v, targetUniform->csData, uniformType);
Jamie Madill4148fd72017-09-14 15:46:20 -04002339 mComputeUniformsDirty = true;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002340 }
2341}
2342
2343template <int cols, int rows>
Jamie Madill80823cc2017-09-14 15:46:21 -04002344bool ProgramD3D::setUniformMatrixfvImpl(GLint location,
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002345 GLsizei countIn,
2346 GLboolean transpose,
2347 const GLfloat *value,
2348 uint8_t *targetData,
2349 GLenum targetUniformType)
2350{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002351 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002352
Olli Etuaho465835d2017-09-26 13:34:10 +03002353 unsigned int elementCount = targetUniform->getArraySizeProduct();
Olli Etuaho1734e172017-10-27 15:30:27 +03002354 unsigned int arrayElementOffset = mState.getUniformLocations()[location].arrayIndex;
Olli Etuahoc8538042017-09-27 11:20:15 +03002355 unsigned int count =
2356 std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002357
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002358 const unsigned int targetMatrixStride = (4 * rows);
Olli Etuahoc8538042017-09-27 11:20:15 +03002359 GLfloat *target = reinterpret_cast<GLfloat *>(
2360 targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002361
Jamie Madill80823cc2017-09-14 15:46:21 -04002362 bool dirty = false;
2363
Jamie Madill62d31cb2015-09-11 13:25:51 -04002364 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002365 {
2366 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2367 if (transpose == GL_FALSE)
2368 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002369 dirty = TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002370 }
2371 else
2372 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002373 dirty = ExpandMatrix<GLfloat, cols, rows>(target, value) || dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002374 }
2375 target += targetMatrixStride;
2376 value += cols * rows;
2377 }
Jamie Madill80823cc2017-09-14 15:46:21 -04002378
2379 return dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002380}
2381
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002382template <int cols, int rows>
2383void ProgramD3D::setUniformMatrixfvInternal(GLint location,
2384 GLsizei countIn,
2385 GLboolean transpose,
2386 const GLfloat *value,
2387 GLenum targetUniformType)
2388{
2389 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
2390
2391 if (targetUniform->vsData)
2392 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002393 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2394 targetUniform->vsData, targetUniformType))
2395 {
2396 mVertexUniformsDirty = true;
2397 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002398 }
2399
2400 if (targetUniform->psData)
2401 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002402 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2403 targetUniform->psData, targetUniformType))
2404 {
2405 mFragmentUniformsDirty = true;
2406 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002407 }
2408
2409 if (targetUniform->csData)
2410 {
Jamie Madill80823cc2017-09-14 15:46:21 -04002411 if (setUniformMatrixfvImpl<cols, rows>(location, countIn, transpose, value,
2412 targetUniform->csData, targetUniformType))
2413 {
2414 mComputeUniformsDirty = true;
2415 }
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002416 }
2417}
2418
Jamie Madill62d31cb2015-09-11 13:25:51 -04002419void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002420{
Olli Etuaho465835d2017-09-26 13:34:10 +03002421 for (size_t uniformIndex = 0; uniformIndex < mD3DUniforms.size(); ++uniformIndex)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002422 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002423 if (mD3DUniforms[uniformIndex]->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002424 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002425 assignSamplerRegisters(uniformIndex);
Jamie Madillfb536032015-09-11 13:19:49 -04002426 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002427 }
2428}
2429
Olli Etuaho465835d2017-09-26 13:34:10 +03002430void ProgramD3D::assignSamplerRegisters(size_t uniformIndex)
Jamie Madillfb536032015-09-11 13:19:49 -04002431{
Olli Etuaho465835d2017-09-26 13:34:10 +03002432 D3DUniform *d3dUniform = mD3DUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002433 ASSERT(d3dUniform->isSampler());
Olli Etuaho465835d2017-09-26 13:34:10 +03002434 // If the uniform is an array of arrays, then we have separate entries for each inner array in
2435 // mD3DUniforms. However, the sampler register info is stored in the shader only for the
2436 // outermost array.
2437 std::vector<unsigned int> subscripts;
2438 const std::string baseName = gl::ParseResourceName(d3dUniform->name, &subscripts);
2439 unsigned int registerOffset = mState.getUniforms()[uniformIndex].flattenedOffsetInParentArrays *
2440 d3dUniform->getArraySizeProduct();
2441
Xinghua Caob1239382016-12-13 15:07:05 +08002442 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2443 if (computeShader)
Jamie Madillfb536032015-09-11 13:19:49 -04002444 {
Xinghua Caob1239382016-12-13 15:07:05 +08002445 const ShaderD3D *computeShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedComputeShader());
Olli Etuaho465835d2017-09-26 13:34:10 +03002446 ASSERT(computeShaderD3D->hasUniform(baseName));
2447 d3dUniform->csRegisterIndex =
2448 computeShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002449 ASSERT(d3dUniform->csRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002450 AssignSamplers(d3dUniform->csRegisterIndex, d3dUniform->typeInfo,
2451 d3dUniform->getArraySizeProduct(), mSamplersCS, &mUsedComputeSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002452 }
Xinghua Caob1239382016-12-13 15:07:05 +08002453 else
Jamie Madillfb536032015-09-11 13:19:49 -04002454 {
Xinghua Caob1239382016-12-13 15:07:05 +08002455 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2456 const ShaderD3D *fragmentShaderD3D =
2457 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho465835d2017-09-26 13:34:10 +03002458 ASSERT(vertexShaderD3D->hasUniform(baseName) || fragmentShaderD3D->hasUniform(baseName));
2459 if (vertexShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002460 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002461 d3dUniform->vsRegisterIndex =
2462 vertexShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002463 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002464 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->typeInfo,
2465 d3dUniform->getArraySizeProduct(), mSamplersVS,
2466 &mUsedVertexSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002467 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002468 if (fragmentShaderD3D->hasUniform(baseName))
Xinghua Caob1239382016-12-13 15:07:05 +08002469 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002470 d3dUniform->psRegisterIndex =
2471 fragmentShaderD3D->getUniformRegister(baseName) + registerOffset;
Xinghua Caob1239382016-12-13 15:07:05 +08002472 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Olli Etuaho465835d2017-09-26 13:34:10 +03002473 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->typeInfo,
2474 d3dUniform->getArraySizeProduct(), mSamplersPS, &mUsedPixelSamplerRange);
Xinghua Caob1239382016-12-13 15:07:05 +08002475 }
Jamie Madillfb536032015-09-11 13:19:49 -04002476 }
2477}
2478
Jamie Madill62d31cb2015-09-11 13:25:51 -04002479// static
2480void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madill33bb7c42017-09-09 23:32:51 -04002481 const gl::UniformTypeInfo &typeInfo,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002482 unsigned int samplerCount,
2483 std::vector<Sampler> &outSamplers,
2484 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002485{
2486 unsigned int samplerIndex = startSamplerIndex;
2487
2488 do
2489 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002490 ASSERT(samplerIndex < outSamplers.size());
2491 Sampler *sampler = &outSamplers[samplerIndex];
2492 sampler->active = true;
Jamie Madill33bb7c42017-09-09 23:32:51 -04002493 sampler->textureType = typeInfo.samplerTextureType;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002494 sampler->logicalTextureUnit = 0;
2495 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002496 samplerIndex++;
2497 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002498}
2499
Brandon Jonesc9610c52014-08-25 17:02:59 -07002500void ProgramD3D::reset()
2501{
Xinghua Caob1239382016-12-13 15:07:05 +08002502 mVertexExecutables.clear();
2503 mPixelExecutables.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002504
Xinghua Caob1239382016-12-13 15:07:05 +08002505 for (auto &geometryExecutable : mGeometryExecutables)
Jamie Madill4e31ad52015-10-29 10:32:57 -04002506 {
Xinghua Caob1239382016-12-13 15:07:05 +08002507 geometryExecutable.reset(nullptr);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002508 }
Brandon Joneseb994362014-09-24 10:27:28 -07002509
Xinghua Caob1239382016-12-13 15:07:05 +08002510 mComputeExecutable.reset(nullptr);
2511
Brandon Jones22502d52014-08-29 16:58:36 -07002512 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002513 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002514
2515 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002516 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002517 mUsesFragDepth = false;
Martin Radev41ac68e2017-06-06 12:16:58 +03002518 mHasANGLEMultiviewEnabled = false;
2519 mUsesViewID = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002520 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002521 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002522 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002523
Jamie Madill62d31cb2015-09-11 13:25:51 -04002524 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002525 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002526
Xinghua Caob1239382016-12-13 15:07:05 +08002527 mVertexUniformStorage.reset(nullptr);
2528 mFragmentUniformStorage.reset(nullptr);
2529 mComputeUniformStorage.reset(nullptr);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002530
2531 mSamplersPS.clear();
2532 mSamplersVS.clear();
Xinghua Caob1239382016-12-13 15:07:05 +08002533 mSamplersCS.clear();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002534
2535 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002536 mUsedPixelSamplerRange = 0;
Xinghua Caob1239382016-12-13 15:07:05 +08002537 mUsedComputeSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002538 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002539
Jamie Madill8047c0d2016-03-07 13:02:12 -05002540 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002541
Jamie Madill9fc36822015-11-18 13:08:07 -05002542 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002543
2544 mGeometryShaderPreamble.clear();
Jamie Madill561ed3a2017-08-31 16:48:09 -04002545
Jamie Madill4148fd72017-09-14 15:46:20 -04002546 dirtyAllUniforms();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002547
2548 mCachedPixelExecutableIndex.reset();
2549 mCachedVertexExecutableIndex.reset();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002550}
2551
Geoff Lang7dd2e102014-11-10 15:19:26 -05002552unsigned int ProgramD3D::getSerial() const
2553{
2554 return mSerial;
2555}
2556
2557unsigned int ProgramD3D::issueSerial()
2558{
2559 return mCurrentSerial++;
2560}
2561
Jamie Madillbd044ed2017-06-05 12:59:21 -04002562void ProgramD3D::initAttribLocationsToD3DSemantic(const gl::Context *context)
Jamie Madill63805b42015-08-25 13:17:39 -04002563{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002564 gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002565 ASSERT(vertexShader != nullptr);
2566
2567 // Init semantic index
Jamie Madill34ca4f52017-06-13 11:49:39 -04002568 int semanticIndex = 0;
2569 for (const sh::Attribute &attribute : vertexShader->getActiveAttributes(context))
Jamie Madill63805b42015-08-25 13:17:39 -04002570 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002571 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill34ca4f52017-06-13 11:49:39 -04002572 GLuint location = mState.getAttributeLocation(attribute.name);
2573 ASSERT(location != std::numeric_limits<GLuint>::max());
Jamie Madill63805b42015-08-25 13:17:39 -04002574
Jamie Madill8047c0d2016-03-07 13:02:12 -05002575 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002576 {
Jamie Madill34ca4f52017-06-13 11:49:39 -04002577 mAttribLocationToD3DSemantic[location + reg] = semanticIndex++;
Jamie Madill63805b42015-08-25 13:17:39 -04002578 }
2579 }
Jamie Madill437d2662014-12-05 14:23:35 -05002580}
2581
Jamie Madilla779b612017-07-24 11:46:05 -04002582void ProgramD3D::updateCachedInputLayout(Serial associatedSerial, const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002583{
Jamie Madilla779b612017-07-24 11:46:05 -04002584 if (mCurrentVertexArrayStateSerial == associatedSerial)
2585 {
2586 return;
2587 }
2588
2589 mCurrentVertexArrayStateSerial = associatedSerial;
Jamie Madillbd136f92015-08-10 14:51:37 -04002590 mCachedInputLayout.clear();
Jamie Madill0e7f1732017-09-09 23:32:50 -04002591
Jamie Madilld3dfda22015-07-06 08:28:49 -04002592 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002593
Jamie Madill6de51852017-04-12 09:53:01 -04002594 for (size_t locationIndex : mState.getActiveAttribLocationsMask())
Jamie Madilld3dfda22015-07-06 08:28:49 -04002595 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002596 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002597
Jamie Madill8047c0d2016-03-07 13:02:12 -05002598 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002599 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002600 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002601 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002602 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002603 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002604 mCachedInputLayout[d3dSemantic] =
2605 GetVertexFormatType(vertexAttributes[locationIndex],
2606 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002607 }
2608 }
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002609
2610 VertexExecutable::getSignature(mRenderer, mCachedInputLayout, &mCachedVertexSignature);
Jamie Madill0e7f1732017-09-09 23:32:50 -04002611
2612 updateCachedVertexExecutableIndex();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002613}
2614
2615void ProgramD3D::updateCachedOutputLayout(const gl::Context *context,
2616 const gl::Framebuffer *framebuffer)
2617{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002618 mPixelShaderOutputLayoutCache.clear();
2619
2620 FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(framebuffer);
2621 const auto &colorbuffers = fboD3D->getColorAttachmentsForRender(context);
2622
2623 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
2624 {
2625 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
2626
2627 if (colorbuffer)
2628 {
2629 auto binding = colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0
2630 : colorbuffer->getBinding();
2631 mPixelShaderOutputLayoutCache.push_back(binding);
2632 }
2633 else
2634 {
2635 mPixelShaderOutputLayoutCache.push_back(GL_NONE);
2636 }
2637 }
2638
2639 updateCachedPixelExecutableIndex();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002640}
2641
Jamie Madill192745a2016-12-22 15:58:21 -05002642void ProgramD3D::gatherTransformFeedbackVaryings(const gl::VaryingPacking &varyingPacking,
2643 const BuiltinInfo &builtins)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002644{
Jamie Madill9fc36822015-11-18 13:08:07 -05002645 const std::string &varyingSemantic =
2646 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2647
Jamie Madillccdf74b2015-08-18 10:46:12 -04002648 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002649 mStreamOutVaryings.clear();
2650
Jamie Madill48ef11b2016-04-27 15:21:52 -04002651 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002652 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2653 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002654 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002655 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2656 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002657 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002658 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002659 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002660 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2661 builtins.glPosition.index, 4, outputSlot));
2662 }
2663 }
2664 else if (tfVaryingName == "gl_FragCoord")
2665 {
2666 if (builtins.glFragCoord.enabled)
2667 {
2668 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2669 builtins.glFragCoord.index, 4, outputSlot));
2670 }
2671 }
2672 else if (tfVaryingName == "gl_PointSize")
2673 {
2674 if (builtins.glPointSize.enabled)
2675 {
2676 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2677 }
2678 }
2679 else
2680 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002681 std::vector<unsigned int> subscripts;
2682 std::string baseName = gl::ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002683 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002684 if (!subscripts.empty())
2685 {
2686 subscript = subscripts.back();
2687 }
Jamie Madill192745a2016-12-22 15:58:21 -05002688 for (const auto &registerInfo : varyingPacking.getRegisterList())
Jamie Madill9fc36822015-11-18 13:08:07 -05002689 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002690 const auto &varying = *registerInfo.packedVarying->varying;
2691 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002692 int componentCount = gl::VariableColumnCount(transposedType);
2693 ASSERT(!varying.isBuiltIn());
2694
Jamie Madill55c25d02015-11-18 13:08:08 -05002695 // Transform feedback for varying structs is underspecified.
2696 // See Khronos bug 9856.
2697 // TODO(jmadill): Figure out how to be spec-compliant here.
2698 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2699 continue;
2700
Jamie Madill9fc36822015-11-18 13:08:07 -05002701 // There can be more than one register assigned to a particular varying, and each
2702 // register needs its own stream out entry.
jchen10a9042d32017-03-17 08:50:45 +08002703 if (baseName == registerInfo.packedVarying->varying->name &&
2704 (subscript == GL_INVALID_INDEX || subscript == registerInfo.varyingArrayIndex))
Jamie Madill9fc36822015-11-18 13:08:07 -05002705 {
2706 mStreamOutVaryings.push_back(D3DVarying(
2707 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2708 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002709 }
2710 }
2711 }
2712}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002713
2714D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2715{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002716 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002717}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002718
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002719const D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location) const
2720{
2721 return mD3DUniforms[mState.getUniformLocations()[location].index];
2722}
2723
Sami Väisänen46eaa942016-06-29 10:26:37 +03002724void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2725 GLenum genMode,
2726 GLint components,
2727 const GLfloat *coeffs)
2728{
2729 UNREACHABLE();
2730}
2731
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002732bool ProgramD3D::hasVertexExecutableForCachedInputLayout()
2733{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002734 return mCachedVertexExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002735}
2736
2737bool ProgramD3D::hasGeometryExecutableForPrimitiveType(GLenum drawMode)
2738{
2739 if (!usesGeometryShader(drawMode))
2740 {
2741 // No shader necessary mean we have the required (null) executable.
2742 return true;
2743 }
2744
2745 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
2746 return mGeometryExecutables[geometryShaderType].get() != nullptr;
2747}
2748
2749bool ProgramD3D::hasPixelExecutableForCachedOutputLayout()
2750{
Jamie Madill0e7f1732017-09-09 23:32:50 -04002751 return mCachedPixelExecutableIndex.valid();
Jamie Madill4c19a8a2017-07-24 11:46:06 -04002752}
2753
Jamie Madill54164b02017-08-28 15:17:37 -04002754template <typename DestT>
2755void ProgramD3D::getUniformInternal(GLint location, DestT *dataOut) const
2756{
2757 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
2758 const gl::LinkedUniform &uniform = mState.getUniforms()[locationInfo.index];
2759
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002760 const D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Olli Etuaho1734e172017-10-27 15:30:27 +03002761 const uint8_t *srcPointer = targetUniform->getDataPtrToElement(locationInfo.arrayIndex);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002762
2763 if (gl::IsMatrixType(uniform.type))
2764 {
2765 GetMatrixUniform(gl::VariableColumnCount(uniform.type), gl::VariableRowCount(uniform.type),
2766 dataOut, reinterpret_cast<const DestT *>(srcPointer));
2767 }
2768 else
2769 {
2770 memcpy(dataOut, srcPointer, uniform.getElementSize());
2771 }
Jamie Madill54164b02017-08-28 15:17:37 -04002772}
2773
2774void ProgramD3D::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
2775{
2776 getUniformInternal(location, params);
2777}
2778
2779void ProgramD3D::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
2780{
2781 getUniformInternal(location, params);
2782}
2783
2784void ProgramD3D::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
2785{
2786 getUniformInternal(location, params);
2787}
2788
Jamie Madill0e7f1732017-09-09 23:32:50 -04002789void ProgramD3D::updateCachedVertexExecutableIndex()
2790{
2791 mCachedVertexExecutableIndex.reset();
2792 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
2793 {
2794 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
2795 {
2796 mCachedVertexExecutableIndex = executableIndex;
2797 break;
2798 }
2799 }
2800}
2801
2802void ProgramD3D::updateCachedPixelExecutableIndex()
2803{
2804 mCachedPixelExecutableIndex.reset();
2805 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
2806 {
2807 if (mPixelExecutables[executableIndex]->matchesSignature(mPixelShaderOutputLayoutCache))
2808 {
2809 mCachedPixelExecutableIndex = executableIndex;
2810 break;
2811 }
2812 }
2813}
2814
Jamie Madill6db1c2e2017-11-08 09:17:40 -05002815void ProgramD3D::linkResources(const gl::Context *context,
2816 const gl::ProgramLinkedResources &resources)
2817{
2818 UniformBlockInfo uniformBlockInfo;
2819
2820 if (mState.getAttachedVertexShader())
2821 {
2822 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedVertexShader());
2823 }
2824
2825 if (mState.getAttachedFragmentShader())
2826 {
2827 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedFragmentShader());
2828 }
2829
2830 if (mState.getAttachedComputeShader())
2831 {
2832 uniformBlockInfo.getShaderBlockInfo(context, mState.getAttachedComputeShader());
2833 }
2834
2835 // Gather interface block info.
2836 auto getUniformBlockSize = [&uniformBlockInfo](const std::string &name,
2837 const std::string &mappedName, size_t *sizeOut) {
2838 return uniformBlockInfo.getBlockSize(name, mappedName, sizeOut);
2839 };
2840
2841 auto getUniformBlockMemberInfo = [&uniformBlockInfo](const std::string &name,
2842 const std::string &mappedName,
2843 sh::BlockMemberInfo *infoOut) {
2844 return uniformBlockInfo.getBlockMemberInfo(name, mappedName, infoOut);
2845 };
2846
2847 resources.uniformBlockLinker.linkBlocks(getUniformBlockSize, getUniformBlockMemberInfo);
2848 initializeUniformBlocks();
2849
2850 // TODO(jiajia.qin@intel.com): Determine correct shader storage block info.
2851 auto getShaderStorageBlockSize = [](const std::string &name, const std::string &mappedName,
2852 size_t *sizeOut) {
2853 *sizeOut = 0;
2854 return true;
2855 };
2856
2857 auto getShaderStorageBlockMemberInfo =
2858 [](const std::string &name, const std::string &mappedName, sh::BlockMemberInfo *infoOut) {
2859 *infoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2860 return true;
2861 };
2862
2863 resources.shaderStorageBlockLinker.linkBlocks(getShaderStorageBlockSize,
2864 getShaderStorageBlockMemberInfo);
2865}
2866
Jamie Madill8047c0d2016-03-07 13:02:12 -05002867} // namespace rx