blob: fb224c818f8d1f0f9956a696f17915ddfd157c9c [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
Dian Xianga4928832015-09-15 10:11:17 -070011#include "common/BitSetIterator.h"
Jamie Madill437d2662014-12-05 14:23:35 -050012#include "common/utilities.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Framebuffer.h"
14#include "libANGLE/FramebufferAttachment.h"
15#include "libANGLE/Program.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040016#include "libANGLE/Uniform.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040017#include "libANGLE/VertexArray.h"
Jamie Madill6df9b372015-02-18 21:28:19 +000018#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill85a18042015-03-05 15:41:41 -050020#include "libANGLE/renderer/d3d/FramebufferD3D.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050021#include "libANGLE/renderer/d3d/RendererD3D.h"
22#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang359ef262015-01-05 14:42:29 -050023#include "libANGLE/renderer/d3d/ShaderExecutableD3D.h"
Jamie Madill437d2662014-12-05 14:23:35 -050024#include "libANGLE/renderer/d3d/VertexDataManager.h"
Jamie Madill120040e2016-12-07 14:46:16 -050025#include "libANGLE/renderer/d3d/hlsl/VaryingPacking.h"
Geoff Lang22072132014-11-20 15:15:01 -050026
Jamie Madill01074252016-11-28 15:55:51 -050027using namespace angle;
28
Brandon Jonesc9610c52014-08-25 17:02:59 -070029namespace rx
30{
31
Brandon Joneseb994362014-09-24 10:27:28 -070032namespace
33{
34
Jamie Madillf8dd7b12015-08-05 13:50:08 -040035gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader)
Brandon Joneseb994362014-09-24 10:27:28 -070036{
Jamie Madillbd136f92015-08-10 14:51:37 -040037 gl::InputLayout defaultLayout;
38 for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes())
Brandon Joneseb994362014-09-24 10:27:28 -070039 {
Brandon Joneseb994362014-09-24 10:27:28 -070040 if (shaderAttr.type != GL_NONE)
41 {
42 GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type);
43
Jamie Madilld3dfda22015-07-06 08:28:49 -040044 for (size_t rowIndex = 0;
Jamie Madill334d6152015-10-22 14:00:28 -040045 static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); ++rowIndex)
Brandon Joneseb994362014-09-24 10:27:28 -070046 {
Jamie Madilld3dfda22015-07-06 08:28:49 -040047 GLenum componentType = gl::VariableComponentType(transposedType);
Jamie Madill334d6152015-10-22 14:00:28 -040048 GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType));
Jamie Madilld3dfda22015-07-06 08:28:49 -040049 bool pureInt = (componentType != GL_FLOAT);
Jamie Madill334d6152015-10-22 14:00:28 -040050 gl::VertexFormatType defaultType =
51 gl::GetVertexFormatType(componentType, GL_FALSE, components, pureInt);
Brandon Joneseb994362014-09-24 10:27:28 -070052
Jamie Madillbd136f92015-08-10 14:51:37 -040053 defaultLayout.push_back(defaultType);
Brandon Joneseb994362014-09-24 10:27:28 -070054 }
55 }
56 }
Jamie Madillf8dd7b12015-08-05 13:50:08 -040057
58 return defaultLayout;
Brandon Joneseb994362014-09-24 10:27:28 -070059}
60
Jamie Madill334d6152015-10-22 14:00:28 -040061std::vector<GLenum> GetDefaultOutputLayoutFromShader(
62 const std::vector<PixelShaderOutputVariable> &shaderOutputVars)
Brandon Joneseb994362014-09-24 10:27:28 -070063{
Jamie Madillb4463142014-12-19 14:56:54 -050064 std::vector<GLenum> defaultPixelOutput;
Brandon Joneseb994362014-09-24 10:27:28 -070065
Jamie Madillb4463142014-12-19 14:56:54 -050066 if (!shaderOutputVars.empty())
67 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -070068 defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 +
69 static_cast<unsigned int>(shaderOutputVars[0].outputIndex));
Jamie Madillb4463142014-12-19 14:56:54 -050070 }
Brandon Joneseb994362014-09-24 10:27:28 -070071
72 return defaultPixelOutput;
73}
74
Brandon Jones1a8a7e32014-10-01 12:49:30 -070075bool IsRowMajorLayout(const sh::InterfaceBlockField &var)
76{
77 return var.isRowMajorLayout;
78}
79
80bool IsRowMajorLayout(const sh::ShaderVariable &var)
81{
82 return false;
83}
84
Jamie Madill9fc36822015-11-18 13:08:07 -050085// true if varying x has a higher priority in packing than y
86bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
87{
Jamie Madill55c25d02015-11-18 13:08:08 -050088 return gl::CompareShaderVar(*x.varying, *y.varying);
Jamie Madill9fc36822015-11-18 13:08:07 -050089}
90
Jamie Madillca03b352015-09-02 12:38:13 -040091std::vector<PackedVarying> MergeVaryings(const gl::Shader &vertexShader,
92 const gl::Shader &fragmentShader,
93 const std::vector<std::string> &tfVaryings)
Jamie Madillada9ecc2015-08-17 12:53:37 -040094{
Jamie Madillca03b352015-09-02 12:38:13 -040095 std::vector<PackedVarying> packedVaryings;
96
97 for (const sh::Varying &output : vertexShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -040098 {
Jamie Madillca03b352015-09-02 12:38:13 -040099 bool packed = false;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400100
101 // Built-in varyings obey special rules
Jamie Madillca03b352015-09-02 12:38:13 -0400102 if (output.isBuiltIn())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400103 {
104 continue;
105 }
106
Jamie Madillca03b352015-09-02 12:38:13 -0400107 for (const sh::Varying &input : fragmentShader.getVaryings())
Jamie Madillada9ecc2015-08-17 12:53:37 -0400108 {
Jamie Madillca03b352015-09-02 12:38:13 -0400109 if (output.name == input.name)
Jamie Madillada9ecc2015-08-17 12:53:37 -0400110 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500111 if (output.isStruct())
112 {
113 ASSERT(!output.isArray());
114 for (const auto &field : output.fields)
115 {
116 ASSERT(!field.isStruct() && !field.isArray());
117 packedVaryings.push_back(
118 PackedVarying(field, input.interpolation, input.name));
119 }
120 }
121 else
122 {
123 packedVaryings.push_back(PackedVarying(input, input.interpolation));
124 }
Jamie Madillca03b352015-09-02 12:38:13 -0400125 packed = true;
Jamie Madillada9ecc2015-08-17 12:53:37 -0400126 break;
127 }
128 }
129
Jamie Madillca03b352015-09-02 12:38:13 -0400130 // Keep Transform FB varyings in the merged list always.
131 if (!packed)
132 {
133 for (const std::string &tfVarying : tfVaryings)
134 {
135 if (tfVarying == output.name)
136 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500137 // Transform feedback for varying structs is underspecified.
138 // See Khronos bug 9856.
139 // TODO(jmadill): Figure out how to be spec-compliant here.
140 if (!output.isStruct())
141 {
142 packedVaryings.push_back(PackedVarying(output, output.interpolation));
143 packedVaryings.back().vertexOnly = true;
144 }
Jamie Madillca03b352015-09-02 12:38:13 -0400145 break;
146 }
147 }
148 }
Jamie Madillada9ecc2015-08-17 12:53:37 -0400149 }
150
Jamie Madill9fc36822015-11-18 13:08:07 -0500151 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
152
Jamie Madillca03b352015-09-02 12:38:13 -0400153 return packedVaryings;
Brandon Joneseb994362014-09-24 10:27:28 -0700154}
155
Jamie Madill62d31cb2015-09-11 13:25:51 -0400156template <typename VarT>
157void GetUniformBlockInfo(const std::vector<VarT> &fields,
158 const std::string &prefix,
159 sh::BlockLayoutEncoder *encoder,
160 bool inRowMajorLayout,
161 std::map<std::string, sh::BlockMemberInfo> *blockInfoOut)
162{
163 for (const VarT &field : fields)
164 {
165 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
166
167 if (field.isStruct())
168 {
169 bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field));
170
171 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
172 {
173 encoder->enterAggregateType();
174
175 const std::string uniformElementName =
176 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
177 GetUniformBlockInfo(field.fields, uniformElementName, encoder, rowMajorLayout,
178 blockInfoOut);
179
180 encoder->exitAggregateType();
181 }
182 }
183 else
184 {
185 bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout);
186 (*blockInfoOut)[fieldName] =
187 encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix);
188 }
189 }
190}
191
Jamie Madill334d6152015-10-22 14:00:28 -0400192template <typename T>
Jacek Caban2302e692015-12-01 12:44:43 +0100193static inline void SetIfDirty(T *dest, const T &source, bool *dirtyFlag)
194{
195 ASSERT(dest != NULL);
196 ASSERT(dirtyFlag != NULL);
197
198 *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0);
199 *dest = source;
200}
201
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800202template <typename T, int cols, int rows>
203bool TransposeExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400204{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800205 constexpr int targetWidth = 4;
206 constexpr int targetHeight = rows;
207 constexpr int srcWidth = rows;
208 constexpr int srcHeight = cols;
209
210 constexpr int copyWidth = std::min(targetHeight, srcWidth);
211 constexpr int copyHeight = std::min(targetWidth, srcHeight);
212
213 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400214
215 for (int x = 0; x < copyWidth; x++)
216 {
217 for (int y = 0; y < copyHeight; y++)
218 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800219 staging[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400220 }
221 }
222
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800223 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
224 {
225 return false;
226 }
227
228 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
229 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400230}
231
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800232template <typename T, int cols, int rows>
233bool ExpandMatrix(T *target, const GLfloat *value)
Jamie Madill334d6152015-10-22 14:00:28 -0400234{
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800235 constexpr int targetWidth = 4;
236 constexpr int targetHeight = rows;
237 constexpr int srcWidth = cols;
238 constexpr int srcHeight = rows;
239
240 constexpr int copyWidth = std::min(targetWidth, srcWidth);
241 constexpr int copyHeight = std::min(targetHeight, srcHeight);
242
243 T staging[targetWidth * targetHeight] = {0};
Jamie Madill334d6152015-10-22 14:00:28 -0400244
245 for (int y = 0; y < copyHeight; y++)
246 {
247 for (int x = 0; x < copyWidth; x++)
248 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800249 staging[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
Jamie Madill334d6152015-10-22 14:00:28 -0400250 }
251 }
252
Corentin Wallezb58e9ba2016-12-15 14:07:56 -0800253 if (memcmp(target, staging, targetWidth * targetHeight * sizeof(T)) == 0)
254 {
255 return false;
256 }
257
258 memcpy(target, staging, targetWidth * targetHeight * sizeof(T));
259 return true;
Jamie Madill334d6152015-10-22 14:00:28 -0400260}
261
Jamie Madill4e31ad52015-10-29 10:32:57 -0400262gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
263{
264 switch (drawMode)
265 {
266 // Uses the point sprite geometry shader.
267 case GL_POINTS:
268 return gl::PRIMITIVE_POINTS;
269
270 // All line drawing uses the same geometry shader.
271 case GL_LINES:
272 case GL_LINE_STRIP:
273 case GL_LINE_LOOP:
274 return gl::PRIMITIVE_LINES;
275
276 // The triangle fan primitive is emulated with strips in D3D11.
277 case GL_TRIANGLES:
278 case GL_TRIANGLE_FAN:
279 return gl::PRIMITIVE_TRIANGLES;
280
281 // Special case for triangle strips.
282 case GL_TRIANGLE_STRIP:
283 return gl::PRIMITIVE_TRIANGLE_STRIP;
284
285 default:
286 UNREACHABLE();
287 return gl::PRIMITIVE_TYPE_MAX;
288 }
289}
290
Jamie Madillada9ecc2015-08-17 12:53:37 -0400291} // anonymous namespace
292
Jamie Madill28afae52015-11-09 15:07:57 -0500293// D3DUniform Implementation
294
Jamie Madill62d31cb2015-09-11 13:25:51 -0400295D3DUniform::D3DUniform(GLenum typeIn,
296 const std::string &nameIn,
297 unsigned int arraySizeIn,
298 bool defaultBlock)
299 : type(typeIn),
300 name(nameIn),
301 arraySize(arraySizeIn),
302 data(nullptr),
303 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400304 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400305 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400306 registerCount(0),
307 registerElement(0)
308{
309 // We use data storage for default block uniforms to cache values that are sent to D3D during
310 // rendering
311 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
312 if (defaultBlock)
313 {
314 size_t bytes = gl::VariableInternalSize(type) * elementCount();
315 data = new uint8_t[bytes];
316 memset(data, 0, bytes);
317
318 // TODO(jmadill): is this correct with non-square matrices?
319 registerCount = gl::VariableRowCount(type) * elementCount();
320 }
321}
322
323D3DUniform::~D3DUniform()
324{
325 SafeDeleteArray(data);
326}
327
328bool D3DUniform::isSampler() const
329{
330 return gl::IsSamplerType(type);
331}
332
333bool D3DUniform::isReferencedByVertexShader() const
334{
335 return vsRegisterIndex != GL_INVALID_INDEX;
336}
337
338bool D3DUniform::isReferencedByFragmentShader() const
339{
340 return psRegisterIndex != GL_INVALID_INDEX;
341}
342
Jamie Madill28afae52015-11-09 15:07:57 -0500343// D3DVarying Implementation
344
Jamie Madill9fc36822015-11-18 13:08:07 -0500345D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500346{
347}
348
Jamie Madill9fc36822015-11-18 13:08:07 -0500349D3DVarying::D3DVarying(const std::string &semanticNameIn,
350 unsigned int semanticIndexIn,
351 unsigned int componentCountIn,
352 unsigned int outputSlotIn)
353 : semanticName(semanticNameIn),
354 semanticIndex(semanticIndexIn),
355 componentCount(componentCountIn),
356 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500357{
358}
359
Jamie Madille39a3f02015-11-17 20:42:15 -0500360// ProgramD3DMetadata Implementation
361
Jamie Madillc9bde922016-07-24 17:58:50 -0400362ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500363 const ShaderD3D *vertexShader,
364 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400365 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
366 mShaderModelSuffix(renderer->getShaderModelSuffix()),
367 mUsesInstancedPointSpriteEmulation(
368 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
369 mUsesViewScale(renderer->presentPathFastEnabled()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500370 mVertexShader(vertexShader),
371 mFragmentShader(fragmentShader)
372{
373}
374
375int ProgramD3DMetadata::getRendererMajorShaderModel() const
376{
377 return mRendererMajorShaderModel;
378}
379
Jamie Madill9082b982016-04-27 15:21:51 -0400380bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500381{
Martin Radev1be913c2016-07-11 17:59:16 +0300382 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500383}
384
Jamie Madill48ef11b2016-04-27 15:21:52 -0400385bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500386{
Jamie Madill63286672015-11-24 13:00:08 -0500387 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500388}
389
390bool ProgramD3DMetadata::usesPointCoord() const
391{
392 return mFragmentShader->usesPointCoord();
393}
394
395bool ProgramD3DMetadata::usesFragCoord() const
396{
397 return mFragmentShader->usesFragCoord();
398}
399
400bool ProgramD3DMetadata::usesPointSize() const
401{
402 return mVertexShader->usesPointSize();
403}
404
405bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
406{
Jamie Madillc9bde922016-07-24 17:58:50 -0400407 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
408 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500409}
410
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800411bool ProgramD3DMetadata::usesViewScale() const
412{
413 return mUsesViewScale;
414}
415
Jamie Madille39a3f02015-11-17 20:42:15 -0500416bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
417{
Jamie Madillc9bde922016-07-24 17:58:50 -0400418 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500419 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400420 // Even with a geometry shader, the app can render triangles or lines and reference
421 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
422 // simplicity, we always add this to the vertex shader when the fragment shader
423 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500424 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
425 usesInsertedPointCoordValue();
426}
427
428bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
429{
430 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
431 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
432 // the vertex shader in this case. This saves us 1 output vector.
433 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
434}
435
436bool ProgramD3DMetadata::usesSystemValuePointSize() const
437{
438 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
439}
440
441bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
442{
443 return mFragmentShader->usesMultipleRenderTargets();
444}
445
446GLint ProgramD3DMetadata::getMajorShaderVersion() const
447{
448 return mVertexShader->getData().getShaderVersion();
449}
450
451const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
452{
453 return mFragmentShader;
454}
455
Jamie Madill120040e2016-12-07 14:46:16 -0500456void ProgramD3DMetadata::updatePackingBuiltins(ShaderType shaderType, VaryingPacking *packing)
457{
458 const std::string &userSemantic =
459 GetVaryingSemantic(mRendererMajorShaderModel, usesSystemValuePointSize());
460
461 unsigned int reservedSemanticIndex = packing->getMaxSemanticIndex();
462
463 VaryingPacking::BuiltinInfo *builtins = &packing->builtins(shaderType);
464
465 if (mRendererMajorShaderModel >= 4)
466 {
467 builtins->dxPosition.enableSystem("SV_Position");
468 }
469 else if (shaderType == SHADER_PIXEL)
470 {
471 builtins->dxPosition.enableSystem("VPOS");
472 }
473 else
474 {
475 builtins->dxPosition.enableSystem("POSITION");
476 }
477
478 if (usesTransformFeedbackGLPosition())
479 {
480 builtins->glPosition.enable(userSemantic, reservedSemanticIndex++);
481 }
482
483 if (usesFragCoord())
484 {
485 builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++);
486 }
487
488 if (shaderType == SHADER_VERTEX ? addsPointCoordToVertexShader() : usesPointCoord())
489 {
490 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
491 // In D3D11 we manually compute gl_PointCoord in the GS.
492 if (mRendererMajorShaderModel >= 4)
493 {
494 builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++);
495 }
496 else
497 {
498 builtins->glPointCoord.enable("TEXCOORD", 0);
499 }
500 }
501
502 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
503 if (usesSystemValuePointSize() &&
504 (shaderType != SHADER_PIXEL || mRendererMajorShaderModel >= 4))
505 {
506 builtins->glPointSize.enableSystem("PSIZE");
507 }
508}
509
Jamie Madill28afae52015-11-09 15:07:57 -0500510// ProgramD3D Implementation
511
Jamie Madilld3dfda22015-07-06 08:28:49 -0400512ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
513 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500514 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400515 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700516{
Brandon Joneseb994362014-09-24 10:27:28 -0700517}
518
519ProgramD3D::VertexExecutable::~VertexExecutable()
520{
521 SafeDelete(mShaderExecutable);
522}
523
Jamie Madilld3dfda22015-07-06 08:28:49 -0400524// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500525ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
526 GLenum type)
527{
528 switch (type)
529 {
530 case GL_INT:
531 return HLSLAttribType::SIGNED_INT;
532 case GL_UNSIGNED_INT:
533 return HLSLAttribType::UNSIGNED_INT;
534 case GL_SIGNED_NORMALIZED:
535 case GL_UNSIGNED_NORMALIZED:
536 case GL_FLOAT:
537 return HLSLAttribType::FLOAT;
538 default:
539 UNREACHABLE();
540 return HLSLAttribType::FLOAT;
541 }
542}
543
544// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400545void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
546 const gl::InputLayout &inputLayout,
547 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700548{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500549 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400550
551 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700552 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400553 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500554 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
555 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400556
Jamie Madillbdec2f42016-03-02 16:35:32 -0500557 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
558 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
559 continue;
560
561 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
562 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700563 }
Brandon Joneseb994362014-09-24 10:27:28 -0700564}
565
Jamie Madilld3dfda22015-07-06 08:28:49 -0400566bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
567{
Jamie Madillbd136f92015-08-10 14:51:37 -0400568 size_t limit = std::max(mSignature.size(), signature.size());
569 for (size_t index = 0; index < limit; ++index)
570 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500571 // treat undefined indexes as FLOAT
572 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
573 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400574 if (a != b)
575 return false;
576 }
577
578 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400579}
580
581ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
582 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400583 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700584{
585}
586
587ProgramD3D::PixelExecutable::~PixelExecutable()
588{
589 SafeDelete(mShaderExecutable);
590}
591
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700592ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
593{
594}
595
Geoff Lang7dd2e102014-11-10 15:19:26 -0500596unsigned int ProgramD3D::mCurrentSerial = 1;
597
Jamie Madill48ef11b2016-04-27 15:21:52 -0400598ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
599 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700600 mRenderer(renderer),
601 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400602 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700603 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400604 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700605 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700606 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700607 mUsedVertexSamplerRange(0),
608 mUsedPixelSamplerRange(0),
609 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500610 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700611{
Brandon Joneseb994362014-09-24 10:27:28 -0700612 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700613}
614
615ProgramD3D::~ProgramD3D()
616{
617 reset();
618 SafeDelete(mDynamicHLSL);
619}
620
Brandon Jones44151a92014-09-10 11:32:25 -0700621bool ProgramD3D::usesPointSpriteEmulation() const
622{
623 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
624}
625
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400626bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700627{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400628 if (drawMode != GL_POINTS)
629 {
630 return mUsesFlatInterpolation;
631 }
632
Cooper Partine6664f02015-01-09 16:22:24 -0800633 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
634}
635
636bool ProgramD3D::usesInstancedPointSpriteEmulation() const
637{
638 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700639}
640
Jamie Madill334d6152015-10-22 14:00:28 -0400641GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
642 unsigned int samplerIndex,
643 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700644{
645 GLint logicalTextureUnit = -1;
646
647 switch (type)
648 {
Jamie Madill334d6152015-10-22 14:00:28 -0400649 case gl::SAMPLER_PIXEL:
650 ASSERT(samplerIndex < caps.maxTextureImageUnits);
651 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
652 {
653 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
654 }
655 break;
656 case gl::SAMPLER_VERTEX:
657 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
658 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
659 {
660 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
661 }
662 break;
663 default:
664 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700665 }
666
Jamie Madill334d6152015-10-22 14:00:28 -0400667 if (logicalTextureUnit >= 0 &&
668 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700669 {
670 return logicalTextureUnit;
671 }
672
673 return -1;
674}
675
676// Returns the texture type for a given Direct3D 9 sampler type and
677// index (0-15 for the pixel shader and 0-3 for the vertex shader).
678GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
679{
680 switch (type)
681 {
Jamie Madill334d6152015-10-22 14:00:28 -0400682 case gl::SAMPLER_PIXEL:
683 ASSERT(samplerIndex < mSamplersPS.size());
684 ASSERT(mSamplersPS[samplerIndex].active);
685 return mSamplersPS[samplerIndex].textureType;
686 case gl::SAMPLER_VERTEX:
687 ASSERT(samplerIndex < mSamplersVS.size());
688 ASSERT(mSamplersVS[samplerIndex].active);
689 return mSamplersVS[samplerIndex].textureType;
690 default:
691 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700692 }
693
694 return GL_TEXTURE_2D;
695}
696
Olli Etuaho618bebc2016-01-15 16:40:00 +0200697GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700698{
699 switch (type)
700 {
Jamie Madill334d6152015-10-22 14:00:28 -0400701 case gl::SAMPLER_PIXEL:
702 return mUsedPixelSamplerRange;
703 case gl::SAMPLER_VERTEX:
704 return mUsedVertexSamplerRange;
705 default:
706 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200707 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700708 }
709}
710
711void ProgramD3D::updateSamplerMapping()
712{
713 if (!mDirtySamplerMapping)
714 {
715 return;
716 }
717
718 mDirtySamplerMapping = false;
719
720 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400721 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700722 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400723 if (!d3dUniform->dirty)
724 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700725
Jamie Madill62d31cb2015-09-11 13:25:51 -0400726 if (!d3dUniform->isSampler())
727 continue;
728
729 int count = d3dUniform->elementCount();
730 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
731
732 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700733 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400734 unsigned int firstIndex = d3dUniform->psRegisterIndex;
735
736 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700737 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400738 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700739
Jamie Madill62d31cb2015-09-11 13:25:51 -0400740 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700741 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400742 ASSERT(mSamplersPS[samplerIndex].active);
743 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700744 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400745 }
746 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700747
Jamie Madill62d31cb2015-09-11 13:25:51 -0400748 if (d3dUniform->isReferencedByVertexShader())
749 {
750 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
751
752 for (int i = 0; i < count; i++)
753 {
754 unsigned int samplerIndex = firstIndex + i;
755
756 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700757 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400758 ASSERT(mSamplersVS[samplerIndex].active);
759 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700760 }
761 }
762 }
763 }
764}
765
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500766LinkResult ProgramD3D::load(const ContextImpl *contextImpl,
767 gl::InfoLog &infoLog,
768 gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700769{
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500770 // TODO(jmadill): Use Renderer from contextImpl.
771
Jamie Madill62d31cb2015-09-11 13:25:51 -0400772 reset();
773
Jamie Madill334d6152015-10-22 14:00:28 -0400774 DeviceIdentifier binaryDeviceIdentifier = {0};
775 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
776 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700777
778 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
779 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
780 {
781 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500782 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700783 }
784
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500785 int compileFlags = stream->readInt<int>();
786 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
787 {
Jamie Madillf6113162015-05-07 11:49:21 -0400788 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500789 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500790 }
791
Jamie Madill8047c0d2016-03-07 13:02:12 -0500792 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400793 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500794 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400795 }
796
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700797 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
798 for (unsigned int i = 0; i < psSamplerCount; ++i)
799 {
800 Sampler sampler;
801 stream->readBool(&sampler.active);
802 stream->readInt(&sampler.logicalTextureUnit);
803 stream->readInt(&sampler.textureType);
804 mSamplersPS.push_back(sampler);
805 }
806 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
807 for (unsigned int i = 0; i < vsSamplerCount; ++i)
808 {
809 Sampler sampler;
810 stream->readBool(&sampler.active);
811 stream->readInt(&sampler.logicalTextureUnit);
812 stream->readInt(&sampler.textureType);
813 mSamplersVS.push_back(sampler);
814 }
815
816 stream->readInt(&mUsedVertexSamplerRange);
817 stream->readInt(&mUsedPixelSamplerRange);
818
819 const unsigned int uniformCount = stream->readInt<unsigned int>();
820 if (stream->error())
821 {
Jamie Madillf6113162015-05-07 11:49:21 -0400822 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500823 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700824 }
825
Jamie Madill48ef11b2016-04-27 15:21:52 -0400826 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400827 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700828 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
829 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400830 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700831
Jamie Madill62d31cb2015-09-11 13:25:51 -0400832 D3DUniform *d3dUniform =
833 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
834 linkedUniform.isInDefaultBlock());
835 stream->readInt(&d3dUniform->psRegisterIndex);
836 stream->readInt(&d3dUniform->vsRegisterIndex);
837 stream->readInt(&d3dUniform->registerCount);
838 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700839
Jamie Madill62d31cb2015-09-11 13:25:51 -0400840 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700841 }
842
Jamie Madill4a3c2342015-10-08 12:58:45 -0400843 const unsigned int blockCount = stream->readInt<unsigned int>();
844 if (stream->error())
845 {
846 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500847 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400848 }
849
850 ASSERT(mD3DUniformBlocks.empty());
851 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
852 {
853 D3DUniformBlock uniformBlock;
854 stream->readInt(&uniformBlock.psRegisterIndex);
855 stream->readInt(&uniformBlock.vsRegisterIndex);
856 mD3DUniformBlocks.push_back(uniformBlock);
857 }
858
Jamie Madill9fc36822015-11-18 13:08:07 -0500859 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
860 mStreamOutVaryings.resize(streamOutVaryingCount);
861 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700862 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500863 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700864
Jamie Madill28afae52015-11-09 15:07:57 -0500865 stream->readString(&varying->semanticName);
866 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500867 stream->readInt(&varying->componentCount);
868 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700869 }
870
Brandon Jones22502d52014-08-29 16:58:36 -0700871 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400872 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500873 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700874 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400875 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -0500876 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -0700877 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700878 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400879 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700880
881 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
882 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400883 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
884 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700885 {
886 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
887 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
888 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
889 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
890 }
891
Jamie Madill4e31ad52015-10-29 10:32:57 -0400892 stream->readString(&mGeometryShaderPreamble);
893
Jamie Madill334d6152015-10-22 14:00:28 -0400894 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700895
Jamie Madillb0a838b2016-11-13 20:02:12 -0500896 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
897
Brandon Joneseb994362014-09-24 10:27:28 -0700898 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400899 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
900 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700901 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400902 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400903 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700904
Jamie Madilld3dfda22015-07-06 08:28:49 -0400905 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700906 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400907 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700908 }
909
Jamie Madill334d6152015-10-22 14:00:28 -0400910 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700911 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400912
Jamie Madillada9ecc2015-08-17 12:53:37 -0400913 ShaderExecutableD3D *shaderExecutable = nullptr;
914
Jamie Madillb0a838b2016-11-13 20:02:12 -0500915 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
916 mStreamOutVaryings, separateAttribs,
917 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400918
Brandon Joneseb994362014-09-24 10:27:28 -0700919 if (!shaderExecutable)
920 {
Jamie Madillf6113162015-05-07 11:49:21 -0400921 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500922 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700923 }
924
925 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400926 VertexExecutable::Signature signature;
927 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700928
929 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400930 mVertexExecutables.push_back(
931 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700932
933 stream->skip(vertexShaderSize);
934 }
935
936 const size_t pixelShaderCount = stream->readInt<unsigned int>();
937 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
938 {
939 const size_t outputCount = stream->readInt<unsigned int>();
940 std::vector<GLenum> outputs(outputCount);
941 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
942 {
943 stream->readInt(&outputs[outputIndex]);
944 }
945
Jamie Madill334d6152015-10-22 14:00:28 -0400946 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700947 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400948 ShaderExecutableD3D *shaderExecutable = nullptr;
949
Jamie Madillb0a838b2016-11-13 20:02:12 -0500950 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
951 mStreamOutVaryings, separateAttribs,
952 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700953
954 if (!shaderExecutable)
955 {
Jamie Madillf6113162015-05-07 11:49:21 -0400956 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500957 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700958 }
959
960 // add new binary
961 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
962
963 stream->skip(pixelShaderSize);
964 }
965
Jamie Madill4e31ad52015-10-29 10:32:57 -0400966 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
967 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700968 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400969 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
970 if (geometryShaderSize == 0)
971 {
972 mGeometryExecutables[geometryExeIndex] = nullptr;
973 continue;
974 }
975
Brandon Joneseb994362014-09-24 10:27:28 -0700976 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400977
Jamie Madillb0a838b2016-11-13 20:02:12 -0500978 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
979 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
980 &mGeometryExecutables[geometryExeIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -0700981
Jamie Madill4e31ad52015-10-29 10:32:57 -0400982 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700983 {
Jamie Madillf6113162015-05-07 11:49:21 -0400984 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500985 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700986 }
987 stream->skip(geometryShaderSize);
988 }
989
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700990 initializeUniformStorage();
991
Jamie Madillb0a838b2016-11-13 20:02:12 -0500992 return true;
Brandon Jones22502d52014-08-29 16:58:36 -0700993}
994
Geoff Langb543aff2014-09-30 14:52:54 -0400995gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700996{
Austin Kinross137b1512015-06-17 16:14:53 -0700997 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -0400998 // When we load the binary again later, we can validate the device identifier before trying to
999 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -07001000 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -04001001 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
1002 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -07001003
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001004 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1005
Jamie Madill8047c0d2016-03-07 13:02:12 -05001006 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -04001007 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05001008 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -04001009 }
1010
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001011 stream->writeInt(mSamplersPS.size());
1012 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1013 {
1014 stream->writeInt(mSamplersPS[i].active);
1015 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
1016 stream->writeInt(mSamplersPS[i].textureType);
1017 }
1018
1019 stream->writeInt(mSamplersVS.size());
1020 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1021 {
1022 stream->writeInt(mSamplersVS[i].active);
1023 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1024 stream->writeInt(mSamplersVS[i].textureType);
1025 }
1026
1027 stream->writeInt(mUsedVertexSamplerRange);
1028 stream->writeInt(mUsedPixelSamplerRange);
1029
Jamie Madill62d31cb2015-09-11 13:25:51 -04001030 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001031 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001032 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001033 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001034 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1035 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001036 stream->writeInt(uniform->registerCount);
1037 stream->writeInt(uniform->registerElement);
1038 }
1039
Jamie Madill51f522f2016-12-21 15:10:55 -05001040 // Ensure we init the uniform block structure data if we should.
1041 // http://anglebug.com/1637
1042 ensureUniformBlocksInitialized();
1043
Jamie Madill4a3c2342015-10-08 12:58:45 -04001044 stream->writeInt(mD3DUniformBlocks.size());
1045 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1046 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001047 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1048 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001049 }
1050
Jamie Madill9fc36822015-11-18 13:08:07 -05001051 stream->writeInt(mStreamOutVaryings.size());
1052 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001053 {
Brandon Joneseb994362014-09-24 10:27:28 -07001054 stream->writeString(varying.semanticName);
1055 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001056 stream->writeInt(varying.componentCount);
1057 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001058 }
1059
Brandon Jones22502d52014-08-29 16:58:36 -07001060 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001061 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001062 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001063 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001064 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
Jamie Madill408293f2016-12-20 10:11:45 -05001065 sizeof(angle::CompilerWorkaroundsD3D));
Brandon Jones22502d52014-08-29 16:58:36 -07001066 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001067 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001068 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001069
Brandon Joneseb994362014-09-24 10:27:28 -07001070 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001071 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001072 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1073 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001074 {
Brandon Joneseb994362014-09-24 10:27:28 -07001075 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001076 stream->writeInt(variable.type);
1077 stream->writeString(variable.name);
1078 stream->writeString(variable.source);
1079 stream->writeInt(variable.outputIndex);
1080 }
1081
Jamie Madill4e31ad52015-10-29 10:32:57 -04001082 stream->writeString(mGeometryShaderPreamble);
1083
Brandon Joneseb994362014-09-24 10:27:28 -07001084 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001085 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1086 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001087 {
1088 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1089
Jamie Madilld3dfda22015-07-06 08:28:49 -04001090 const auto &inputLayout = vertexExecutable->inputs();
1091 stream->writeInt(inputLayout.size());
1092
1093 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001094 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001095 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001096 }
1097
1098 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1099 stream->writeInt(vertexShaderSize);
1100
1101 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1102 stream->writeBytes(vertexBlob, vertexShaderSize);
1103 }
1104
1105 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001106 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1107 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001108 {
1109 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1110
1111 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1112 stream->writeInt(outputs.size());
1113 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1114 {
1115 stream->writeInt(outputs[outputIndex]);
1116 }
1117
1118 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1119 stream->writeInt(pixelShaderSize);
1120
1121 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1122 stream->writeBytes(pixelBlob, pixelShaderSize);
1123 }
1124
Jamie Madill4e31ad52015-10-29 10:32:57 -04001125 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001126 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001127 if (geometryExe == nullptr)
1128 {
1129 stream->writeInt(0);
1130 continue;
1131 }
1132
1133 size_t geometryShaderSize = geometryExe->getLength();
1134 stream->writeInt(geometryShaderSize);
1135 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001136 }
1137
Jamie Madill51f522f2016-12-21 15:10:55 -05001138 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001139}
1140
Geoff Langc5629752015-12-07 16:29:04 -05001141void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1142{
1143}
1144
Jamie Madill334d6152015-10-22 14:00:28 -04001145gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1146 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001147{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001148 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001149
Jamie Madill85a18042015-03-05 15:41:41 -05001150 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001151 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001152
1153 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1154 {
1155 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1156
1157 if (colorbuffer)
1158 {
Jamie Madill334d6152015-10-22 14:00:28 -04001159 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1160 ? GL_COLOR_ATTACHMENT0
1161 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001162 }
1163 else
1164 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001165 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001166 }
1167 }
1168
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001169 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001170}
1171
Jamie Madill97399232014-12-23 12:31:15 -05001172gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001173 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001174 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001175{
1176 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1177 {
1178 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1179 {
Geoff Langb543aff2014-09-30 14:52:54 -04001180 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001181 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001182 }
1183 }
1184
Jamie Madill334d6152015-10-22 14:00:28 -04001185 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1186 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001187
1188 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001189 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001190
1191 gl::InfoLog tempInfoLog;
1192 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1193
Jamie Madill01074252016-11-28 15:55:51 -05001194 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001195 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001196 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001197 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001198
Jamie Madill97399232014-12-23 12:31:15 -05001199 if (pixelExecutable)
1200 {
1201 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1202 }
1203 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001204 {
1205 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001206 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001207 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1208 }
Brandon Jones22502d52014-08-29 16:58:36 -07001209
Geoff Langb543aff2014-09-30 14:52:54 -04001210 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001211 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001212}
1213
Jamie Madilld3dfda22015-07-06 08:28:49 -04001214gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001215 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001216 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001217{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001218 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001219
1220 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1221 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001222 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001223 {
Geoff Langb543aff2014-09-30 14:52:54 -04001224 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
Jamie Madill51f522f2016-12-21 15:10:55 -05001225 return gl::NoError();
Brandon Joneseb994362014-09-24 10:27:28 -07001226 }
1227 }
1228
Brandon Jones22502d52014-08-29 16:58:36 -07001229 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001230 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001231 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001232
1233 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001234 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001235
1236 gl::InfoLog tempInfoLog;
1237 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1238
Jamie Madill01074252016-11-28 15:55:51 -05001239 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001240 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001241 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001242 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001243
Jamie Madill97399232014-12-23 12:31:15 -05001244 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001245 {
Jamie Madill334d6152015-10-22 14:00:28 -04001246 mVertexExecutables.push_back(
1247 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001248 }
Jamie Madill97399232014-12-23 12:31:15 -05001249 else if (!infoLog)
1250 {
1251 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001252 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001253 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1254 }
Brandon Jones22502d52014-08-29 16:58:36 -07001255
Geoff Langb543aff2014-09-30 14:52:54 -04001256 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001257 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001258}
1259
Jamie Madill9082b982016-04-27 15:21:51 -04001260gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001261 GLenum drawMode,
1262 ShaderExecutableD3D **outExecutable,
1263 gl::InfoLog *infoLog)
1264{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001265 if (outExecutable)
1266 {
1267 *outExecutable = nullptr;
1268 }
1269
Austin Kinross88829e82016-01-12 13:04:41 -08001270 // Return a null shader if the current rendering doesn't use a geometry shader
1271 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001272 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001273 return gl::NoError();
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001274 }
1275
Jamie Madill4e31ad52015-10-29 10:32:57 -04001276 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1277
1278 if (mGeometryExecutables[geometryShaderType] != nullptr)
1279 {
1280 if (outExecutable)
1281 {
1282 *outExecutable = mGeometryExecutables[geometryShaderType];
1283 }
Jamie Madill51f522f2016-12-21 15:10:55 -05001284 return gl::NoError();
Jamie Madill4e31ad52015-10-29 10:32:57 -04001285 }
1286
1287 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001288 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001289 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001290
1291 gl::InfoLog tempInfoLog;
1292 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1293
1294 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001295 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill408293f2016-12-20 10:11:45 -05001296 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS),
1297 angle::CompilerWorkaroundsD3D(), &mGeometryExecutables[geometryShaderType]);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001298
1299 if (!infoLog && error.isError())
1300 {
1301 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1302 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1303 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1304 }
1305
1306 if (outExecutable)
1307 {
1308 *outExecutable = mGeometryExecutables[geometryShaderType];
1309 }
1310 return error;
1311}
1312
Jamie Madill01074252016-11-28 15:55:51 -05001313class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001314{
Jamie Madill01074252016-11-28 15:55:51 -05001315 public:
1316 GetExecutableTask(ProgramD3D *program)
1317 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001318 {
Brandon Joneseb994362014-09-24 10:27:28 -07001319 }
1320
Jamie Madill01074252016-11-28 15:55:51 -05001321 virtual gl::Error run() = 0;
1322
1323 void operator()() override { mError = run(); }
1324
1325 const gl::Error &getError() const { return mError; }
1326 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1327 ShaderExecutableD3D *getResult() { return mResult; }
1328
1329 protected:
1330 ProgramD3D *mProgram;
1331 gl::Error mError;
1332 gl::InfoLog mInfoLog;
1333 ShaderExecutableD3D *mResult;
1334};
1335
1336class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1337{
1338 public:
1339 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1340 gl::Error run() override
1341 {
1342 const auto &defaultInputLayout =
1343 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1344
1345 ANGLE_TRY(
1346 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1347
1348 return gl::NoError();
1349 }
1350};
1351
1352class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1353{
1354 public:
1355 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1356 gl::Error run() override
1357 {
1358 const auto &defaultPixelOutput =
1359 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1360
1361 ANGLE_TRY(
1362 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1363
1364 return gl::NoError();
1365 }
1366};
1367
1368class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1369{
1370 public:
1371 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1372 : GetExecutableTask(program), mContextState(contextState)
1373 {
1374 }
1375
1376 gl::Error run() override
1377 {
1378 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1379 // D3D11.
1380 if (mProgram->usesGeometryShader(GL_POINTS))
1381 {
1382 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1383 &mResult, &mInfoLog));
1384 }
1385
1386 return gl::NoError();
1387 }
1388
1389 private:
1390 const gl::ContextState &mContextState;
1391};
1392
1393LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1394 gl::InfoLog &infoLog)
1395{
1396 // Ensure the compiler is initialized to avoid race conditions.
1397 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1398
1399 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1400
1401 GetVertexExecutableTask vertexTask(this);
1402 GetPixelExecutableTask pixelTask(this);
1403 GetGeometryExecutableTask geometryTask(this, contextState);
1404
1405 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1406 workerPool->postWorkerTask(&pixelTask),
1407 workerPool->postWorkerTask(&geometryTask)}};
1408
1409 WaitableEvent::WaitMany(&waitEvents);
1410
1411 infoLog << vertexTask.getInfoLog().str();
1412 infoLog << pixelTask.getInfoLog().str();
1413 infoLog << geometryTask.getInfoLog().str();
1414
1415 ANGLE_TRY(vertexTask.getError());
1416 ANGLE_TRY(pixelTask.getError());
1417 ANGLE_TRY(geometryTask.getError());
1418
1419 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1420 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1421 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1422
Jamie Madill48ef11b2016-04-27 15:21:52 -04001423 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001424
1425 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001426 {
Jamie Madill334d6152015-10-22 14:00:28 -04001427 // Geometry shaders are currently only used internally, so there is no corresponding shader
1428 // object at the interface level. For now the geometry shader debug info is prepended to
1429 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001430 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001431 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001432 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1433 }
1434
1435 if (defaultVertexExecutable)
1436 {
1437 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1438 }
1439
1440 if (defaultPixelExecutable)
1441 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001442 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001443 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001444 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1445 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001446
Jamie Madillb0a838b2016-11-13 20:02:12 -05001447 return (defaultVertexExecutable && defaultPixelExecutable &&
1448 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001449}
1450
Jamie Madill9082b982016-04-27 15:21:51 -04001451LinkResult ProgramD3D::link(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001452{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001453 reset();
1454
Jamie Madill48ef11b2016-04-27 15:21:52 -04001455 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1456 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001457
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001458 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1459 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1460
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001461 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1462 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001463
Arun Patole44efa0b2015-03-04 17:11:05 +05301464 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001465 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1466
Jamie Madill53ea9cc2016-05-17 10:12:52 -04001467 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
Austin Kinross02df7962015-07-01 10:03:42 -07001468 {
1469 if (fragmentShaderD3D->usesFrontFacing())
1470 {
1471 infoLog << "The current renderer doesn't support gl_FrontFacing";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001472 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001473 }
1474 }
1475
Jamie Madillca03b352015-09-02 12:38:13 -04001476 std::vector<PackedVarying> packedVaryings =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001477 MergeVaryings(*vertexShader, *fragmentShader, mState.getTransformFeedbackVaryingNames());
Jamie Madillca03b352015-09-02 12:38:13 -04001478
Brandon Jones22502d52014-08-29 16:58:36 -07001479 // Map the varyings to the register file
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001480 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors);
Jamie Madill2ad1c492016-12-07 14:46:18 -05001481 if (!varyingPacking.packUserVaryings(infoLog, packedVaryings,
1482 mState.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001483 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001484 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001485 }
1486
Jamie Madillc9bde922016-07-24 17:58:50 -04001487 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madille39a3f02015-11-17 20:42:15 -05001488
Jamie Madill120040e2016-12-07 14:46:16 -05001489 metadata.updatePackingBuiltins(SHADER_VERTEX, &varyingPacking);
1490 metadata.updatePackingBuiltins(SHADER_PIXEL, &varyingPacking);
Jamie Madill9fc36822015-11-18 13:08:07 -05001491
Jamie Madill2ad1c492016-12-07 14:46:18 -05001492 if (!varyingPacking.validateBuiltins())
Jamie Madill9fc36822015-11-18 13:08:07 -05001493 {
1494 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001495 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001496 }
1497
1498 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1499 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1500 // intelligently, but D3D9 assumes one semantic per register.
1501 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001502 varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001503 {
1504 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001505 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001506 }
1507
Jamie Madill48ef11b2016-04-27 15:21:52 -04001508 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, varyingPacking, &mPixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -05001509 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001510 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001511 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001512 }
1513
Brandon Jones44151a92014-09-10 11:32:25 -07001514 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001515 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1516 mUsesFragDepth = metadata.usesFragDepth();
Brandon Jones44151a92014-09-10 11:32:25 -07001517
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001518 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001519 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001520 for (const auto &varying : packedVaryings)
1521 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001522 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001523 {
1524 mUsesFlatInterpolation = true;
1525 break;
1526 }
1527 }
1528
Jamie Madill4e31ad52015-10-29 10:32:57 -04001529 if (mRenderer->getMajorShaderModel() >= 4)
1530 {
Jamie Madill120040e2016-12-07 14:46:16 -05001531 metadata.updatePackingBuiltins(SHADER_GEOMETRY, &varyingPacking);
Jamie Madill9fc36822015-11-18 13:08:07 -05001532 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001533 }
1534
Jamie Madill8047c0d2016-03-07 13:02:12 -05001535 initAttribLocationsToD3DSemantic();
Jamie Madill437d2662014-12-05 14:23:35 -05001536
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001537 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001538
Jamie Madill9fc36822015-11-18 13:08:07 -05001539 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001540
Jamie Madill4e31ad52015-10-29 10:32:57 -04001541 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001542 if (result.isError())
Geoff Lang65d17e52016-09-08 09:52:58 -04001543 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001544 infoLog << result.getError().getMessage();
Geoff Lang65d17e52016-09-08 09:52:58 -04001545 return result;
1546 }
Jamie Madillb0a838b2016-11-13 20:02:12 -05001547 else if (!result.getResult())
Jamie Madill31c8c562015-08-19 14:08:03 -04001548 {
1549 infoLog << "Failed to create D3D shaders.";
1550 return result;
1551 }
1552
Jamie Madill4a3c2342015-10-08 12:58:45 -04001553 initUniformBlockInfo();
1554
Jamie Madillb0a838b2016-11-13 20:02:12 -05001555 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001556}
1557
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001558GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001559{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001560 // TODO(jmadill): Do something useful here?
1561 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001562}
1563
Jamie Madill4a3c2342015-10-08 12:58:45 -04001564void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001565{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001566 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001567
Jamie Madill62d31cb2015-09-11 13:25:51 -04001568 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1569 {
1570 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1571 continue;
1572
Jamie Madill4a3c2342015-10-08 12:58:45 -04001573 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001574 continue;
1575
Jamie Madill4a3c2342015-10-08 12:58:45 -04001576 size_t dataSize = getUniformBlockInfo(vertexBlock);
1577 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001578 }
1579
Jamie Madill48ef11b2016-04-27 15:21:52 -04001580 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001581
1582 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1583 {
1584 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1585 continue;
1586
Jamie Madill4a3c2342015-10-08 12:58:45 -04001587 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001588 continue;
1589
Jamie Madill4a3c2342015-10-08 12:58:45 -04001590 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1591 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001592 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001593}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001594
Jamie Madill51f522f2016-12-21 15:10:55 -05001595void ProgramD3D::ensureUniformBlocksInitialized()
Jamie Madill4a3c2342015-10-08 12:58:45 -04001596{
Jamie Madill51f522f2016-12-21 15:10:55 -05001597 // Lazy init.
1598 if (mState.getUniformBlocks().empty() || !mD3DUniformBlocks.empty())
1599 {
1600 return;
1601 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001602
1603 // Assign registers and update sizes.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001604 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1605 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001606
Jamie Madill48ef11b2016-04-27 15:21:52 -04001607 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001608 {
1609 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1610
Jamie Madill4a3c2342015-10-08 12:58:45 -04001611 D3DUniformBlock d3dUniformBlock;
1612
Jamie Madill62d31cb2015-09-11 13:25:51 -04001613 if (uniformBlock.vertexStaticUse)
1614 {
1615 unsigned int baseRegister =
1616 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001617 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001618 }
1619
1620 if (uniformBlock.fragmentStaticUse)
1621 {
1622 unsigned int baseRegister =
1623 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001624 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001625 }
1626
Jamie Madill4a3c2342015-10-08 12:58:45 -04001627 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001628 }
1629}
1630
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001631void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001632{
1633 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001634 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001635 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001636 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001637 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001638 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001639 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001640 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001641 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001642 vertexRegisters = std::max(vertexRegisters,
1643 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001644 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001645 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001646 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001647 fragmentRegisters = std::max(
1648 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001649 }
1650 }
1651 }
1652
Jamie Madill334d6152015-10-22 14:00:28 -04001653 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001654 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1655}
1656
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001657gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001658{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001659 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001660
Jamie Madill01074252016-11-28 15:55:51 -05001661 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001662
Jamie Madill62d31cb2015-09-11 13:25:51 -04001663 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001664 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001665 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001666 }
1667
Jamie Madill51f522f2016-12-21 15:10:55 -05001668 return gl::NoError();
Brandon Jones18bd4102014-09-22 14:21:44 -07001669}
1670
Jamie Madill9082b982016-04-27 15:21:51 -04001671gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001672{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001673 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001674 {
Jamie Madill51f522f2016-12-21 15:10:55 -05001675 return gl::NoError();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001676 }
1677
Jamie Madill51f522f2016-12-21 15:10:55 -05001678 ensureUniformBlocksInitialized();
Jamie Madill4a3c2342015-10-08 12:58:45 -04001679
Jamie Madill03260fa2015-06-22 13:57:22 -04001680 mVertexUBOCache.clear();
1681 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001682
1683 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1684 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1685
Jamie Madill4a3c2342015-10-08 12:58:45 -04001686 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001687 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001688 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001689 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001690 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001691
Brandon Jones18bd4102014-09-22 14:21:44 -07001692 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001693 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001694 {
1695 continue;
1696 }
1697
Jamie Madill4a3c2342015-10-08 12:58:45 -04001698 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001699 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001700 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001701 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001702
Jamie Madill969194d2015-07-20 14:36:56 -04001703 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001704 {
1705 mVertexUBOCache.resize(registerIndex + 1, -1);
1706 }
1707
1708 ASSERT(mVertexUBOCache[registerIndex] == -1);
1709 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001710 }
1711
Jamie Madill4a3c2342015-10-08 12:58:45 -04001712 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001713 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001714 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001715 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001716
1717 if (mFragmentUBOCache.size() <= registerIndex)
1718 {
1719 mFragmentUBOCache.resize(registerIndex + 1, -1);
1720 }
1721
1722 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1723 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001724 }
1725 }
1726
Jamie Madill03260fa2015-06-22 13:57:22 -04001727 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001728}
1729
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001730void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001731{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001732 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001733 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001734 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001735 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001736}
1737
Jamie Madill334d6152015-10-22 14:00:28 -04001738void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001739{
1740 setUniform(location, count, v, GL_FLOAT);
1741}
1742
1743void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1744{
1745 setUniform(location, count, v, GL_FLOAT_VEC2);
1746}
1747
1748void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1749{
1750 setUniform(location, count, v, GL_FLOAT_VEC3);
1751}
1752
1753void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1754{
1755 setUniform(location, count, v, GL_FLOAT_VEC4);
1756}
1757
Jamie Madill334d6152015-10-22 14:00:28 -04001758void ProgramD3D::setUniformMatrix2fv(GLint location,
1759 GLsizei count,
1760 GLboolean transpose,
1761 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001762{
1763 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1764}
1765
Jamie Madill334d6152015-10-22 14:00:28 -04001766void ProgramD3D::setUniformMatrix3fv(GLint location,
1767 GLsizei count,
1768 GLboolean transpose,
1769 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001770{
1771 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1772}
1773
Jamie Madill334d6152015-10-22 14:00:28 -04001774void ProgramD3D::setUniformMatrix4fv(GLint location,
1775 GLsizei count,
1776 GLboolean transpose,
1777 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001778{
1779 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1780}
1781
Jamie Madill334d6152015-10-22 14:00:28 -04001782void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1783 GLsizei count,
1784 GLboolean transpose,
1785 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001786{
1787 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1788}
1789
Jamie Madill334d6152015-10-22 14:00:28 -04001790void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1791 GLsizei count,
1792 GLboolean transpose,
1793 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001794{
1795 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1796}
1797
Jamie Madill334d6152015-10-22 14:00:28 -04001798void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1799 GLsizei count,
1800 GLboolean transpose,
1801 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001802{
1803 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1804}
1805
Jamie Madill334d6152015-10-22 14:00:28 -04001806void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1807 GLsizei count,
1808 GLboolean transpose,
1809 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001810{
1811 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1812}
1813
Jamie Madill334d6152015-10-22 14:00:28 -04001814void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1815 GLsizei count,
1816 GLboolean transpose,
1817 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001818{
1819 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1820}
1821
Jamie Madill334d6152015-10-22 14:00:28 -04001822void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1823 GLsizei count,
1824 GLboolean transpose,
1825 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001826{
1827 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1828}
1829
1830void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1831{
1832 setUniform(location, count, v, GL_INT);
1833}
1834
1835void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1836{
1837 setUniform(location, count, v, GL_INT_VEC2);
1838}
1839
1840void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1841{
1842 setUniform(location, count, v, GL_INT_VEC3);
1843}
1844
1845void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1846{
1847 setUniform(location, count, v, GL_INT_VEC4);
1848}
1849
1850void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1851{
1852 setUniform(location, count, v, GL_UNSIGNED_INT);
1853}
1854
1855void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1856{
1857 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1858}
1859
1860void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1861{
1862 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1863}
1864
1865void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1866{
1867 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1868}
1869
Jamie Madill4a3c2342015-10-08 12:58:45 -04001870void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1871 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001872{
1873}
1874
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001875void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001876{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001877 D3DUniformMap uniformMap;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001878 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001879 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001880
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001881 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001882 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001883 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001884 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001885 }
1886 }
1887
Jamie Madill48ef11b2016-04-27 15:21:52 -04001888 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001889 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001890 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001891 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001892 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001893 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001894 }
1895 }
1896
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001897 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001898 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001899 {
1900 if (!glUniform.isInDefaultBlock())
1901 continue;
1902
1903 auto mapEntry = uniformMap.find(glUniform.name);
1904 ASSERT(mapEntry != uniformMap.end());
1905 mD3DUniforms.push_back(mapEntry->second);
1906 }
1907
Jamie Madill62d31cb2015-09-11 13:25:51 -04001908 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001909 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001910}
1911
Jamie Madill91445bc2015-09-23 16:47:53 -04001912void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001913 const sh::Uniform &uniform,
1914 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001915{
Olli Etuaho96963162016-03-21 11:54:33 +02001916 // Samplers get their registers assigned in assignAllSamplerRegisters.
1917 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001918 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001919 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001920 return;
1921 }
1922
Jamie Madill91445bc2015-09-23 16:47:53 -04001923 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1924
1925 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1926 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001927 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001928 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001929
Jamie Madill91445bc2015-09-23 16:47:53 -04001930 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001931}
1932
Jamie Madill62d31cb2015-09-11 13:25:51 -04001933D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1934{
1935 for (D3DUniform *d3dUniform : mD3DUniforms)
1936 {
1937 if (d3dUniform->name == name)
1938 {
1939 return d3dUniform;
1940 }
1941 }
1942
1943 return nullptr;
1944}
1945
Jamie Madill91445bc2015-09-23 16:47:53 -04001946void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001947 const sh::ShaderVariable &uniform,
1948 const std::string &fullName,
1949 sh::HLSLBlockEncoder *encoder,
1950 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001951{
1952 if (uniform.isStruct())
1953 {
1954 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1955 {
1956 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1957
Jamie Madill55def582015-05-04 11:24:57 -04001958 if (encoder)
1959 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001960
1961 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1962 {
Jamie Madill334d6152015-10-22 14:00:28 -04001963 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001964 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1965
Olli Etuaho96963162016-03-21 11:54:33 +02001966 // Samplers get their registers assigned in assignAllSamplerRegisters.
1967 // Also they couldn't use the same encoder as the rest of the struct, since they are
1968 // extracted out of the struct by the shader translator.
1969 if (gl::IsSamplerType(field.type))
1970 {
1971 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
1972 }
1973 else
1974 {
1975 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
1976 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001977 }
1978
Jamie Madill55def582015-05-04 11:24:57 -04001979 if (encoder)
1980 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001981 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001982 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001983 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001984
1985 // Not a struct. Arrays are treated as aggregate types.
1986 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001987 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001988 encoder->enterAggregateType();
1989 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001990
Jamie Madill62d31cb2015-09-11 13:25:51 -04001991 // Advance the uniform offset, to track registers allocation for structs
1992 sh::BlockMemberInfo blockInfo =
1993 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
1994 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001995
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001996 auto uniformMapEntry = uniformMap->find(fullName);
1997 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05001998
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001999 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002000 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002001 d3dUniform = uniformMapEntry->second;
2002 }
2003 else
2004 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002005 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002006 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002007 }
2008
2009 if (encoder)
2010 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002011 d3dUniform->registerElement =
2012 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002013 unsigned int reg =
2014 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002015 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002016 {
2017 d3dUniform->psRegisterIndex = reg;
2018 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002019 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002020 {
Jamie Madill91445bc2015-09-23 16:47:53 -04002021 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002022 d3dUniform->vsRegisterIndex = reg;
2023 }
Jamie Madillfb536032015-09-11 13:19:49 -04002024
2025 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002026 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002027 {
2028 encoder->exitAggregateType();
2029 }
2030 }
2031}
2032
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002033template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002034void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002035{
Jamie Madill334d6152015-10-22 14:00:28 -04002036 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002037 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2038
Jamie Madill62d31cb2015-09-11 13:25:51 -04002039 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002040
Jamie Madill62d31cb2015-09-11 13:25:51 -04002041 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002042 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002043 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002044
2045 if (targetUniform->type == targetUniformType)
2046 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002047 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002048
Jamie Madill62d31cb2015-09-11 13:25:51 -04002049 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002050 {
Jamie Madill334d6152015-10-22 14:00:28 -04002051 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002052 const T *source = v + (i * components);
2053
2054 for (int c = 0; c < components; c++)
2055 {
2056 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2057 }
2058 for (int c = components; c < 4; c++)
2059 {
2060 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2061 }
2062 }
2063 }
2064 else if (targetUniform->type == targetBoolType)
2065 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002066 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002067
Jamie Madill62d31cb2015-09-11 13:25:51 -04002068 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002069 {
Jamie Madill334d6152015-10-22 14:00:28 -04002070 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002071 const T *source = v + (i * components);
2072
2073 for (int c = 0; c < components; c++)
2074 {
Jamie Madill334d6152015-10-22 14:00:28 -04002075 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2076 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002077 }
2078 for (int c = components; c < 4; c++)
2079 {
2080 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2081 }
2082 }
2083 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002084 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002085 {
2086 ASSERT(targetUniformType == GL_INT);
2087
Jamie Madill62d31cb2015-09-11 13:25:51 -04002088 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002089
2090 bool wasDirty = targetUniform->dirty;
2091
Jamie Madill62d31cb2015-09-11 13:25:51 -04002092 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002093 {
Jamie Madill334d6152015-10-22 14:00:28 -04002094 GLint *dest = target + (i * 4);
2095 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002096
2097 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2098 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2099 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2100 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2101 }
2102
2103 if (!wasDirty && targetUniform->dirty)
2104 {
2105 mDirtySamplerMapping = true;
2106 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002107 }
Jamie Madill334d6152015-10-22 14:00:28 -04002108 else
2109 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002110}
2111
2112template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002113void ProgramD3D::setUniformMatrixfv(GLint location,
2114 GLsizei countIn,
2115 GLboolean transpose,
2116 const GLfloat *value,
2117 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002118{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002119 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002120
Jamie Madill62d31cb2015-09-11 13:25:51 -04002121 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002122 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002123 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002124
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002125 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002126 GLfloat *target =
2127 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002128
Jamie Madill62d31cb2015-09-11 13:25:51 -04002129 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002130 {
2131 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2132 if (transpose == GL_FALSE)
2133 {
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002134 targetUniform->dirty =
2135 TransposeExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002136 }
2137 else
2138 {
Jamie Madill334d6152015-10-22 14:00:28 -04002139 targetUniform->dirty =
Corentin Wallezb58e9ba2016-12-15 14:07:56 -08002140 ExpandMatrix<GLfloat, cols, rows>(target, value) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002141 }
2142 target += targetMatrixStride;
2143 value += cols * rows;
2144 }
2145}
2146
Jamie Madill4a3c2342015-10-08 12:58:45 -04002147size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002148{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002149 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002150
Jamie Madill62d31cb2015-09-11 13:25:51 -04002151 // define member uniforms
2152 sh::Std140BlockEncoder std140Encoder;
2153 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2154 sh::BlockLayoutEncoder *encoder = nullptr;
2155
2156 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002157 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002158 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002159 }
2160 else
2161 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002162 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002163 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002164
Jamie Madill39046162016-02-08 15:05:17 -05002165 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2166 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002167
2168 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002169}
2170
Jamie Madill62d31cb2015-09-11 13:25:51 -04002171void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002172{
Olli Etuaho96963162016-03-21 11:54:33 +02002173 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002174 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002175 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002176 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002177 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002178 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002179 }
2180}
2181
Olli Etuaho96963162016-03-21 11:54:33 +02002182void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002183{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002184 ASSERT(d3dUniform->isSampler());
Jamie Madill48ef11b2016-04-27 15:21:52 -04002185 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2186 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho96963162016-03-21 11:54:33 +02002187 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) || fragmentShaderD3D->hasUniform(d3dUniform));
2188 if (vertexShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002189 {
Olli Etuaho96963162016-03-21 11:54:33 +02002190 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2191 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002192 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2193 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002194 }
Olli Etuaho96963162016-03-21 11:54:33 +02002195 if (fragmentShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002196 {
Olli Etuaho96963162016-03-21 11:54:33 +02002197 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2198 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002199 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2200 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002201 }
2202}
2203
Jamie Madill62d31cb2015-09-11 13:25:51 -04002204// static
2205void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002206 GLenum samplerType,
2207 unsigned int samplerCount,
2208 std::vector<Sampler> &outSamplers,
2209 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002210{
2211 unsigned int samplerIndex = startSamplerIndex;
2212
2213 do
2214 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002215 ASSERT(samplerIndex < outSamplers.size());
2216 Sampler *sampler = &outSamplers[samplerIndex];
2217 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002218 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002219 sampler->logicalTextureUnit = 0;
2220 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002221 samplerIndex++;
2222 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002223}
2224
Brandon Jonesc9610c52014-08-25 17:02:59 -07002225void ProgramD3D::reset()
2226{
Brandon Joneseb994362014-09-24 10:27:28 -07002227 SafeDeleteContainer(mVertexExecutables);
2228 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002229
2230 for (auto &element : mGeometryExecutables)
2231 {
2232 SafeDelete(element);
2233 }
Brandon Joneseb994362014-09-24 10:27:28 -07002234
Brandon Jones22502d52014-08-29 16:58:36 -07002235 mVertexHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002236 mVertexWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002237
2238 mPixelHLSL.clear();
Jamie Madill408293f2016-12-20 10:11:45 -05002239 mPixelWorkarounds = angle::CompilerWorkaroundsD3D();
Brandon Jones22502d52014-08-29 16:58:36 -07002240 mUsesFragDepth = false;
2241 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002242 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002243 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002244
Jamie Madill62d31cb2015-09-11 13:25:51 -04002245 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002246 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002247
Brandon Jonesc9610c52014-08-25 17:02:59 -07002248 SafeDelete(mVertexUniformStorage);
2249 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002250
2251 mSamplersPS.clear();
2252 mSamplersVS.clear();
2253
2254 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002255 mUsedPixelSamplerRange = 0;
2256 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002257
Jamie Madill8047c0d2016-03-07 13:02:12 -05002258 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002259
Jamie Madill9fc36822015-11-18 13:08:07 -05002260 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002261
2262 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002263}
2264
Geoff Lang7dd2e102014-11-10 15:19:26 -05002265unsigned int ProgramD3D::getSerial() const
2266{
2267 return mSerial;
2268}
2269
2270unsigned int ProgramD3D::issueSerial()
2271{
2272 return mCurrentSerial++;
2273}
2274
Jamie Madill8047c0d2016-03-07 13:02:12 -05002275void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002276{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002277 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002278 ASSERT(vertexShader != nullptr);
2279
2280 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002281 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002282 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002283 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2284 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002285
Jamie Madill8047c0d2016-03-07 13:02:12 -05002286 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002287 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002288 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002289 }
2290 }
Jamie Madill437d2662014-12-05 14:23:35 -05002291}
2292
Jamie Madill63805b42015-08-25 13:17:39 -04002293void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002294{
Jamie Madillbd136f92015-08-10 14:51:37 -04002295 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002296 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002297
Jamie Madill01074252016-11-28 15:55:51 -05002298 for (unsigned int locationIndex : IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002299 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002300 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002301
Jamie Madill8047c0d2016-03-07 13:02:12 -05002302 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002303 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002304 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002305 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002306 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002307 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002308 mCachedInputLayout[d3dSemantic] =
2309 GetVertexFormatType(vertexAttributes[locationIndex],
2310 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002311 }
2312 }
2313}
2314
Jamie Madill9fc36822015-11-18 13:08:07 -05002315void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002316{
Jamie Madill9fc36822015-11-18 13:08:07 -05002317 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2318
2319 const std::string &varyingSemantic =
2320 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2321
Jamie Madillccdf74b2015-08-18 10:46:12 -04002322 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002323 mStreamOutVaryings.clear();
2324
Jamie Madill48ef11b2016-04-27 15:21:52 -04002325 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002326 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2327 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002328 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002329 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2330 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002331 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002332 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002333 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002334 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2335 builtins.glPosition.index, 4, outputSlot));
2336 }
2337 }
2338 else if (tfVaryingName == "gl_FragCoord")
2339 {
2340 if (builtins.glFragCoord.enabled)
2341 {
2342 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2343 builtins.glFragCoord.index, 4, outputSlot));
2344 }
2345 }
2346 else if (tfVaryingName == "gl_PointSize")
2347 {
2348 if (builtins.glPointSize.enabled)
2349 {
2350 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2351 }
2352 }
2353 else
2354 {
2355 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2356 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002357 const auto &varying = *registerInfo.packedVarying->varying;
2358 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002359 int componentCount = gl::VariableColumnCount(transposedType);
2360 ASSERT(!varying.isBuiltIn());
2361
Jamie Madill55c25d02015-11-18 13:08:08 -05002362 // Transform feedback for varying structs is underspecified.
2363 // See Khronos bug 9856.
2364 // TODO(jmadill): Figure out how to be spec-compliant here.
2365 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2366 continue;
2367
Jamie Madill9fc36822015-11-18 13:08:07 -05002368 // There can be more than one register assigned to a particular varying, and each
2369 // register needs its own stream out entry.
2370 if (tfVaryingName == varying.name)
2371 {
2372 mStreamOutVaryings.push_back(D3DVarying(
2373 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2374 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002375 }
2376 }
2377 }
2378}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002379
2380D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2381{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002382 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002383}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002384
2385bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2386{
2387 std::string baseName = blockName;
2388 gl::ParseAndStripArrayIndex(&baseName);
2389
2390 auto sizeIter = mBlockDataSizes.find(baseName);
2391 if (sizeIter == mBlockDataSizes.end())
2392 {
2393 *sizeOut = 0;
2394 return false;
2395 }
2396
2397 *sizeOut = sizeIter->second;
2398 return true;
2399}
2400
2401bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2402 sh::BlockMemberInfo *memberInfoOut) const
2403{
2404 auto infoIter = mBlockInfo.find(memberUniformName);
2405 if (infoIter == mBlockInfo.end())
2406 {
2407 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2408 return false;
2409 }
2410
2411 *memberInfoOut = infoIter->second;
2412 return true;
2413}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002414
2415void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2416 GLenum genMode,
2417 GLint components,
2418 const GLfloat *coeffs)
2419{
2420 UNREACHABLE();
2421}
2422
Jamie Madill8047c0d2016-03-07 13:02:12 -05002423} // namespace rx