blob: a1a67dbac60d973f9032b12a2ab8e6433eba3c23 [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
202template <typename T>
Jamie Madill334d6152015-10-22 14:00:28 -0400203bool TransposeMatrix(T *target,
204 const GLfloat *value,
205 int targetWidth,
206 int targetHeight,
207 int srcWidth,
208 int srcHeight)
209{
210 bool dirty = false;
211 int copyWidth = std::min(targetHeight, srcWidth);
212 int copyHeight = std::min(targetWidth, srcHeight);
213
214 for (int x = 0; x < copyWidth; x++)
215 {
216 for (int y = 0; y < copyHeight; y++)
217 {
218 SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]),
219 &dirty);
220 }
221 }
222 // clear unfilled right side
223 for (int y = 0; y < copyWidth; y++)
224 {
225 for (int x = copyHeight; x < targetWidth; x++)
226 {
227 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
228 }
229 }
230 // clear unfilled bottom.
231 for (int y = copyWidth; y < targetHeight; y++)
232 {
233 for (int x = 0; x < targetWidth; x++)
234 {
235 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
236 }
237 }
238
239 return dirty;
240}
241
242template <typename T>
243bool ExpandMatrix(T *target,
244 const GLfloat *value,
245 int targetWidth,
246 int targetHeight,
247 int srcWidth,
248 int srcHeight)
249{
250 bool dirty = false;
251 int copyWidth = std::min(targetWidth, srcWidth);
252 int copyHeight = std::min(targetHeight, srcHeight);
253
254 for (int y = 0; y < copyHeight; y++)
255 {
256 for (int x = 0; x < copyWidth; x++)
257 {
258 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]),
259 &dirty);
260 }
261 }
262 // clear unfilled right side
263 for (int y = 0; y < copyHeight; y++)
264 {
265 for (int x = copyWidth; x < targetWidth; x++)
266 {
267 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
268 }
269 }
270 // clear unfilled bottom.
271 for (int y = copyHeight; y < targetHeight; y++)
272 {
273 for (int x = 0; x < targetWidth; x++)
274 {
275 SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty);
276 }
277 }
278
279 return dirty;
280}
281
Jamie Madill4e31ad52015-10-29 10:32:57 -0400282gl::PrimitiveType GetGeometryShaderTypeFromDrawMode(GLenum drawMode)
283{
284 switch (drawMode)
285 {
286 // Uses the point sprite geometry shader.
287 case GL_POINTS:
288 return gl::PRIMITIVE_POINTS;
289
290 // All line drawing uses the same geometry shader.
291 case GL_LINES:
292 case GL_LINE_STRIP:
293 case GL_LINE_LOOP:
294 return gl::PRIMITIVE_LINES;
295
296 // The triangle fan primitive is emulated with strips in D3D11.
297 case GL_TRIANGLES:
298 case GL_TRIANGLE_FAN:
299 return gl::PRIMITIVE_TRIANGLES;
300
301 // Special case for triangle strips.
302 case GL_TRIANGLE_STRIP:
303 return gl::PRIMITIVE_TRIANGLE_STRIP;
304
305 default:
306 UNREACHABLE();
307 return gl::PRIMITIVE_TYPE_MAX;
308 }
309}
310
Jamie Madillada9ecc2015-08-17 12:53:37 -0400311} // anonymous namespace
312
Jamie Madill28afae52015-11-09 15:07:57 -0500313// D3DUniform Implementation
314
Jamie Madill62d31cb2015-09-11 13:25:51 -0400315D3DUniform::D3DUniform(GLenum typeIn,
316 const std::string &nameIn,
317 unsigned int arraySizeIn,
318 bool defaultBlock)
319 : type(typeIn),
320 name(nameIn),
321 arraySize(arraySizeIn),
322 data(nullptr),
323 dirty(true),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400324 vsRegisterIndex(GL_INVALID_INDEX),
Geoff Lang98c56da2015-09-15 15:45:34 -0400325 psRegisterIndex(GL_INVALID_INDEX),
Jamie Madill62d31cb2015-09-11 13:25:51 -0400326 registerCount(0),
327 registerElement(0)
328{
329 // We use data storage for default block uniforms to cache values that are sent to D3D during
330 // rendering
331 // Uniform blocks/buffers are treated separately by the Renderer (ES3 path only)
332 if (defaultBlock)
333 {
334 size_t bytes = gl::VariableInternalSize(type) * elementCount();
335 data = new uint8_t[bytes];
336 memset(data, 0, bytes);
337
338 // TODO(jmadill): is this correct with non-square matrices?
339 registerCount = gl::VariableRowCount(type) * elementCount();
340 }
341}
342
343D3DUniform::~D3DUniform()
344{
345 SafeDeleteArray(data);
346}
347
348bool D3DUniform::isSampler() const
349{
350 return gl::IsSamplerType(type);
351}
352
353bool D3DUniform::isReferencedByVertexShader() const
354{
355 return vsRegisterIndex != GL_INVALID_INDEX;
356}
357
358bool D3DUniform::isReferencedByFragmentShader() const
359{
360 return psRegisterIndex != GL_INVALID_INDEX;
361}
362
Jamie Madill28afae52015-11-09 15:07:57 -0500363// D3DVarying Implementation
364
Jamie Madill9fc36822015-11-18 13:08:07 -0500365D3DVarying::D3DVarying() : semanticIndex(0), componentCount(0), outputSlot(0)
Jamie Madill28afae52015-11-09 15:07:57 -0500366{
367}
368
Jamie Madill9fc36822015-11-18 13:08:07 -0500369D3DVarying::D3DVarying(const std::string &semanticNameIn,
370 unsigned int semanticIndexIn,
371 unsigned int componentCountIn,
372 unsigned int outputSlotIn)
373 : semanticName(semanticNameIn),
374 semanticIndex(semanticIndexIn),
375 componentCount(componentCountIn),
376 outputSlot(outputSlotIn)
Jamie Madill28afae52015-11-09 15:07:57 -0500377{
378}
379
Jamie Madille39a3f02015-11-17 20:42:15 -0500380// ProgramD3DMetadata Implementation
381
Jamie Madillc9bde922016-07-24 17:58:50 -0400382ProgramD3DMetadata::ProgramD3DMetadata(RendererD3D *renderer,
Jamie Madille39a3f02015-11-17 20:42:15 -0500383 const ShaderD3D *vertexShader,
384 const ShaderD3D *fragmentShader)
Jamie Madillc9bde922016-07-24 17:58:50 -0400385 : mRendererMajorShaderModel(renderer->getMajorShaderModel()),
386 mShaderModelSuffix(renderer->getShaderModelSuffix()),
387 mUsesInstancedPointSpriteEmulation(
388 renderer->getWorkarounds().useInstancedPointSpriteEmulation),
389 mUsesViewScale(renderer->presentPathFastEnabled()),
Jamie Madille39a3f02015-11-17 20:42:15 -0500390 mVertexShader(vertexShader),
391 mFragmentShader(fragmentShader)
392{
393}
394
395int ProgramD3DMetadata::getRendererMajorShaderModel() const
396{
397 return mRendererMajorShaderModel;
398}
399
Jamie Madill9082b982016-04-27 15:21:51 -0400400bool ProgramD3DMetadata::usesBroadcast(const gl::ContextState &data) const
Jamie Madille39a3f02015-11-17 20:42:15 -0500401{
Martin Radev1be913c2016-07-11 17:59:16 +0300402 return (mFragmentShader->usesFragColor() && data.getClientMajorVersion() < 3);
Jamie Madille39a3f02015-11-17 20:42:15 -0500403}
404
Jamie Madill48ef11b2016-04-27 15:21:52 -0400405bool ProgramD3DMetadata::usesFragDepth() const
Jamie Madille39a3f02015-11-17 20:42:15 -0500406{
Jamie Madill63286672015-11-24 13:00:08 -0500407 return mFragmentShader->usesFragDepth();
Jamie Madille39a3f02015-11-17 20:42:15 -0500408}
409
410bool ProgramD3DMetadata::usesPointCoord() const
411{
412 return mFragmentShader->usesPointCoord();
413}
414
415bool ProgramD3DMetadata::usesFragCoord() const
416{
417 return mFragmentShader->usesFragCoord();
418}
419
420bool ProgramD3DMetadata::usesPointSize() const
421{
422 return mVertexShader->usesPointSize();
423}
424
425bool ProgramD3DMetadata::usesInsertedPointCoordValue() const
426{
Jamie Madillc9bde922016-07-24 17:58:50 -0400427 return (!usesPointSize() || !mUsesInstancedPointSpriteEmulation) && usesPointCoord() &&
428 mRendererMajorShaderModel >= 4;
Jamie Madille39a3f02015-11-17 20:42:15 -0500429}
430
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800431bool ProgramD3DMetadata::usesViewScale() const
432{
433 return mUsesViewScale;
434}
435
Jamie Madille39a3f02015-11-17 20:42:15 -0500436bool ProgramD3DMetadata::addsPointCoordToVertexShader() const
437{
Jamie Madillc9bde922016-07-24 17:58:50 -0400438 // PointSprite emulation requiress that gl_PointCoord is present in the vertex shader
Jamie Madille39a3f02015-11-17 20:42:15 -0500439 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Jamie Madillc9bde922016-07-24 17:58:50 -0400440 // Even with a geometry shader, the app can render triangles or lines and reference
441 // gl_PointCoord in the fragment shader, requiring us to provide a dummy value. For
442 // simplicity, we always add this to the vertex shader when the fragment shader
443 // references gl_PointCoord, even if we could skip it in the geometry shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500444 return (mUsesInstancedPointSpriteEmulation && usesPointCoord()) ||
445 usesInsertedPointCoordValue();
446}
447
448bool ProgramD3DMetadata::usesTransformFeedbackGLPosition() const
449{
450 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
451 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
452 // the vertex shader in this case. This saves us 1 output vector.
453 return !(mRendererMajorShaderModel >= 4 && mShaderModelSuffix != "");
454}
455
456bool ProgramD3DMetadata::usesSystemValuePointSize() const
457{
458 return !mUsesInstancedPointSpriteEmulation && usesPointSize();
459}
460
461bool ProgramD3DMetadata::usesMultipleFragmentOuts() const
462{
463 return mFragmentShader->usesMultipleRenderTargets();
464}
465
466GLint ProgramD3DMetadata::getMajorShaderVersion() const
467{
468 return mVertexShader->getData().getShaderVersion();
469}
470
471const ShaderD3D *ProgramD3DMetadata::getFragmentShader() const
472{
473 return mFragmentShader;
474}
475
Jamie Madill120040e2016-12-07 14:46:16 -0500476void ProgramD3DMetadata::updatePackingBuiltins(ShaderType shaderType, VaryingPacking *packing)
477{
478 const std::string &userSemantic =
479 GetVaryingSemantic(mRendererMajorShaderModel, usesSystemValuePointSize());
480
481 unsigned int reservedSemanticIndex = packing->getMaxSemanticIndex();
482
483 VaryingPacking::BuiltinInfo *builtins = &packing->builtins(shaderType);
484
485 if (mRendererMajorShaderModel >= 4)
486 {
487 builtins->dxPosition.enableSystem("SV_Position");
488 }
489 else if (shaderType == SHADER_PIXEL)
490 {
491 builtins->dxPosition.enableSystem("VPOS");
492 }
493 else
494 {
495 builtins->dxPosition.enableSystem("POSITION");
496 }
497
498 if (usesTransformFeedbackGLPosition())
499 {
500 builtins->glPosition.enable(userSemantic, reservedSemanticIndex++);
501 }
502
503 if (usesFragCoord())
504 {
505 builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++);
506 }
507
508 if (shaderType == SHADER_VERTEX ? addsPointCoordToVertexShader() : usesPointCoord())
509 {
510 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
511 // In D3D11 we manually compute gl_PointCoord in the GS.
512 if (mRendererMajorShaderModel >= 4)
513 {
514 builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++);
515 }
516 else
517 {
518 builtins->glPointCoord.enable("TEXCOORD", 0);
519 }
520 }
521
522 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
523 if (usesSystemValuePointSize() &&
524 (shaderType != SHADER_PIXEL || mRendererMajorShaderModel >= 4))
525 {
526 builtins->glPointSize.enableSystem("PSIZE");
527 }
528}
529
Jamie Madill28afae52015-11-09 15:07:57 -0500530// ProgramD3D Implementation
531
Jamie Madilld3dfda22015-07-06 08:28:49 -0400532ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout,
533 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500534 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400535 : mInputs(inputLayout), mSignature(signature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700536{
Brandon Joneseb994362014-09-24 10:27:28 -0700537}
538
539ProgramD3D::VertexExecutable::~VertexExecutable()
540{
541 SafeDelete(mShaderExecutable);
542}
543
Jamie Madilld3dfda22015-07-06 08:28:49 -0400544// static
Jamie Madillbdec2f42016-03-02 16:35:32 -0500545ProgramD3D::VertexExecutable::HLSLAttribType ProgramD3D::VertexExecutable::GetAttribType(
546 GLenum type)
547{
548 switch (type)
549 {
550 case GL_INT:
551 return HLSLAttribType::SIGNED_INT;
552 case GL_UNSIGNED_INT:
553 return HLSLAttribType::UNSIGNED_INT;
554 case GL_SIGNED_NORMALIZED:
555 case GL_UNSIGNED_NORMALIZED:
556 case GL_FLOAT:
557 return HLSLAttribType::FLOAT;
558 default:
559 UNREACHABLE();
560 return HLSLAttribType::FLOAT;
561 }
562}
563
564// static
Jamie Madilld3dfda22015-07-06 08:28:49 -0400565void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer,
566 const gl::InputLayout &inputLayout,
567 Signature *signatureOut)
Brandon Joneseb994362014-09-24 10:27:28 -0700568{
Jamie Madillbdec2f42016-03-02 16:35:32 -0500569 signatureOut->assign(inputLayout.size(), HLSLAttribType::FLOAT);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400570
571 for (size_t index = 0; index < inputLayout.size(); ++index)
Brandon Joneseb994362014-09-24 10:27:28 -0700572 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400573 gl::VertexFormatType vertexFormatType = inputLayout[index];
Jamie Madillbdec2f42016-03-02 16:35:32 -0500574 if (vertexFormatType == gl::VERTEX_FORMAT_INVALID)
575 continue;
Jamie Madillbd136f92015-08-10 14:51:37 -0400576
Jamie Madillbdec2f42016-03-02 16:35:32 -0500577 VertexConversionType conversionType = renderer->getVertexConversionType(vertexFormatType);
578 if ((conversionType & VERTEX_CONVERT_GPU) == 0)
579 continue;
580
581 GLenum componentType = renderer->getVertexComponentType(vertexFormatType);
582 (*signatureOut)[index] = GetAttribType(componentType);
Brandon Joneseb994362014-09-24 10:27:28 -0700583 }
Brandon Joneseb994362014-09-24 10:27:28 -0700584}
585
Jamie Madilld3dfda22015-07-06 08:28:49 -0400586bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const
587{
Jamie Madillbd136f92015-08-10 14:51:37 -0400588 size_t limit = std::max(mSignature.size(), signature.size());
589 for (size_t index = 0; index < limit; ++index)
590 {
Jamie Madillbdec2f42016-03-02 16:35:32 -0500591 // treat undefined indexes as FLOAT
592 auto a = index < signature.size() ? signature[index] : HLSLAttribType::FLOAT;
593 auto b = index < mSignature.size() ? mSignature[index] : HLSLAttribType::FLOAT;
Jamie Madillbd136f92015-08-10 14:51:37 -0400594 if (a != b)
595 return false;
596 }
597
598 return true;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400599}
600
601ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature,
602 ShaderExecutableD3D *shaderExecutable)
Jamie Madill334d6152015-10-22 14:00:28 -0400603 : mOutputSignature(outputSignature), mShaderExecutable(shaderExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -0700604{
605}
606
607ProgramD3D::PixelExecutable::~PixelExecutable()
608{
609 SafeDelete(mShaderExecutable);
610}
611
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700612ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D)
613{
614}
615
Geoff Lang7dd2e102014-11-10 15:19:26 -0500616unsigned int ProgramD3D::mCurrentSerial = 1;
617
Jamie Madill48ef11b2016-04-27 15:21:52 -0400618ProgramD3D::ProgramD3D(const gl::ProgramState &state, RendererD3D *renderer)
619 : ProgramImpl(state),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700620 mRenderer(renderer),
621 mDynamicHLSL(NULL),
Jamie Madill4e31ad52015-10-29 10:32:57 -0400622 mGeometryExecutables(gl::PRIMITIVE_TYPE_MAX, nullptr),
Brandon Jones44151a92014-09-10 11:32:25 -0700623 mUsesPointSize(false),
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400624 mUsesFlatInterpolation(false),
Brandon Jonesc9610c52014-08-25 17:02:59 -0700625 mVertexUniformStorage(NULL),
Brandon Jones44151a92014-09-10 11:32:25 -0700626 mFragmentUniformStorage(NULL),
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700627 mUsedVertexSamplerRange(0),
628 mUsedPixelSamplerRange(0),
629 mDirtySamplerMapping(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500630 mSerial(issueSerial())
Brandon Jonesc9610c52014-08-25 17:02:59 -0700631{
Brandon Joneseb994362014-09-24 10:27:28 -0700632 mDynamicHLSL = new DynamicHLSL(renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700633}
634
635ProgramD3D::~ProgramD3D()
636{
637 reset();
638 SafeDelete(mDynamicHLSL);
639}
640
Brandon Jones44151a92014-09-10 11:32:25 -0700641bool ProgramD3D::usesPointSpriteEmulation() const
642{
643 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
644}
645
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400646bool ProgramD3D::usesGeometryShader(GLenum drawMode) const
Brandon Jones44151a92014-09-10 11:32:25 -0700647{
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400648 if (drawMode != GL_POINTS)
649 {
650 return mUsesFlatInterpolation;
651 }
652
Cooper Partine6664f02015-01-09 16:22:24 -0800653 return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation();
654}
655
656bool ProgramD3D::usesInstancedPointSpriteEmulation() const
657{
658 return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Brandon Jones44151a92014-09-10 11:32:25 -0700659}
660
Jamie Madill334d6152015-10-22 14:00:28 -0400661GLint ProgramD3D::getSamplerMapping(gl::SamplerType type,
662 unsigned int samplerIndex,
663 const gl::Caps &caps) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700664{
665 GLint logicalTextureUnit = -1;
666
667 switch (type)
668 {
Jamie Madill334d6152015-10-22 14:00:28 -0400669 case gl::SAMPLER_PIXEL:
670 ASSERT(samplerIndex < caps.maxTextureImageUnits);
671 if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active)
672 {
673 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
674 }
675 break;
676 case gl::SAMPLER_VERTEX:
677 ASSERT(samplerIndex < caps.maxVertexTextureImageUnits);
678 if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active)
679 {
680 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
681 }
682 break;
683 default:
684 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700685 }
686
Jamie Madill334d6152015-10-22 14:00:28 -0400687 if (logicalTextureUnit >= 0 &&
688 logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits))
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700689 {
690 return logicalTextureUnit;
691 }
692
693 return -1;
694}
695
696// Returns the texture type for a given Direct3D 9 sampler type and
697// index (0-15 for the pixel shader and 0-3 for the vertex shader).
698GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const
699{
700 switch (type)
701 {
Jamie Madill334d6152015-10-22 14:00:28 -0400702 case gl::SAMPLER_PIXEL:
703 ASSERT(samplerIndex < mSamplersPS.size());
704 ASSERT(mSamplersPS[samplerIndex].active);
705 return mSamplersPS[samplerIndex].textureType;
706 case gl::SAMPLER_VERTEX:
707 ASSERT(samplerIndex < mSamplersVS.size());
708 ASSERT(mSamplersVS[samplerIndex].active);
709 return mSamplersVS[samplerIndex].textureType;
710 default:
711 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700712 }
713
714 return GL_TEXTURE_2D;
715}
716
Olli Etuaho618bebc2016-01-15 16:40:00 +0200717GLuint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700718{
719 switch (type)
720 {
Jamie Madill334d6152015-10-22 14:00:28 -0400721 case gl::SAMPLER_PIXEL:
722 return mUsedPixelSamplerRange;
723 case gl::SAMPLER_VERTEX:
724 return mUsedVertexSamplerRange;
725 default:
726 UNREACHABLE();
Olli Etuaho618bebc2016-01-15 16:40:00 +0200727 return 0u;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700728 }
729}
730
731void ProgramD3D::updateSamplerMapping()
732{
733 if (!mDirtySamplerMapping)
734 {
735 return;
736 }
737
738 mDirtySamplerMapping = false;
739
740 // Retrieve sampler uniform values
Jamie Madill62d31cb2015-09-11 13:25:51 -0400741 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700742 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400743 if (!d3dUniform->dirty)
744 continue;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700745
Jamie Madill62d31cb2015-09-11 13:25:51 -0400746 if (!d3dUniform->isSampler())
747 continue;
748
749 int count = d3dUniform->elementCount();
750 const GLint(*v)[4] = reinterpret_cast<const GLint(*)[4]>(d3dUniform->data);
751
752 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700753 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400754 unsigned int firstIndex = d3dUniform->psRegisterIndex;
755
756 for (int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700757 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400758 unsigned int samplerIndex = firstIndex + i;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700759
Jamie Madill62d31cb2015-09-11 13:25:51 -0400760 if (samplerIndex < mSamplersPS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700761 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400762 ASSERT(mSamplersPS[samplerIndex].active);
763 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700764 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400765 }
766 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700767
Jamie Madill62d31cb2015-09-11 13:25:51 -0400768 if (d3dUniform->isReferencedByVertexShader())
769 {
770 unsigned int firstIndex = d3dUniform->vsRegisterIndex;
771
772 for (int i = 0; i < count; i++)
773 {
774 unsigned int samplerIndex = firstIndex + i;
775
776 if (samplerIndex < mSamplersVS.size())
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700777 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400778 ASSERT(mSamplersVS[samplerIndex].active);
779 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700780 }
781 }
782 }
783 }
784}
785
Geoff Lang7dd2e102014-11-10 15:19:26 -0500786LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -0700787{
Jamie Madill62d31cb2015-09-11 13:25:51 -0400788 reset();
789
Jamie Madill334d6152015-10-22 14:00:28 -0400790 DeviceIdentifier binaryDeviceIdentifier = {0};
791 stream->readBytes(reinterpret_cast<unsigned char *>(&binaryDeviceIdentifier),
792 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -0700793
794 DeviceIdentifier identifier = mRenderer->getAdapterIdentifier();
795 if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0)
796 {
797 infoLog << "Invalid program binary, device configuration has changed.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500798 return false;
Austin Kinross137b1512015-06-17 16:14:53 -0700799 }
800
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500801 int compileFlags = stream->readInt<int>();
802 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
803 {
Jamie Madillf6113162015-05-07 11:49:21 -0400804 infoLog << "Mismatched compilation flags.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500805 return false;
Jamie Madill2db1fbb2014-12-03 10:58:55 -0500806 }
807
Jamie Madill8047c0d2016-03-07 13:02:12 -0500808 for (int &index : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -0400809 {
Jamie Madill8047c0d2016-03-07 13:02:12 -0500810 stream->readInt(&index);
Jamie Madill63805b42015-08-25 13:17:39 -0400811 }
812
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700813 const unsigned int psSamplerCount = stream->readInt<unsigned int>();
814 for (unsigned int i = 0; i < psSamplerCount; ++i)
815 {
816 Sampler sampler;
817 stream->readBool(&sampler.active);
818 stream->readInt(&sampler.logicalTextureUnit);
819 stream->readInt(&sampler.textureType);
820 mSamplersPS.push_back(sampler);
821 }
822 const unsigned int vsSamplerCount = stream->readInt<unsigned int>();
823 for (unsigned int i = 0; i < vsSamplerCount; ++i)
824 {
825 Sampler sampler;
826 stream->readBool(&sampler.active);
827 stream->readInt(&sampler.logicalTextureUnit);
828 stream->readInt(&sampler.textureType);
829 mSamplersVS.push_back(sampler);
830 }
831
832 stream->readInt(&mUsedVertexSamplerRange);
833 stream->readInt(&mUsedPixelSamplerRange);
834
835 const unsigned int uniformCount = stream->readInt<unsigned int>();
836 if (stream->error())
837 {
Jamie Madillf6113162015-05-07 11:49:21 -0400838 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500839 return false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700840 }
841
Jamie Madill48ef11b2016-04-27 15:21:52 -0400842 const auto &linkedUniforms = mState.getUniforms();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400843 ASSERT(mD3DUniforms.empty());
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700844 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++)
845 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400846 const gl::LinkedUniform &linkedUniform = linkedUniforms[uniformIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700847
Jamie Madill62d31cb2015-09-11 13:25:51 -0400848 D3DUniform *d3dUniform =
849 new D3DUniform(linkedUniform.type, linkedUniform.name, linkedUniform.arraySize,
850 linkedUniform.isInDefaultBlock());
851 stream->readInt(&d3dUniform->psRegisterIndex);
852 stream->readInt(&d3dUniform->vsRegisterIndex);
853 stream->readInt(&d3dUniform->registerCount);
854 stream->readInt(&d3dUniform->registerElement);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700855
Jamie Madill62d31cb2015-09-11 13:25:51 -0400856 mD3DUniforms.push_back(d3dUniform);
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700857 }
858
Jamie Madill4a3c2342015-10-08 12:58:45 -0400859 const unsigned int blockCount = stream->readInt<unsigned int>();
860 if (stream->error())
861 {
862 infoLog << "Invalid program binary.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500863 return false;
Jamie Madill4a3c2342015-10-08 12:58:45 -0400864 }
865
866 ASSERT(mD3DUniformBlocks.empty());
867 for (unsigned int blockIndex = 0; blockIndex < blockCount; ++blockIndex)
868 {
869 D3DUniformBlock uniformBlock;
870 stream->readInt(&uniformBlock.psRegisterIndex);
871 stream->readInt(&uniformBlock.vsRegisterIndex);
872 mD3DUniformBlocks.push_back(uniformBlock);
873 }
874
Jamie Madill9fc36822015-11-18 13:08:07 -0500875 const unsigned int streamOutVaryingCount = stream->readInt<unsigned int>();
876 mStreamOutVaryings.resize(streamOutVaryingCount);
877 for (unsigned int varyingIndex = 0; varyingIndex < streamOutVaryingCount; ++varyingIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700878 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500879 D3DVarying *varying = &mStreamOutVaryings[varyingIndex];
Brandon Joneseb994362014-09-24 10:27:28 -0700880
Jamie Madill28afae52015-11-09 15:07:57 -0500881 stream->readString(&varying->semanticName);
882 stream->readInt(&varying->semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -0500883 stream->readInt(&varying->componentCount);
884 stream->readInt(&varying->outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -0700885 }
886
Brandon Jones22502d52014-08-29 16:58:36 -0700887 stream->readString(&mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400888 stream->readBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
889 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700890 stream->readString(&mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -0400891 stream->readBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
892 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -0700893 stream->readBool(&mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -0700894 stream->readBool(&mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400895 stream->readBool(&mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -0700896
897 const size_t pixelShaderKeySize = stream->readInt<unsigned int>();
898 mPixelShaderKey.resize(pixelShaderKeySize);
Jamie Madill334d6152015-10-22 14:00:28 -0400899 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize;
900 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -0700901 {
902 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type);
903 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name);
904 stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source);
905 stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex);
906 }
907
Jamie Madill4e31ad52015-10-29 10:32:57 -0400908 stream->readString(&mGeometryShaderPreamble);
909
Jamie Madill334d6152015-10-22 14:00:28 -0400910 const unsigned char *binary = reinterpret_cast<const unsigned char *>(stream->data());
Brandon Joneseb994362014-09-24 10:27:28 -0700911
Jamie Madillb0a838b2016-11-13 20:02:12 -0500912 bool separateAttribs = (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS);
913
Brandon Joneseb994362014-09-24 10:27:28 -0700914 const unsigned int vertexShaderCount = stream->readInt<unsigned int>();
Jamie Madill334d6152015-10-22 14:00:28 -0400915 for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount;
916 vertexShaderIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700917 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400918 size_t inputLayoutSize = stream->readInt<size_t>();
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400919 gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID);
Brandon Joneseb994362014-09-24 10:27:28 -0700920
Jamie Madilld3dfda22015-07-06 08:28:49 -0400921 for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -0700922 {
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400923 inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>();
Brandon Joneseb994362014-09-24 10:27:28 -0700924 }
925
Jamie Madill334d6152015-10-22 14:00:28 -0400926 unsigned int vertexShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700927 const unsigned char *vertexShaderFunction = binary + stream->offset();
Geoff Langb543aff2014-09-30 14:52:54 -0400928
Jamie Madillada9ecc2015-08-17 12:53:37 -0400929 ShaderExecutableD3D *shaderExecutable = nullptr;
930
Jamie Madillb0a838b2016-11-13 20:02:12 -0500931 ANGLE_TRY(mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, SHADER_VERTEX,
932 mStreamOutVaryings, separateAttribs,
933 &shaderExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -0400934
Brandon Joneseb994362014-09-24 10:27:28 -0700935 if (!shaderExecutable)
936 {
Jamie Madillf6113162015-05-07 11:49:21 -0400937 infoLog << "Could not create vertex shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500938 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700939 }
940
941 // generated converted input layout
Jamie Madilld3dfda22015-07-06 08:28:49 -0400942 VertexExecutable::Signature signature;
943 VertexExecutable::getSignature(mRenderer, inputLayout, &signature);
Brandon Joneseb994362014-09-24 10:27:28 -0700944
945 // add new binary
Jamie Madill334d6152015-10-22 14:00:28 -0400946 mVertexExecutables.push_back(
947 new VertexExecutable(inputLayout, signature, shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700948
949 stream->skip(vertexShaderSize);
950 }
951
952 const size_t pixelShaderCount = stream->readInt<unsigned int>();
953 for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++)
954 {
955 const size_t outputCount = stream->readInt<unsigned int>();
956 std::vector<GLenum> outputs(outputCount);
957 for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++)
958 {
959 stream->readInt(&outputs[outputIndex]);
960 }
961
Jamie Madill334d6152015-10-22 14:00:28 -0400962 const size_t pixelShaderSize = stream->readInt<unsigned int>();
Brandon Joneseb994362014-09-24 10:27:28 -0700963 const unsigned char *pixelShaderFunction = binary + stream->offset();
Jamie Madillada9ecc2015-08-17 12:53:37 -0400964 ShaderExecutableD3D *shaderExecutable = nullptr;
965
Jamie Madillb0a838b2016-11-13 20:02:12 -0500966 ANGLE_TRY(mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL,
967 mStreamOutVaryings, separateAttribs,
968 &shaderExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -0700969
970 if (!shaderExecutable)
971 {
Jamie Madillf6113162015-05-07 11:49:21 -0400972 infoLog << "Could not create pixel shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -0500973 return false;
Brandon Joneseb994362014-09-24 10:27:28 -0700974 }
975
976 // add new binary
977 mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable));
978
979 stream->skip(pixelShaderSize);
980 }
981
Jamie Madill4e31ad52015-10-29 10:32:57 -0400982 for (unsigned int geometryExeIndex = 0; geometryExeIndex < gl::PRIMITIVE_TYPE_MAX;
983 ++geometryExeIndex)
Brandon Joneseb994362014-09-24 10:27:28 -0700984 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400985 unsigned int geometryShaderSize = stream->readInt<unsigned int>();
986 if (geometryShaderSize == 0)
987 {
988 mGeometryExecutables[geometryExeIndex] = nullptr;
989 continue;
990 }
991
Brandon Joneseb994362014-09-24 10:27:28 -0700992 const unsigned char *geometryShaderFunction = binary + stream->offset();
Jamie Madill4e31ad52015-10-29 10:32:57 -0400993
Jamie Madillb0a838b2016-11-13 20:02:12 -0500994 ANGLE_TRY(mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize,
995 SHADER_GEOMETRY, mStreamOutVaryings, separateAttribs,
996 &mGeometryExecutables[geometryExeIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -0700997
Jamie Madill4e31ad52015-10-29 10:32:57 -0400998 if (!mGeometryExecutables[geometryExeIndex])
Brandon Joneseb994362014-09-24 10:27:28 -0700999 {
Jamie Madillf6113162015-05-07 11:49:21 -04001000 infoLog << "Could not create geometry shader.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001001 return false;
Brandon Joneseb994362014-09-24 10:27:28 -07001002 }
1003 stream->skip(geometryShaderSize);
1004 }
1005
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001006 initializeUniformStorage();
1007
Jamie Madillb0a838b2016-11-13 20:02:12 -05001008 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001009}
1010
Geoff Langb543aff2014-09-30 14:52:54 -04001011gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream)
Brandon Jones22502d52014-08-29 16:58:36 -07001012{
Austin Kinross137b1512015-06-17 16:14:53 -07001013 // Output the DeviceIdentifier before we output any shader code
Jamie Madill334d6152015-10-22 14:00:28 -04001014 // When we load the binary again later, we can validate the device identifier before trying to
1015 // compile any HLSL
Austin Kinross137b1512015-06-17 16:14:53 -07001016 DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier();
Jamie Madill334d6152015-10-22 14:00:28 -04001017 stream->writeBytes(reinterpret_cast<unsigned char *>(&binaryIdentifier),
1018 sizeof(DeviceIdentifier));
Austin Kinross137b1512015-06-17 16:14:53 -07001019
Jamie Madill2db1fbb2014-12-03 10:58:55 -05001020 stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
1021
Jamie Madill8047c0d2016-03-07 13:02:12 -05001022 for (int d3dSemantic : mAttribLocationToD3DSemantic)
Jamie Madill63805b42015-08-25 13:17:39 -04001023 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05001024 stream->writeInt(d3dSemantic);
Jamie Madill63805b42015-08-25 13:17:39 -04001025 }
1026
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001027 stream->writeInt(mSamplersPS.size());
1028 for (unsigned int i = 0; i < mSamplersPS.size(); ++i)
1029 {
1030 stream->writeInt(mSamplersPS[i].active);
1031 stream->writeInt(mSamplersPS[i].logicalTextureUnit);
1032 stream->writeInt(mSamplersPS[i].textureType);
1033 }
1034
1035 stream->writeInt(mSamplersVS.size());
1036 for (unsigned int i = 0; i < mSamplersVS.size(); ++i)
1037 {
1038 stream->writeInt(mSamplersVS[i].active);
1039 stream->writeInt(mSamplersVS[i].logicalTextureUnit);
1040 stream->writeInt(mSamplersVS[i].textureType);
1041 }
1042
1043 stream->writeInt(mUsedVertexSamplerRange);
1044 stream->writeInt(mUsedPixelSamplerRange);
1045
Jamie Madill62d31cb2015-09-11 13:25:51 -04001046 stream->writeInt(mD3DUniforms.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04001047 for (const D3DUniform *uniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001048 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001049 // Type, name and arraySize are redundant, so aren't stored in the binary.
Jamie Madille2e406c2016-06-02 13:04:10 -04001050 stream->writeIntOrNegOne(uniform->psRegisterIndex);
1051 stream->writeIntOrNegOne(uniform->vsRegisterIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001052 stream->writeInt(uniform->registerCount);
1053 stream->writeInt(uniform->registerElement);
1054 }
1055
1056 stream->writeInt(mD3DUniformBlocks.size());
1057 for (const D3DUniformBlock &uniformBlock : mD3DUniformBlocks)
1058 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001059 stream->writeIntOrNegOne(uniformBlock.psRegisterIndex);
1060 stream->writeIntOrNegOne(uniformBlock.vsRegisterIndex);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001061 }
1062
Jamie Madill9fc36822015-11-18 13:08:07 -05001063 stream->writeInt(mStreamOutVaryings.size());
1064 for (const auto &varying : mStreamOutVaryings)
Brandon Joneseb994362014-09-24 10:27:28 -07001065 {
Brandon Joneseb994362014-09-24 10:27:28 -07001066 stream->writeString(varying.semanticName);
1067 stream->writeInt(varying.semanticIndex);
Jamie Madill9fc36822015-11-18 13:08:07 -05001068 stream->writeInt(varying.componentCount);
1069 stream->writeInt(varying.outputSlot);
Brandon Joneseb994362014-09-24 10:27:28 -07001070 }
1071
Brandon Jones22502d52014-08-29 16:58:36 -07001072 stream->writeString(mVertexHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001073 stream->writeBytes(reinterpret_cast<unsigned char *>(&mVertexWorkarounds),
1074 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001075 stream->writeString(mPixelHLSL);
Jamie Madill334d6152015-10-22 14:00:28 -04001076 stream->writeBytes(reinterpret_cast<unsigned char *>(&mPixelWorkarounds),
1077 sizeof(D3DCompilerWorkarounds));
Brandon Jones22502d52014-08-29 16:58:36 -07001078 stream->writeInt(mUsesFragDepth);
Brandon Jones44151a92014-09-10 11:32:25 -07001079 stream->writeInt(mUsesPointSize);
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001080 stream->writeInt(mUsesFlatInterpolation);
Brandon Jones22502d52014-08-29 16:58:36 -07001081
Brandon Joneseb994362014-09-24 10:27:28 -07001082 const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -07001083 stream->writeInt(pixelShaderKey.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001084 for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size();
1085 pixelShaderKeyIndex++)
Brandon Jones22502d52014-08-29 16:58:36 -07001086 {
Brandon Joneseb994362014-09-24 10:27:28 -07001087 const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex];
Brandon Jones22502d52014-08-29 16:58:36 -07001088 stream->writeInt(variable.type);
1089 stream->writeString(variable.name);
1090 stream->writeString(variable.source);
1091 stream->writeInt(variable.outputIndex);
1092 }
1093
Jamie Madill4e31ad52015-10-29 10:32:57 -04001094 stream->writeString(mGeometryShaderPreamble);
1095
Brandon Joneseb994362014-09-24 10:27:28 -07001096 stream->writeInt(mVertexExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001097 for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size();
1098 vertexExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001099 {
1100 VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex];
1101
Jamie Madilld3dfda22015-07-06 08:28:49 -04001102 const auto &inputLayout = vertexExecutable->inputs();
1103 stream->writeInt(inputLayout.size());
1104
1105 for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001106 {
Jamie Madille2e406c2016-06-02 13:04:10 -04001107 stream->writeInt(static_cast<unsigned int>(inputLayout[inputIndex]));
Brandon Joneseb994362014-09-24 10:27:28 -07001108 }
1109
1110 size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength();
1111 stream->writeInt(vertexShaderSize);
1112
1113 const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction();
1114 stream->writeBytes(vertexBlob, vertexShaderSize);
1115 }
1116
1117 stream->writeInt(mPixelExecutables.size());
Jamie Madill334d6152015-10-22 14:00:28 -04001118 for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size();
1119 pixelExecutableIndex++)
Brandon Joneseb994362014-09-24 10:27:28 -07001120 {
1121 PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex];
1122
1123 const std::vector<GLenum> outputs = pixelExecutable->outputSignature();
1124 stream->writeInt(outputs.size());
1125 for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++)
1126 {
1127 stream->writeInt(outputs[outputIndex]);
1128 }
1129
1130 size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength();
1131 stream->writeInt(pixelShaderSize);
1132
1133 const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction();
1134 stream->writeBytes(pixelBlob, pixelShaderSize);
1135 }
1136
Jamie Madill4e31ad52015-10-29 10:32:57 -04001137 for (const ShaderExecutableD3D *geometryExe : mGeometryExecutables)
Brandon Joneseb994362014-09-24 10:27:28 -07001138 {
Jamie Madill4e31ad52015-10-29 10:32:57 -04001139 if (geometryExe == nullptr)
1140 {
1141 stream->writeInt(0);
1142 continue;
1143 }
1144
1145 size_t geometryShaderSize = geometryExe->getLength();
1146 stream->writeInt(geometryShaderSize);
1147 stream->writeBytes(geometryExe->getFunction(), geometryShaderSize);
Brandon Joneseb994362014-09-24 10:27:28 -07001148 }
1149
Geoff Langb543aff2014-09-30 14:52:54 -04001150 return gl::Error(GL_NO_ERROR);
Brandon Jones22502d52014-08-29 16:58:36 -07001151}
1152
Geoff Langc5629752015-12-07 16:29:04 -05001153void ProgramD3D::setBinaryRetrievableHint(bool /* retrievable */)
1154{
1155}
1156
Jamie Madill334d6152015-10-22 14:00:28 -04001157gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo,
1158 ShaderExecutableD3D **outExecutable)
Brandon Jones22502d52014-08-29 16:58:36 -07001159{
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001160 mPixelShaderOutputFormatCache.clear();
Brandon Joneseb994362014-09-24 10:27:28 -07001161
Jamie Madill85a18042015-03-05 15:41:41 -05001162 const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001163 const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender();
Brandon Joneseb994362014-09-24 10:27:28 -07001164
1165 for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment)
1166 {
1167 const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment];
1168
1169 if (colorbuffer)
1170 {
Jamie Madill334d6152015-10-22 14:00:28 -04001171 mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK
1172 ? GL_COLOR_ATTACHMENT0
1173 : colorbuffer->getBinding());
Brandon Joneseb994362014-09-24 10:27:28 -07001174 }
1175 else
1176 {
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001177 mPixelShaderOutputFormatCache.push_back(GL_NONE);
Brandon Joneseb994362014-09-24 10:27:28 -07001178 }
1179 }
1180
Geoff Lang7a26a1a2015-03-25 12:29:06 -04001181 return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr);
Brandon Joneseb994362014-09-24 10:27:28 -07001182}
1183
Jamie Madill97399232014-12-23 12:31:15 -05001184gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature,
Geoff Lang359ef262015-01-05 14:42:29 -05001185 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001186 gl::InfoLog *infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001187{
1188 for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++)
1189 {
1190 if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature))
1191 {
Geoff Langb543aff2014-09-30 14:52:54 -04001192 *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable();
1193 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001194 }
1195 }
1196
Jamie Madill334d6152015-10-22 14:00:28 -04001197 std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(
1198 mPixelHLSL, mPixelShaderKey, mUsesFragDepth, outputSignature);
Brandon Jones22502d52014-08-29 16:58:36 -07001199
1200 // Generate new pixel executable
Geoff Lang359ef262015-01-05 14:42:29 -05001201 ShaderExecutableD3D *pixelExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001202
1203 gl::InfoLog tempInfoLog;
1204 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1205
Jamie Madill01074252016-11-28 15:55:51 -05001206 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001207 *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001208 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001209 &pixelExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001210
Jamie Madill97399232014-12-23 12:31:15 -05001211 if (pixelExecutable)
1212 {
1213 mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable));
1214 }
1215 else if (!infoLog)
Brandon Joneseb994362014-09-24 10:27:28 -07001216 {
1217 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001218 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Brandon Joneseb994362014-09-24 10:27:28 -07001219 ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]);
1220 }
Brandon Jones22502d52014-08-29 16:58:36 -07001221
Geoff Langb543aff2014-09-30 14:52:54 -04001222 *outExectuable = pixelExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001223 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001224}
1225
Jamie Madilld3dfda22015-07-06 08:28:49 -04001226gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout,
Geoff Lang359ef262015-01-05 14:42:29 -05001227 ShaderExecutableD3D **outExectuable,
Jamie Madill97399232014-12-23 12:31:15 -05001228 gl::InfoLog *infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001229{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001230 VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature);
Brandon Joneseb994362014-09-24 10:27:28 -07001231
1232 for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++)
1233 {
Jamie Madilld3dfda22015-07-06 08:28:49 -04001234 if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature))
Brandon Joneseb994362014-09-24 10:27:28 -07001235 {
Geoff Langb543aff2014-09-30 14:52:54 -04001236 *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable();
1237 return gl::Error(GL_NO_ERROR);
Brandon Joneseb994362014-09-24 10:27:28 -07001238 }
1239 }
1240
Brandon Jones22502d52014-08-29 16:58:36 -07001241 // Generate new dynamic layout with attribute conversions
Jamie Madillc349ec02015-08-21 16:53:12 -04001242 std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001243 mVertexHLSL, inputLayout, mState.getAttributes());
Brandon Jones22502d52014-08-29 16:58:36 -07001244
1245 // Generate new vertex executable
Geoff Lang359ef262015-01-05 14:42:29 -05001246 ShaderExecutableD3D *vertexExecutable = NULL;
Jamie Madill97399232014-12-23 12:31:15 -05001247
1248 gl::InfoLog tempInfoLog;
1249 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1250
Jamie Madill01074252016-11-28 15:55:51 -05001251 ANGLE_TRY(mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001252 *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001253 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds,
Jamie Madill01074252016-11-28 15:55:51 -05001254 &vertexExecutable));
Geoff Langb543aff2014-09-30 14:52:54 -04001255
Jamie Madill97399232014-12-23 12:31:15 -05001256 if (vertexExecutable)
Brandon Joneseb994362014-09-24 10:27:28 -07001257 {
Jamie Madill334d6152015-10-22 14:00:28 -04001258 mVertexExecutables.push_back(
1259 new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable));
Brandon Joneseb994362014-09-24 10:27:28 -07001260 }
Jamie Madill97399232014-12-23 12:31:15 -05001261 else if (!infoLog)
1262 {
1263 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001264 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
Jamie Madill97399232014-12-23 12:31:15 -05001265 ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]);
1266 }
Brandon Jones22502d52014-08-29 16:58:36 -07001267
Geoff Langb543aff2014-09-30 14:52:54 -04001268 *outExectuable = vertexExecutable;
Jamie Madill01074252016-11-28 15:55:51 -05001269 return gl::NoError();
Brandon Jones22502d52014-08-29 16:58:36 -07001270}
1271
Jamie Madill9082b982016-04-27 15:21:51 -04001272gl::Error ProgramD3D::getGeometryExecutableForPrimitiveType(const gl::ContextState &data,
Jamie Madill4e31ad52015-10-29 10:32:57 -04001273 GLenum drawMode,
1274 ShaderExecutableD3D **outExecutable,
1275 gl::InfoLog *infoLog)
1276{
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001277 if (outExecutable)
1278 {
1279 *outExecutable = nullptr;
1280 }
1281
Austin Kinross88829e82016-01-12 13:04:41 -08001282 // Return a null shader if the current rendering doesn't use a geometry shader
1283 if (!usesGeometryShader(drawMode))
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001284 {
1285 return gl::Error(GL_NO_ERROR);
1286 }
1287
Jamie Madill4e31ad52015-10-29 10:32:57 -04001288 gl::PrimitiveType geometryShaderType = GetGeometryShaderTypeFromDrawMode(drawMode);
1289
1290 if (mGeometryExecutables[geometryShaderType] != nullptr)
1291 {
1292 if (outExecutable)
1293 {
1294 *outExecutable = mGeometryExecutables[geometryShaderType];
1295 }
1296 return gl::Error(GL_NO_ERROR);
1297 }
1298
1299 std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(
Jamie Madill48ef11b2016-04-27 15:21:52 -04001300 geometryShaderType, data, mState, mRenderer->presentPathFastEnabled(),
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001301 mGeometryShaderPreamble);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001302
1303 gl::InfoLog tempInfoLog;
1304 gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog;
1305
1306 gl::Error error = mRenderer->compileToExecutable(
Jamie Madill9fc36822015-11-18 13:08:07 -05001307 *currentInfoLog, geometryHLSL, SHADER_GEOMETRY, mStreamOutVaryings,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001308 (mState.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), D3DCompilerWorkarounds(),
Jamie Madill4e31ad52015-10-29 10:32:57 -04001309 &mGeometryExecutables[geometryShaderType]);
1310
1311 if (!infoLog && error.isError())
1312 {
1313 std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3);
1314 tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]);
1315 ERR("Error compiling dynamic geometry executable:\n%s\n", &tempCharBuffer[0]);
1316 }
1317
1318 if (outExecutable)
1319 {
1320 *outExecutable = mGeometryExecutables[geometryShaderType];
1321 }
1322 return error;
1323}
1324
Jamie Madill01074252016-11-28 15:55:51 -05001325class ProgramD3D::GetExecutableTask : public Closure
Brandon Jones44151a92014-09-10 11:32:25 -07001326{
Jamie Madill01074252016-11-28 15:55:51 -05001327 public:
1328 GetExecutableTask(ProgramD3D *program)
1329 : mProgram(program), mError(GL_NO_ERROR), mInfoLog(), mResult(nullptr)
Brandon Joneseb994362014-09-24 10:27:28 -07001330 {
Brandon Joneseb994362014-09-24 10:27:28 -07001331 }
1332
Jamie Madill01074252016-11-28 15:55:51 -05001333 virtual gl::Error run() = 0;
1334
1335 void operator()() override { mError = run(); }
1336
1337 const gl::Error &getError() const { return mError; }
1338 const gl::InfoLog &getInfoLog() const { return mInfoLog; }
1339 ShaderExecutableD3D *getResult() { return mResult; }
1340
1341 protected:
1342 ProgramD3D *mProgram;
1343 gl::Error mError;
1344 gl::InfoLog mInfoLog;
1345 ShaderExecutableD3D *mResult;
1346};
1347
1348class ProgramD3D::GetVertexExecutableTask : public ProgramD3D::GetExecutableTask
1349{
1350 public:
1351 GetVertexExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1352 gl::Error run() override
1353 {
1354 const auto &defaultInputLayout =
1355 GetDefaultInputLayoutFromShader(mProgram->mState.getAttachedVertexShader());
1356
1357 ANGLE_TRY(
1358 mProgram->getVertexExecutableForInputLayout(defaultInputLayout, &mResult, &mInfoLog));
1359
1360 return gl::NoError();
1361 }
1362};
1363
1364class ProgramD3D::GetPixelExecutableTask : public ProgramD3D::GetExecutableTask
1365{
1366 public:
1367 GetPixelExecutableTask(ProgramD3D *program) : GetExecutableTask(program) {}
1368 gl::Error run() override
1369 {
1370 const auto &defaultPixelOutput =
1371 GetDefaultOutputLayoutFromShader(mProgram->getPixelShaderKey());
1372
1373 ANGLE_TRY(
1374 mProgram->getPixelExecutableForOutputLayout(defaultPixelOutput, &mResult, &mInfoLog));
1375
1376 return gl::NoError();
1377 }
1378};
1379
1380class ProgramD3D::GetGeometryExecutableTask : public ProgramD3D::GetExecutableTask
1381{
1382 public:
1383 GetGeometryExecutableTask(ProgramD3D *program, const gl::ContextState &contextState)
1384 : GetExecutableTask(program), mContextState(contextState)
1385 {
1386 }
1387
1388 gl::Error run() override
1389 {
1390 // Auto-generate the geometry shader here, if we expect to be using point rendering in
1391 // D3D11.
1392 if (mProgram->usesGeometryShader(GL_POINTS))
1393 {
1394 ANGLE_TRY(mProgram->getGeometryExecutableForPrimitiveType(mContextState, GL_POINTS,
1395 &mResult, &mInfoLog));
1396 }
1397
1398 return gl::NoError();
1399 }
1400
1401 private:
1402 const gl::ContextState &mContextState;
1403};
1404
1405LinkResult ProgramD3D::compileProgramExecutables(const gl::ContextState &contextState,
1406 gl::InfoLog &infoLog)
1407{
1408 // Ensure the compiler is initialized to avoid race conditions.
1409 ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
1410
1411 WorkerThreadPool *workerPool = mRenderer->getWorkerThreadPool();
1412
1413 GetVertexExecutableTask vertexTask(this);
1414 GetPixelExecutableTask pixelTask(this);
1415 GetGeometryExecutableTask geometryTask(this, contextState);
1416
1417 std::array<WaitableEvent, 3> waitEvents = {{workerPool->postWorkerTask(&vertexTask),
1418 workerPool->postWorkerTask(&pixelTask),
1419 workerPool->postWorkerTask(&geometryTask)}};
1420
1421 WaitableEvent::WaitMany(&waitEvents);
1422
1423 infoLog << vertexTask.getInfoLog().str();
1424 infoLog << pixelTask.getInfoLog().str();
1425 infoLog << geometryTask.getInfoLog().str();
1426
1427 ANGLE_TRY(vertexTask.getError());
1428 ANGLE_TRY(pixelTask.getError());
1429 ANGLE_TRY(geometryTask.getError());
1430
1431 ShaderExecutableD3D *defaultVertexExecutable = vertexTask.getResult();
1432 ShaderExecutableD3D *defaultPixelExecutable = pixelTask.getResult();
1433 ShaderExecutableD3D *pointGS = geometryTask.getResult();
1434
Jamie Madill48ef11b2016-04-27 15:21:52 -04001435 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
Jamie Madill847638a2015-11-20 13:01:41 -05001436
1437 if (usesGeometryShader(GL_POINTS) && pointGS)
Tibor den Ouden97049c62014-10-06 21:39:16 +02001438 {
Jamie Madill334d6152015-10-22 14:00:28 -04001439 // Geometry shaders are currently only used internally, so there is no corresponding shader
1440 // object at the interface level. For now the geometry shader debug info is prepended to
1441 // the vertex shader.
Tibor den Ouden97049c62014-10-06 21:39:16 +02001442 vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n");
Jamie Madill847638a2015-11-20 13:01:41 -05001443 vertexShaderD3D->appendDebugInfo(pointGS->getDebugInfo());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001444 vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n");
1445 }
1446
1447 if (defaultVertexExecutable)
1448 {
1449 vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo());
1450 }
1451
1452 if (defaultPixelExecutable)
1453 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001454 const ShaderD3D *fragmentShaderD3D =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001455 GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Tibor den Ouden97049c62014-10-06 21:39:16 +02001456 fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo());
1457 }
Tibor den Ouden97049c62014-10-06 21:39:16 +02001458
Jamie Madillb0a838b2016-11-13 20:02:12 -05001459 return (defaultVertexExecutable && defaultPixelExecutable &&
1460 (!usesGeometryShader(GL_POINTS) || pointGS));
Brandon Jones18bd4102014-09-22 14:21:44 -07001461}
1462
Jamie Madill9082b982016-04-27 15:21:51 -04001463LinkResult ProgramD3D::link(const gl::ContextState &data, gl::InfoLog &infoLog)
Brandon Jones22502d52014-08-29 16:58:36 -07001464{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001465 reset();
1466
Jamie Madill48ef11b2016-04-27 15:21:52 -04001467 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
1468 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Brandon Joneseb994362014-09-24 10:27:28 -07001469
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001470 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader);
1471 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader);
1472
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001473 mSamplersVS.resize(data.getCaps().maxVertexTextureImageUnits);
1474 mSamplersPS.resize(data.getCaps().maxTextureImageUnits);
Brandon Jones22502d52014-08-29 16:58:36 -07001475
Arun Patole44efa0b2015-03-04 17:11:05 +05301476 vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds);
Jamie Madillf5f4ad22015-09-02 18:32:38 +00001477 fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds);
1478
Jamie Madill53ea9cc2016-05-17 10:12:52 -04001479 if (mRenderer->getNativeLimitations().noFrontFacingSupport)
Austin Kinross02df7962015-07-01 10:03:42 -07001480 {
1481 if (fragmentShaderD3D->usesFrontFacing())
1482 {
1483 infoLog << "The current renderer doesn't support gl_FrontFacing";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001484 return false;
Austin Kinross02df7962015-07-01 10:03:42 -07001485 }
1486 }
1487
Jamie Madillca03b352015-09-02 12:38:13 -04001488 std::vector<PackedVarying> packedVaryings =
Jamie Madill48ef11b2016-04-27 15:21:52 -04001489 MergeVaryings(*vertexShader, *fragmentShader, mState.getTransformFeedbackVaryingNames());
Jamie Madillca03b352015-09-02 12:38:13 -04001490
Brandon Jones22502d52014-08-29 16:58:36 -07001491 // Map the varyings to the register file
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001492 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors);
Jamie Madill2ad1c492016-12-07 14:46:18 -05001493 if (!varyingPacking.packUserVaryings(infoLog, packedVaryings,
1494 mState.getTransformFeedbackVaryingNames()))
Brandon Jones22502d52014-08-29 16:58:36 -07001495 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001496 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001497 }
1498
Jamie Madillc9bde922016-07-24 17:58:50 -04001499 ProgramD3DMetadata metadata(mRenderer, vertexShaderD3D, fragmentShaderD3D);
Jamie Madille39a3f02015-11-17 20:42:15 -05001500
Jamie Madill120040e2016-12-07 14:46:16 -05001501 metadata.updatePackingBuiltins(SHADER_VERTEX, &varyingPacking);
1502 metadata.updatePackingBuiltins(SHADER_PIXEL, &varyingPacking);
Jamie Madill9fc36822015-11-18 13:08:07 -05001503
Jamie Madill2ad1c492016-12-07 14:46:18 -05001504 if (!varyingPacking.validateBuiltins())
Jamie Madill9fc36822015-11-18 13:08:07 -05001505 {
1506 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001507 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001508 }
1509
1510 // TODO(jmadill): Implement more sophisticated component packing in D3D9.
1511 // We can fail here because we use one semantic per GLSL varying. D3D11 can pack varyings
1512 // intelligently, but D3D9 assumes one semantic per register.
1513 if (mRenderer->getRendererClass() == RENDERER_D3D9 &&
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001514 varyingPacking.getMaxSemanticIndex() > data.getCaps().maxVaryingVectors)
Jamie Madill9fc36822015-11-18 13:08:07 -05001515 {
1516 infoLog << "Cannot pack these varyings on D3D9.";
Jamie Madillb0a838b2016-11-13 20:02:12 -05001517 return false;
Jamie Madill9fc36822015-11-18 13:08:07 -05001518 }
1519
Jamie Madill48ef11b2016-04-27 15:21:52 -04001520 if (!mDynamicHLSL->generateShaderLinkHLSL(data, mState, metadata, varyingPacking, &mPixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -05001521 &mVertexHLSL))
Brandon Jones22502d52014-08-29 16:58:36 -07001522 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001523 return false;
Brandon Jones22502d52014-08-29 16:58:36 -07001524 }
1525
Brandon Jones44151a92014-09-10 11:32:25 -07001526 mUsesPointSize = vertexShaderD3D->usesPointSize();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001527 mDynamicHLSL->getPixelShaderOutputKey(data, mState, metadata, &mPixelShaderKey);
1528 mUsesFragDepth = metadata.usesFragDepth();
Brandon Jones44151a92014-09-10 11:32:25 -07001529
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001530 // Cache if we use flat shading
Jamie Madill55c25d02015-11-18 13:08:08 -05001531 mUsesFlatInterpolation = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001532 for (const auto &varying : packedVaryings)
1533 {
Jamie Madill55c25d02015-11-18 13:08:08 -05001534 if (varying.interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001535 {
1536 mUsesFlatInterpolation = true;
1537 break;
1538 }
1539 }
1540
Jamie Madill4e31ad52015-10-29 10:32:57 -04001541 if (mRenderer->getMajorShaderModel() >= 4)
1542 {
Jamie Madill120040e2016-12-07 14:46:16 -05001543 metadata.updatePackingBuiltins(SHADER_GEOMETRY, &varyingPacking);
Jamie Madill9fc36822015-11-18 13:08:07 -05001544 mGeometryShaderPreamble = mDynamicHLSL->generateGeometryShaderPreamble(varyingPacking);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001545 }
1546
Jamie Madill8047c0d2016-03-07 13:02:12 -05001547 initAttribLocationsToD3DSemantic();
Jamie Madill437d2662014-12-05 14:23:35 -05001548
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001549 defineUniformsAndAssignRegisters();
Jamie Madille473dee2015-08-18 14:49:01 -04001550
Jamie Madill9fc36822015-11-18 13:08:07 -05001551 gatherTransformFeedbackVaryings(varyingPacking);
Jamie Madillccdf74b2015-08-18 10:46:12 -04001552
Jamie Madill4e31ad52015-10-29 10:32:57 -04001553 LinkResult result = compileProgramExecutables(data, infoLog);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001554 if (result.isError())
Geoff Lang65d17e52016-09-08 09:52:58 -04001555 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001556 infoLog << result.getError().getMessage();
Geoff Lang65d17e52016-09-08 09:52:58 -04001557 return result;
1558 }
Jamie Madillb0a838b2016-11-13 20:02:12 -05001559 else if (!result.getResult())
Jamie Madill31c8c562015-08-19 14:08:03 -04001560 {
1561 infoLog << "Failed to create D3D shaders.";
1562 return result;
1563 }
1564
Jamie Madill4a3c2342015-10-08 12:58:45 -04001565 initUniformBlockInfo();
1566
Jamie Madillb0a838b2016-11-13 20:02:12 -05001567 return true;
Brandon Jones22502d52014-08-29 16:58:36 -07001568}
1569
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001570GLboolean ProgramD3D::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/)
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001571{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001572 // TODO(jmadill): Do something useful here?
1573 return GL_TRUE;
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001574}
1575
Jamie Madill4a3c2342015-10-08 12:58:45 -04001576void ProgramD3D::initUniformBlockInfo()
Jamie Madill62d31cb2015-09-11 13:25:51 -04001577{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001578 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001579
Jamie Madill62d31cb2015-09-11 13:25:51 -04001580 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
1581 {
1582 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
1583 continue;
1584
Jamie Madill4a3c2342015-10-08 12:58:45 -04001585 if (mBlockDataSizes.count(vertexBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001586 continue;
1587
Jamie Madill4a3c2342015-10-08 12:58:45 -04001588 size_t dataSize = getUniformBlockInfo(vertexBlock);
1589 mBlockDataSizes[vertexBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001590 }
1591
Jamie Madill48ef11b2016-04-27 15:21:52 -04001592 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001593
1594 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
1595 {
1596 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
1597 continue;
1598
Jamie Madill4a3c2342015-10-08 12:58:45 -04001599 if (mBlockDataSizes.count(fragmentBlock.name) > 0)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001600 continue;
1601
Jamie Madill4a3c2342015-10-08 12:58:45 -04001602 size_t dataSize = getUniformBlockInfo(fragmentBlock);
1603 mBlockDataSizes[fragmentBlock.name] = dataSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001604 }
Jamie Madill4a3c2342015-10-08 12:58:45 -04001605}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001606
Jamie Madill4a3c2342015-10-08 12:58:45 -04001607void ProgramD3D::assignUniformBlockRegisters()
1608{
1609 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001610
1611 // Assign registers and update sizes.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001612 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
1613 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001614
Jamie Madill48ef11b2016-04-27 15:21:52 -04001615 for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks())
Jamie Madill62d31cb2015-09-11 13:25:51 -04001616 {
1617 unsigned int uniformBlockElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
1618
Jamie Madill4a3c2342015-10-08 12:58:45 -04001619 D3DUniformBlock d3dUniformBlock;
1620
Jamie Madill62d31cb2015-09-11 13:25:51 -04001621 if (uniformBlock.vertexStaticUse)
1622 {
1623 unsigned int baseRegister =
1624 vertexShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001625 d3dUniformBlock.vsRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001626 }
1627
1628 if (uniformBlock.fragmentStaticUse)
1629 {
1630 unsigned int baseRegister =
1631 fragmentShaderD3D->getInterfaceBlockRegister(uniformBlock.name);
Jamie Madill4a3c2342015-10-08 12:58:45 -04001632 d3dUniformBlock.psRegisterIndex = baseRegister + uniformBlockElement;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001633 }
1634
Jamie Madill4a3c2342015-10-08 12:58:45 -04001635 mD3DUniformBlocks.push_back(d3dUniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001636 }
1637}
1638
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001639void ProgramD3D::initializeUniformStorage()
Brandon Jonesc9610c52014-08-25 17:02:59 -07001640{
1641 // Compute total default block size
Jamie Madill334d6152015-10-22 14:00:28 -04001642 unsigned int vertexRegisters = 0;
Brandon Jonesc9610c52014-08-25 17:02:59 -07001643 unsigned int fragmentRegisters = 0;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001644 for (const D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jonesc9610c52014-08-25 17:02:59 -07001645 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001646 if (!d3dUniform->isSampler())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001647 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001648 if (d3dUniform->isReferencedByVertexShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001649 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001650 vertexRegisters = std::max(vertexRegisters,
1651 d3dUniform->vsRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001652 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001653 if (d3dUniform->isReferencedByFragmentShader())
Brandon Jonesc9610c52014-08-25 17:02:59 -07001654 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001655 fragmentRegisters = std::max(
1656 fragmentRegisters, d3dUniform->psRegisterIndex + d3dUniform->registerCount);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001657 }
1658 }
1659 }
1660
Jamie Madill334d6152015-10-22 14:00:28 -04001661 mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u);
Brandon Jonesc9610c52014-08-25 17:02:59 -07001662 mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u);
1663}
1664
Jamie Madill3e14e2b2015-10-29 14:38:53 -04001665gl::Error ProgramD3D::applyUniforms(GLenum drawMode)
Brandon Jones18bd4102014-09-22 14:21:44 -07001666{
Olli Etuahoe5df3062015-11-18 13:45:19 +02001667 ASSERT(!mDirtySamplerMapping);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001668
Jamie Madill01074252016-11-28 15:55:51 -05001669 ANGLE_TRY(mRenderer->applyUniforms(*this, drawMode, mD3DUniforms));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001670
Jamie Madill62d31cb2015-09-11 13:25:51 -04001671 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001672 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001673 d3dUniform->dirty = false;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001674 }
1675
1676 return gl::Error(GL_NO_ERROR);
Brandon Jones18bd4102014-09-22 14:21:44 -07001677}
1678
Jamie Madill9082b982016-04-27 15:21:51 -04001679gl::Error ProgramD3D::applyUniformBuffers(const gl::ContextState &data)
Brandon Jones18bd4102014-09-22 14:21:44 -07001680{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001681 if (mState.getUniformBlocks().empty())
Jamie Madill4a3c2342015-10-08 12:58:45 -04001682 {
1683 return gl::Error(GL_NO_ERROR);
1684 }
1685
1686 // Lazy init.
1687 if (mD3DUniformBlocks.empty())
1688 {
1689 assignUniformBlockRegisters();
1690 }
1691
Jamie Madill03260fa2015-06-22 13:57:22 -04001692 mVertexUBOCache.clear();
1693 mFragmentUBOCache.clear();
Brandon Jones18bd4102014-09-22 14:21:44 -07001694
1695 const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers();
1696 const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers();
1697
Jamie Madill4a3c2342015-10-08 12:58:45 -04001698 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mD3DUniformBlocks.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001699 uniformBlockIndex++)
Brandon Jones18bd4102014-09-22 14:21:44 -07001700 {
Jamie Madill4a3c2342015-10-08 12:58:45 -04001701 const D3DUniformBlock &uniformBlock = mD3DUniformBlocks[uniformBlockIndex];
Jamie Madill48ef11b2016-04-27 15:21:52 -04001702 GLuint blockBinding = mState.getUniformBlockBinding(uniformBlockIndex);
Brandon Jones18bd4102014-09-22 14:21:44 -07001703
Brandon Jones18bd4102014-09-22 14:21:44 -07001704 // Unnecessary to apply an unreferenced standard or shared UBO
Jamie Madill4a3c2342015-10-08 12:58:45 -04001705 if (!uniformBlock.vertexStaticUse() && !uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001706 {
1707 continue;
1708 }
1709
Jamie Madill4a3c2342015-10-08 12:58:45 -04001710 if (uniformBlock.vertexStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001711 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001712 unsigned int registerIndex = uniformBlock.vsRegisterIndex - reservedBuffersInVS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001713 ASSERT(registerIndex < data.getCaps().maxVertexUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001714
Jamie Madill969194d2015-07-20 14:36:56 -04001715 if (mVertexUBOCache.size() <= registerIndex)
Jamie Madill03260fa2015-06-22 13:57:22 -04001716 {
1717 mVertexUBOCache.resize(registerIndex + 1, -1);
1718 }
1719
1720 ASSERT(mVertexUBOCache[registerIndex] == -1);
1721 mVertexUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001722 }
1723
Jamie Madill4a3c2342015-10-08 12:58:45 -04001724 if (uniformBlock.fragmentStaticUse())
Brandon Jones18bd4102014-09-22 14:21:44 -07001725 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001726 unsigned int registerIndex = uniformBlock.psRegisterIndex - reservedBuffersInFS;
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001727 ASSERT(registerIndex < data.getCaps().maxFragmentUniformBlocks);
Jamie Madill03260fa2015-06-22 13:57:22 -04001728
1729 if (mFragmentUBOCache.size() <= registerIndex)
1730 {
1731 mFragmentUBOCache.resize(registerIndex + 1, -1);
1732 }
1733
1734 ASSERT(mFragmentUBOCache[registerIndex] == -1);
1735 mFragmentUBOCache[registerIndex] = blockBinding;
Brandon Jones18bd4102014-09-22 14:21:44 -07001736 }
1737 }
1738
Jamie Madill03260fa2015-06-22 13:57:22 -04001739 return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache);
Brandon Jones18bd4102014-09-22 14:21:44 -07001740}
1741
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001742void ProgramD3D::dirtyAllUniforms()
Brandon Jones18bd4102014-09-22 14:21:44 -07001743{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001744 for (D3DUniform *d3dUniform : mD3DUniforms)
Brandon Jones18bd4102014-09-22 14:21:44 -07001745 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001746 d3dUniform->dirty = true;
Brandon Jones18bd4102014-09-22 14:21:44 -07001747 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001748}
1749
Jamie Madill334d6152015-10-22 14:00:28 -04001750void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001751{
1752 setUniform(location, count, v, GL_FLOAT);
1753}
1754
1755void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1756{
1757 setUniform(location, count, v, GL_FLOAT_VEC2);
1758}
1759
1760void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1761{
1762 setUniform(location, count, v, GL_FLOAT_VEC3);
1763}
1764
1765void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1766{
1767 setUniform(location, count, v, GL_FLOAT_VEC4);
1768}
1769
Jamie Madill334d6152015-10-22 14:00:28 -04001770void ProgramD3D::setUniformMatrix2fv(GLint location,
1771 GLsizei count,
1772 GLboolean transpose,
1773 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001774{
1775 setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
1776}
1777
Jamie Madill334d6152015-10-22 14:00:28 -04001778void ProgramD3D::setUniformMatrix3fv(GLint location,
1779 GLsizei count,
1780 GLboolean transpose,
1781 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001782{
1783 setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
1784}
1785
Jamie Madill334d6152015-10-22 14:00:28 -04001786void ProgramD3D::setUniformMatrix4fv(GLint location,
1787 GLsizei count,
1788 GLboolean transpose,
1789 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001790{
1791 setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
1792}
1793
Jamie Madill334d6152015-10-22 14:00:28 -04001794void ProgramD3D::setUniformMatrix2x3fv(GLint location,
1795 GLsizei count,
1796 GLboolean transpose,
1797 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001798{
1799 setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
1800}
1801
Jamie Madill334d6152015-10-22 14:00:28 -04001802void ProgramD3D::setUniformMatrix3x2fv(GLint location,
1803 GLsizei count,
1804 GLboolean transpose,
1805 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001806{
1807 setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
1808}
1809
Jamie Madill334d6152015-10-22 14:00:28 -04001810void ProgramD3D::setUniformMatrix2x4fv(GLint location,
1811 GLsizei count,
1812 GLboolean transpose,
1813 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001814{
1815 setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
1816}
1817
Jamie Madill334d6152015-10-22 14:00:28 -04001818void ProgramD3D::setUniformMatrix4x2fv(GLint location,
1819 GLsizei count,
1820 GLboolean transpose,
1821 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001822{
1823 setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
1824}
1825
Jamie Madill334d6152015-10-22 14:00:28 -04001826void ProgramD3D::setUniformMatrix3x4fv(GLint location,
1827 GLsizei count,
1828 GLboolean transpose,
1829 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001830{
1831 setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
1832}
1833
Jamie Madill334d6152015-10-22 14:00:28 -04001834void ProgramD3D::setUniformMatrix4x3fv(GLint location,
1835 GLsizei count,
1836 GLboolean transpose,
1837 const GLfloat *value)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001838{
1839 setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
1840}
1841
1842void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1843{
1844 setUniform(location, count, v, GL_INT);
1845}
1846
1847void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1848{
1849 setUniform(location, count, v, GL_INT_VEC2);
1850}
1851
1852void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1853{
1854 setUniform(location, count, v, GL_INT_VEC3);
1855}
1856
1857void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1858{
1859 setUniform(location, count, v, GL_INT_VEC4);
1860}
1861
1862void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1863{
1864 setUniform(location, count, v, GL_UNSIGNED_INT);
1865}
1866
1867void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1868{
1869 setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
1870}
1871
1872void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1873{
1874 setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
1875}
1876
1877void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1878{
1879 setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
1880}
1881
Jamie Madill4a3c2342015-10-08 12:58:45 -04001882void ProgramD3D::setUniformBlockBinding(GLuint /*uniformBlockIndex*/,
1883 GLuint /*uniformBlockBinding*/)
Geoff Lang5d124a62015-09-15 13:03:27 -04001884{
1885}
1886
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001887void ProgramD3D::defineUniformsAndAssignRegisters()
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001888{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001889 D3DUniformMap uniformMap;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001890 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001891 for (const sh::Uniform &vertexUniform : vertexShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001892
Jamie Madilla2eb02c2015-09-11 12:31:41 +00001893 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001894 if (vertexUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001895 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001896 defineUniformBase(vertexShader, vertexUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001897 }
1898 }
1899
Jamie Madill48ef11b2016-04-27 15:21:52 -04001900 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001901 for (const sh::Uniform &fragmentUniform : fragmentShader->getUniforms())
Jamie Madillfb536032015-09-11 13:19:49 -04001902 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001903 if (fragmentUniform.staticUse)
Jamie Madillfb536032015-09-11 13:19:49 -04001904 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001905 defineUniformBase(fragmentShader, fragmentUniform, &uniformMap);
Jamie Madillfb536032015-09-11 13:19:49 -04001906 }
1907 }
1908
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001909 // Initialize the D3DUniform list to mirror the indexing of the GL layer.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001910 for (const gl::LinkedUniform &glUniform : mState.getUniforms())
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001911 {
1912 if (!glUniform.isInDefaultBlock())
1913 continue;
1914
1915 auto mapEntry = uniformMap.find(glUniform.name);
1916 ASSERT(mapEntry != uniformMap.end());
1917 mD3DUniforms.push_back(mapEntry->second);
1918 }
1919
Jamie Madill62d31cb2015-09-11 13:25:51 -04001920 assignAllSamplerRegisters();
Jamie Madillfb536032015-09-11 13:19:49 -04001921 initializeUniformStorage();
Jamie Madillfb536032015-09-11 13:19:49 -04001922}
1923
Jamie Madill91445bc2015-09-23 16:47:53 -04001924void ProgramD3D::defineUniformBase(const gl::Shader *shader,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001925 const sh::Uniform &uniform,
1926 D3DUniformMap *uniformMap)
Jamie Madillfb536032015-09-11 13:19:49 -04001927{
Olli Etuaho96963162016-03-21 11:54:33 +02001928 // Samplers get their registers assigned in assignAllSamplerRegisters.
1929 if (uniform.isBuiltIn() || gl::IsSamplerType(uniform.type))
Jamie Madillfb536032015-09-11 13:19:49 -04001930 {
Jamie Madill91445bc2015-09-23 16:47:53 -04001931 defineUniform(shader->getType(), uniform, uniform.name, nullptr, uniformMap);
Jamie Madill55def582015-05-04 11:24:57 -04001932 return;
1933 }
1934
Jamie Madill91445bc2015-09-23 16:47:53 -04001935 const ShaderD3D *shaderD3D = GetImplAs<ShaderD3D>(shader);
1936
1937 unsigned int startRegister = shaderD3D->getUniformRegister(uniform.name);
1938 ShShaderOutput outputType = shaderD3D->getCompilerOutputType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001939 sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType));
Jamie Madill62d31cb2015-09-11 13:25:51 -04001940 encoder.skipRegisters(startRegister);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001941
Jamie Madill91445bc2015-09-23 16:47:53 -04001942 defineUniform(shader->getType(), uniform, uniform.name, &encoder, uniformMap);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001943}
1944
Jamie Madill62d31cb2015-09-11 13:25:51 -04001945D3DUniform *ProgramD3D::getD3DUniformByName(const std::string &name)
1946{
1947 for (D3DUniform *d3dUniform : mD3DUniforms)
1948 {
1949 if (d3dUniform->name == name)
1950 {
1951 return d3dUniform;
1952 }
1953 }
1954
1955 return nullptr;
1956}
1957
Jamie Madill91445bc2015-09-23 16:47:53 -04001958void ProgramD3D::defineUniform(GLenum shaderType,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001959 const sh::ShaderVariable &uniform,
1960 const std::string &fullName,
1961 sh::HLSLBlockEncoder *encoder,
1962 D3DUniformMap *uniformMap)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001963{
1964 if (uniform.isStruct())
1965 {
1966 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
1967 {
1968 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
1969
Jamie Madill55def582015-05-04 11:24:57 -04001970 if (encoder)
1971 encoder->enterAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001972
1973 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
1974 {
Jamie Madill334d6152015-10-22 14:00:28 -04001975 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001976 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
1977
Olli Etuaho96963162016-03-21 11:54:33 +02001978 // Samplers get their registers assigned in assignAllSamplerRegisters.
1979 // Also they couldn't use the same encoder as the rest of the struct, since they are
1980 // extracted out of the struct by the shader translator.
1981 if (gl::IsSamplerType(field.type))
1982 {
1983 defineUniform(shaderType, field, fieldFullName, nullptr, uniformMap);
1984 }
1985 else
1986 {
1987 defineUniform(shaderType, field, fieldFullName, encoder, uniformMap);
1988 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001989 }
1990
Jamie Madill55def582015-05-04 11:24:57 -04001991 if (encoder)
1992 encoder->exitAggregateType();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001993 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001994 return;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001995 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001996
1997 // Not a struct. Arrays are treated as aggregate types.
1998 if (uniform.isArray() && encoder)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07001999 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002000 encoder->enterAggregateType();
2001 }
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002002
Jamie Madill62d31cb2015-09-11 13:25:51 -04002003 // Advance the uniform offset, to track registers allocation for structs
2004 sh::BlockMemberInfo blockInfo =
2005 encoder ? encoder->encodeType(uniform.type, uniform.arraySize, false)
2006 : sh::BlockMemberInfo::getDefaultBlockInfo();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002007
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002008 auto uniformMapEntry = uniformMap->find(fullName);
2009 D3DUniform *d3dUniform = nullptr;
Jamie Madill2857f482015-02-09 15:35:29 -05002010
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002011 if (uniformMapEntry != uniformMap->end())
Jamie Madill62d31cb2015-09-11 13:25:51 -04002012 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002013 d3dUniform = uniformMapEntry->second;
2014 }
2015 else
2016 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002017 d3dUniform = new D3DUniform(uniform.type, fullName, uniform.arraySize, true);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002018 (*uniformMap)[fullName] = d3dUniform;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 }
2020
2021 if (encoder)
2022 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002023 d3dUniform->registerElement =
2024 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002025 unsigned int reg =
2026 static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo));
Jamie Madill91445bc2015-09-23 16:47:53 -04002027 if (shaderType == GL_FRAGMENT_SHADER)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002028 {
2029 d3dUniform->psRegisterIndex = reg;
2030 }
Jamie Madill91445bc2015-09-23 16:47:53 -04002031 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002032 {
Jamie Madill91445bc2015-09-23 16:47:53 -04002033 ASSERT(shaderType == GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002034 d3dUniform->vsRegisterIndex = reg;
2035 }
Jamie Madillfb536032015-09-11 13:19:49 -04002036
2037 // Arrays are treated as aggregate types
Jamie Madill62d31cb2015-09-11 13:25:51 -04002038 if (uniform.isArray())
Jamie Madillfb536032015-09-11 13:19:49 -04002039 {
2040 encoder->exitAggregateType();
2041 }
2042 }
2043}
2044
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002045template <typename T>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002046void ProgramD3D::setUniform(GLint location, GLsizei countIn, const T *v, GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002047{
Jamie Madill334d6152015-10-22 14:00:28 -04002048 const int components = gl::VariableComponentCount(targetUniformType);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002049 const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType);
2050
Jamie Madill62d31cb2015-09-11 13:25:51 -04002051 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002052
Jamie Madill62d31cb2015-09-11 13:25:51 -04002053 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002054 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002055 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002056
2057 if (targetUniform->type == targetUniformType)
2058 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002059 T *target = reinterpret_cast<T *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002060
Jamie Madill62d31cb2015-09-11 13:25:51 -04002061 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002062 {
Jamie Madill334d6152015-10-22 14:00:28 -04002063 T *dest = target + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002064 const T *source = v + (i * components);
2065
2066 for (int c = 0; c < components; c++)
2067 {
2068 SetIfDirty(dest + c, source[c], &targetUniform->dirty);
2069 }
2070 for (int c = components; c < 4; c++)
2071 {
2072 SetIfDirty(dest + c, T(0), &targetUniform->dirty);
2073 }
2074 }
2075 }
2076 else if (targetUniform->type == targetBoolType)
2077 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002078 GLint *boolParams = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002079
Jamie Madill62d31cb2015-09-11 13:25:51 -04002080 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002081 {
Jamie Madill334d6152015-10-22 14:00:28 -04002082 GLint *dest = boolParams + (i * 4);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002083 const T *source = v + (i * components);
2084
2085 for (int c = 0; c < components; c++)
2086 {
Jamie Madill334d6152015-10-22 14:00:28 -04002087 SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE,
2088 &targetUniform->dirty);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002089 }
2090 for (int c = components; c < 4; c++)
2091 {
2092 SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty);
2093 }
2094 }
2095 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002096 else if (targetUniform->isSampler())
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002097 {
2098 ASSERT(targetUniformType == GL_INT);
2099
Jamie Madill62d31cb2015-09-11 13:25:51 -04002100 GLint *target = reinterpret_cast<GLint *>(targetUniform->data) + arrayElement * 4;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002101
2102 bool wasDirty = targetUniform->dirty;
2103
Jamie Madill62d31cb2015-09-11 13:25:51 -04002104 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002105 {
Jamie Madill334d6152015-10-22 14:00:28 -04002106 GLint *dest = target + (i * 4);
2107 const GLint *source = reinterpret_cast<const GLint *>(v) + (i * components);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002108
2109 SetIfDirty(dest + 0, source[0], &targetUniform->dirty);
2110 SetIfDirty(dest + 1, 0, &targetUniform->dirty);
2111 SetIfDirty(dest + 2, 0, &targetUniform->dirty);
2112 SetIfDirty(dest + 3, 0, &targetUniform->dirty);
2113 }
2114
2115 if (!wasDirty && targetUniform->dirty)
2116 {
2117 mDirtySamplerMapping = true;
2118 }
Brandon Jones18bd4102014-09-22 14:21:44 -07002119 }
Jamie Madill334d6152015-10-22 14:00:28 -04002120 else
2121 UNREACHABLE();
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002122}
2123
2124template <int cols, int rows>
Jamie Madill62d31cb2015-09-11 13:25:51 -04002125void ProgramD3D::setUniformMatrixfv(GLint location,
2126 GLsizei countIn,
2127 GLboolean transpose,
2128 const GLfloat *value,
2129 GLenum targetUniformType)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002130{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002131 D3DUniform *targetUniform = getD3DUniformFromLocation(location);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002132
Jamie Madill62d31cb2015-09-11 13:25:51 -04002133 unsigned int elementCount = targetUniform->elementCount();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002134 unsigned int arrayElement = mState.getUniformLocations()[location].element;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002135 unsigned int count = std::min(elementCount - arrayElement, static_cast<unsigned int>(countIn));
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002136
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002137 const unsigned int targetMatrixStride = (4 * rows);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002138 GLfloat *target =
2139 (GLfloat *)(targetUniform->data + arrayElement * sizeof(GLfloat) * targetMatrixStride);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002140
Jamie Madill62d31cb2015-09-11 13:25:51 -04002141 for (unsigned int i = 0; i < count; i++)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002142 {
2143 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
2144 if (transpose == GL_FALSE)
2145 {
Jamie Madill334d6152015-10-22 14:00:28 -04002146 targetUniform->dirty = TransposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) ||
2147 targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002148 }
2149 else
2150 {
Jamie Madill334d6152015-10-22 14:00:28 -04002151 targetUniform->dirty =
2152 ExpandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002153 }
2154 target += targetMatrixStride;
2155 value += cols * rows;
2156 }
2157}
2158
Jamie Madill4a3c2342015-10-08 12:58:45 -04002159size_t ProgramD3D::getUniformBlockInfo(const sh::InterfaceBlock &interfaceBlock)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002160{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002161 ASSERT(interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002162
Jamie Madill62d31cb2015-09-11 13:25:51 -04002163 // define member uniforms
2164 sh::Std140BlockEncoder std140Encoder;
2165 sh::HLSLBlockEncoder hlslEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED);
2166 sh::BlockLayoutEncoder *encoder = nullptr;
2167
2168 if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002169 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002170 encoder = &std140Encoder;
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002171 }
2172 else
2173 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002174 encoder = &hlslEncoder;
Jamie Madill61b8dd92015-09-09 19:04:04 +00002175 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002176
Jamie Madill39046162016-02-08 15:05:17 -05002177 GetUniformBlockInfo(interfaceBlock.fields, interfaceBlock.fieldPrefix(), encoder,
2178 interfaceBlock.isRowMajorLayout, &mBlockInfo);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002179
2180 return encoder->getBlockSize();
Jamie Madill61b8dd92015-09-09 19:04:04 +00002181}
2182
Jamie Madill62d31cb2015-09-11 13:25:51 -04002183void ProgramD3D::assignAllSamplerRegisters()
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002184{
Olli Etuaho96963162016-03-21 11:54:33 +02002185 for (D3DUniform *d3dUniform : mD3DUniforms)
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002186 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002187 if (d3dUniform->isSampler())
Jamie Madillfb536032015-09-11 13:19:49 -04002188 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002189 assignSamplerRegisters(d3dUniform);
Jamie Madillfb536032015-09-11 13:19:49 -04002190 }
Jamie Madilla2eb02c2015-09-11 12:31:41 +00002191 }
2192}
2193
Olli Etuaho96963162016-03-21 11:54:33 +02002194void ProgramD3D::assignSamplerRegisters(D3DUniform *d3dUniform)
Jamie Madillfb536032015-09-11 13:19:49 -04002195{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002196 ASSERT(d3dUniform->isSampler());
Jamie Madill48ef11b2016-04-27 15:21:52 -04002197 const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedVertexShader());
2198 const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mState.getAttachedFragmentShader());
Olli Etuaho96963162016-03-21 11:54:33 +02002199 ASSERT(vertexShaderD3D->hasUniform(d3dUniform) || fragmentShaderD3D->hasUniform(d3dUniform));
2200 if (vertexShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002201 {
Olli Etuaho96963162016-03-21 11:54:33 +02002202 d3dUniform->vsRegisterIndex = vertexShaderD3D->getUniformRegister(d3dUniform->name);
2203 ASSERT(d3dUniform->vsRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002204 AssignSamplers(d3dUniform->vsRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2205 mSamplersVS, &mUsedVertexSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002206 }
Olli Etuaho96963162016-03-21 11:54:33 +02002207 if (fragmentShaderD3D->hasUniform(d3dUniform))
Jamie Madillfb536032015-09-11 13:19:49 -04002208 {
Olli Etuaho96963162016-03-21 11:54:33 +02002209 d3dUniform->psRegisterIndex = fragmentShaderD3D->getUniformRegister(d3dUniform->name);
2210 ASSERT(d3dUniform->psRegisterIndex != GL_INVALID_INDEX);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002211 AssignSamplers(d3dUniform->psRegisterIndex, d3dUniform->type, d3dUniform->arraySize,
2212 mSamplersPS, &mUsedPixelSamplerRange);
Jamie Madillfb536032015-09-11 13:19:49 -04002213 }
2214}
2215
Jamie Madill62d31cb2015-09-11 13:25:51 -04002216// static
2217void ProgramD3D::AssignSamplers(unsigned int startSamplerIndex,
Jamie Madilld3dfda22015-07-06 08:28:49 -04002218 GLenum samplerType,
2219 unsigned int samplerCount,
2220 std::vector<Sampler> &outSamplers,
2221 GLuint *outUsedRange)
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002222{
2223 unsigned int samplerIndex = startSamplerIndex;
2224
2225 do
2226 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002227 ASSERT(samplerIndex < outSamplers.size());
2228 Sampler *sampler = &outSamplers[samplerIndex];
2229 sampler->active = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002230 sampler->textureType = gl::SamplerTypeToTextureType(samplerType);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002231 sampler->logicalTextureUnit = 0;
2232 *outUsedRange = std::max(samplerIndex + 1, *outUsedRange);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002233 samplerIndex++;
2234 } while (samplerIndex < startSamplerIndex + samplerCount);
Brandon Jones18bd4102014-09-22 14:21:44 -07002235}
2236
Brandon Jonesc9610c52014-08-25 17:02:59 -07002237void ProgramD3D::reset()
2238{
Brandon Joneseb994362014-09-24 10:27:28 -07002239 SafeDeleteContainer(mVertexExecutables);
2240 SafeDeleteContainer(mPixelExecutables);
Jamie Madill4e31ad52015-10-29 10:32:57 -04002241
2242 for (auto &element : mGeometryExecutables)
2243 {
2244 SafeDelete(element);
2245 }
Brandon Joneseb994362014-09-24 10:27:28 -07002246
Brandon Jones22502d52014-08-29 16:58:36 -07002247 mVertexHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002248 mVertexWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002249
2250 mPixelHLSL.clear();
Geoff Lang6941a552015-07-27 11:06:45 -04002251 mPixelWorkarounds = D3DCompilerWorkarounds();
Brandon Jones22502d52014-08-29 16:58:36 -07002252 mUsesFragDepth = false;
2253 mPixelShaderKey.clear();
Brandon Jones44151a92014-09-10 11:32:25 -07002254 mUsesPointSize = false;
Jamie Madill3e14e2b2015-10-29 14:38:53 -04002255 mUsesFlatInterpolation = false;
Brandon Jones22502d52014-08-29 16:58:36 -07002256
Jamie Madill62d31cb2015-09-11 13:25:51 -04002257 SafeDeleteContainer(mD3DUniforms);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002258 mD3DUniformBlocks.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002259
Brandon Jonesc9610c52014-08-25 17:02:59 -07002260 SafeDelete(mVertexUniformStorage);
2261 SafeDelete(mFragmentUniformStorage);
Brandon Jones1a8a7e32014-10-01 12:49:30 -07002262
2263 mSamplersPS.clear();
2264 mSamplersVS.clear();
2265
2266 mUsedVertexSamplerRange = 0;
Jamie Madill334d6152015-10-22 14:00:28 -04002267 mUsedPixelSamplerRange = 0;
2268 mDirtySamplerMapping = true;
Jamie Madill437d2662014-12-05 14:23:35 -05002269
Jamie Madill8047c0d2016-03-07 13:02:12 -05002270 mAttribLocationToD3DSemantic.fill(-1);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002271
Jamie Madill9fc36822015-11-18 13:08:07 -05002272 mStreamOutVaryings.clear();
Jamie Madill4e31ad52015-10-29 10:32:57 -04002273
2274 mGeometryShaderPreamble.clear();
Brandon Jonesc9610c52014-08-25 17:02:59 -07002275}
2276
Geoff Lang7dd2e102014-11-10 15:19:26 -05002277unsigned int ProgramD3D::getSerial() const
2278{
2279 return mSerial;
2280}
2281
2282unsigned int ProgramD3D::issueSerial()
2283{
2284 return mCurrentSerial++;
2285}
2286
Jamie Madill8047c0d2016-03-07 13:02:12 -05002287void ProgramD3D::initAttribLocationsToD3DSemantic()
Jamie Madill63805b42015-08-25 13:17:39 -04002288{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002289 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill63805b42015-08-25 13:17:39 -04002290 ASSERT(vertexShader != nullptr);
2291
2292 // Init semantic index
Jamie Madill48ef11b2016-04-27 15:21:52 -04002293 for (const sh::Attribute &attribute : mState.getAttributes())
Jamie Madill63805b42015-08-25 13:17:39 -04002294 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002295 int d3dSemantic = vertexShader->getSemanticIndex(attribute.name);
2296 int regCount = gl::VariableRegisterCount(attribute.type);
Jamie Madill63805b42015-08-25 13:17:39 -04002297
Jamie Madill8047c0d2016-03-07 13:02:12 -05002298 for (int reg = 0; reg < regCount; ++reg)
Jamie Madill63805b42015-08-25 13:17:39 -04002299 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002300 mAttribLocationToD3DSemantic[attribute.location + reg] = d3dSemantic + reg;
Jamie Madill63805b42015-08-25 13:17:39 -04002301 }
2302 }
Jamie Madill437d2662014-12-05 14:23:35 -05002303}
2304
Jamie Madill63805b42015-08-25 13:17:39 -04002305void ProgramD3D::updateCachedInputLayout(const gl::State &state)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002306{
Jamie Madillbd136f92015-08-10 14:51:37 -04002307 mCachedInputLayout.clear();
Jamie Madilld3dfda22015-07-06 08:28:49 -04002308 const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes();
Jamie Madillf8dd7b12015-08-05 13:50:08 -04002309
Jamie Madill01074252016-11-28 15:55:51 -05002310 for (unsigned int locationIndex : IterateBitSet(mState.getActiveAttribLocationsMask()))
Jamie Madilld3dfda22015-07-06 08:28:49 -04002311 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002312 int d3dSemantic = mAttribLocationToD3DSemantic[locationIndex];
Jamie Madilld3dfda22015-07-06 08:28:49 -04002313
Jamie Madill8047c0d2016-03-07 13:02:12 -05002314 if (d3dSemantic != -1)
Jamie Madilld3dfda22015-07-06 08:28:49 -04002315 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002316 if (mCachedInputLayout.size() < static_cast<size_t>(d3dSemantic + 1))
Jamie Madillbd136f92015-08-10 14:51:37 -04002317 {
Jamie Madill8047c0d2016-03-07 13:02:12 -05002318 mCachedInputLayout.resize(d3dSemantic + 1, gl::VERTEX_FORMAT_INVALID);
Jamie Madillbd136f92015-08-10 14:51:37 -04002319 }
Jamie Madill8047c0d2016-03-07 13:02:12 -05002320 mCachedInputLayout[d3dSemantic] =
2321 GetVertexFormatType(vertexAttributes[locationIndex],
2322 state.getVertexAttribCurrentValue(locationIndex).Type);
Jamie Madilld3dfda22015-07-06 08:28:49 -04002323 }
2324 }
2325}
2326
Jamie Madill9fc36822015-11-18 13:08:07 -05002327void ProgramD3D::gatherTransformFeedbackVaryings(const VaryingPacking &varyingPacking)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002328{
Jamie Madill9fc36822015-11-18 13:08:07 -05002329 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
2330
2331 const std::string &varyingSemantic =
2332 GetVaryingSemantic(mRenderer->getMajorShaderModel(), usesPointSize());
2333
Jamie Madillccdf74b2015-08-18 10:46:12 -04002334 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill9fc36822015-11-18 13:08:07 -05002335 mStreamOutVaryings.clear();
2336
Jamie Madill48ef11b2016-04-27 15:21:52 -04002337 const auto &tfVaryingNames = mState.getTransformFeedbackVaryingNames();
Jamie Madill9fc36822015-11-18 13:08:07 -05002338 for (unsigned int outputSlot = 0; outputSlot < static_cast<unsigned int>(tfVaryingNames.size());
2339 ++outputSlot)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002340 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002341 const auto &tfVaryingName = tfVaryingNames[outputSlot];
2342 if (tfVaryingName == "gl_Position")
Jamie Madillccdf74b2015-08-18 10:46:12 -04002343 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002344 if (builtins.glPosition.enabled)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002345 {
Jamie Madill9fc36822015-11-18 13:08:07 -05002346 mStreamOutVaryings.push_back(D3DVarying(builtins.glPosition.semantic,
2347 builtins.glPosition.index, 4, outputSlot));
2348 }
2349 }
2350 else if (tfVaryingName == "gl_FragCoord")
2351 {
2352 if (builtins.glFragCoord.enabled)
2353 {
2354 mStreamOutVaryings.push_back(D3DVarying(builtins.glFragCoord.semantic,
2355 builtins.glFragCoord.index, 4, outputSlot));
2356 }
2357 }
2358 else if (tfVaryingName == "gl_PointSize")
2359 {
2360 if (builtins.glPointSize.enabled)
2361 {
2362 mStreamOutVaryings.push_back(D3DVarying("PSIZE", 0, 1, outputSlot));
2363 }
2364 }
2365 else
2366 {
2367 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
2368 {
Jamie Madill55c25d02015-11-18 13:08:08 -05002369 const auto &varying = *registerInfo.packedVarying->varying;
2370 GLenum transposedType = gl::TransposeMatrixType(varying.type);
Jamie Madill9fc36822015-11-18 13:08:07 -05002371 int componentCount = gl::VariableColumnCount(transposedType);
2372 ASSERT(!varying.isBuiltIn());
2373
Jamie Madill55c25d02015-11-18 13:08:08 -05002374 // Transform feedback for varying structs is underspecified.
2375 // See Khronos bug 9856.
2376 // TODO(jmadill): Figure out how to be spec-compliant here.
2377 if (registerInfo.packedVarying->isStructField() || varying.isStruct())
2378 continue;
2379
Jamie Madill9fc36822015-11-18 13:08:07 -05002380 // There can be more than one register assigned to a particular varying, and each
2381 // register needs its own stream out entry.
2382 if (tfVaryingName == varying.name)
2383 {
2384 mStreamOutVaryings.push_back(D3DVarying(
2385 varyingSemantic, registerInfo.semanticIndex, componentCount, outputSlot));
2386 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002387 }
2388 }
2389 }
2390}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002391
2392D3DUniform *ProgramD3D::getD3DUniformFromLocation(GLint location)
2393{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002394 return mD3DUniforms[mState.getUniformLocations()[location].index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002395}
Jamie Madill4a3c2342015-10-08 12:58:45 -04002396
2397bool ProgramD3D::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const
2398{
2399 std::string baseName = blockName;
2400 gl::ParseAndStripArrayIndex(&baseName);
2401
2402 auto sizeIter = mBlockDataSizes.find(baseName);
2403 if (sizeIter == mBlockDataSizes.end())
2404 {
2405 *sizeOut = 0;
2406 return false;
2407 }
2408
2409 *sizeOut = sizeIter->second;
2410 return true;
2411}
2412
2413bool ProgramD3D::getUniformBlockMemberInfo(const std::string &memberUniformName,
2414 sh::BlockMemberInfo *memberInfoOut) const
2415{
2416 auto infoIter = mBlockInfo.find(memberUniformName);
2417 if (infoIter == mBlockInfo.end())
2418 {
2419 *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo();
2420 return false;
2421 }
2422
2423 *memberInfoOut = infoIter->second;
2424 return true;
2425}
Sami Väisänen46eaa942016-06-29 10:26:37 +03002426
2427void ProgramD3D::setPathFragmentInputGen(const std::string &inputName,
2428 GLenum genMode,
2429 GLint components,
2430 const GLfloat *coeffs)
2431{
2432 UNREACHABLE();
2433}
2434
Jamie Madill8047c0d2016-03-07 13:02:12 -05002435} // namespace rx